Skip to content

deeplabcut.pose_estimation_tensorflow.lib.crossvalutils

Backwards compatibility.

Modules:

Name Description
auxfun_multianimal

DeepLabCut2.0 Toolbox (deeplabcut.org)

auxiliaryfunctions

DeepLabCut2.0 Toolbox (deeplabcut.org)

Functions:

Name Description
find_closest_neighbors

Greedy matching of predicted keypoints to ground truth keypoints.

find_closest_neighbors

find_closest_neighbors(query: ndarray, ref: ndarray, k: int = 3) -> np.ndarray

Greedy matching of predicted keypoints to ground truth keypoints.

Parameters:

Name Type Description Default

query

ndarray

the query keypoints

required

ref

ndarray

the reference keypoints

required

k

int

The list of k-th nearest neighbors to return.

3

Returns:

Type Description
ndarray

an array of shape (len(query), ) containing the index of the closest reference keypoint for each query keypoint

Source code in deeplabcut/core/crossvalutils.py
def find_closest_neighbors(query: np.ndarray, ref: np.ndarray, k: int = 3) -> np.ndarray:
    """Greedy matching of predicted keypoints to ground truth keypoints.

    Args:
        query: the query keypoints
        ref: the reference keypoints
        k: The list of k-th nearest neighbors to return.

    Returns:
        an array of shape (len(query), ) containing the index of the closest
        reference keypoint for each query keypoint
    """
    n_preds = ref.shape[0]
    tree = cKDTree(ref)
    dist, inds = tree.query(query, k=k)
    idx = np.argsort(dist[:, 0])
    neighbors = np.full(len(query), -1, dtype=int)
    picked = {tree.n}
    for i, ind in enumerate(inds[idx]):
        for j in ind:
            if j not in picked:
                picked.add(j)
                neighbors[idx[i]] = j
                break
        if len(picked) == (n_preds + 1):
            break
    return neighbors