segmentation_convert
sleap_nn.inference.segmentation_convert
¶
sleap-nn <-> sleap-io conversion helpers for instance segmentation masks.
Single owner of the mapping between a predicted boolean mask array and a
sio.PredictedSegmentationMask (mirrors centroid_convert.py for
centroids), and of the inverse decode used by every mask consumer. Keeping
this in one place means the Outputs packaging path, the eval IoU, the
training data loader, and any future export path emit and read masks
identically.
Functions:
| Name | Description |
|---|---|
build_predicted_roi |
Build a Douglas-Peucker-simplified |
build_predicted_segmentation_mask |
Build a |
decode_mask_to_image_res |
Decode a sio segmentation mask to a boolean array on the IMAGE-pixel grid. |
build_predicted_roi(mask_obj, score, epsilon=0.01)
¶
Build a Douglas-Peucker-simplified sio.PredictedROI from a sio mask.
mask_obj must carry its scale/offset so to_polygon() returns
IMAGE-space geometry. The exterior silhouette is simplified with Shapely's
Douglas-Peucker (geometry.simplify) at a tolerance of epsilon times
the silhouette perimeter.
Cost note: to_polygon() builds and unions one box per RLE run, so this is
CPU-heavy for fragmented/speckled masks (thousands of runs). Callers exposing
polygon output should pair it with mask_cleanup (keep-largest-CC) so each
instance is a single clean component before polygonization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask_obj
|
'sio.SegmentationMask'
|
A sio (Predicted)SegmentationMask. |
required |
score
|
float
|
Detection confidence carried onto the ROI. |
required |
epsilon
|
float
|
Simplification tolerance as a fraction of the perimeter. |
0.01
|
Returns:
| Type | Description |
|---|---|
'Optional[sio.PredictedROI]'
|
A |
Source code in sleap_nn/inference/segmentation_convert.py
build_predicted_segmentation_mask(mask, score, scale=(1.0, 1.0), offset=(0.0, 0.0), instance=None, track=None, tracking_score=None)
¶
Build a sio.PredictedSegmentationMask from a boolean mask array.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
ndarray
|
2-D boolean (or 0/1) array. At the model output-stride resolution
when |
required |
score
|
float
|
Detection confidence (the instance-center peak value). |
required |
scale
|
Tuple[float, float]
|
sio resolution ratio |
(1.0, 1.0)
|
offset
|
Tuple[float, float]
|
Origin |
(0.0, 0.0)
|
instance
|
'Optional[sio.PredictedInstance]'
|
Optional paired |
None
|
track
|
'Optional[sio.Track]'
|
Optional |
None
|
tracking_score
|
Optional[float]
|
Optional track-assignment confidence set on the mask's
|
None
|
Returns:
| Type | Description |
|---|---|
'sio.PredictedSegmentationMask'
|
A |
Source code in sleap_nn/inference/segmentation_convert.py
decode_mask_to_image_res(m)
¶
Decode a sio segmentation mask to a boolean array on the IMAGE-pixel grid.
sio .data decodes at the mask's stored (possibly output-stride)
resolution. When the mask carries a non-identity scale (masks encoded
at output-stride by :class:SegmentationLayer), this nearest-neighbor
resamples it up to its image extent so every consumer — eval IoU
(:func:sleap_nn.evaluation._frame_masks) and the segmentation training data
loader — compares masks on a common original-image grid. Scale-1,
offset-0 masks (legacy full-res GT/preds, all bottom-up predictions) take a
zero-copy fast path, so old .slp files behave exactly as before.
A non-identity offset (the crop origin (x, y) of a top-down
crop-centered mask) is baked in by top-left zero-padding so the mask lands
at its full-frame location for IoU/placement; without this, two crops at
different offsets would both decode to the origin and collide (sio
resampled/image_extent drop the offset). Offset-0 masks are
unaffected.
Note
image_extent can differ from the true frame size by ±1 px because
the mask resolution is round(orig * scale) at encode time (e.g. a
1024-px dimension can recover as 1025). It is therefore NOT authoritative
for the frame dimensions; callers that index a real image must clamp to
the actual frame size. For IoU on a shared max-canvas the ±1 lands on a
background row/col and does not change the result.