Skip to content
BIASED

Object detection

Boxes, IoU, anchors and queries, a one-stage vs two-stage sketch, detection losses and mAP.

The task: boxes and classes

The topic Convolutional architectures built backbones that turn an image into one label. A street photograph holds many objects, and a detector must list them all: which classes, how many, and where. The answer is a set of bounding boxes with class scores. The set length changes per image, so a fixed-output classifier cannot produce it. Detectors keep the backbones and change the head.

Three things make this harder than classification: the output is a set of unknown size; localization is continuous, so “correct” needs an overlap tolerance; objects span scales from tens to hundreds of pixels, and most of the image is background. Sliding a classifier over every window is correct in principle and hopeless in cost (~10510^5 full forward passes per image).

Key idea. Replace “find the objects” by many fixed questions about reference boxes — proposals or anchors. Label each by overlap with the ground truth; classify and regress; remove duplicates.

Intersection over union

Whether a predicted box matches a true one is measured by intersection over union

IoU⁡(b,b′)=∣b∩b′∣∣b∪b′∣=∣b∩b′∣∣b∣+∣b′∣−∣b∩b′∣.\operatorname{IoU}(b, b') = \frac{\lvert b \cap b' \rvert}{\lvert b \cup b' \rvert} = \frac{\lvert b \cap b' \rvert}{\lvert b \rvert + \lvert b' \rvert - \lvert b \cap b' \rvert}.

Axis-aligned intersection has corners (max⁡x1,max⁡y1,min⁡x2,min⁡y2)(\max x_1, \max y_1, \min x_2, \min y_2) and area zero when empty. IoU is 11 for identical boxes and 00 for disjoint ones; it is scale-free.

Worked example: A=(1,1,5,5)A = (1,1,5,5) and B=(3,2,7,6)B = (3,2,7,6) both have area 16; intersection area 6; IoU⁡=6/26≈0.231\operatorname{IoU} = 6/26 \approx 0.231. A same-size box shifted by a fraction uu of its side along one axis has IoU⁡=(1−u)/(1+u)\operatorname{IoU} = (1-u)/(1+u), so threshold 0.50.5 allows a third of a side and 0.750.75 only a seventh. A 2-pixel error hurts a 10×1010 \times 10 box (IoU=0.667\mathrm{IoU} = 0.667) far more than a 100×100100 \times 100 one (0.9610.961).

Two-stage detectors

The R-CNN line applies the two rescues one at a time.

R-CNN runs ~2000 selective-search proposals, warps each to a fixed size, and classifies with a CNN — accurate for 2014, ~13 s/image on GPU because every proposal is a separate forward pass.

Boxes are predicted as corrections to a reference a=(xa,ya,wa,ha)a = (x_a, y_a, w_a, h_a):

tx=x−xawa,ty=y−yaha,tw=log⁡wwa,th=log⁡hha.t_x = \frac{x - x_a}{w_a},\quad t_y = \frac{y - y_a}{h_a},\quad t_w = \log\frac{w}{w_a},\quad t_h = \log\frac{h}{h_a}.

Shifts are relative to the reference size; the log keeps decoded widths positive. Training often uses smooth L1 (Huber) per coordinate.

Fast R-CNN runs the backbone once and RoI-pools each proposal to a fixed 7×77 \times 7 grid from the shared feature map; a multi-task loss combines softmax over K+1K+1 classes with box regression (off for background).

Faster R-CNN learns proposals with a region proposal network: at each feature location, k=9k = 9 anchors (3 scales × 3 aspect ratios) get an objectness score and offsets. Anchors with IoU >0.7> 0.7 (or best for an object) are positive; IoU <0.3< 0.3 negative; the rest ignored. With VGG-16 the system ran ~5 FPS and reached 73.2% mAP on VOC 2007. Feature pyramids later became standard for small objects.

One-stage detectors

A one-stage detector skips the proposal stage and predicts final classes and boxes on the feature map directly.

YOLO divides a 448×448448 \times 448 image into an S×SS \times S grid; each cell predicts BB boxes (coordinates + confidence) and one set of class probabilities. For VOC, S=7S = 7, B=2B = 2, C=20C = 20 → a 7×7×307 \times 7 \times 30 tensor (1,4701{,}470 numbers, 98 boxes). Fast (45 FPS) but weaker on localization and on multiple small centres in one cell.

SSD predicts from several feature maps of decreasing resolution with default boxes (anchors) at each cell — fine maps for small objects, coarse for large — and mines hard negatives so background does not dominate.

The shared obstacle: 10410^4–10510^5 candidates per image, almost all easy background whose tiny losses still sum to own the gradient.

Focal loss

Focal loss (Lin et al., 2017) reshapes cross-entropy. With ptp_t the probability of the true class,

FL⁡(pt)=−αt (1−pt)γlog⁡pt.\operatorname{FL}(p_t) = -\alpha_t\, (1 - p_t)^{\gamma} \log p_t.

At γ=2\gamma = 2 and pt=0.9p_t = 0.9 the factor (1−pt)γ=0.01(1-p_t)^\gamma = 0.01 cuts the loss a hundredfold; at pt=0.5p_t = 0.5 it is only 4×4\times smaller. Easy negatives stop drowning hard positives. RetinaNet applies this to all ~100k anchors on a ResNet feature pyramid (positives at IoU ≥ 0.5, negatives below 0.4), with no sampling, and matched two-stage detectors of its time on COCO.

NMS and mAP

Overlapping predictions for one object are removed by non-maximum suppression per class: sort by score; keep the top box; drop every remaining box with IoU >τ> \tau; repeat. At τ=0.5\tau = 0.5 in the note’s six-box example the survivors are A, D and F. Lower τ\tau kills crowds; higher τ\tau leaves duplicates.

Average precision walks detections of one class by decreasing score. A detection is a true positive if its IoU with an unmatched ground-truth box of that class is at least τ\tau; a second hit on the same object is a false positive. Precision and recall after each detection form a curve; AP is the area under the interpolated curve; mAP averages AP over classes. VOC uses τ=0.5\tau = 0.5; COCO averages τ=0.50,…,0.95\tau = 0.50,\ldots,0.95 and reports that mean as AP. Tight boxes matter for the COCO metric; AP50 alone does not.

Boxes remain a coarse description: a thin pole or an outstretched arm fills little of its rectangle. The next topic, Semantic segmentation, labels every pixel instead — dense prediction with encoder–decoders, skips and mIoU.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Go deeper — optional

Code and labs do not count toward the main percentage or the certificate.

  • IoU in codeoptional

    Compute Intersection over Union for xyxy boxes — the match measure in detection.

    ~15 min · easier on a computer · The Object detection topic, IoU block.

    Open practice

Full notes

Answers are saved on the server and tied to this browser until you sign in.