Sub topic 6
Removing Duplicate Boxes with Non-Maximum Suppression
This chapter assumes you understand bounding boxes in (x1, y1, x2, y2) format, intersection over union (IoU), and detector outputs containing boxes, confidence scores, and class IDs.
Why duplicate boxes appear
A YOLO model predicts many candidate boxes. Several candidates may describe the same object because nearby prediction locations can all respond to it. For example, one car might produce five overlapping boxes with different scores.
Returning every candidate would count one object several times. Non-maximum suppression (NMS) is a post-processing algorithm that keeps the strongest box and removes nearby weaker boxes.
NMS uses two thresholds:
| Setting | Meaning |
|---|---|
| Confidence threshold | Discards candidates with low scores before NMS |
| IoU threshold | Controls how much overlap causes a weaker box to be removed |
The greedy NMS algorithm
For one object class, greedy NMS follows this process:
- Sort boxes by confidence, from highest to lowest.
- Keep the highest-scoring box.
- Compute its IoU with every remaining box.
- Remove boxes whose IoU is at least the NMS threshold.
- Repeat until no boxes remain.
The algorithm assumes that two boxes with large overlap probably describe the same object. The assumption can fail when two real objects are close together.
def iou(a, b):
# Boxes use (x1, y1, x2, y2), with x2 > x1 and y2 > y1.
ax1, ay1, ax2, ay2 = a
bx1, by1, bx2, by2 = b
ix1, iy1 = max(ax1, bx1), max(ay1, by1)
ix2, iy2 = min(ax2, bx2), min(ay2, by2)
intersection = max(0, ix2 - ix1) * max(0, iy2 - iy1)
area_a = (ax2 - ax1) * (ay2 - ay1)
area_b = (bx2 - bx1) * (by2 - by1)
union = area_a + area_b - intersection
return intersection / union if union else 0.0
def nms(boxes, scores, iou_threshold=0.5):
order = sorted(range(len(boxes)), key=scores.__getitem__, reverse=True)
keep = []
while order:
best = order.pop(0)
keep.append(best)
order = [
index for index in order
if iou(boxes[best], boxes[index]) < iou_threshold
]
return keep
boxes = [(10, 10, 100, 100), (20, 20, 95, 95), (150, 20, 230, 100)]
scores = [0.90, 0.70, 0.80]
print(nms(boxes, scores, iou_threshold=0.5))
# The overlapping lower-scoring box is removed.The returned values are indices into the original boxes and scores lists. In this example, the first and third boxes should remain.
Applying NMS by class
NMS should usually run separately for each class. A person box should not suppress a bicycle box just because the two boxes overlap. Class-aware NMS groups candidates by class, applies the algorithm within each group, and combines the survivors.
Some libraries offer class-agnostic NMS as an option. It can be useful when classes overlap by design, but it can also remove valid detections from different classes.
Exercise
If two boxes have IoU 0.72, what happens when the NMS IoU threshold is 0.5? What about when it is 0.8, assuming the second box has a lower score?
Show answer
At 0.5, the lower-scoring box is removed because 0.72 is at least the threshold. At 0.8, it remains because 0.72 is below the threshold.