utils
sleap_nn.data.utils
¶
Miscellaneous utility functions for data processing.
Functions:
Name | Description |
---|---|
check_cache_memory |
Check memory requirements for in-memory caching dataset pipeline. |
check_memory |
Return memory required for caching the image samples from a single labels object. |
ensure_list |
Convert the input into a list if it is not already. |
expand_to_rank |
Expand a tensor to a target rank by adding singleton dimensions in PyTorch. |
gaussian_pdf |
Compute the PDF of an unnormalized 0-centered Gaussian distribution. |
make_grid_vectors |
Make sampling grid vectors from image dimensions. |
check_cache_memory(train_labels, val_labels, memory_buffer=0.2)
¶
Check memory requirements for in-memory caching dataset pipeline.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
train_labels
|
List[Labels]
|
List of |
required |
val_labels
|
List[Labels]
|
List of |
required |
memory_buffer
|
float
|
Fraction of the total image memory required for caching that should be reserved as a buffer. |
0.2
|
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the total memory required for caching is within available system memory, False otherwise. |
Source code in sleap_nn/data/utils.py
check_memory(labels)
¶
Return memory required for caching the image samples from a single labels object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
labels
|
Labels
|
A |
required |
Returns:
Type | Description |
---|---|
int
|
Memory in bytes required to cache the image samples from the labels object. |
Source code in sleap_nn/data/utils.py
ensure_list(x)
¶
expand_to_rank(x, target_rank, prepend=True)
¶
Expand a tensor to a target rank by adding singleton dimensions in PyTorch.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
Any |
required |
target_rank
|
int
|
Rank to expand the input to. |
required |
prepend
|
bool
|
If True, singleton dimensions are added before the first axis of the data. If False, singleton dimensions are added after the last axis. |
True
|
Returns:
Type | Description |
---|---|
Tensor
|
The expanded tensor of the same dtype as the input, but with rank |
Source code in sleap_nn/data/utils.py
gaussian_pdf(x, sigma)
¶
Compute the PDF of an unnormalized 0-centered Gaussian distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
Tensor
|
A tensor of dtype torch.float32 with values to compute the PDF for. |
required |
sigma
|
float
|
Standard deviation of the Gaussian distribution. |
required |
Returns:
Type | Description |
---|---|
Tensor
|
A tensor of the same shape as |
Source code in sleap_nn/data/utils.py
make_grid_vectors(image_height, image_width, output_stride=1)
¶
Make sampling grid vectors from image dimensions.
This is a useful function for creating the x- and y-vectors that define a sampling grid over an image space. These vectors can be used to generate a full meshgrid or for equivalent broadcasting operations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
image_height
|
int
|
Height of the image grid that will be sampled, specified as a scalar integer. |
required |
image_width
|
int
|
width of the image grid that will be sampled, specified as a scalar integer. |
required |
output_stride
|
int
|
Sampling step size, specified as a scalar integer. This can be used to specify a sampling grid that has a smaller shape than the image grid but with values span the same range. This can be thought of as the reciprocal of the output scale, i.e., it will induce subsampling when set to values greater than 1. |
1
|
Returns:
Type | Description |
---|---|
Tuple[Tensor, Tensor]
|
Tuple of grid vectors (xv, yv). These are tensors of dtype tf.float32 with shapes (grid_width,) and (grid_height,) respectively. The grid dimensions are calculated as: grid_width = image_width // output_stride grid_height = image_height // output_stride |