peak_finding
sleap_nn.inference.peak_finding
¶
Peak finding for inference.
Functions:
| Name | Description |
|---|---|
crop_bboxes |
Crop bounding boxes from a batch of images using fast tensor indexing. |
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. |
crop_bboxes(images, bboxes, sample_inds)
¶
Crop bounding boxes from a batch of images using fast tensor indexing.
This uses tensor unfold operations to extract patches, which is significantly faster than kornia's crop_and_resize (17-51x speedup) as it avoids perspective transform computations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images
|
Tensor
|
Tensor of shape (samples, channels, height, width) of a batch of images. |
required |
bboxes
|
Tensor
|
Tensor of shape (n_bboxes, 4, 2) and dtype torch.float32, where n_bboxes
is the number of centroids, and the second dimension represents the four
corner points of the bounding boxes, each with x and y coordinates.
The order of the corners follows a clockwise arrangement: top-left,
top-right, bottom-right, and bottom-left. This can be generated from
centroids using |
required |
sample_inds
|
Tensor
|
Tensor of shape (n_bboxes,) specifying which samples each bounding box should be cropped from. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
A tensor of shape (n_bboxes, channels, crop_height, crop_width) of the same dtype as the input image. The crop size is inferred from the bounding box coordinates. |
Notes
This function expects bounding boxes with coordinates at the centers of the pixels in the box limits. Technically, the box will span (x1 - 0.5, x2 + 0.5) and (y1 - 0.5, y2 + 0.5).
For example, a 3x3 patch centered at (1, 1) would be specified by
(y1, x1, y2, x2) = (0, 0, 2, 2). This would be exactly equivalent to indexing
the image with image[:, :, 0:3, 0:3].
See also: make_centered_bboxes
Source code in sleap_nn/inference/peak_finding.py
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
|
Confidence maps. Tensor of shape (samples, channels, height, width). |
required |
threshold
|
float
|
Minimum confidence threshold. Peaks with values below this will ignored. |
0.2
|
refinement
|
Optional[str]
|
If |
None
|
integral_patch_size
|
int
|
Size of patches to crop around each rough peak as an integer scalar. |
5
|
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
A tuple of (peak_points, peak_vals). peak_points: float32 tensor of shape (samples, channels, 2), where the last axis indicates peak locations in xy order. peak_vals: float32 tensor of shape (samples, channels) containing the values at the peak points. |
Source code in sleap_nn/inference/peak_finding.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
|
Tensor of shape (samples, channels, height, width). |
required |
threshold
|
float
|
Scalar float specifying the minimum confidence value for peaks. Peaks with values below this threshold will be replaced with NaNs. |
0.1
|
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
A tuple of (peak_points, peak_vals). peak_points: float32 tensor of shape (samples, channels, 2), where the last axis indicates peak locations in xy order. peak_vals: float32 tensor of shape (samples, channels) containing the values at the peak points. |
Source code in sleap_nn/inference/peak_finding.py
find_local_peaks(cms, threshold=0.2, refinement=None, integral_patch_size=5)
¶
Find local peaks with optional refinement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cms
|
Tensor
|
Confidence maps. Tensor of shape (samples, channels, height, width). |
required |
threshold
|
float
|
Minimum confidence threshold. Peaks with values below this will ignored. |
0.2
|
refinement
|
Optional[str]
|
If |
None
|
integral_patch_size
|
int
|
Size of patches to crop around each rough peak as an integer scalar. |
5
|
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor, Tensor, Tensor]
|
A tuple of (peak_points, peak_vals, peak_sample_inds, peak_channel_inds). peak_points: float32 tensor of shape (n_peaks, 2), where the last axis indicates peak locations in xy order. peak_vals: float32 tensor of shape (n_peaks,) containing the values at the peak points. peak_sample_inds: int32 tensor of shape (n_peaks,) containing the indices of the sample each peak belongs to. peak_channel_inds: int32 tensor of shape (n_peaks,) containing the indices of the channel each peak belongs to. |
Source code in sleap_nn/inference/peak_finding.py
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 365 366 367 368 369 370 371 372 373 374 375 376 | |
find_local_peaks_rough(cms, threshold=0.2)
¶
Find local maxima via non-maximum suppression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cms
|
Tensor
|
Tensor of shape (samples, channels, height, width). |
required |
threshold
|
float
|
Scalar float specifying the minimum confidence value for peaks. Peaks with values below this threshold will not be returned. |
0.2
|
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor, Tensor, Tensor]
|
A tuple of (peak_points, peak_vals, peak_sample_inds, peak_channel_inds). peak_points: float32 tensor of shape (n_peaks, 2), where the last axis indicates peak locations in xy order. peak_vals: float32 tensor of shape (n_peaks,) containing the values at the peak points. peak_sample_inds: int32 tensor of shape (n_peaks,) containing the indices of the sample each peak belongs to. peak_channel_inds: int32 tensor of shape (n_peaks,) containing the indices of the channel each peak belongs to. |
Source code in sleap_nn/inference/peak_finding.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 (samples, channels, height, width). |
required |
xv
|
Tensor
|
X grid vector torch.float32 of grid coordinates to sample. |
required |
yv
|
Tensor
|
Y grid vector torch.float32 of grid coordinates to sample. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Tensor, Tensor]
|
A tuple of (x_hat, y_hat) with the regressed x- and y-coordinates for each channel of the confidence maps. x_hat and y_hat are of shape (samples, channels) |