#319mAP for Object DetectionHard

mAP for Object Detection

Background

Mean Average Precision (mAP) at IoU threshold τ\tau is the standard object-detection eval. For each class, walk the predicted boxes in score order, match each to the highest-IoU unmatched ground-truth box if IoUτ\text{IoU} \ge \tau, otherwise mark as a false positive. AP is the area under the resulting precision-recall curve; mAP is the average AP across classes.

Problem statement

Implement map_detection(detections, golds, iou_threshold=0.5):

  1. Group detections and golds by class.
  2. For each class:
    • Sort detections by score descending.
    • Walk through, computing IoU to each gold; if a match τ\ge \tau and that gold is unmatched, count as TP and mark gold as matched; else FP.
    • Compute precision and recall after each row; AP = i(RiRi1)Pi\sum_i (R_i - R_{i-1}) P_i.
  3. Return the mean of class APs.

Input

  • detectionslist[tuple[class, score, box]]. box = (x1, y1, x2, y2).
  • goldslist[tuple[class, box]].
  • iou_thresholdfloat in [0,1][0, 1] (default 0.5).

Output

Returns a Python float in [0,1][0, 1].

Examples

Example 1 — perfect detection

Input:  one detection that exactly matches a single gold box
Output: 1.0

Example 2 — no detections

Input:  detections=[], gold present
Output: 0.0

Example 3 — IoU below threshold → no match

Input:  detection and gold of same class but disjoint boxes
Output: 0.0

Constraints

  • A gold box can be matched at most once across all detections (greedy).
  • If a class has no gold, its AP is 0.0.
  • Returns Python float.

Notes

  • mAP@0.5 vs mAP@[.5:.95]. COCO reports the average mAP across IoU thresholds from 0.5 to 0.95; this kernel implements one fixed threshold.
  • Greedy matching. Strict bipartite matching would be optimal but slower; greedy is the standard approximation.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • reference example: perfect detection scores 1.0
  • sample: greedy match uses each gold once
  • reference example: precision-recall curve area