normalization
sleap_nn.data.normalization
¶
This module implements data pipeline blocks for normalization operations.
Functions:
| Name | Description |
|---|---|
apply_normalization |
Normalize image tensor from uint8 [0, 255] to float32 [0, 1]. |
convert_to_grayscale |
Convert given image to Grayscale image (single-channel). |
convert_to_rgb |
Convert given image to RGB image (three-channel image). |
normalize_on_gpu |
Normalize image tensor on GPU after transfer. |
apply_normalization(image)
¶
Normalize image tensor from uint8 [0, 255] to float32 [0, 1].
This function is used during training data preprocessing where augmentation operations (kornia) require float32 input.
For inference, normalization is deferred to GPU via normalize_on_gpu() in the
model's forward() method to reduce PCIe bandwidth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Tensor
|
Tensor image (typically uint8 with values in [0, 255]). |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Float32 tensor normalized to [0, 1] range. |
Source code in sleap_nn/data/normalization.py
convert_to_grayscale(image)
¶
Convert given image to Grayscale image (single-channel).
This functions converts the input image to grayscale only if the given image is not a single-channeled image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Tensor
|
Tensor image of shape (..., 3, H, W) |
required |
Returns:
| Type | Description |
|---|---|
|
Tensor image of shape (..., 1, H, W). |
Source code in sleap_nn/data/normalization.py
convert_to_rgb(image)
¶
Convert given image to RGB image (three-channel image).
This functions converts the input image to RGB only if the given image is not a RGB image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Tensor
|
Tensor image of shape (..., 1, H, W) |
required |
Returns:
| Type | Description |
|---|---|
|
Tensor image of shape (..., 3, H, W). |
Source code in sleap_nn/data/normalization.py
normalize_on_gpu(image)
¶
Normalize image tensor on GPU after transfer.
This function is called in the model's forward() method after the image has been transferred to GPU. It converts uint8 images to float32 and normalizes to [0, 1].
By performing normalization on GPU after transfer, we reduce PCIe bandwidth by 4x (transferring 1 byte/pixel as uint8 instead of 4 bytes/pixel as float32). This provides up to 17x speedup for the transfer+normalization stage.
This function handles two cases: 1. uint8 tensor with values in [0, 255] -> convert to float32 and divide by 255 2. float32 tensor with values in [0, 255] (e.g., from preprocessing that cast to float32 without normalizing) -> divide by 255
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
Tensor
|
Tensor image that may be uint8 or float32 with values in [0, 255] range. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Float32 tensor normalized to [0, 1] range. |