Sub topic 3
Duplicate Detections and Non-Maximum Suppression
Prerequisites: you should know that an object detector returns bounding boxes, class labels, and confidence scores. You should also know how Intersection over Union (IoU) measures the overlap between two boxes.
A detector can produce several boxes for one object. For example, a person detector might return three boxes around the same person with scores of 0.94, 0.87, and 0.61. These are usually not three people. They are duplicate detections caused by nearby candidate locations or overlapping predictions.
Duplicate boxes make evaluation worse. After matching, only one box can usually count as a true positive. The other boxes become false positives. They also make the displayed output confusing.
The idea behind non-maximum suppression
Non-maximum suppression (NMS) is a post-processing algorithm that removes overlapping boxes while keeping the highest-scoring box. “Maximum” refers to the confidence score. “Suppression” means removing lower-scoring alternatives.
The standard greedy NMS procedure is:
- Sort boxes by confidence score from highest to lowest.
- Keep the highest-scoring box.
- Compare it with the remaining boxes.
- Remove boxes whose IoU with the kept box is above a chosen threshold.
- Repeat with the next remaining box.
The NMS threshold controls how much overlap is allowed. A threshold of 0.5 removes a box when its IoU with a selected box is greater than 0.5. A lower threshold removes more boxes. A higher threshold preserves more boxes.
A working implementation
The following implementation assumes boxes use corner coordinates (x1, y1, x2, y2). It applies NMS to one class at a time.
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, ix2 - ix1) * max(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 nms(detections, iou_threshold=0.5):
"""Return detections that survive greedy NMS.
Each detection is a tuple: (score, (x1, y1, x2, y2)).
"""
remaining = sorted(detections, key=lambda item: item[0], reverse=True)
kept = []
while remaining:
best = remaining.pop(0)
kept.append(best)
remaining = [
detection
for detection in remaining
if iou(best[1], detection[1]) <= iou_threshold
]
return kept
detections = [
(0.94, (10, 10, 50, 80)),
(0.87, (12, 12, 49, 79)), # duplicate of the first box
(0.61, (100, 20, 140, 90)),
]
for score, box in nms(detections, iou_threshold=0.5):
print(score, box)The first and second boxes overlap strongly. The first has the larger score, so NMS keeps it and removes the second. The third box remains because it describes a different image region.
Applying NMS by class
NMS should normally run separately for each class. A person box and a bicycle box can overlap legitimately. Class-agnostic NMS compares all boxes together and might incorrectly remove one of them.
A typical pipeline first filters boxes below a confidence threshold, then applies class-wise NMS. The confidence threshold controls whether a prediction is considered worth processing. The NMS threshold controls whether two retained candidates are too similar.
| Setting | Main question | Common effect |
|---|---|---|
| Confidence threshold | Is this prediction strong enough to consider? | Removes low-score candidates |
| NMS IoU threshold | Is this candidate a duplicate of a kept box? | Removes overlapping candidates |
| Per-class grouping | Do these boxes represent the same category? | Prevents different classes from suppressing each other |
Failure modes and alternatives
A single threshold cannot fit every scene. If two objects of the same class are close together, their boxes may overlap enough for NMS to remove a real object. This is common with crowded people, animals, or parked vehicles.
If the threshold is too low, real neighboring objects disappear. If it is too high, duplicate boxes survive. Test the threshold on validation data rather than choosing it from one image.
Soft-NMS is an alternative that lowers the scores of overlapping boxes instead of removing them immediately. This can help in crowded scenes. DIoU-NMS also considers the distance between box centers, not only their overlap. These methods add complexity, so standard class-wise NMS remains a useful baseline.
Exercise
Three same-class detections have scores 0.95, 0.80, and 0.70. The first and second boxes have IoU 0.65, the first and third have IoU 0.20, and the second and third have IoU 0.10. With an NMS threshold of 0.5, which boxes survive?
Show answer
The 0.95 box is kept. The 0.80 box is suppressed because its IoU with the kept box is 0.65. The 0.70 box survives because its IoU with the kept box is 0.20.