Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The calculation of Average Precision is wrong. #101

Open
Uchan1996 opened this issue Oct 31, 2024 · 0 comments
Open

The calculation of Average Precision is wrong. #101

Uchan1996 opened this issue Oct 31, 2024 · 0 comments

Comments

@Uchan1996
Copy link

The calculation of Average Precision in compute_ap is wrong.
The current calculation outputs small AP since the integral range is narrower than [0, 1].
I think the correct calculation is as below.
Please let me know if my calculation is wrong.

def compute_ap(pred_annos, num_gt):
    pred_annos = sorted(pred_annos, key=cmp_to_key(cmp))

    num_tp = np.zeros(len(pred_annos))
    for i in range(len(pred_annos)):
        num_tp[i] = 0 if i == 0 else num_tp[i - 1]
        if pred_annos[i]["type"] == "tp":
            num_tp[i] += 1
    # logger.debug("num tp = {}".format(num_tp))

    precision = num_tp / np.arange(1, len(pred_annos) + 1) # Precision = TP / (TP + FP)
    recall = num_tp / num_gt # Recall = TP / (TP + FN)

    recall = np.concatenate([[0], recall, [1]])
    precision = np.concatenate([precision, [precision[-1]]])

    for i in range(len(pred_annos) - 1, 0, -1):
        precision[i - 1] = max(precision[i], precision[i - 1])

    dr = recall[1:] - recall[:-1]
    ap = np.sum(precision * dr)
    return ap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant