Definition
Object detection is the task of finding every instance of a set of object classes in an image and returning, for each one, an axis-aligned bounding box, a class label and a confidence score. The number of objects is not known in advance. The standard benchmarks are PASCAL VOC (Everingham et al., IJCV 2010; 20 classes) and MS COCO (Lin et al., ECCV 2014; 80 classes).
Classification answers "what is in the image"; detection answers "what is where, and how many". This adds three difficulties: objects range from a few pixels to the whole frame, they overlap and occlude each other, and the output is a variable-size set. Before deep learning, detectors slid a window over an image pyramid and scored each window with hand-built features: Viola–Jones for faces (2001), HOG with a linear SVM for pedestrians (Dalal and Triggs, 2005), and deformable part models (DPM, Felzenszwalb et al., 2010).
Boxes and IoU
A box is or (centre, width, height) in pixels. Two boxes are compared by intersection over union, which is 1 for identical boxes and 0 for disjoint ones and does not depend on the image scale:
- area of the overlap rectangle,
- box areas
IoU is used three times: to decide which anchors or proposals are positives in training, to remove duplicates at test time, and to decide whether a detection counts as correct. As a training loss it appears in Intersection over Union (IoU) loss.
Three families
Two-stage detectors first generate class-agnostic region proposals, then classify and refine each one. R-CNN (2014) ran a CNN on about 2,000 warped crops from selective search. Fast R-CNN (2015) ran the CNN once and pooled each region from the feature map. Faster R-CNN (2015) replaced selective search with a learned region proposal network on anchors. Mask R-CNN (2017) added a mask branch and RoIAlign.
One-stage detectors skip proposals and predict class scores and a box at every feature-map location. YOLO (2016) divides the image into a grid. SSD (2016) uses default boxes on several feature maps. RetinaNet (2017) matched two-stage accuracy by using Focal loss against the extreme foreground–background imbalance. Anchor-based heads regress offsets from preset boxes; anchor-free heads such as FCOS (Tian et al., 2019) predict the distances from a point to the four box sides, or, as in CornerNet and CenterNet, keypoints.
Set prediction: DETR (Carion et al., 2020) decodes learned object queries with a transformer and matches them one to one to the ground truth with the Hungarian algorithm, so duplicates are penalised in training and no NMS is needed. Deformable DETR (2021) attends to a few sampled points per query, which converges much faster and helps small objects.
Non-maximum suppression
- 1Drop detections below a score threshold (e.g. 0.05) and sort the rest by score.
- 2Take the highest-scoring box and keep it.
- 3Remove every remaining box of the same class whose IoU with it exceeds (typically 0.3 to 0.7).
- 4Repeat on what is left; keep at most 100 boxes per image (the COCO limit).
A dense or proposal-based detector fires many times on each object, so NMS is part of the model at inference. Its threshold is a trade-off: too low and a second, overlapping object is deleted; too high and duplicates survive as false positives.
Evaluation: average precision
Detections of one class are sorted by score over the whole test set. Each is a true positive if it overlaps a not-yet-matched ground-truth box with IoU , otherwise a false positive. Sweeping down the list gives a precision–recall curve; AP is the area under its monotone envelope:
- precision at the point of the ranked list where recall reaches (0 if never reached)
- IoU threshold for a match; VOC uses only , COCO averages ten
- also averaged over classes; COCO calls this mean simply "AP" and writes AP50 and AP75 for single thresholds
Benchmarks and reference results
- PASCAL VOC
- 20 classes; VOC 2007 has 9,963 images (5,011 trainval, 4,952 test); mAP at IoU 0.5, 11-point interpolated for VOC 2007, area under the curve from VOC 2010 on
- MS COCO
- 80 classes; train2017 about 118k images, val2017 5k; reports AP (IoU 0.50:0.95), AP50, AP75 and AP for small (area ), medium and large () objects, with up to 100 detections per image
- Faster R-CNN, VGG-16
- 73.2% mAP on VOC 2007 test (trained on VOC 2007 + 2012), about 5 FPS
- YOLO (v1)
- 63.4% mAP on VOC 2007 test at 45 FPS
- RetinaNet, ResNet-101-FPN
- 39.1 AP on COCO test-dev
- DETR, ResNet-50
- 42.0 AP on COCO val, matching a tuned Faster R-CNN with FPN, after a 500-epoch schedule
Worked example: NMS and AP for the person class
| Step | Computation | Result |
|---|---|---|
| IoU(A, B) | 6804 / (8024 + 8576 − 6804) = 6804 / 9796 | 0.695 |
| IoU(A, C) | 5109 / (8024 + 7923 − 5109) = 5109 / 10838 | 0.471 |
| NMS, τ = 0.5 | keep A (0.95); B: 0.695 > 0.5, removed; C: 0.471 ≤ 0.5, kept | A, C |
| Match at t = 0.5 | A → person 1 (IoU 0.924); C → person 2 (IoU 0.930) | TP, TP |
| AP50, τ = 0.5 | precision 1 at recall 0.5 and 1.0 | 1.000 |
| NMS, τ = 0.7 | B survives (0.695 ≤ 0.7); ranked A, B, C = TP, FP, TP | A, B, C |
| AP50, τ = 0.7 | precision 1 for r ≤ 0.5 (51 points), 2/3 for r > 0.5 (50 points): (51 + 33.33) / 101 | 0.835 |
B overlaps person 1 with IoU 0.731, enough to be a correct box on its own, but person 1 is already taken by A, so B is a false positive. Detection metrics count each object once.
1import torch2from torchvision.io import read_image3from torchvision.models.detection import fasterrcnn_resnet50_fpn_v2, FasterRCNN_ResNet50_FPN_V2_Weights4from torchvision.ops import box_iou, batched_nms56weights = FasterRCNN_ResNet50_FPN_V2_Weights.COCO_V17model = fasterrcnn_resnet50_fpn_v2(weights=weights, box_score_thresh=0.05).eval()8img = read_image("street.jpg")9with torch.no_grad():10 out = model([weights.transforms()(img)])[0] # dict: boxes (N, 4) xyxy, labels, scores11names = weights.meta["categories"]12for b, l, s in zip(out["boxes"], out["labels"], out["scores"]):13 if s > 0.5:14 print(names[l], round(s.item(), 3), b.round().tolist())1516# NMS is built into the model; this is the same operation done by hand17boxes = torch.tensor([[62, 42, 121, 178], [52, 34, 116, 168], [82, 47, 139, 186.]])18scores = torch.tensor([0.95, 0.85, 0.70])19labels = torch.zeros(3, dtype=torch.long) # same class: suppression applies20print(box_iou(boxes, boxes)[0]) # tensor([1.0000, 0.6946, 0.4714])21print(batched_nms(boxes, scores, labels, iou_threshold=0.5)) # tensor([0, 2])Use when
- +Two-stage: accurate localisation, per-region features are easy to extend (masks, keypoints)
- +One-stage: one dense pass, simple and fast; the basis of most real-time detectors
- +Set prediction: no anchors and no NMS; the loss directly targets a duplicate-free set
Avoid when
- −Two-stage: two networks or heads and more latency per image
- −One-stage: extreme class imbalance (handled by focal loss or sampling); needs NMS
- −Set prediction (original DETR): slow convergence and weak small-object AP
Check yourself
Two people stand close together; their ground-truth boxes have IoU 0.47. What happens with NMS at τ = 0.3?
Check yourself
Which change raises COCO AP@[.5:.95] but leaves AP50 unchanged?
Remember
- ✓Output: a variable-size set of (box, class, score); boxes are compared by IoU = intersection / union.
- ✓Two-stage (R-CNN family): propose, then classify each region. One-stage (YOLO, SSD, RetinaNet, FCOS): predict at every location. Set prediction (DETR): N queries, Hungarian matching, no NMS.
- ✓Greedy per-class NMS removes same-class boxes with IoU > τ; too low a τ deletes overlapping objects, too high keeps duplicates.
- ✓AP = area under the interpolated precision–recall curve; VOC uses IoU 0.5, COCO averages AP over IoU 0.50 to 0.95 and over 80 classes.