Segmentation¶
optari.segmentation.segmenter ¶
Segmentation model adapters.
ModelAdapterBase is the extension point for a new segmentation model: a subclass implements preprocess(), infer() and postprocess() for its own model format, and the model registry references it by class name in adapter_class.
ModelAdapterBase ¶
ModelAdapterBase(model_config)
Bases: ABC
Base class for the segmentation adapters. Adapters have to implement preprocess, infer and postprocess methods, operating on single 2D grayscale frames.
Store the model config and its class id -> name mapping.
Source code in src/optari/segmentation/segmenter.py
106 107 108 109 | |
infer
abstractmethod
¶
infer(frame_2d)
Run the model on a preprocessed frame and return the raw predicted mask.
Source code in src/optari/segmentation/segmenter.py
120 121 122 123 | |
postprocess
abstractmethod
¶
postprocess(mask_2d, frame_shape)
Turn the raw predicted mask into the final class id map for the original frame.
Implementations resize the mask back to frame_shape and clean up the class predictions.
Source code in src/optari/segmentation/segmenter.py
125 126 127 128 129 130 131 132 133 134 | |
predict ¶
predict(frames)
Segment a batch of frames.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frames
|
ndarray
|
(nframes, H, W), use frames[np.newaxis] for a single frame. |
required |
Source code in src/optari/segmentation/segmenter.py
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 | |
preprocess
abstractmethod
¶
preprocess(frame_2d)
Prepare a single 2D grayscale frame for infer().
Implementations resize and normalize the frame to the model's expected input shape.
Source code in src/optari/segmentation/segmenter.py
111 112 113 114 115 116 117 118 | |
SegmentationModelConfig
dataclass
¶
SegmentationModelConfig(model_id, filename, input_height, input_width, class_names, default_class, adapter_class, postprocessing_config=None, url=None)
Config for a segmentation model.
SegmentationResult
dataclass
¶
SegmentationResult(seg, class_names, blank=False)
Result of segmenting a single 2D US frame.
seg
(H, W) integer class id map.
class_names: Mapping of class id -> display name. blank: The frame was constant (e.g. a zero-padded frame that was never acquired) and got an empty mask instead of a prediction.
UKErUSSegAdapter ¶
UKErUSSegAdapter(model_config)
Bases: ModelAdapterBase
Ultrasound segmentation adapter for scans taken at the university hospital of Erlangen using itheras Acuity Echo MSOT scanners. Requires trained ONNX model file and a config entry in segmentation_models.json.
Load the ONNX model weights and open an inference session.
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
The model weights have not been downloaded yet. |
Source code in src/optari/segmentation/segmenter.py
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 201 202 203 204 205 206 207 208 | |
infer ¶
infer(frame_2d)
Run the ONNX session on the frame and return the per-pixel class id map.
Source code in src/optari/segmentation/segmenter.py
222 223 224 225 226 227 228 229 230 231 | |
postprocess ¶
postprocess(mask_2d, frame_shape)
Perform custom postprocessing on the segmentation masks
Source code in src/optari/segmentation/segmenter.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
preprocess ¶
preprocess(frame_2d)
Resize the frame to the model's input shape and apply z-score normalization.
Source code in src/optari/segmentation/segmenter.py
210 211 212 213 214 215 216 217 218 219 220 | |
create_segmenter ¶
create_segmenter(model_config)
Factory to create a segmentation adapter from a config.
Source code in src/optari/segmentation/segmenter.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
load_model_registry ¶
load_model_registry()
Load model configs and return a mapping of model_id -> config.
Source code in src/optari/segmentation/segmenter.py
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 | |
optari.segmentation.segmentation_presets ¶
Validation and normalization for segmentation preset settings.
validate_segmentation_settings ¶
validate_segmentation_settings(settings, class_names=None)
Validate segmentation preset settings and normalize
Source code in src/optari/segmentation/segmentation_presets.py
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 | |