topdown_segmentation
sleap_nn.inference.layers.topdown_segmentation
¶
Top-down (crop-centered) instance-segmentation inference layers (#622).
Two pieces, mirroring the keypoint top-down stack:
-
:class:
CenteredInstanceMaskLayer— the stage-2 analog of :class:~sleap_nn.inference.layers.centered_instance.CenteredInstanceLayer. Runs the trainedcentered_instance_segmentationmodel on per-instance crops and returns one boolean foreground mask per crop (at the head's output-stride resolution) instead of keypoints. The crop-resolution masks + per-crop scores are carried onOutputsfor the composed layer to place. -
:class:
TopDownSegmentationLayer— subclasses :class:~sleap_nn.inference.layers.topdown.TopDownLayerto reuse its stage-1 (centroid) + sizematch + crop machinery verbatim, overriding only the stage-2 emission. Each crop mask is emitted intoOutputs.pred_maskswith the DQ5 offset/scale contract (see :meth:_run_stage_2), sodecode_mask_to_image_resupsamples the crop mask to the crop's image-space size and (offset-aware) top-left pads it to land at the correct full-frame location.
Classes:
| Name | Description |
|---|---|
CenteredInstanceMaskLayer |
Per-crop foreground-mask prediction layer (top-down stage 2). |
TopDownSegmentationLayer |
Composed centroid + per-crop-mask two-stage segmentation layer. |
CenteredInstanceMaskLayer
¶
Bases: InferenceLayer
Per-crop foreground-mask prediction layer (top-down stage 2).
Runs a trained centered_instance_segmentation model on per-instance
crops and decodes the SegmentationHead logits into one boolean
foreground mask per crop. The structural twin of
:class:CenteredInstanceLayer, but returns masks (on Outputs.crops as
a stacked tensor + a per-crop score) rather than keypoints — the composed
:class:TopDownSegmentationLayer places each crop mask back into the frame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
ModelBackend
|
Runtime backend wrapping the seg Lightning module. Its
|
required |
output_stride
|
int
|
Head map → crop-pixel stride (default 2). |
required |
max_stride
|
int
|
Backbone max stride; crops are padded to a multiple of it. |
1
|
fg_threshold
|
float
|
Foreground probability threshold for binarization (sigmoid(logits) > fg_threshold). Default 0.5. |
0.5
|
preprocess_config / postprocess_config
|
Standard knobs. The crops are
already sized (extracted from the sizematched image), so only the
model's own |
required |
Methods:
| Name | Description |
|---|---|
__init__ |
Compose the layer with default configs when omitted. |
postprocess |
Decode |
Attributes:
| Name | Type | Description |
|---|---|---|
warmup_input_shape |
Tiny single-channel warmup shape. |
Source code in sleap_nn/inference/layers/topdown_segmentation.py
warmup_input_shape
property
¶
Tiny single-channel warmup shape.
__init__(backend, output_stride, max_stride=1, fg_threshold=0.5, preprocess_config=None, postprocess_config=None)
¶
Compose the layer with default configs when omitted.
Source code in sleap_nn/inference/layers/topdown_segmentation.py
postprocess(raw_out, info)
¶
Decode SegmentationHead logits → one bool mask per crop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_out
|
dict
|
Backend output dict carrying the seg-head logits. |
required |
info
|
PreprocInfo
|
Preprocessing metadata (unused for placement — the composed layer owns the crop→frame mapping). |
required |
Returns:
| Type | Description |
|---|---|
Outputs
|
|
Source code in sleap_nn/inference/layers/topdown_segmentation.py
TopDownSegmentationLayer
¶
Bases: TopDownLayer
Composed centroid + per-crop-mask two-stage segmentation layer.
Subclasses :class:TopDownLayer to reuse stage 1 (centroid) + sizematch +
crop extraction verbatim, overriding only :meth:_run_stage_2 to emit
per-crop masks into Outputs.pred_masks instead of keypoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
centroid_layer
|
CentroidLayer
|
Stage-1 :class: |
required |
centered_instance_layer
|
CenteredInstanceMaskLayer
|
Stage-2 :class: |
required |
crop_size
|
Tuple[int, int]
|
|
required |
mask_output
|
str
|
Output representation forwarded to |
'mask'
|
polygon_epsilon
|
float
|
Douglas-Peucker tolerance for polygon/both output. |
0.01
|
centroid_nms / centroid_nms_threshold
|
Optional centroid dedup before stage 2 (inherited). |
required |
Methods:
| Name | Description |
|---|---|
__init__ |
Stash inner layers, crop size, and mask packaging knobs. |
Source code in sleap_nn/inference/layers/topdown_segmentation.py
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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 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 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
__init__(centroid_layer, centered_instance_layer, crop_size, mask_output='mask', polygon_epsilon=0.01, centroid_nms=False, centroid_nms_threshold=0.5)
¶
Stash inner layers, crop size, and mask packaging knobs.