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.

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

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


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.

  • 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.


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


See this blog post for performance comparisons of different approaches.


>>> 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.

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

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


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.


This uses Euclidean distance and the Hungarian algorithm for matching.

See also: tf_linear_sum_assignment

sleap.nn.utils.reset_input_layer(keras_model:, new_shape: Optional[Tuple[Optional[int], Optional[int], Optional[int], int]] = None)[source]#

Returns a copy of keras_model with input shape reset to new_shape.

This method was modified from

  • keras_modeltf.keras.Model to return a copy of (with input shape reset).

  • new_shape – Shape of the returned model’s input layer.


A copy of keras_model with input shape new_shape.

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.


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


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