utils

utils

Functions

Name Description
aggregate_embeddings Aggregates multiple embeddings into a single embedding.
annotate_img_with_kps Annotate an image with keypoints.
compute_ss_ds Compute cosine similarities between the cartesian product of two arrays X and Z and
cosine_score Compute cosine similarity between two 1D embeddings.
transform_keypoints Transforms keypoints from the original image space to the aligned image space.

aggregate_embeddings

utils.aggregate_embeddings(embeddings, weights=None, method='mean')

Aggregates multiple embeddings into a single embedding.

Parameters

embeddings : np.ndarray

A 2D array of shape (num_embeddings, embedding_dim) containing the embeddings to be aggregated.

weights : np.ndarray | None = None

A 1D array of shape (num_embeddings,) containing the weights to assign to each embedding. If not provided, all embeddings are equally weighted.

method : str = 'mean'

Aggregation method. Possible values are "mean" and "median".

Returns

: np.ndarray

np.ndarray: A 1D array of shape (embedding_dim,) containing the

: np.ndarray

aggregated embedding.

annotate_img_with_kps

utils.annotate_img_with_kps(
    bgr_img,
    kps,
    colors=DEFAULT_KEYPOINT_COLORS,
    radius=2,
)

Annotate an image with keypoints.

Parameters: bgr_img (numpy.ndarray): The input image in BGR format. kps (numpy.ndarray): A numpy array of shape (5, 2) containing the keypoints. colors (tuple[str, str, str, str, str], optional): The color of each keypoint. By default, keypoint index 1 is red and the others are green. Options are ‘red’, ‘blue’, ‘green’, ‘white’, ‘black’. radius (int, optional): The radius of the keypoints. Default is 2.

Returns: numpy.ndarray: The image with keypoints annotated.

compute_ss_ds

utils.compute_ss_ds(
    X,
    x_id,
    Z=None,
    z_id=None,
    return_pair_indices=False,
    block_size=2048,
)

Compute cosine similarities between the cartesian product of two arrays X and Z and return same-source (ss) and different-source (ds) scores. If only the array X and x_id are provided, compute the cosine similarities between all pairwise combination in X.

Parameters

X : np.ndarray

2D numpy array with one embedding per row.

x_id : np.ndarray

1D numpy array with identity labels for X.

Z : np.ndarray | None = None

Optional 2D numpy array with one embedding per row.

z_id : np.ndarray | None = None

Optional 1D numpy array with identity labels for Z.

return_pair_indices : bool = False

Whether to return int32 pair indices as the third return value. If False, pair indices are not computed and the third return value is None.

block_size : int = 2048

Maximum number of rows and columns in each similarity tile. Smaller values reduce peak working memory without changing the returned arrays.

Returns

: np.ndarray

tuple[np.ndarray, np.ndarray, np.ndarray | None]: Scores, Boolean

: np.ndarray

same-source labels, and optional int32 pair indices. True means

: np.ndarray | None

same-source and False means different-source. For X vs X,

: tuple[np.ndarray, np.ndarray, np.ndarray | None]

both index columns refer to rows in X. For X vs Z, the first

: tuple[np.ndarray, np.ndarray, np.ndarray | None]

column refers to X and the second column refers to Z.

: tuple[np.ndarray, np.ndarray, np.ndarray | None]

Same-source scores precede different-source scores. Ordering within

: tuple[np.ndarray, np.ndarray, np.ndarray | None]

those two sections is unspecified.

cosine_score

utils.cosine_score(x, z)

Compute cosine similarity between two 1D embeddings.

transform_keypoints

utils.transform_keypoints(keypoints, M)

Transforms keypoints from the original image space to the aligned image space.

Parameters

keypoints : np.ndarray

A 2D array of shape (5, 2) representing the original keypoints.

M : np.ndarray

The 2x3 affine transformation matrix.

Returns

: np.ndarray

np.ndarray: A 2D array of shape (5, 2) representing the transformed keypoints.