760. Compare two shelf-camera detectors with mean average precision
You are choosing between two versions of a shelf-camera model that detects products in store photos. The standard way to compare object detectors is mean average precision (mAP), the metric behind the COCO and PASCAL VOC benchmarks. It rewards finding every object, ranking confident boxes first, and not producing duplicate boxes.
Write mean_average_precision(preds, gts, iou_thr=0.5).
- Boxes are [x1, y1, x2, y2] with x1 < x2 and y1 < y2.
- preds is a list of [image_id, class_id, score, box]. gts is a list of [image_id, class_id, box].
- IoU is the intersection area divided by the union area.
For each class that appears in gts:
1. Sort that class's predictions by score, highest first. Keep input order for equal scores.
2. Go through them in order. A prediction is a true positive if, among the not yet matched ground truth boxes of the same class in the same image, the one with the HIGHEST IoU has IoU >= iou_thr; that box then becomes matched. Taking the first box that clears the threshold instead can steal a box another prediction needed. Otherwise the prediction is a false positive (a duplicate of an already matched box is a false positive).
3. After each prediction, compute precision = TP / (TP + FP) and recall = TP / (number of ground truth boxes of that class).
4. Average precision uses all-point interpolation: replace each precision with the maximum precision at that point or any later point, then add up precision times the increase in recall at every step.
A class with no predictions has AP 0. Predictions for classes with no ground truth are ignored. Return the mean AP over the ground truth classes, rounded to 4 decimals, or 0.0 if gts is empty.
def mean_average_precision(preds, gts, iou_thr=0.5)Example 1
one image, one cereal box [0, 0, 10, 10], one prediction with score 0.9 and box [0, 0, 10, 10]. AP for that class is 1.0, so the result is 1.0.
Example 2
Input: preds = [[0, "cat", 0.9, [0, 0, 10, 10]]], gts = [[0, "cat", [0, 0, 10, 10]]]
Output: 1.0
Example 3
Input: preds = [[0, "cat", 0.9, [50, 50, 60, 60]], [0, "cat", 0.8, [0, 0, 10, 10]]], gts = [[0, "cat", [0, 0, 10, 10]]]
Output: 0.5
Constraints
- Up to 10,000 predictions and 10,000 ground truth boxes
- Scores are floats, boxes have positive area
Hint 1
Group ground truth by class and keep a used flag per box.
Hint 2
Only compare against boxes in the same image.
Hint 3
Build the precision envelope from the right before summing.
Common mistakes
- Dividing recall by the number of predictions.
- Letting two predictions match the same ground truth box.
- Forgetting the envelope step, which gives a lower AP.
- Averaging over classes that only appear in predictions.
Understand the idea
Mean average precision combines localization and ranking. IoU decides whether a box counts as finding an object, and the ranking by score decides the shape of the precision recall curve.
ground truth has 3 objects, and the detector ranks a miss first, then the 3 hits. Precision runs 0, 0.5, 0.67, 0.75 while recall runs 0, 0.33, 0.67, 1.0. The envelope makes the first three precisions 0.75, so AP = 0.75 x (0.33 + 0.33 + 0.33) = 0.75. Benchmarks differ in details such as 11-point versus all-point interpolation and averaging over several IoU thresholds, so always say which version you compute.