Key ideas
- Task: for every object, output a class label and a bounding box. A box is 4 numbers: corners or centre format , with pointing down in pixel coordinates. Classification answers "what", detection also "where"; pixel masks are segmentation.
- IoU (Jaccard index) = area of intersection / area of union: 1 for identical boxes, 0 for disjoint ones1. A detection counts as correct only if its IoU with a ground-truth box reaches a threshold, 0.5 in PASCAL VOC's metric4; IoU punishes missed area as much as extra background.
- Anchor boxes: reference boxes of several scales and aspect ratios centred on a grid; the network predicts a class and four offsets per anchor1. d2l gives an anchor of scale and ratio the size and keeps the scale–ratio pairs that contain or at every pixel1. Offsets measure the centre shift in units of the anchor's width and height and the size change as a log ratio1,4; d2l also divides them by 0.1 (centre) and 0.2 (size) so the targets are more evenly spread and easier to fit1. The log treats doubling and halving symmetrically (), and decoding always gives a positive width.
- Labelling anchors: in Faster R-CNN an anchor is positive if it has the highest IoU with some ground-truth box or an IoU above 0.7, negative if its IoU is below 0.3 for every ground-truth box, and ignored otherwise4. d2l likewise first gives every ground-truth box its best anchor, then applies a threshold1.
- Non-maximum suppression: sort the boxes by score, keep the best, delete the remaining boxes whose IoU with it exceeds a threshold, repeat with the next survivor1. A box that only overlaps an already deleted box survives. d2l's NMS, like torchvision's
nms, puts all classes into one list;batched_nmsruns it per class, so it never suppresses across classes (see the code). The greedy rule is hand-crafted, but a network can also learn to suppress duplicates8. - Two-stage detectors3: R-CNN runs a CNN on each of about 2000 selective-search proposals (slow). Fast R-CNN runs the CNN once per image and uses RoI pooling, which splits each proposal's window into an grid and max-pools every cell, so all proposals give features of the same shape. Faster R-CNN drops selective search and lets a learned region proposal network (RPN) propose regions from the shared feature map. The RPN predicts objectness scores and box coordinates per position for anchors (3 scales × 3 ratios)4.
- One-stage detectors predict classes and boxes for all positions in a single pass, which makes them fast5. YOLO frames detection as one regression from the image to an tensor. The cell containing an object's centre is responsible for it and predicts the centre relative to the cell and the size relative to the image; a cell's few boxes share one set of class probabilities, so groups of small objects (flocks of birds) are hard5. SSD predicts from dense anchors on several feature maps with convolutions that output class channels and offset channels for anchors per position2; deeper, coarser maps have larger receptive fields and detect larger objects2.
- Feature pyramids: FPN adds a top-down pathway with lateral connections, so even the fine, high-resolution maps carry strong semantics and can detect small objects; its RPN puts anchors of a single scale (three aspect ratios) on each pyramid level7.
- Class imbalance: almost every candidate location is easy background. Two-stage detectors cut the candidates to one or two thousand proposals and sample minibatches with a fixed positive/negative ratio (the RPN: 256 anchors per image, up to 1:1)4,6. One-stage detectors face the full imbalance: SSD-style models mine hard negatives, while RetinaNet uses the focal loss, which down-weights well-classified examples, and initializes the classifier bias so that every anchor starts at foreground probability 6.
- Box regression loss: L1 (d2l's SSD) or smooth L1 (Faster R-CNN) on the offsets, computed for positive anchors only2,4.
- Evaluation: go down the detections in order of confidence and pair each with a still unclaimed ground-truth object; every object can be claimed only once, so a second detection of it is a false positive that costs precision8. A detection is a true positive if it claims a ground-truth box at the IoU threshold, otherwise a false positive; ground-truth boxes nobody claims are false negatives. AP summarizes the precision–recall curve of one class, and mAP averages AP over classes. COCO's main metric averages AP over the IoU thresholds , written mAP@[.5, .95], while PASCAL VOC uses mAP@0.54. A detector can win at 0.5 and lose on the stricter average if its boxes are loose4.
Formulas
Faster R-CNN regresses these offsets directly4; d2l divides by 0.1 and by 0.21.
Here is the predicted probability of the true class and the prior bias6. Smooth L1 with parameter is for and otherwise2.
Worked example
Boxes score 0.9, score 0.8, score 0.7, score 0.6.
- : overlap , union , so . : overlap , union , so .
- NMS at threshold 0.5: keep , delete (0.681 > 0.5), keep (only 1/3 with ), keep . Result: .
- AP with 3 ground-truth objects and ranked detections TP, FP, TP, TP: precisions at the TPs are , , , so AP .
In code
import torch
from torchvision.ops import batched_nms, box_iou, nms
boxes = torch.tensor([[0., 0., 10., 10.], # A (x1, y1, x2, y2)
[5., 0., 15., 10.], # B
[1., 1., 11., 11.], # C
[20., 20., 30., 30.]]) # D
scores = torch.tensor([0.9, 0.8, 0.7, 0.6])
print(box_iou(boxes, boxes)[0]) # tensor([1.0000, 0.3333, 0.6807, 0.0000])
print(nms(boxes, scores, iou_threshold=0.5)) # tensor([0, 1, 3])
labels = torch.tensor([0, 0, 1, 0]) # now C belongs to another class
print(batched_nms(boxes, scores, labels, 0.5)) # tensor([0, 1, 2, 3]) C survives
Common pitfalls
- A predicted box with a tiny IoU is a false positive, not a false negative; a false negative is a ground-truth object that nothing detected.
- A box lying entirely inside the object can still fail the 0.5 threshold, because IoU also punishes the missed area.
- Two detections of the same object: only the higher-ranked match is a TP, the other is a FP8.
- Plain NMS ignores classes: a dog box can delete an overlapping cat box unless NMS runs per class (
batched_nms). - Geometric augmentation must transform the boxes too: a horizontal flip maps to , and random crops must clip or drop boxes.
- YOLO predicts numbers per cell: class probabilities are shared by the cell's boxes, not repeated per box5.
- Deep, coarse feature maps are for large objects (large receptive fields); small objects need the fine, early maps2.
- A higher AP@0.5 does not imply a higher COCO AP@[.5, .95]4.
References
- Dive into Deep Learning — Sec. 14.4.1 Generating Multiple Anchor Boxes; 14.4.2 Intersection over Union (IoU); 14.4.3 Labeling Anchor Boxes in Training Data; 14.4.4 Predicting Bounding Boxes with Non-Maximum Suppression
- Dive into Deep Learning — Sec. 14.7.1 Model (14.7.1.1 Class Prediction Layer; 14.7.1.2 Bounding Box Prediction Layer); 14.7.2.2 Defining Loss and Evaluation Functions; 14.7.5 Exercises (smooth L1 loss)
- Dive into Deep Learning — Sec. 14.8.1 R-CNNs; 14.8.2 Fast R-CNN; 14.8.3 Faster R-CNN
- Ren et al. (2015), Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks — Sec. 3.1.1 Anchors; 3.1.2 Loss Function (Eq. 2); 3.1.3 Training RPNs; 4.2 Experiments on MS COCO (Table 11)
- Redmon et al. (2015), You Only Look Once: Unified, Real-Time Object Detection — Sec. 1 Introduction; 2 Unified Detection; 2.4 Limitations of YOLO
- Lin et al. (2017), Focal Loss for Dense Object Detection — Sec. 2 Related Work (Class Imbalance); 3.2 Focal Loss Definition; 3.3 Class Imbalance and Model Initialization; 3.4 Class Imbalance and Two-stage Detectors; 4.1 Inference and Training (Initialization)
- Lin et al. (2016), Feature Pyramid Networks for Object Detection — Sec. 3 Feature Pyramid Networks (Top-down pathway and lateral connections); 4.1 Feature Pyramid Networks for RPN
- Hosang et al. (2017), Learning non-maximum suppression — Sec. 1 Introduction; 4 Doing NMS with a convnet (4.1 Loss, matching of detections to objects)