import torch


def calc_dists(preds, target):
    """
    Calculate the average distance from predictions to their ground-truth
    :param preds: predicted coordinates of key-points from estimations
    :param target: predicted coordionates of key-point from ground-truth maps
    :return: the average distance
    """
    preds = preds.float()
    target = target.float()
    cnt = 0
    dists = 0
    for n in range(preds.size(0)):
        for c in range(preds.size(1)):
            if target[n, c, 0] > 1 and target[n, c, 1] > 1:
                dists += torch.dist(preds[n, c, :], target[n, c, :])
                cnt += 1
    dists = dists / cnt
    return dists