sleap.nn.utils

This module contains generic utilities used for training and inference.

sleap.nn.utils.compute_iou(bbox1: numpy.ndarray, bbox2: numpy.ndarray)float[source]

Computes the intersection over union for a pair of bounding boxes.

Parameters
  • bbox1 – Bounding box specified by corner coordinates [y1, x1, y2, x2].

  • bbox2 – Bounding box specified by corner coordinates [y1, x1, y2, x2].

Returns

A float scalar calculated as the ratio between the areas of the intersection and the union of the two bounding boxes.

sleap.nn.utils.group_array(X: numpy.ndarray, groups: numpy.ndarray, axis: int = 0)Dict[numpy.ndarray, numpy.ndarray][source]

Groups an array into a dictionary keyed by a grouping vector.

Parameters
  • X – Numpy array with length n along the specified axis.

  • groups – Vector of n values denoting the group that each slice of X should be assigned to. This is also referred to as an indicator, indexing, class, or labels vector.

  • axis – Dimension of X to group on. The length of this axis in X must correspond to the length of groups.

Returns

A dictionary with keys mapping each unique value in groups to a subset of X.

References

See this blog post for performance comparisons of different approaches.

Example

>>> group_array(np.arange(5), np.array([1, 5, 2, 1, 5]))
{1: array([0, 3]), 5: array([1, 4]), 2: array([2])}
sleap.nn.utils.match_points(points1: tensorflow.python.framework.ops.Tensor, points2: tensorflow.python.framework.ops.Tensor)Tuple[tensorflow.python.framework.ops.Tensor, tensorflow.python.framework.ops.Tensor][source]

Match closest points across two sets.

Parameters
  • points1 – A set of reference points of shape (N, D).

  • points2 – A set of unmatched points of shape (M, D).

Returns

A tuple of (inds1, inds2) with the indices of matching points across the two lists of points.

inds1 is a tf.int32 tensor of shape (n,) where n <= N that indexes into points1.

inds2 is a tf.int32 tensor of shape (m,) where m <= M that indexes into points2.

Notes

This uses Euclidean distance and the Hungarian algorithm for matching.

See also: tf_linear_sum_assignment

sleap.nn.utils.tf_linear_sum_assignment(cost_matrix: tensorflow.python.framework.ops.Tensor)tensorflow.python.framework.ops.Tensor

Run linear_sum_assignment as a TensorFlow function.

Parameters

cost_matrix – Cost matrix of shape (n_src, n_dst). Make sure to replace NaN`s with `np.inf.

Returns

A tuple of (row, col) with the indices of the optimal assignments.