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, with_offsets: bool = False, offsets_threshold: float = 0.2, flatten_offsets: bool = True)[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.

Type

float

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.

Type

int

all_instances#

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

Type

bool

with_offsets#

If True, also return offsets for refining the peaks.

Type

bool

offsets_threshold#

Minimum confidence map value below which offsets will be replaced with zeros.

Type

float

flatten_offsets#

If True, the last two channels of the offset maps will be flattened to produce rank-3 tensors. If False, the generated offset maps will be rank-4 with shape (height, width, n_nodes, 2).

Type

bool

property input_keys: List[str]#

Return the keys that incoming elements are expected to have.

property output_keys: List[str]#

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.

If the with_offsets attribute is True, example will contain a "offsets" key.

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, with_offsets: bool = False, offsets_threshold: float = 0.2, flatten_offsets: bool = True)[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.

Type

float

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.

Type

int

centroids#

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

Type

bool

with_offsets#

If True, also return offsets for refining the peaks.

Type

bool

offsets_threshold#

Minimum confidence map value below which offsets will be replaced with zeros.

Type

float

flatten_offsets#

If True, the last two channels of the offset maps will be flattened to produce rank-3 tensors. If False, the generated offset maps will be rank-4 with shape (height, width, n_nodes, 2).

Type

bool

property input_keys: List[str]#

Return the keys that incoming elements are expected to have.

property output_keys: List[str]#

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.

If the with_offsets attribute is True, example will contain a "offsets" key.

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, with_offsets: bool = False, offsets_threshold: float = 0.2, flatten_offsets: bool = True)[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.

Type

float

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.

Type

int

with_offsets#

If True, also return offsets for refining the peaks.

Type

bool

offsets_threshold#

Minimum confidence map value below which offsets will be replaced with zeros.

Type

float

flatten_offsets#

If True, the last two channels of the offset maps will be flattened to produce rank-3 tensors. If False, the generated offset maps will be rank-4 with shape (height, width, n_nodes, 2).

Type

bool

property input_keys: List[str]#

Return the keys that incoming elements are expected to have.

property output_keys: List[str]#

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 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 computed individually for each instance and immediately max-reduced to avoid maintaining the entire set of all instance maps. This enables memory-efficient generation of multi-instance maps for examples with large numbers of instances.

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

sleap.nn.data.confidence_maps.make_multi_confmaps_with_offsets(instances: tensorflow.python.framework.ops.Tensor, xv: tensorflow.python.framework.ops.Tensor, yv: tensorflow.python.framework.ops.Tensor, stride: int, sigma: float, offsets_threshold: float) tensorflow.python.framework.ops.Tensor[source]#

Make confidence maps and offsets for multiple instances through 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.

  • stride – Scaling factor for offset coordinates. The individual offset vectors will be divided by this value. Useful for adjusting for strided sampling grids so that the offsets point to the smaller grid coordinates.

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

  • offsets_threshold – Minimum confidence map value below which offsets will be replaced with zeros.

  • flatten_offsets – If True, the last two channels of the offset maps will be flattened to produce rank-3 tensors. If False, the generated offset maps will be rank-4 with shape (height, width, n_nodes, 2).

Returns

A tuple of (confmaps, offsets).

confmaps are confidence maps as a tensor of shape (grid_height, grid_width, n_nodes) and dtype tf.float32.

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

offsets are offset maps as a tf.Tensor of shape (grid_height, grid_width, n_nodes, 2) and dtype tf.float32. The last axis corresponds to the x- and y-offsets at each grid point for each node.

Notes

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

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