← All answersObject detection concepts
What is Non-Max Suppression (NMS), and why is it crucial in object detection?
Short answer
A detector predicts many overlapping boxes for a single object. Non-max suppression cleans that up: for each class, it keeps the highest-confidence box, removes every other box that overlaps it by more than an IoU threshold, and repeats. Without NMS you'd get a dozen stacked boxes on every object instead of one.
NMS is the tidy-up step that turns a messy pile of raw predictions into one box per object. The algorithm is short:
def nms(boxes, scores, iou_thresh=0.5):
"""Return indices of boxes to keep."""
order = scores.argsort()[::-1] # high score first
keep = []
while len(order) > 0:
i = order[0]
keep.append(i)
# drop boxes that overlap the winner too much
rest = order[1:]
order = rest[[iou(boxes[i], boxes[j]) < iou_thresh for j in rest]]
return keepWhy the threshold matters
- Too high (e.g. 0.9): near-duplicate boxes survive, so you over-count.
- Too low (e.g. 0.2): two genuinely close objects get merged into one, so you under-count.
- A common default is around 0.45-0.5 for general scenes; tune it when objects are densely packed.
Variants like Soft-NMS decay scores instead of hard-deleting boxes, which helps in crowded scenes where real objects overlap heavily.
Related questions
Hands-on tutorials
Stuck on this in a real project?
Book a free call and I’ll give you a straight answer on your specific case.
Book a consultation