peaks
sleap_nn.inference.ops.peaks
¶
Peak finding ops for confidence maps.
Functions provided:
- :func:
morphological_dilation— pure-PyTorch 3×3 dilation (max over the 8 neighbors). - :func:
morphological_dilation_split— the same 8-neighbor max, split into the neighbors before and after each pixel in raster order. - :func:
integral_regression— sub-pixel refinement by integrating expected coordinates over a local patch. - :func:
find_global_peaks_rough/ :func:find_global_peaks— single peak per (sample, channel) for centroid / single-instance confmaps. - :func:
find_local_peaks_rough/ :func:find_local_peaks— multi-peak finding for bottom-up confmaps. - :func:
quarter_offset_refinement— sub-pixel refinement of global peaks (refinement="local").
PR 1 is a pure relocation — every function below preserves the exact
signature and behavior of its old home in sleap_nn/inference/peak_finding.py.
Functions:
| Name | Description |
|---|---|
find_global_peaks |
Find global peaks with optional refinement. |
find_global_peaks_rough |
Find the global maximum for each sample and channel. |
find_local_peaks |
Find local peaks with optional refinement. |
find_local_peaks_rough |
Find local maxima via non-maximum suppression. |
integral_regression |
Compute regression by integrating over the confidence maps on a grid. |
morphological_dilation |
Compute the per-pixel max over the 8-neighborhood (excluding center). |
morphological_dilation_split |
Compute the 8-neighbor max split into earlier and later neighbors. |
quarter_offset_refinement |
Refine global peaks with the quarter-offset rule ( |
find_global_peaks(cms, threshold=0.2, refinement=None, integral_patch_size=5)
¶
Find global peaks with optional refinement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cms
|
Tensor
|
|
required |
threshold
|
float
|
Peaks below this are NaN-padded. |
0.2
|
refinement
|
Optional[str]
|
|
None
|
integral_patch_size
|
int
|
Side length of the refinement patch. |
5
|
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in sleap_nn/inference/ops/peaks.py
find_global_peaks_rough(cms, threshold=0.1)
¶
Find the global maximum for each sample and channel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cms
|
Tensor
|
|
required |
threshold
|
float
|
Peaks below this are replaced with NaN. |
0.1
|
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
|
Source code in sleap_nn/inference/ops/peaks.py
find_local_peaks(cms, threshold=0.2, refinement=None, integral_patch_size=5)
¶
Find local peaks with optional refinement.
Same return shape as :func:find_local_peaks_rough. refinement
accepts None / "none" (no refinement), "integral" or
"local" (quarter-pixel offset towards the higher neighbor, see
:func:quarter_offset_refinement); other names raise ValueError.
Source code in sleap_nn/inference/ops/peaks.py
find_local_peaks_rough(cms, threshold=0.2)
¶
Find local maxima via non-maximum suppression.
A pixel is a peak if it is above threshold and greater than all 8 of
its neighbors. A flat top of equal pixels (common with fp16 confidence
maps) is one peak if it fits in a 2x2 block (2 pixels, an L of 3, or all
4) and is higher than every pixel around it; its point is the mean of the
pixels' coordinates, so it can be fractional. A wider flat top, or one
next to a higher pixel, gives no peak. See #781.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cms
|
Tensor
|
|
required |
threshold
|
float
|
Peaks below this are dropped. |
0.2
|
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor, Tensor, Tensor]
|
|
Source code in sleap_nn/inference/ops/peaks.py
integral_regression(cms, xv, yv)
¶
Compute regression by integrating over the confidence maps on a grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cms
|
Tensor
|
Confidence maps with shape |
required |
xv
|
Tensor
|
|
required |
yv
|
Tensor
|
|
required |
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
|
Source code in sleap_nn/inference/ops/peaks.py
morphological_dilation(image, kernel)
¶
Compute the per-pixel max over the 8-neighborhood (excluding center).
The kernel argument is preserved for API compatibility but is
currently ignored — the 8-neighbor pattern is hardcoded as shifted slices
of a padded image combined with elementwise torch.maximum, which
exports to ONNX (PR 5 of #508 rewrote the original Tensor.unfold
formulation that the legacy ONNX exporter rejected).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Tensor
|
Input tensor of shape |
required |
kernel
|
Tensor
|
Legacy 3×3 NMS kernel; unused. Kept so existing callers continue to work without modification. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Same shape as |
Source code in sleap_nn/inference/ops/peaks.py
morphological_dilation_split(image)
¶
Compute the 8-neighbor max split into earlier and later neighbors.
The 8 neighbors of each pixel are split by raster order (row, then column):
the 4 that come before it (NW, N, NE, W) and the 4 that come after it
(E, SW, S, SE). The elementwise max of the two outputs equals
:func:morphological_dilation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Tensor
|
Input tensor of shape |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
|
Source code in sleap_nn/inference/ops/peaks.py
quarter_offset_refinement(cms, rough_peaks)
¶
Refine global peaks with the quarter-offset rule (refinement="local").
Each coordinate moves 0.25 confmap pixels towards its higher neighbor
(x += 0.25 * sign(cm[y, x+1] - cm[y, x-1]), same for y), as in
SLEAP's "local" refinement and the Hourglass/SimpleBaseline decoders.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cms
|
Tensor
|
|
required |
rough_peaks
|
Tensor
|
|
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
|