Sub topic 9
Removing Duplicates with Non-Maximum Suppression
Why detectors produce duplicate boxes
Assume you know basic Python, NumPy, convolutional feature maps, classification, and box regression. This chapter focuses on inference, the stage where a trained detector produces final predictions.
A detector often predicts several boxes for the same object. Nearby anchors can all recognize one car, for example. Each prediction may have a class label and a confidence score:
| Box | Class | Score |
|---|---|---|
| A | car | 0.92 |
| B | car | 0.85 |
| C | car | 0.61 |
These boxes are useful during training because several locations may contain evidence for the car. They are inconvenient as final output because an image should usually contain one box for that car. Non-maximum suppression (NMS) is a post-processing algorithm that keeps the strongest box and removes nearby, weaker duplicates.
NMS uses Intersection over Union (IoU), which measures the overlap between two boxes:
IoU = \frac{\text{area of intersection}}{\text{area of union}}An IoU of 1 means identical boxes. An IoU of 0 means that the boxes do not overlap.
The greedy NMS procedure
The standard algorithm is greedy:
- Sort boxes by confidence score from highest to lowest.
- Keep the highest-scoring box.
- Compare it with every remaining box.
- Remove boxes whose IoU is greater than a chosen threshold.
- Repeat until no boxes remain.
A threshold of 0.5 means that a remaining box is removed when it overlaps the selected box by more than 50 percent. A lower threshold removes more boxes. A higher threshold keeps more boxes, including possible duplicates.
def iou(a, b):
"""IoU for boxes represented as (x1, y1, x2, y2)."""
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.0, ix2 - ix1) * max(0.0, iy2 - iy1)
area_a = max(0.0, a[2] - a[0]) * max(0.0, a[3] - a[1])
area_b = max(0.0, b[2] - b[0]) * max(0.0, b[3] - b[1])
union = area_a + area_b - intersection
return intersection / union if union else 0.0
def nms(boxes, scores, iou_threshold=0.5):
"""Return indices of boxes kept by greedy NMS."""
order = sorted(range(len(boxes)), key=lambda i: scores[i], reverse=True)
keep = []
while order:
best = order.pop(0)
keep.append(best)
order = [
i for i in order
if iou(boxes[best], boxes[i]) <= iou_threshold
]
return keep
boxes = [
(0, 0, 10, 10),
(1, 1, 11, 11),
(50, 50, 60, 60),
]
scores = [0.9, 0.8, 0.7]
print(nms(boxes, scores)) # [0, 2]The second box is removed because it overlaps the first box strongly. The third box remains because it is far away.
Important choices and failure modes
NMS is usually applied separately for each class. A person box should not suppress a nearby bicycle box merely because their rectangles overlap. Implementations may also use class-aware offsets to process all classes efficiently in one operation.
The threshold is a trade-off. If it is too low, two nearby objects can suppress each other. If it is too high, duplicate detections remain. Crowded scenes are especially difficult because distinct objects may have high IoU.
Variants such as soft-NMS reduce a box's score instead of deleting it immediately. This can help when objects overlap heavily, but it adds another parameter and does not remove the need for careful evaluation.
Exercise
With an IoU threshold of 0.5, which box is kept first for scores [0.4, 0.9, 0.7]?
Show answer
The box with score 0.9 is kept first, regardless of its position in the input list.