[78ef36]: / slideflow / studio / utils.py

Download this file

187 lines (153 with data), 5.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""Utilities for Slideflow Studio."""
from typing import Any, List
import imgui
import os
import slideflow as sf
import numpy as np
from os.path import join, exists
from slideflow import log
from typing import Tuple, Optional
if sf.util.tf_available:
import tensorflow as tf
sf.util.allow_gpu_memory_growth()
if sf.util.torch_available:
import slideflow.model.torch
#----------------------------------------------------------------------------
LEFT_MOUSE_BUTTON = 0
RIGHT_MOUSE_BUTTON = 1
#----------------------------------------------------------------------------
class EasyDict(dict):
"""Convenience class that behaves like a dict but allows access with the attribute syntax."""
def __getattr__(self, name: str) -> Any:
try:
return self[name]
except KeyError:
raise AttributeError(name)
def __setattr__(self, name: str, value: Any) -> None:
self[name] = value
def __delattr__(self, name: str) -> None:
del self[name]
#----------------------------------------------------------------------------
def prediction_to_string(
predictions: np.ndarray,
outcomes: List[str],
is_classification: bool
) -> str:
"""Convert a prediction array to a human-readable string."""
#TODO: support multi-outcome models
if is_classification:
return f'{outcomes[str(np.argmax(predictions))]} ({np.max(predictions)*100:.1f}%)'
else:
return f'{predictions[0]:.2f}'
def _load_umap_encoders(path, model) -> EasyDict:
import tensorflow as tf
layers = [d for d in os.listdir(path) if os.path.isdir(join(path, d))]
log.debug("Layers found at path {} in _load_umap_encoders: {}".format(path, layers))
features = sf.model.Features.from_model(
model,
include_preds=True,
layers=layers,
pooling='avg'
)
outputs = []
for i, layer in enumerate(layers):
# Add outputs for each UMAP encoder
encoder = tf.keras.models.load_model(join(path, layer, 'encoder'))
encoder._name = f'{layer}_encoder'
outputs += [encoder(features.model.outputs[i])]
# Add the predictions output
outputs += [features.model.outputs[-1]]
# Build the encoder model for all layers
encoder_model = tf.keras.models.Model(
inputs=features.model.input,
outputs=outputs
)
return EasyDict(
encoder=encoder_model,
layers=layers,
range={
layer: np.load(join(path, layer, 'range_clip.npz'))['range']
for layer in layers
},
clip={
layer: np.load(join(path, layer, 'range_clip.npz'))['clip']
for layer in layers
}
)
def _load_model_and_saliency(model_path, device=None):
log.debug("Loading model at {}...".format(model_path))
_umap_encoders = None
_saliency = None
# Load a PyTorch model
if sf.util.torch_available and sf.util.path_to_ext(model_path) == 'zip':
import slideflow.model.torch
_device = sf.model.torch.torch_utils.get_device()
_model = sf.model.torch.load(model_path)
_model.to(_device)
_model.eval()
if device is not None:
_model = _model.to(device)
_saliency = sf.grad.SaliencyMap(_model, class_idx=0) #TODO: auto-update from heatmaps logit
# Load a TFLite model
elif sf.util.tf_available and sf.util.path_to_ext(model_path) == 'tflite':
interpreter = tf.lite.Interpreter(model_path)
_model = interpreter.get_signature_runner()
# Load a Tensorflow model
elif sf.util.tf_available:
import slideflow.model.tensorflow
_model = sf.model.tensorflow.load(model_path, method='weights')
_saliency = sf.grad.SaliencyMap(_model, class_idx=0) #TODO: auto-update from heatmaps logit
if exists(join(model_path, 'umap_encoders')):
_umap_encoders = _load_umap_encoders(join(model_path, 'umap_encoders'), _model)
else:
raise ValueError(f"Unable to interpret model {model_path}")
return _model, _saliency, _umap_encoders
#----------------------------------------------------------------------------
class StatusMessage:
"""A class to manage status messages."""
def __init__(
self,
viz: Any,
message: str,
description: Optional[str] = None,
*,
color: Tuple[float, float, float, float] = (0.7, 0, 0, 1),
text_color: Tuple[float, float, float, float] = (1, 1, 1, 1),
rounding: int = 0,
) -> None:
self.viz = viz
self.message = message
self.description = description
self.color = color
self.text_color = text_color
self.rounding = rounding
def render(self):
"""Render the status message."""
# Calculations.
h = self.viz.status_bar_height
r = self.viz.pixel_ratio
y_pos = int((self.viz.content_frame_height - (h * r)) / r)
size = imgui.calc_text_size(self.message)
# Center the text.
x_start = self.viz.content_width/2 - size.x/2
imgui.same_line()
imgui.set_cursor_pos_x(x_start)
# Draw the background.
draw_list = imgui.get_window_draw_list()
pad = self.viz.spacing * 2
draw_list.add_rect_filled(
x_start - pad - 4,
y_pos,
x_start + size.x + pad,
y_pos + h,
imgui.get_color_u32_rgba(*self.color),
rounding=self.rounding
)
# Draw the text.
imgui.push_style_color(imgui.COLOR_TEXT, *self.text_color)
imgui.text(self.message)
imgui.pop_style_color(1)
# Set the tooltip.
if self.description is not None:
if imgui.is_item_hovered():
imgui.set_tooltip(self.description)