Detection in real time: the challenge and the headline
Scoring one detection: is IoU ≥ threshold?
Drag the red box, or use the sliders. Green dashed = ground truth.
Why it mattered
Two-stage detectors classify ~2,000 region proposals per image. YOLO reframes detection as one regression from pixels to box coordinates and class probabilities, trained end to end on a single loss, so the whole image (global context) informs every box. Price: about 7–10 mAP less than the best two-stage systems in 2016, mostly from localisation errors.
Walk an image through the network
Forward equations of the whole network
head h = φ( W₁ · vec(a⁽²⁴⁾) + b₁ ), vec(a⁽²⁴⁾) ∈ ℝ^(7·7·1024 = 50,176), h ∈ ℝ⁴⁰⁹⁶, dropout 0.5 on h
z = W₂ · h + b₂ ∈ ℝ¹⁴⁷⁰ (linear) → reshape to Y ∈ ℝ^(7 × 7 × 30)
cell (i, j), predictor b ∈ {1, 2} Y[i, j, 5b−5 … 5b−1] = (x̂, ŷ, √ŵ, √ĥ, Ĉ), Y[i, j, 10 … 29] = p̂(c | object), c = 1 … 20
The three ideas that made it work
Idea 1 · The cell that contains the centre is responsible
Click or tap anywhere to move the object's centre.
w = box_w ÷ 448, h = box_h ÷ 448 → targets (x, y, √w, √h)
Idea 2 · Predict √w and √h so a small box's error costs more
Idea 3 · Class-specific confidence, then greedy NMS
The defining rule: one pass, one S × S × (5B + C) tensor
The loss, written out
+ λ_coord Σᵢ Σⱼ 𝟙ᵢⱼ^obj [ (√wᵢ − √ŵᵢ)² + (√hᵢ − √ĥᵢ)² ]
+ Σᵢ Σⱼ 𝟙ᵢⱼ^obj (Cᵢ − Ĉᵢ)² + λ_noobj Σᵢ Σⱼ 𝟙ᵢⱼ^noobj (Cᵢ − Ĉᵢ)²
+ Σᵢ 𝟙ᵢ^obj Σ_c (pᵢ(c) − p̂ᵢ(c))², λ_coord = 5, λ_noobj = 0.5, target Cᵢ = IoU(pred, truth) when responsible, else 0
Derivation: the gradient through the head
Why the λ's: with 98 predictors and typically 1–3 responsible ones, the unweighted noobj terms would contribute about 30–90 × more gradient to Ĉ than the object terms and drive every confidence to zero early in training.
Reference implementation of the loss (PyTorch-style)
# pred, target: (N, S, S, 5B + C); target boxes stored in slot 0, class one-hot in 5B: def yolo_v1_loss(pred, target, S=7, B=2, C=20, l_coord=5.0, l_noobj=0.5): obj = target[..., 4] # 1 if an object centre is in the cell boxes = pred[..., :5*B].reshape(*pred.shape[:3], B, 5) # (x, y, sqrt w, sqrt h, conf) tbox = target[..., :4] # (x, y, sqrt w, sqrt h) ious = torch.stack([iou(cell_to_img(boxes[..., b, :4]), cell_to_img(tbox)) for b in range(B)], -1) best = ious.argmax(-1, keepdim=True) # responsible predictor resp = torch.zeros_like(ious).scatter_(-1, best, 1.0) * obj[..., None] noobj = 1.0 - resp b = boxes l_xy = ((b[..., :2] - tbox[..., None, :2]) ** 2).sum(-1) l_wh = ((b[..., 2:4] - tbox[..., None, 2:4]) ** 2).sum(-1) # already sqrt l_obj = (b[..., 4] - ious.detach()) ** 2 # target C = IoU l_no = b[..., 4] ** 2 l_cls = ((pred[..., 5*B:] - target[..., 5*B:]) ** 2).sum(-1) * obj loss = (l_coord * resp * (l_xy + l_wh) + resp * l_obj + l_noobj * noobj * l_no).sum() + l_cls.sum() return loss / pred.shape[0]
Shapes by hand and by eye
Worked example by hand: a 4 × 4 input and one 3 × 3 filter
This filter (+1 left column, −1 right column) responds to dark-to-light vertical edges. Redo each step on paper: nine products, one sum, then the leaky ReLU.
With b = 0, S = 1, P = 0. In YOLO the same operation runs with 64 to 1,024 filters and over all input channels at once: the sum gains a third index over Cin.
Where the parameters live
The arithmetic for that layer appears here.
Parameters
Compute (MACs)
Worked example: counting the parameters
Complexity in general: conv weights (K²·Cin + 1)·Cout, conv MACs Hout·Wout·Cout·K²·Cin, activation memory Σ H·W·C values per image (times 4 bytes in float32, times about 2 for training because the backward pass keeps them).
The family and its variants
| model | year | backbone | conv layers | params | benchmark | score | fps |
|---|
Pick a row.
Trade-offs, in one line each. Fast YOLO: 3.4 × the speed for −10.7 mAP. YOLO-VGG16: +3.0 mAP but less than half the speed. v2: batch norm, anchor boxes and a fully convolutional head raise recall and allow any input size. v3: three output scales fix small objects. v4: a better backbone and “bag of freebies” training.
The cost of going deep
t_epoch ≈ 3 · t_img · 16,551 (fwd + bwd)
t_run = 135 epochs · t_epoch
Reuse it instead of retraining it
Which recipe?
Training details from the paper
- pretraining
- first 20 convs + avg-pool + FC on ImageNet 1000-class, 224 × 224, ≈ 1 week, 88 % top-5 (single crop)
- detection
- + 4 conv + 2 FC (random init), input raised to 448 × 448
- schedule
- 135 epochs on VOC 2007+2012, batch 64, momentum 0.9, weight decay 0.0005
- learning rate
- warm up 10⁻³ → 10⁻², 10⁻² for 75 epochs, 10⁻³ for 30, 10⁻⁴ for 30
- regularise
- dropout 0.5 after FC1; random scaling and translation up to 20 %; exposure and saturation × up to 1.5 in HSV
History: accuracy and speed on VOC 2007
Two-stage (Fast R-CNN) vs YOLO · tap a card to flip
Where it is used today
SSD (2016)
One stage like YOLO, but predicts from several feature maps with default boxes: 74.3 mAP on VOC 2007 for SSD300 at real-time speed. Showed that multi-scale heads fix small objects.
YOLOv2 / v3 (2017–18)
Anchors, batch norm, fully convolutional heads, three scales and logistic class outputs. The v1 FC head and its ≈ 78 % of the parameters disappear.
RetinaNet (2017)
Focal loss down-weights easy background examples, a principled version of v1's λ_noobj = 0.5; it lets a one-stage detector match two-stage accuracy.
DETR (2020)
A transformer predicts a set of boxes with bipartite matching, so NMS is no longer needed. The “one object per cell” rule becomes “one object per query”.