sleap.nn.data.confidence_maps

Transformers for confidence map generation.

class sleap.nn.data.confidence_maps.InstanceConfidenceMapGenerator(sigma: float = 1.0, output_stride: int = 1, all_instances: bool = False)[source]

Transformer to generate instance-centered confidence maps.

sigma

Standard deviation of the 2D Gaussian distribution sampled to generate confidence maps. This defines the spread in units of the input image’s grid, i.e., it does not take scaling in previous steps into account.

output_stride

Relative stride of the generated confidence maps. This is effectively the reciprocal of the output scale, i.e., increase this to generate confidence maps that are smaller than the input images.

all_instances

If True, will also generate the multi-instance confidence maps.

property input_keys

Return the keys that incoming elements are expected to have.

property output_keys

Return the keys that outgoing elements will have.

transform_dataset(input_ds: tensorflow.python.data.ops.dataset_ops.DatasetV2) → tensorflow.python.data.ops.dataset_ops.DatasetV2[source]

Create a dataset that contains the generated confidence maps.

Parameters

input_ds – A dataset with elements that contain the keys “instance_image”, “center_instance” and, if the attribute all_instances is True, “all_instances”.

Returns

A tf.data.Dataset with the same keys as the input, as well as “instance_confidence_maps” and, if the attribute all_instances is True, “all_instance_confidence_maps” keys containing the generated confidence maps.

Notes

The output stride is relative to the current scale of the image. To map points on the confidence maps to the raw image, first multiply them by the output stride, and then scale the x- and y-coordinates by the “scale” key.

Importantly, the sigma will be proportional to the current image grid, not the original grid prior to scaling operations.

class sleap.nn.data.confidence_maps.MultiConfidenceMapGenerator(sigma: float = 1.0, output_stride: int = 1, centroids: bool = False)[source]

Transformer to generate multi-instance confidence maps.

sigma

Standard deviation of the 2D Gaussian distribution sampled to generate confidence maps. This defines the spread in units of the input image’s grid, i.e., it does not take scaling in previous steps into account.

output_stride

Relative stride of the generated confidence maps. This is effectively the reciprocal of the output scale, i.e., increase this to generate confidence maps that are smaller than the input images.

centroids

If True, generate confidence maps for centroids rather than instance points.

property input_keys

Return the keys that incoming elements are expected to have.

property output_keys

Return the keys that outgoing elements will have.

transform_dataset(input_ds: tensorflow.python.data.ops.dataset_ops.DatasetV2) → tensorflow.python.data.ops.dataset_ops.DatasetV2[source]

Create a dataset that contains the generated confidence maps.

Parameters

input_ds – A dataset with elements that contain the keys “image”, “scale” and either “instances” or “centroids” depending on whether the centroids attribute is set to True.

Returns

A tf.data.Dataset with the same keys as the input, as well as a “confidence_maps” or “centroid_confidence_maps” key containing the generated confidence maps.

Notes

The output stride is relative to the current scale of the image. To map points on the confidence maps to the raw image, first multiply them by the output stride, and then scale the x- and y-coordinates by the “scale” key.

Importantly, the sigma will be proportional to the current image grid, not the original grid prior to scaling operations.

class sleap.nn.data.confidence_maps.SingleInstanceConfidenceMapGenerator(sigma: float = 1.0, output_stride: int = 1)[source]

Transformer to generate single-instance confidence maps.

sigma

Standard deviation of the 2D Gaussian distribution sampled to generate confidence maps. This defines the spread in units of the input image’s grid, i.e., it does not take scaling in previous steps into account.

output_stride

Relative stride of the generated confidence maps. This is effectively the reciprocal of the output scale, i.e., increase this to generate confidence maps that are smaller than the input images.

property input_keys

Return the keys that incoming elements are expected to have.

property output_keys

Return the keys that outgoing elements will have.

transform_dataset(input_ds: tensorflow.python.data.ops.dataset_ops.DatasetV2) → tensorflow.python.data.ops.dataset_ops.DatasetV2[source]

Create a dataset that contains the generated confidence maps.

Parameters
  • input_ds – A dataset with elements that contain the keys “instances” and

  • "image".

Returns

A tf.data.Dataset with the same keys as the input, as well as “confidence_maps” containing the generated confidence maps.

Notes

The output stride is relative to the current scale of the image. To map points on the confidence maps to the raw image, first multiply them by the output stride, and then scale the x- and y-coordinates by the “scale” key.

Importantly, the sigma will be proportional to the current image grid, not the original grid prior to scaling operations.

sleap.nn.data.confidence_maps.make_confmaps(points: tensorflow.python.framework.ops.Tensor, xv: tensorflow.python.framework.ops.Tensor, yv: tensorflow.python.framework.ops.Tensor, sigma: float) → tensorflow.python.framework.ops.Tensor[source]

Make confidence maps from a set of points from a single instance.

Parameters
  • points – A tensor of points of shape (n_nodes, 2) and dtype tf.float32 where the last axis corresponds to (x, y) pixel coordinates on the image. These can contain NaNs to indicate missing points.

  • xv – Sampling grid vector for x-coordinates of shape (grid_width,) and dtype tf.float32. This can be generated by sleap.nn.data.utils.make_grid_vectors.

  • yv – Sampling grid vector for y-coordinates of shape (grid_height,) and dtype tf.float32. This can be generated by sleap.nn.data.utils.make_grid_vectors.

  • sigma – Standard deviation of the 2D Gaussian distribution sampled to generate confidence maps.

Returns

Confidence maps as a tensor of shape (grid_height, grid_width, n_nodes) of dtype tf.float32.

Each channel of the confidence maps will contain the unnormalized PDF of a 2D Gaussian distribution with a mean centered at the coordinates of the corresponding point, and diagonal covariance matrix (i.e., the same standard deviation for both dimensions).

When the point is perfectly aligned to the sampling grid, the value at that grid coordinate is 1.0 since the PDF is not normalized.

If a point was missing (indicated by NaNs), the corresponding channel will contain all zeros.

See also: sleap.nn.data.make_grid_vectors, make_multi_confmaps

sleap.nn.data.confidence_maps.make_multi_confmaps(instances: tensorflow.python.framework.ops.Tensor, xv: tensorflow.python.framework.ops.Tensor, yv: tensorflow.python.framework.ops.Tensor, sigma: float) → tensorflow.python.framework.ops.Tensor[source]

Make confidence maps for multiple instances through max reduction.

Parameters
  • instances – A tensor of shape (n_instances, n_nodes, 2) and dtype tf.float32 containing instance points where the last axis corresponds to (x, y) pixel coordinates on the image. This must be rank-3 even if a single instance is present.

  • xv – Sampling grid vector for x-coordinates of shape (grid_width,) and dtype tf.float32. This can be generated by sleap.nn.data.utils.make_grid_vectors.

  • yv – Sampling grid vector for y-coordinates of shape (grid_height,) and dtype tf.float32. This can be generated by sleap.nn.data.utils.make_grid_vectors.

  • sigma – Standard deviation of the 2D Gaussian distribution sampled to generate confidence maps.

Returns

Confidence maps as a tensor of shape (grid_height, grid_width, n_nodes) of dtype tf.float32.

Each channel will contain the elementwise maximum of the confidence maps generated from all individual points for the associated node.

Notes

The confidence maps are for each instance are computed individually and immediately max-reduced to avoid maintaining the entire set of all instance confidence maps. This enables memory-efficient generation of multi-instance confidence maps for examples with large numbers of instances.

See also: sleap.nn.data.make_grid_vectors, make_confmaps