Sub topic 13
Choosing Confidence and IoU Thresholds
Assume you know basic Python, bounding boxes, intersection over union (IoU), and how to run a pretrained YOLO model. This chapter focuses on turning raw detections into useful predictions.
Two thresholds with different jobs
A YOLO model gives each candidate detection a class, a bounding box, and a confidence score. The confidence score estimates how likely the box is a real object of the predicted class.
The confidence threshold, often written as conf, removes weak detections. A higher value produces fewer predictions and usually fewer false positives. A lower value keeps more possible objects and usually improves recall, but it can also add noise.
Non-maximum suppression (NMS) removes duplicate boxes for the same object. NMS compares boxes using IoU, which measures the area of their overlap divided by their combined area. The NMS IoU threshold, often written as iou, controls when two boxes count as duplicates.
- A lower NMS IoU threshold removes boxes even when they overlap moderately.
- A higher NMS IoU threshold keeps more overlapping boxes.
These thresholds are independent. conf decides whether a detection is strong enough to consider. iou decides whether two considered detections are too similar to keep together.
Tune confidence for the application
The best confidence threshold depends on the cost of errors. In a safety inspection system, missing a defect may cost more than reviewing an extra false alarm, so use a lower threshold and inspect the resulting precision. In an automatic counting system, duplicate or false detections may be more damaging.
Use a validation set that represents real inputs. Try several confidence values, such as 0.10, 0.25, 0.50, and 0.75. Measure precision and recall at each value.
Precision is the fraction of reported detections that are correct. Recall is the fraction of real objects that the detector finds. Increasing confidence usually increases precision while decreasing recall, but the exact behavior depends on the data.
Tune NMS IoU carefully
The NMS IoU threshold affects crowded scenes. If two nearby objects overlap heavily, a low threshold can incorrectly suppress one of them. Raising the threshold may preserve both objects. However, a high threshold can leave duplicate boxes around one object.
For ordinary scenes, a value near 0.5 to 0.7 is a reasonable starting point. For crowded objects, test higher values and inspect images rather than relying only on a single metric.
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
for conf in (0.10, 0.25, 0.50, 0.75):
results = model.predict("bus.jpg", conf=conf, iou=0.60, verbose=False)
count = sum(len(result.boxes) for result in results)
print(f"conf={conf:.2f}: {count} detections")The iou=0.60 argument here is the NMS threshold. It is not the IoU threshold used to decide whether a prediction matches a labeled object during evaluation. Those are separate uses of IoU.
Common threshold mistakes
A frequent mistake is lowering confidence to recover missed objects without checking the false-positive rate. Another is changing the NMS IoU threshold when the real problem is poor localization or incorrect class predictions. Thresholds cannot repair mislabeled training data, unsuitable image sizes, or a model that has not learned the target objects.
Exercise
A detector misses objects in a crowded image because overlapping boxes are being removed. Which threshold should you test first, and what trade-off can result?
Show answer
Test a higher NMS IoU threshold. It may preserve boxes for separate nearby objects, but it can also keep duplicate boxes around one object.