Object Detection Explained: Anchors, IoU, Non-Maximum Suppression (NMS) & YOLO vs. DETR
How neural networks locate AND classify multiple objects in one image: Intersection over Union (IoU), greedy Non-Maximum Suppression (NMS), mAP@50:95 evaluation, and real-time YOLO vs. end-to-end Transformer DETR!
Image classification says 'there is a car somewhere' — Object Detection outputs exact pixel coordinates [x1, y1, x2, y2] and class labels for every pedestrian, cyclist, and traffic light in real time!
Implementing IoU and NMS cleanly (and knowing when to pick YOLO vs. RT-DETR) is a core computer vision engineering skill.
Understanding mAP@50:95 prevents shipping a detector that gets high recall by spamming 50 noisy boxes per frame!
Classic Computer Vision coding interview: 'Implement IoU and Non-Maximum Suppression (NMS) in NumPy/PyTorch, and explain how DETR eliminates NMS!'
In greedy Non-Maximum Suppression (NMS), after keeping the highest-confidence box B_max, which remaining boxes get deleted?
- Compute exact Intersection over Union (IoU) between predicted and ground-truth bounding boxes
- Implement Non-Maximum Suppression (NMS) to remove duplicate overlapping detections
- Understand mAP@50 and mAP@50:95 precision-recall evaluation across classes and IoU thresholds
- Compare real-time one-stage YOLO detectors against end-to-end DETR (Hungarian bipartite matching, zero NMS!)
0.0 (disjoint) to 1.0 (exact pixel match).mAP@50:95 averages Average Precision across 10 IoU thresholds from 0.50 to 0.95!Imagine 10 spotters all pointing at the same deer in a photo and drawing slightly shifted rectangles around it. NMS picks the most confident spotter's rectangle and tells everyone else pointing at the same spot (IoU > 0.5) to lower their hands — while letting a spotter pointing at a second deer keep their box!
1. Bounding Boxes & Intersection over Union (IoU)
Every detection outputs [x1, y1, x2, y2, confidence, class_id]. To grade whether a predicted box is a True Positive, compute IoU (intersection / union). Modern losses like GIoU / CIoU optimize IoU directly instead of raw L1 box coordinates!
2. Non-Maximum Suppression (NMS)
Dense grid detectors emit thousands of candidate boxes per image. NMS cleans them up in 3 lines: sort by confidence, pop the best box into keep[], and remove all remaining boxes with IoU > thresh until none remain!
NMS IoU Suppression Threshold Simulator
3. Real-Time YOLO vs. End-to-End DETR
- YOLO (v8/v10/v11): Anchor-free dense CNN grid running at 100+ FPS on edge GPUs.
- DETR / RT-DETR: Uses Transformer Object Queries + Hungarian Bipartite Matching during training so each query claims exactly one ground-truth object — eliminating hand-tuned anchors and NMS completely!
Why does DETR not need Non-Maximum Suppression (NMS) at inference time?
The exact honest questions almost every beginner wonders — answered in plain English without hiding behind jargon.
In crowded scenes (e.g., two pedestrians standing right next to each other with IoU = 0.55), hard NMS deletes the second person! Soft-NMS decays the overlapping box's confidence smoothly instead of hard-deleting it.
Non-Maximum Suppression (NMS) Greedy Box Filtering
Watch NMS keep the highest-confidence box and prune overlapping duplicates.
Autonomous Driving, Retail Analytics & Medical Detection
YOLO/RT-DETR pipelines power real-time pedestrian detection, warehouse inventory counting, and radiology lesion localization.
import torch
# Vectorized Box IoU & Greedy Non-Maximum Suppression (NMS) from scratch
def box_iou(box: torch.Tensor, boxes: torch.Tensor) -> torch.Tensor:
x1 = torch.maximum(box[0], boxes[:, 0])
y1 = torch.maximum(box[1], boxes[:, 1])
x2 = torch.minimum(box[2], boxes[:, 2])
y2 = torch.minimum(box[3], boxes[:, 3])
inter = (x2 - x1).clamp(min=0) * (y2 - y1).clamp(min=0)
area_a = (box[2] - box[0]) * (box[3] - box[1])
area_b = (boxes[:, 2] - boxes[:, 0]) * (boxes[:, 3] - boxes[:, 1])
return inter / (area_a + area_b - inter + 1e-6)
def greedy_nms(boxes: torch.Tensor, scores: torch.Tensor, iou_thresh: float = 0.5):
order = scores.argsort(descending=True)
keep = []
while order.numel() > 0:
i = order[0].item()
keep.append(i)
if order.numel() == 1:
break
ious = box_iou(boxes[i], boxes[order[1:]])
order = order[1:][ious <= iou_thresh]
return keep
boxes = torch.tensor([[10., 10., 50., 50.], [12., 12., 48., 48.], [80., 80., 120., 120.]])
scores = torch.tensor([0.95, 0.80, 0.88])
print("Kept box indices after NMS:", greedy_nms(boxes, scores, iou_thresh=0.5))
person and bicycle — run NMS per class so you don't delete the rider!10×10 boxes overlap completely (intersection = 100). What is their IoU?