instance_cropping
sleap_nn.data.instance_cropping
¶
Handle cropping of instances.
Functions:
Name | Description |
---|---|
find_instance_crop_size |
Compute the size of the largest instance bounding box from labels. |
generate_crops |
Generate cropped image for the given centroid. |
make_centered_bboxes |
Create centered bounding boxes around centroid. |
find_instance_crop_size(labels, padding=0, maximum_stride=2, input_scaling=1.0, min_crop_size=None)
¶
Compute the size of the largest instance bounding box from labels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
labels
|
Labels
|
A |
required |
padding
|
int
|
Integer number of pixels to add to the bounds as margin padding. |
0
|
maximum_stride
|
int
|
Ensure that the returned crop size is divisible by this value. Useful for ensuring that the crop size will not be truncated in a given architecture. |
2
|
input_scaling
|
float
|
Float factor indicating the scale of the input images if any scaling will be done before cropping. |
1.0
|
min_crop_size
|
Optional[int]
|
The crop size set by the user. |
None
|
Returns:
Type | Description |
---|---|
int
|
An integer crop size denoting the length of the side of the bounding boxes that
will contain the instances when cropped. The returned crop size will be larger
or equal to the input This accounts for stride, padding and scaling when ensuring divisibility. |
Source code in sleap_nn/data/instance_cropping.py
generate_crops(image, instance, centroid, crop_size)
¶
Generate cropped image for the given centroid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image
|
Tensor
|
Input source image. (n_samples, C, H, W) |
required |
instance
|
Tensor
|
Keypoints for the instance to be cropped. (n_nodes, 2) |
required |
centroid
|
Tensor
|
Centroid of the instance to be cropped. (2) |
required |
crop_size
|
Tuple[int]
|
(height, width) of the crop to be generated. |
required |
Returns:
Type | Description |
---|---|
Dict[str, Tensor]
|
A dictionary with cropped images, bounding box for the cropped instance, keypoints and centroids adjusted to the crop. |
Source code in sleap_nn/data/instance_cropping.py
make_centered_bboxes(centroids, box_height, box_width)
¶
Create centered bounding boxes around centroid.
To be used with kornia.geometry.transform.crop_and_resize
in the following
(clockwise) order: top-left, top-right, bottom-right and bottom-left.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
centroids
|
Tensor
|
A tensor of centroids with shape (n_centroids, 2), where n_centroids is the number of centroids, and the last dimension represents x and y coordinates. |
required |
box_height
|
int
|
The desired height of the bounding boxes. |
required |
box_width
|
int
|
The desired width of the bounding boxes. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor: A tensor containing bounding box coordinates for each centroid. The output tensor has shape (n_centroids, 4, 2), where n_centroids 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. |