Sub topic 7
Average Precision: Area Under the Interpolated Curve
Assume you know basic Python, object-detection outputs, and the meanings of precision and recall. This chapter explains how a ranked list of detections becomes average precision, or AP, for one object class.
From detections to precision and recall
An object detector gives each predicted box a confidence score. A higher score means the detector is more confident. To evaluate the detector, sort predictions from highest to lowest score.
Each prediction is compared with the ground-truth boxes for the same image and class. A prediction is a true positive if it matches an unused ground-truth box and its Intersection over Union, or IoU, reaches the chosen threshold. Otherwise, it is a false positive.
A false positive can be caused by:
- A box around the wrong object.
- A correct class with poor localization.
- A duplicate prediction for an object already matched.
- A prediction in an image with no matching object.
For a ranked list, calculate the running values after each prediction:
| Prediction rank | Result | Cumulative true positives | Precision | Recall |
|---|---|---|---|---|
| 1 | TP | 1 | 1.00 | 0.50 |
| 2 | FP | 1 | 0.50 | 0.50 |
| 3 | TP | 2 | 0.67 | 1.00 |
Suppose the dataset contains two ground-truth objects. After the first prediction, one object has been found, so recall is 1 divided by 2, or 0.50. After the third prediction, both objects have been found, so recall is 1.00.
Precision and recall are calculated as:
- Precision = cumulative true positives divided by the number of predictions considered.
- Recall = cumulative true positives divided by the total number of ground-truth objects.
Building the precision-recall curve
A precision-recall curve plots recall on the horizontal axis and precision on the vertical axis. Each confidence-score cutoff produces one point. Lowering the cutoff includes more detections. This usually increases recall, but it can lower precision by adding false positives.
The points do not always form a smooth or steadily decreasing curve. A later true positive can raise precision, while a false positive can lower it. This makes direct area calculation sensitive to small changes in the ranking.
Interpolating the curve
Average precision uses an interpolated precision-recall curve. The interpolation creates a precision envelope. At each recall level, use the highest precision observed at that recall level or at any larger recall level.
If the original precision values are:
| Recall | Original precision | Interpolated precision |
|---|---|---|
| 0.00 | 0.00 | 1.00 |
| 0.25 | 1.00 | 1.00 |
| 0.50 | 0.50 | 0.67 |
| 0.75 | 0.67 | 0.67 |
| 1.00 | 0.50 | 0.50 |
The interpolated value at recall 0.50 is 0.67 because a later point reaches that precision. The envelope removes downward steps that are followed by better precision.
AP is the area under this interpolated curve. With discrete points, calculate it by summing each rectangle:
AP = sum of recall increase multiplied by interpolated precision
For the table above, the recall changes are 0.25. The approximate AP is:
- 0.25 × 1.00
- 0.25 × 0.67
- 0.25 × 0.67
- 0.25 × 0.50
- AP ≈ 0.71
Different evaluation standards use different sampling rules. Older Pascal VOC versions used an 11-point method. Modern implementations commonly use all distinct recall points and the precision envelope. Therefore, always identify the protocol before comparing AP values.
Computing AP in Python
The following implementation sorts detections, performs one-to-one matching, builds the precision envelope, and sums its area.
def iou(a, b):
ix1 = max(a[0], b[0])
iy1 = max(a[1], b[1])
ix2 = min(a[2], b[2])
iy2 = min(a[3], b[3])
intersection = max(0.0, ix2 - ix1) * max(0.0, iy2 - iy1)
area_a = (a[2] - a[0]) * (a[3] - a[1])
area_b = (b[2] - b[0]) * (b[3] - b[1])
union = area_a + area_b - intersection
return intersection / union if union else 0.0
def average_precision(detections, ground_truths, threshold=0.5):
total_ground_truths = sum(len(boxes) for boxes in ground_truths.values())
if total_ground_truths == 0:
return 0.0
matched = {
image_id: [False] * len(boxes)
for image_id, boxes in ground_truths.items()
}
results = []
for image_id, score, box in sorted(detections, key=lambda item: -item[1]):
best_iou = 0.0
best_index = -1
for index, truth in enumerate(ground_truths.get(image_id, [])):
overlap = iou(box, truth)
if overlap > best_iou:
best_iou = overlap
best_index = index
is_true_positive = (
best_index >= 0
and best_iou >= threshold
and not matched[image_id][best_index]
)
if is_true_positive:
matched[image_id][best_index] = True
results.append(1 if is_true_positive else 0)
precisions = []
recalls = []
true_positives = 0
for rank, result in enumerate(results, start=1):
true_positives += result
precisions.append(true_positives / rank)
recalls.append(true_positives / total_ground_truths)
precisions = [0.0] + precisions + [0.0]
recalls = [0.0] + recalls + [1.0]
for index in range(len(precisions) - 2, -1, -1):
precisions[index] = max(precisions[index], precisions[index + 1])
area = 0.0
for index in range(1, len(recalls)):
area += (recalls[index] - recalls[index - 1]) * precisions[index]
return area
truths = {"image-1": [(0, 0, 10, 10), (20, 20, 30, 30)]}
predictions = [
("image-1", 0.9, (0, 0, 10, 10)),
("image-1", 0.8, (1, 1, 10, 10)),
("image-1", 0.7, (20, 20, 30, 30)),
]
print(average_precision(predictions, truths))Exercise
A detector produces three ranked predictions for two ground-truth objects. The results are TP, FP, TP. What are the final recall and the precision after all three predictions?
Show answer
Final recall is 2 divided by 2, or 1.00. Final precision is 2 divided by 3, or about 0.67.