Sub topic 5
Precision and Recall from a Ranked List of Detections
This chapter assumes you know that a detector outputs bounding boxes, class labels, and confidence scores. It also assumes you know the basic classification meanings of precision and recall. Here, we apply those ideas to object detection.
Why the Ranking Matters
A detector can produce many candidate boxes. Each candidate has a confidence score that estimates how likely the box is to be a correct detection. Evaluation sorts detections from highest to lowest score.
The evaluator then examines this ranked list from top to bottom. At each position, it asks whether the detection matches an unused ground-truth box of the same class. The match must meet a chosen Intersection over Union (IoU) threshold, such as 0.5.
- A true positive (TP) is a correct detection matched to one ground-truth object.
- A false positive (FP) is an incorrect detection, a detection of the wrong class, or a duplicate detection for an object already matched.
- A false negative (FN) is a ground-truth object that has not been detected.
The same detection list can produce different precision and recall values as you move down the ranking.
The Detection Matching Rule
Suppose an image contains three ground-truth objects. A detection matches a ground-truth box only when:
- The class labels are the same.
- Their IoU is at least the selected threshold.
- That ground-truth box has not already been matched.
The third rule prevents one object from being counted as detected several times. Therefore, two overlapping boxes around one object usually produce one TP and one FP. Which one becomes the TP depends on their confidence scores and the matching procedure.
The evaluator should process detections in descending score order. A lower-scoring detection cannot take a ground-truth match away from an earlier, higher-scoring detection.
Computing Cumulative Precision and Recall
Consider three ground-truth objects and this ranked detection list. The TP and FP labels are the results of IoU matching.
| Rank | Score | Result | Cumulative TP | Cumulative FP | Precision | Recall |
|---|---|---|---|---|---|---|
| 1 | 0.95 | TP | 1 | 0 | 1.00 | 0.33 |
| 2 | 0.90 | FP | 1 | 1 | 0.50 | 0.33 |
| 3 | 0.80 | TP | 2 | 1 | 0.67 | 0.67 |
| 4 | 0.60 | FP | 2 | 2 | 0.50 | 0.67 |
| 5 | 0.40 | TP | 3 | 2 | 0.60 | 1.00 |
At rank 3, the detector has found two of the three objects. Its recall is therefore:
\text{recall} = \frac{\text{cumulative TP}}{\text{total ground truth}} = \frac{2}{3}Its precision is:
\text{precision} = \frac{\text{cumulative TP}}{\text{detections considered}} = \frac{2}{3}In general:
\text{precision} = \frac{TP}{TP + FP}\text{recall} = \frac{TP}{TP + FN}For this ranked-list calculation, the number of ground-truth objects is fixed. Thus, after processing a detection, recall is cumulative TP divided by the total number of ground-truth objects.
A Confidence Threshold Changes the Trade-Off
A deployed detector often keeps only detections above a confidence threshold. In the example, keeping scores of at least 0.8 gives two TPs and one FP:
- Precision: \(2 / 3 \approx 0.67\)
- Recall: \(2 / 3 \approx 0.67\)
Lowering the threshold to 0.4 includes every detection:
- Precision: \(3 / 5 = 0.60\)
- Recall: \(3 / 3 = 1.00\)
The lower threshold finds the final object, so recall increases. It also admits another false positive, so precision decreases. This is the central precision-recall trade-off.
The complete ranked list gives a sequence of precision-recall points. These points form the basis of a precision-recall curve. Average precision later summarizes that curve into one value.
A Small Python Calculation
The following code calculates cumulative precision and recall when the matching results are already known:
results = ["TP", "FP", "TP", "FP", "TP"]
total_ground_truth = 3
true_positives = 0
false_positives = 0
for rank, result in enumerate(results, start=1):
if result == "TP":
true_positives += 1
else:
false_positives += 1
precision = true_positives / (true_positives + false_positives)
recall = true_positives / total_ground_truth
print(f"rank={rank}, precision={precision:.2f}, recall={recall:.2f}")This code does not perform IoU matching. A complete evaluator must first compare each detection with ground-truth boxes, enforce one-to-one matching, and produce the TP or FP sequence.
Exercise
A dataset has four ground-truth objects. The ranked results are TP, FP, FP, TP, TP. What are precision and recall after rank 4?
Show answer
There are two true positives and two false positives. Precision is 2 / 4 = 0.50, and recall is 2 / 4 = 0.50.