Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Look at the image once
R-CNN ran its CNN 2,000 times per image, once for every proposal, mostly recomputing the same features. Fast R-CNN runs the CNN once on the whole image. For each proposal it cuts the matching patch out of the feature map and squeezes it into a fixed-size grid (RoI pooling). A small network then says what the object is and how to adjust the box, both learned together. Training and testing became dramatically faster, and accuracy improved too.
Sharing computation across regions is the key to every practical detector. RoI pooling and its successor RoIAlign are still used in two-stage detectors for driving and aerial imagery.
- 1. Fast R-CNN runs the CNN…
- 2. RoI pooling turns each proposal into…
- 3. Classes and boxes are learned…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Projecting a proposal onto a stride-16 feature map: divide by 16 and round. So (40, 70, 200, 330) → (2, 4, 12, 21) feature cells.
- RoI max pooling: split the projected RoI into 7 × 7 bins of size w/7 × h/7, and take the maximum in each. On the 4 × 4 map with entries 0…15, a 2 × 2 pooling of the whole map gives [[5, 7], [13, 15]].
- Multi-task loss: L = −log p_u + λ[u ≥ 1]·Σ smooth_L1(t_i − v_i), with λ = 1.
- smooth_L1(d) = 0.5d² if |d| < 1, else |d| − 0.5;
- so smooth_L1(0.5) = 0.125 and smooth_L1(2) = 1.5.
- Its gradient is d for small errors and ±1 for large ones.
- Sampling: mini-batches of 2 images × 64 RoIs, 25% foreground (IoU ≥ 0.5) and the rest background (IoU in [0.1, 0.5)).
- Saving: one CNN pass replaces 2,000, so convolutional work falls 2,000-fold. The per-RoI head still runs 2,000 times.
- 1. smooth_L1(3):
- 2. Foreground share of sampled RoIs:
- 3. The box loss counts only for…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Fast R-CNN in code
Exercise 1.
- RoI max pooling produced the expected 2 × 2 maxima.
- Smooth L1 gave 0.125, 1.5 and 2.5 for errors 0.5, 2 and −3.
- The multi-task loss ignored box errors on background RoIs.
Practice.
- torchvision provides
torchvision.ops.roi_poolandroi_align;fasterrcnn_resnet50_fpncontains a Fast R-CNN head. - Training:
- end to end with SGD;
- the conv layers from conv3_1 up are fine-tuned, the first layers frozen;
- horizontal flips and multi-scale training are optional.
- Truncated SVD of the large fully connected layers sped up detection by about 30% with little accuracy loss, because the head runs once per RoI.
- Proposals still came from selective search, about 2 s per image on a CPU, now the slowest step.
feats = backbone(image) # once per image
pooled = torchvision.ops.roi_pool(feats, rois, (7, 7), spatial_scale=1 / 16)
cls_logits, box_deltas = head(pooled.flatten(1)) # per RoI
loss = F.cross_entropy(cls_logits, labels) + F.smooth_l1_loss(box_deltas[fg], targets[fg])- 1. torchvision's RoI pooling op:
- 2. Truncated SVD sped up…
- 3. After Fast R-CNN, the slowest step was…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why sharing works. Convolutional features are translation-equivariant, so the features of a region are, up to borders, a crop of the whole image’s features. SPPnet (He et al., 2014) first exploited this with spatial pyramid pooling, but it could not fine-tune layers below the pooling. Fast R-CNN made the RoI pooling layer differentiable: gradients flow back to the argmax positions. That allowed end-to-end training of the whole network. Sampling many RoIs from few images made back-propagation efficient, because RoIs from the same image share computation.
Smooth L1. Squared loss has gradient proportional to the error, so a few badly mislabelled or ambiguous boxes can dominate training. The paper noted that L2 needed careful learning-rate tuning to avoid exploding gradients. Smooth L1 is quadratic near zero, for precise convergence, and linear for large errors, so the gradient is capped at 1.
Quantisation. Rounding the RoI to the feature grid and the bins to whole cells misaligns pooled features with the true box by up to half a stride (8 pixels at stride 16). This hardly matters for large objects but matters for small ones and for pixel-accurate tasks. RoIAlign (Mask R-CNN, 2017) uses bilinear sampling without rounding.
- 1. SPPnet could not…
- 2. Smooth L1's gradient for large errors is…
- 3. RoIAlign replaces rounding with…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.What do rounding and the robust loss do?
History.
- K. He, X. Zhang, S. Ren and J. Sun (2014) introduced SPPnet.
- R. Girshick (2015) introduced Fast R-CNN at ICCV. With VGG-16 it trained 9 times and tested 213 times faster than R-CNN, and reached 66.9% mAP on VOC 2007 (70.0% with VOC 2012 training data added).
- K. He, G. Gkioxari, P. Dollár and R. Girshick (2017) introduced RoIAlign in Mask R-CNN.
Exercise 2. Two calculations:
- For 4,000 random boxes of each size class, the IoU between the true box and the region that RoI pooling reads after rounding to a stride-16 grid.
- A linear regression of box offsets where 5% of the targets are gross outliers (off by 5 to 10), fitted by gradient descent with squared loss and with smooth L1, measuring the error of the fitted weights.
| RoI size | mean IoU with the region read |
|---|---|
| small (16–48 px) | 0.599 |
| medium (48–128 px) | 0.827 |
| large (128–384 px) | 0.935 |
| loss | error of fitted weights (5% outliers) |
|---|---|
| squared (L2) | 0.197 |
| smooth L1 | 0.027 |
- Rounding hurts small objects badly. An 8-pixel misalignment is a large fraction of a 30-pixel box, and the paper’s own detector also struggled with small objects. This motivated RoIAlign and feature pyramids (E9-5).
- Smooth L1 resisted the outliers, recovering the true weights about 7 times more accurately. A second seed gave 0.126 against 0.013, a tenfold gap.
- Neither result measures detection mAP. The paper reports that smooth L1 trained as well as L2 without needing careful tuning.
Fast R-CNN in AI, robotics and aerospace
- Two-stage detectors: the Fast R-CNN head, with RoIAlign, is part of Faster and Mask R-CNN models used for vehicle, building and aircraft detection in aerial imagery.
- Robust losses: smooth L1 and the related Huber loss are standard for regression from noisy labels, such as pose estimation and odometry.
- Small objects: quantisation effects are a key concern when detecting small, distant objects from drones.
References
- R. Girshick (2015). “Fast R-CNN.” Proc. ICCV.
- K. He, X. Zhang, S. Ren, J. Sun (2014). “Spatial pyramid pooling in deep convolutional networks for visual recognition.” Proc. ECCV.
- K. He, G. Gkioxari, P. Dollár, R. Girshick (2017). “Mask R-CNN.” Proc. ICCV.