napari_cellseg3d.code_models.workers_utils
Several worker-related utilities for inference and training.
Classes:
| Name | Description |
|---|---|
InferenceResult |
Class to record results of a segmentation job. |
LogSignal |
Signal to send messages to be logged from another thread. |
ONNXModelWrapper |
Class to replace torch model by ONNX Runtime session. |
QuantileNormalization |
MONAI-style transform to normalize each image in a batch individually by quantile normalization. |
QuantileNormalizationd |
MONAI-style dict transform to normalize each image in a batch individually by quantile normalization. |
RemapTensor |
Remap the values of a tensor to a new range. |
Threshold |
Threshold a tensor to 0 or 1. |
TqdmToLogSignal |
File-like object to redirect tqdm output to the logger widget in the GUI that self.log emits to. |
TrainingReport |
Class to record results of a training job. |
WeightsDownloader |
A utility class the downloads the weights of a model when needed. |
InferenceResult
dataclass
Class to record results of a segmentation job.
Source code in napari_cellseg3d/code_models/workers_utils.py
293 294 295 296 297 298 299 300 301 302 303 | |
LogSignal
Bases: WorkerBaseSignals
Signal to send messages to be logged from another thread.
Separate from Worker instances as indicated on this post_
.. _on this post: https://stackoverflow.com/questions/2970312/pyqt4-qtcore-pyqtsignal-object-has-no-attribute-connect
Methods:
| Name | Description |
|---|---|
__init__ |
Creates a LogSignal. |
Attributes:
| Name | Type | Description |
|---|---|---|
error_signal |
qtpy.QtCore.Signal: signal to be sent when some error should be emitted in main thread |
|
log_signal |
qtpy.QtCore.Signal: signal to be sent when some text should be logged |
|
log_w_replace_signal |
qtpy.QtCore.Signal: signal to be sent when some text should be logged, replacing the last line |
|
warn_signal |
qtpy.QtCore.Signal: signal to be sent when some warning should be emitted in main thread |
Source code in napari_cellseg3d/code_models/workers_utils.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
error_signal
class-attribute
instance-attribute
error_signal = Signal(Exception, str)
qtpy.QtCore.Signal: signal to be sent when some error should be emitted in main thread
log_signal
class-attribute
instance-attribute
log_signal = Signal(str)
qtpy.QtCore.Signal: signal to be sent when some text should be logged
log_w_replace_signal
class-attribute
instance-attribute
log_w_replace_signal = Signal(str)
qtpy.QtCore.Signal: signal to be sent when some text should be logged, replacing the last line
warn_signal
class-attribute
instance-attribute
warn_signal = Signal(str)
qtpy.QtCore.Signal: signal to be sent when some warning should be emitted in main thread
__init__
__init__(parent=None)
Creates a LogSignal.
Source code in napari_cellseg3d/code_models/workers_utils.py
138 139 140 | |
ONNXModelWrapper
Bases: Module
Class to replace torch model by ONNX Runtime session.
Methods:
| Name | Description |
|---|---|
__init__ |
Creates an ONNXModelWrapper. |
eval |
Dummy function. |
forward |
Wraps ONNX output in a torch tensor. |
to |
Dummy function. |
Source code in napari_cellseg3d/code_models/workers_utils.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
__init__
__init__(file_location)
Creates an ONNXModelWrapper.
Source code in napari_cellseg3d/code_models/workers_utils.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | |
eval
eval()
Dummy function.
Replaces model.eval().
Source code in napari_cellseg3d/code_models/workers_utils.py
188 189 190 191 192 193 | |
forward
forward(modeL_input)
Wraps ONNX output in a torch tensor.
Source code in napari_cellseg3d/code_models/workers_utils.py
183 184 185 186 | |
to
to(device)
Dummy function.
Replaces model.to(device).
Source code in napari_cellseg3d/code_models/workers_utils.py
195 196 197 198 199 200 | |
QuantileNormalization
Bases: Transform
MONAI-style transform to normalize each image in a batch individually by quantile normalization.
Methods:
| Name | Description |
|---|---|
__call__ |
Normalize each image in a batch individually by quantile normalization. |
Source code in napari_cellseg3d/code_models/workers_utils.py
229 230 231 232 233 234 | |
__call__
__call__(img)
Normalize each image in a batch individually by quantile normalization.
Source code in napari_cellseg3d/code_models/workers_utils.py
232 233 234 | |
QuantileNormalizationd
Bases: MapTransform
MONAI-style dict transform to normalize each image in a batch individually by quantile normalization.
Methods:
| Name | Description |
|---|---|
__call__ |
Normalize each image in a batch individually by quantile normalization. |
__init__ |
Creates a QuantileNormalizationd transform. |
normalizer |
Normalize each image in a batch individually by quantile normalization. |
Source code in napari_cellseg3d/code_models/workers_utils.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |
__call__
__call__(data)
Normalize each image in a batch individually by quantile normalization.
Source code in napari_cellseg3d/code_models/workers_utils.py
210 211 212 213 214 215 | |
__init__
__init__(keys, allow_missing_keys: bool = False)
Creates a QuantileNormalizationd transform.
Source code in napari_cellseg3d/code_models/workers_utils.py
206 207 208 | |
normalizer
normalizer(image: Tensor)
Normalize each image in a batch individually by quantile normalization.
Source code in napari_cellseg3d/code_models/workers_utils.py
217 218 219 220 221 222 223 224 225 226 | |
RemapTensor
Bases: Transform
Remap the values of a tensor to a new range.
Methods:
| Name | Description |
|---|---|
__call__ |
Remap the values of a tensor to a new range. |
__init__ |
Creates a RemapTensor transform. |
Source code in napari_cellseg3d/code_models/workers_utils.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
__call__
__call__(img)
Remap the values of a tensor to a new range.
Source code in napari_cellseg3d/code_models/workers_utils.py
251 252 253 | |
__init__
__init__(new_max, new_min)
Creates a RemapTensor transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_max
|
float
|
new maximum value |
required |
new_min
|
float
|
new minimum value |
required |
Source code in napari_cellseg3d/code_models/workers_utils.py
240 241 242 243 244 245 246 247 248 249 | |
Threshold
Bases: Transform
Threshold a tensor to 0 or 1.
Methods:
| Name | Description |
|---|---|
__call__ |
Threshold a tensor to 0 or 1. |
__init__ |
Creates a Threshold transform. |
Source code in napari_cellseg3d/code_models/workers_utils.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | |
__call__
__call__(img)
Threshold a tensor to 0 or 1.
Source code in napari_cellseg3d/code_models/workers_utils.py
287 288 289 290 | |
__init__
__init__(threshold=0.5)
Creates a Threshold transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
threshold
|
float
|
threshold value |
0.5
|
Source code in napari_cellseg3d/code_models/workers_utils.py
278 279 280 281 282 283 284 285 | |
TqdmToLogSignal
File-like object to redirect tqdm output to the logger widget in the GUI that self.log emits to.
Methods:
| Name | Description |
|---|---|
__init__ |
Creates a TqdmToLogSignal. |
flush |
Flushes the output. Unused. |
write |
Writes the output to the log_func. |
Source code in napari_cellseg3d/code_models/workers_utils.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
__init__
__init__(log_func)
Creates a TqdmToLogSignal.
Args: log_func (callable): function to call to log the output.
Source code in napari_cellseg3d/code_models/workers_utils.py
146 147 148 149 150 151 152 | |
flush
flush()
Flushes the output. Unused.
Source code in napari_cellseg3d/code_models/workers_utils.py
158 159 160 | |
write
write(x)
Writes the output to the log_func.
Source code in napari_cellseg3d/code_models/workers_utils.py
154 155 156 | |
TrainingReport
dataclass
Class to record results of a training job.
Source code in napari_cellseg3d/code_models/workers_utils.py
306 307 308 309 310 311 312 313 314 315 316 | |
WeightsDownloader
A utility class the downloads the weights of a model when needed.
Methods:
| Name | Description |
|---|---|
__init__ |
Creates a WeightsDownloader, optionally with a log widget to display the progress. |
download_weights |
Downloads a specific pretrained model. |
Source code in napari_cellseg3d/code_models/workers_utils.py
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 | |
__init__
__init__(log_widget: Optional[Log] = None)
Creates a WeightsDownloader, optionally with a log widget to display the progress.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_widget
|
Log
|
a Log to display the progress bar in. If None, uses logger.info() |
None
|
Source code in napari_cellseg3d/code_models/workers_utils.py
27 28 29 30 31 32 33 | |
download_weights
download_weights(model_name: str, model_weights_filename: str) -> Path
Downloads a specific pretrained model.
This code is adapted from DeepLabCut with permission from MWMathis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
name of the model to download |
required |
model_weights_filename
|
str
|
name of the .pth file expected for the model |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Path |
Path
|
path to the weights file, in utils.get_pretrained_weights_dir() |
Source code in napari_cellseg3d/code_models/workers_utils.py
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 | |