Sub topic 13
Evaluating Detectors with AP and mAP
This chapter assumes you know Python and NumPy, understand classification and regression, and know that Intersection over Union (IoU) measures overlap between two boxes.
A detector can produce many boxes with different confidence scores. Evaluation must answer two questions:
- Did each predicted box identify a real object?
- Did the detector find most of the real objects without producing too many false alarms?
From predictions to precision and recall
Choose an IoU threshold, such as 0.5. A prediction is a true positive if its class is correct, its IoU with a ground-truth box is at least 0.5, and that ground-truth box has not already been matched. A duplicate prediction for the same object is a false positive.
Sort predictions from highest to lowest confidence. At each position in this list, calculate:
- Precision: the fraction of predictions so far that are correct.
- Recall: the fraction of ground-truth objects found so far.
\text{precision} = \frac{TP}{TP + FP}\text{recall} = \frac{TP}{TP + FN}Changing the confidence cutoff moves along a precision-recall curve. A high cutoff usually gives high precision but low recall. A low cutoff usually gives higher recall but more false positives.
Average precision
Average precision (AP) summarizes the precision-recall curve for one class at one IoU threshold. It is commonly interpreted as the area under the precision-recall curve. A detector with high precision at every recall level has a high AP.
The exact calculation depends on the evaluation protocol. Older Pascal VOC evaluations used an 11-point interpolation method. Modern protocols usually build an envelope in which precision at a recall level is replaced by the greatest precision measured at that level or any higher recall. The numerical result is therefore meaningful only when you also report the protocol.
The calculation below uses this all-points interpolation. The input has already been converted into true-positive and false-positive labels after box matching.
import numpy as np
def average_precision(scores, true_positive, ground_truth_count):
order = np.argsort(-np.asarray(scores))
tp = np.asarray(true_positive, dtype=float)[order]
fp = 1.0 - tp
precision = np.cumsum(tp) / np.maximum(np.cumsum(tp + fp), 1)
recall = np.cumsum(tp) / ground_truth_count
recall = np.r_[0.0, recall, 1.0]
precision = np.r_[0.0, precision, 0.0]
for i in range(len(precision) - 2, -1, -1):
precision[i] = max(precision[i], precision[i + 1])
changes = np.where(recall[1:] != recall[:-1])[0]
return np.sum((recall[changes + 1] - recall[changes]) *
precision[changes + 1])
scores = [0.95, 0.90, 0.60, 0.40]
is_true_positive = [1, 0, 1, 1]
print(average_precision(scores, is_true_positive, ground_truth_count=3))Mean average precision
Mean average precision (mAP) is the mean of AP values across classes. For classes cat, dog, and car:
\text{mAP} = \frac{AP_{\text{cat}} + AP_{\text{dog}} + AP_{\text{car}}}{3}Some benchmarks also average across IoU thresholds. COCO reports AP averaged over IoU thresholds from 0.50 through 0.95 in steps of 0.05, and across classes. AP50 means AP at IoU 0.50, while AP75 means AP at IoU 0.75.
| Metric | Meaning |
|---|---|
| AP50 | AP for one class at IoU 0.50 |
| AP75 | AP for one class at IoU 0.75 |
| mAP50 | Mean AP across classes at IoU 0.50 |
| COCO AP | Mean across classes and IoU thresholds from 0.50 to 0.95 |
Reading metric differences
A large gap between AP50 and AP75 suggests that the detector finds the correct objects but places boxes inaccurately. Low AP for small objects can be hidden by an overall average, so detailed reports often separate small, medium, and large objects.
Exercise
A detector has AP50 = 0.82 and AP75 = 0.51. What is the most likely weakness?
Show answer
It usually identifies the correct objects, but its predicted box coordinates are not precise enough to meet the stricter IoU threshold.