sleap.nn.utils#
This module contains generic utilities used for training and inference.
- sleap.nn.utils.compute_iou(bbox1: ndarray, bbox2: 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: ndarray, groups: ndarray, axis: int = 0) Dict[ndarray, 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: Tensor, points2: Tensor) Tuple[Tensor, 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 atf.int32
tensor of shape(n,)
wheren <= N
that indexes intopoints1
.inds2
is atf.int32
tensor of shape(m,)
wherem <= M
that indexes intopoints2
.
Notes
This uses Euclidean distance and the Hungarian algorithm for matching.
See also: tf_linear_sum_assignment
- sleap.nn.utils.reset_input_layer(keras_model: Model, new_shape: Tuple[int | None, int | None, int | None, int] | None = None)[source]#
Returns a copy of
keras_model
with input shape reset tonew_shape
.This method was modified from https://stackoverflow.com/a/58485055.
- Parameters:
keras_model –
tf.keras.Model
to return a copy of (with input shape reset).new_shape – Shape of the returned model’s input layer.
- Returns:
A copy of
keras_model
with input shapenew_shape
.
- sleap.nn.utils.tf_linear_sum_assignment(cost_matrix: Tensor) Tensor #
Run
linear_sum_assignment
as a TensorFlow function.- Parameters:
cost_matrix – Cost matrix of shape
(n_src, n_dst)
. Make sure to replaceNaN`s with `np.inf
.- Returns:
A tuple of
(row, col)
with the indices of the optimal assignments.