instance_centroids
sleap_nn.data.instance_centroids
¶
Handle calculation of instance centroids.
Functions:
| Name | Description |
|---|---|
find_points_bbox_midpoint |
Find the midpoint of the bounding box of a set of points. |
find_points_mean |
Find the mean position of a set of points, ignoring NaNs. |
generate_centroids |
Return centroids, falling back to the mean of visible nodes. |
find_points_bbox_midpoint(points)
¶
Find the midpoint of the bounding box of a set of points.
Retained as a utility for callers that explicitly want bbox-midpoint behavior.
The canonical anchor fallback used by :func:generate_centroids is
:func:find_points_mean (mean of visible nodes) — see that function for the
project-wide convention.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Tensor
|
A torch.Tensor of dtype torch.float32 and of shape (..., n_points, 2), i.e., rank >= 2. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The midpoints between the bounds of each set of points. The output will be of shape (..., 2), reducing the rank of the input by 1. NaNs will be ignored in the calculation. |
Notes
The midpoint is calculated as: xy_mid = xy_min + ((xy_max - xy_min) / 2) = ((2 * xy_min) / 2) + ((xy_max - xy_min) / 2) = (2 * xy_min + xy_max - xy_min) / 2 = (xy_min + xy_max) / 2
Source code in sleap_nn/data/instance_centroids.py
find_points_mean(points)
¶
Find the mean position of a set of points, ignoring NaNs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Tensor
|
A torch.Tensor of dtype torch.float32 and of shape (..., n_points, 2), i.e., rank >= 2. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
The NaN-ignoring mean across the |
Source code in sleap_nn/data/instance_centroids.py
generate_centroids(points, anchor_ind=None)
¶
Return centroids, falling back to the mean of visible nodes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
Tensor
|
A torch.Tensor of dtype torch.float32 and of shape (..., n_nodes, 2), i.e., rank >= 2. |
required |
anchor_ind
|
Optional[int]
|
The index of the node to use as the anchor for the centroid. If not provided, or if the anchor node is NaN (not visible) for a given instance, the centroid falls back to the NaN-ignoring mean of all visible nodes for that instance. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
The centroids of the instances. The output will be of shape (..., 2), reducing the rank of the input by 1. NaNs will be ignored in the calculation. |
Note
The missing/occluded-anchor fallback is the mean of visible nodes
(find_points_mean). Pre-#530 this was the bounding-box midpoint
(find_points_bbox_midpoint). The two modes are tracked for a future
revisit in https://github.com/talmolab/sleap-nn/issues/586 — keep this
consistent with the centroid target generated during training.