Sub topic 6
Building the Precision-Recall Curve
This chapter assumes that you know basic Python, lists, bounding boxes, Intersection over Union (IoU), and the meaning of true positives and false positives. It also assumes that detections have already been matched to ground-truth boxes at a chosen IoU threshold.
Why a detection threshold creates a curve
An object detector usually returns a confidence score for every predicted box. A high score means that the detector considers the prediction more likely to be correct.
You can choose a score threshold:
- Keep only detections with a score of at least 0.9.
- Keep detections with a score of at least 0.8.
- Keep detections with a score of at least 0.7.
- Continue lowering the threshold.
A high threshold usually keeps fewer detections. This often gives high precision but misses some objects, so recall is lower. A low threshold keeps more detections. This can increase recall, but it can also add false positives and reduce precision.
The precision-recall (PR) curve shows this trade-off. Each point represents the precision and recall obtained at one score threshold.
Create points from ranked detections
First, sort detections from highest confidence score to lowest. After matching, represent each detection as either:
1for a true positive0for a false positive
Suppose there are three ground-truth objects and the ranked detections have these outcomes:
| Rank | Score | Result | Cumulative TP | Cumulative FP | Precision | Recall |
|---|---|---|---|---|---|---|
| 1 | 0.95 | TP | 1 | 0 | 1.00 | 0.33 |
| 2 | 0.85 | 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 1, the threshold is 0.95. At rank 2, the threshold is 0.85, and so on. The point after each detection is therefore one possible operating point.
Precision and recall are calculated as:
\text{precision} = \frac{TP}{TP + FP}\text{recall} = \frac{TP}{\text{number of ground-truth objects}}A false positive can lower precision without changing recall. A true positive increases recall and can either increase or decrease precision.
def precision_recall_points(results, number_of_ground_truths):
"""Return PR points for ranked results.
results must be ordered from highest to lowest confidence.
Each result is 1 for TP or 0 for FP.
"""
if number_of_ground_truths <= 0:
raise ValueError("The number of ground-truth objects must be positive")
points = []
true_positives = 0
false_positives = 0
for rank, result in enumerate(results, start=1):
if result == 1:
true_positives += 1
elif result == 0:
false_positives += 1
else:
raise ValueError("Each result must be 1 or 0")
precision = true_positives / (true_positives + false_positives)
recall = true_positives / number_of_ground_truths
points.append((rank, precision, recall))
return points
results = [1, 0, 1, 0, 1]
for rank, precision, recall in precision_recall_points(results, 3):
print(f"rank={rank}, precision={precision:.2f}, recall={recall:.2f}")Plotting and interpreting the curve
The horizontal axis is recall, and the vertical axis is precision. As the score threshold decreases, recall can stay the same or increase. It never decreases because detections are only added.
Precision can move in either direction. Adding a true positive may raise or lower it, depending on the current ratio. Adding a false positive usually lowers it. Therefore, the raw curve can contain downward and upward steps.
Practical details that affect the curve
Matching must be performed consistently for every threshold. A prediction that overlaps one ground-truth box enough to be a true positive can claim that box only once. A second prediction for the same object becomes a false positive, usually because it is a duplicate.
Evaluate one class at a time unless your evaluation protocol explicitly combines classes. A detection with the wrong class is normally a false positive for its predicted class and leaves the correct ground-truth object unmatched.
Confidence ties also need a deterministic rule. If two detections have exactly the same score, changing their order can change intermediate PR points. Use a stable sort or a documented tie-breaker.
The next step is to summarize the curve with average precision, which measures the area under an interpolated precision-recall curve.
Exercise
A detector has five ranked results with outcomes [1, 1, 0, 0, 1], and there are four ground-truth objects. What are precision and recall after the fourth result?
Show answer
There are two true positives and two false positives. Precision is 2 / 4 = 0.50, and recall is 2 / 4 = 0.50.