segmentation
sleap_nn.inference.layers.segmentation
¶
SegmentationLayer — bottom-up instance segmentation inference.
Wraps a trained BottomUpSegmentationLightningModule (whose forward
returns the three head maps, with sigmoid already applied to the foreground
head). postprocess groups foreground pixels into instances via the
predicted instance-center offsets and packages them into Outputs.pred_masks.
By default each mask is kept at the model output-stride resolution with a sio
scale/offset carrying the mapping back to image pixels (the #618
~stride^2 RLE win, lossless at model resolution); full_res_masks restores
the legacy original-resolution upsample.
Classes:
| Name | Description |
|---|---|
SegmentationLayer |
Bottom-up instance-segmentation prediction layer. |
SemanticSegmentationLayer |
Whole-frame binary foreground/background segmentation (no grouping). |
SegmentationLayer
¶
Bases: InferenceLayer
Bottom-up instance-segmentation prediction layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
ModelBackend
|
Runtime backend wrapping the segmentation Lightning module.
Its |
required |
output_stride
|
int
|
Stride of the head output maps relative to the model input (all three heads share it). |
required |
max_stride
|
int
|
Backbone max stride; the input is padded to a multiple of it during preprocessing. |
1
|
fg_threshold
|
float
|
Foreground probability threshold for binarization. |
0.5
|
min_mask_area
|
int
|
Minimum area (in ORIGINAL-image pixels) for a predicted
mask to be kept. Masks smaller than this are dropped — useful for
suppressing tiny spurious blobs (over-segmentation). |
0
|
max_instances
|
Optional[int]
|
Optional cap on instances per frame. When more centers
are detected, only the highest-scoring |
None
|
center_nms_kernel
|
int
|
Odd window size for center-peak NMS. Larger values
merge nearby duplicate centers (a lever against over-segmentation).
Default |
3
|
mask_cleanup
|
bool
|
When |
False
|
mask_cleanup_radius
|
int
|
Morphological open->close kernel radius (in
output-stride pixels) applied during |
0
|
distance_gate_alpha
|
Optional[float]
|
Adaptive distance-gate strength forwarded to
:func: |
None
|
merge_fragments
|
bool
|
When |
False
|
merge_method
|
str
|
Merge agglomeration — |
'greedy'
|
merge_thresholds
|
tuple
|
Decreasing affinity thresholds for the greedy merge
phases (default |
(0.85, 0.6, 0.4)
|
merge_w_valley
|
float
|
Weight on the center-valley ridge merge term (default
|
1.0
|
merge_w_offset
|
float
|
Weight on the offset-agreement merge term (default
|
0.25
|
merge_dilate
|
int
|
Dilation iterations for the merge contact test (default
|
1
|
full_res_masks
|
bool
|
When |
False
|
mask_output
|
str
|
Output representation — |
'mask'
|
polygon_epsilon
|
float
|
Douglas-Peucker tolerance (fraction of perimeter) for
|
0.01
|
preprocess_config / postprocess_config
|
Standard knobs;
|
required |
Methods:
| Name | Description |
|---|---|
__init__ |
Store thresholds and standard configs. |
postprocess |
Group foreground pixels into instances and package masks. |
Attributes:
| Name | Type | Description |
|---|---|---|
warmup_input_shape |
Tiny single-channel warmup shape. |
Source code in sleap_nn/inference/layers/segmentation.py
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 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 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
warmup_input_shape
property
¶
Tiny single-channel warmup shape.
__init__(backend, output_stride, max_stride=1, fg_threshold=0.5, min_mask_area=0, max_instances=None, center_nms_kernel=3, mask_cleanup=False, mask_cleanup_radius=0, distance_gate_alpha=None, merge_fragments=False, merge_method='greedy', merge_thresholds=(0.85, 0.6, 0.4), merge_w_valley=1.0, merge_w_offset=0.25, merge_dilate=1, full_res_masks=False, mask_output='mask', polygon_epsilon=0.01, preprocess_config=None, postprocess_config=None)
¶
Store thresholds and standard configs.
Source code in sleap_nn/inference/layers/segmentation.py
postprocess(raw_out, info)
¶
Group foreground pixels into instances and package masks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_out
|
dict
|
Backend output dict with the three segmentation heads. |
required |
info
|
PreprocInfo
|
Preprocessing metadata for mapping masks back to the original image resolution. |
required |
Returns:
| Type | Description |
|---|---|
Outputs
|
|
Source code in sleap_nn/inference/layers/segmentation.py
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 | |
SemanticSegmentationLayer
¶
Bases: SegmentationLayer
Whole-frame binary foreground/background segmentation (no grouping).
The single-head twin of :class:SegmentationLayer. Wraps a trained
whole-frame SemanticSegmentationLightningModule whose forward returns
{"SegmentationHead": sigmoid(logits)} (fg only — NO instance-center or
center-offset heads). postprocess thresholds the foreground probability
at fg_threshold into ONE whole-frame mask (the union of all foreground,
which MAY be disconnected — it is a semantic, not an instance, mask), then
packages it into Outputs.pred_masks with the same output-stride / scale /
offset contract as :class:SegmentationLayer. No
group_instances_from_offsets / merge_instances — there is no notion of
instances here.
Reuses :meth:SegmentationLayer._mask_to_stride /
:meth:SegmentationLayer._mask_to_original (pure geometry) and the
InferenceLayer default preprocess/predict/warmup verbatim;
overrides only __init__ (drops every grouping/merge knob) and
postprocess (fg-only, one mask per frame).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
ModelBackend
|
Runtime backend wrapping the semantic-seg Lightning module. Its
|
required |
output_stride
|
int
|
Stride of the head output map relative to the model input. |
required |
max_stride
|
int
|
Backbone max stride; the input is padded to a multiple of it during preprocessing. |
1
|
fg_threshold
|
float
|
Foreground probability threshold for binarization. |
0.5
|
min_mask_area
|
int
|
Minimum area (in ORIGINAL-image pixels) for the frame's
foreground mask to be kept. |
0
|
full_res_masks
|
bool
|
When |
False
|
mask_output / polygon_epsilon
|
Output-packaging knobs read by the Predictor
and forwarded to |
required | |
preprocess_config / postprocess_config
|
Standard knobs. |
required |
Methods:
| Name | Description |
|---|---|
__init__ |
Store the fg threshold + packaging knobs (no grouping/merge knobs). |
postprocess |
Threshold the foreground head → ONE mask per frame (no grouping). |
Source code in sleap_nn/inference/layers/segmentation.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 | |
__init__(backend, output_stride, max_stride=1, fg_threshold=0.5, min_mask_area=0, full_res_masks=False, mask_output='mask', polygon_epsilon=0.01, preprocess_config=None, postprocess_config=None)
¶
Store the fg threshold + packaging knobs (no grouping/merge knobs).
Source code in sleap_nn/inference/layers/segmentation.py
postprocess(raw_out, info)
¶
Threshold the foreground head → ONE mask per frame (no grouping).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_out
|
dict
|
Backend output dict carrying |
required |
info
|
PreprocInfo
|
Preprocessing metadata for mapping the mask back to the original image resolution. |
required |
Returns:
| Type | Description |
|---|---|
Outputs
|
|