Sub topic 4
Matching detections to ground truth: true and false positives
Assume you know the basics of object detection, bounding boxes, IoU, and classification precision and recall. This chapter focuses on the matching step that turns detector outputs into true positives (TP), false positives (FP), and false negatives (FN).
A detector usually produces several scored predictions for each image. Each prediction has:
- An image identifier
- A class label
- A bounding box
- A confidence score
The ground truth contains the objects that really appear in each image. It has a class label and a box for every labeled object. Evaluation must decide which predictions correctly detect which objects.
Match boxes one time only
For a particular class and image, sort detections by confidence score from highest to lowest. Then process them in that order.
A detection is a true positive when:
- Its class is correct.
- It overlaps an unmatched ground-truth box.
- Its IoU with that box is at least the chosen threshold.
If no suitable ground-truth box exists, the detection is a false positive. A ground-truth object that remains unmatched after all detections are processed is a false negative.
The one-time matching rule matters. A single ground-truth object cannot validate several predictions. Otherwise, a detector could receive credit for repeatedly predicting the same object.
A worked matching example
Suppose one image contains two ground-truth objects of class car. The detector produces three predictions. Use an IoU threshold of 0.5.
| Detection rank | Confidence | Best ground-truth IoU | Result | Reason |
|---|---|---|---|---|
| 1 | 0.90 | 0.80 with car A | TP | Claims car A |
| 2 | 0.80 | 0.75 with car A | FP | Car A is already claimed |
| 3 | 0.70 | 0.65 with car B | TP | Claims car B |
The result is two true positives, one false positive, and no false negatives. The second prediction has good overlap, but it is a duplicate prediction for an object already matched by a higher-scoring detection.
If the third prediction had an IoU of 0.40, it would be an FP because it would fail the threshold. Car B would then remain unmatched and count as an FN.
Greedy matching and confidence scores
This procedure is greedy: it makes the best decision available for the highest-scoring detection, then continues. Confidence ranking is therefore part of evaluation.
Consider two predictions that overlap the same object. The higher-scoring prediction gets the match, even if the lower-scoring prediction has slightly better localization. This behavior reflects how a detector is expected to rank its most useful prediction first.
When two unmatched ground-truth boxes are possible, match the one with the largest IoU, provided that it passes the threshold. Matching only the first box in file order can produce incorrect results.
def iou(a, b):
"""Return IoU for boxes represented as (x1, y1, x2, y2)."""
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 = max(0.0, a[2] - a[0]) * max(0.0, a[3] - a[1])
area_b = max(0.0, b[2] - b[0]) * max(0.0, b[3] - b[1])
union = area_a + area_b - intersection
return intersection / union if union else 0.0
def match_detections(detections, ground_truths, iou_threshold=0.5):
"""
detections: (image_id, class_name, score, box)
ground_truths: {(image_id, class_name): [box, ...]}
"""
used = {
key: [False] * len(boxes)
for key, boxes in ground_truths.items()
}
results = []
ranked = sorted(detections, key=lambda item: item[2], reverse=True)
for image_id, class_name, score, box in ranked:
key = (image_id, class_name)
boxes = ground_truths.get(key, [])
best_iou = 0.0
best_index = -1
for index, gt_box in enumerate(boxes):
if not used[key][index]:
value = iou(box, gt_box)
if value > best_iou:
best_iou = value
best_index = index
if best_index >= 0 and best_iou >= iou_threshold:
used[key][best_index] = True
results.append(("TP", score, best_iou))
else:
results.append(("FP", score, best_iou))
false_negatives = sum(
not claimed
for flags in used.values()
for claimed in flags
)
return results, false_negatives
ground_truths = {
("image-1", "car"): [
(0, 0, 10, 10),
(20, 20, 30, 30),
]
}
detections = [
("image-1", "car", 0.90, (0, 0, 10, 10)),
("image-1", "car", 0.80, (1, 1, 10, 10)),
("image-1", "car", 0.70, (20, 20, 30, 30)),
]
print(match_detections(detections, ground_truths))
# ([('TP', 0.9, 1.0), ('FP', 0.8, 0.81), ('TP', 0.7, 1.0)], 0)Class labels and unmatched predictions
Matching is normally performed separately for each class. A predicted dog cannot match a ground-truth cat, even if their boxes are identical. A prediction for an unknown or absent object is an FP.
Ground-truth boxes with no matching prediction are FNs. False negatives are not usually added as rows to the ranked detection list. Instead, they affect the denominator used for recall:
\text{precision} = \frac{TP}{TP + FP}\text{recall} = \frac{TP}{TP + FN}For average precision, detections from all evaluated images are usually combined and sorted by score within one class. The evaluator repeats matching at the selected IoU threshold, then constructs precision and recall after each ranked detection.
Common implementation errors
- Matching a ground-truth box more than once.
- Comparing boxes from different classes.
- Processing detections in input order instead of descending confidence order.
- Counting every unmatched prediction as an FN. Unmatched predictions are FPs; unmatched ground-truth objects are FNs.
- Using an inclusive pixel-area formula in one implementation and an exclusive-coordinate formula in another.
- Applying non-maximum suppression during evaluation and silently changing the detector’s output. NMS may be part of the detector pipeline, but the evaluation protocol should state whether it is used.
Exercise
An image has one ground-truth bicycle. Two bicycle detections have scores 0.95 and 0.60, and both have IoU 0.70 with the ground-truth box. With an IoU threshold of 0.5, what are the TP, FP, and FN counts?
Show answer
The 0.95 detection is a TP and claims the ground-truth box. The 0.60 detection is a duplicate FP. There are no FN objects, so the counts are TP = 1, FP = 1, and FN = 0.