Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Let the network propose its own regions
After Fast R-CNN, the slowest step was the hand-made region proposals. Faster R-CNN lets the network make them. A small extra head looks at each position of the shared feature map and asks, for a handful of preset boxes (“anchors”) of different sizes and shapes centred there: is there an object in this box, and how should the box be adjusted? The best few hundred become proposals for the usual classifier. Proposals became nearly free, and the whole detector became one trainable network.
Faster R-CNN, usually with a feature pyramid (E9-5), is a standard high-accuracy detector for aerial, satellite and driving imagery, and its anchor idea shaped nearly all later detectors.
- 1. Faster R-CNN's proposals come from…
- 2. Anchors are…
- 3. The RPN shares…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Anchors: areas 128², 256² and 512², with aspect ratios h/w of 0.5, 1 and 2, give 9 per position. An anchor of area s² and ratio r has w = s/√r and h = s·√r; for 128 and r = 0.5 that is 181 × 91.
- Count: a 600 × 1000 image with stride 16 gives 37 × 62 positions × 9 = 20,646 anchors. Anchors crossing the image boundary are ignored in training.
- Labels:
- positive if IoU ≥ 0.7 with some object, or if it is an object’s highest-IoU anchor;
- negative if IoU < 0.3 with every object;
- otherwise ignored.
- Mini-batch: 256 anchors from one image, up to half positive.
- RPN head: a 3 × 3 convolution (512 channels for VGG-16), then 1 × 1 convolutions to 2k = 18 objectness scores and 4k = 36 offsets per position.
- At test time: keep the top-scoring proposals, apply NMS at IoU 0.7, and pass the top 300 to the detector head.
- 1. Anchors per position in Faster R-CNN:
- 2. RPN positive IoU threshold:
- 3. Proposals passed to the detector at test time:
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Faster R-CNN in code
Exercise 1.
- The 9 anchors had the right areas and ratios.
- A 600 × 1000 image gave 20,646 anchors.
- The RPN labels marked each object’s best anchor positive, even below IoU 0.7.
- The RPN head had about 2.4 M parameters.
Practice.
- torchvision’s
fasterrcnn_resnet50_fpn_v2and related models are trained on COCO and fine-tune well. - To fine-tune on new classes, replace the box predictor:
model.roi_heads.box_predictor = FastRCNNPredictor(in_features, n_classes). - Training:
- the original paper alternated RPN and detector training in four steps;
- modern code trains everything jointly, summing the RPN losses (objectness + box) and the detector losses.
- Anchor sizes and ratios should match the data. For small objects, add small anchors or use FPN.
model = torchvision.models.detection.fasterrcnn_resnet50_fpn_v2(weights="DEFAULT")
model.roi_heads.box_predictor = FastRCNNPredictor(model.roi_heads.box_predictor.cls_score.in_features, n_classes)- 1. To fine-tune torchvision's Faster R-CNN on new classes, replace…
- 2. Modern training of Faster R-CNN is…
- 3. RPN mini-batch size:
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Translation-invariant anchors. The same 9 anchors and the same small head are applied at every position, so the RPN has few parameters (its output layers have 512·6·9 ≈ 2.8·10⁴, against 6.1·10⁶ for MultiBox’s fully connected output layer) and handles objects anywhere. Anchors of several scales and ratios at one position are a “pyramid of anchors”. This is cheaper than a pyramid of images or of filters, because one feature map at one scale serves all sizes.
Why anchors work, and where they don’t.
- Each anchor’s regressor learns only corrections near its own shape.
- An object needs some anchor with reasonable overlap.
- Objects much smaller than the smallest anchor, or of extreme aspect ratios, get no anchor above the positive threshold. The “best anchor” rule then forces one positive, but with a hard regression target.
Shared features and joint training.
- The RPN and the Fast R-CNN head share the backbone.
- Alternating training was used originally because back-propagating through RoI pooling with respect to the proposal coordinates is not straightforward.
- Approximate joint training ignores that gradient and works about as well, faster.
- 1. A 'pyramid of anchors' is cheaper than…
- 2. Objects much smaller than every anchor…
- 3. Approximate joint training ignores…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Do the anchors cover the objects?
History.
- S. Ren, K. He, R. Girshick and J. Sun (2015) introduced Faster R-CNN at NeurIPS. With VGG-16 it reached 73.2% mAP on VOC 2007 (trained on VOC 07+12) at 5 frames per second.
- D. Erhan et al. (2014) proposed MultiBox class-agnostic proposals.
- Faster R-CNN with ResNet won the COCO 2015 detection challenge (E8-8). FPN (E9-5) and Mask R-CNN (E10) extended it.
Exercise 2. This is a calculation, not a training run. It generates 400 objects with sizes log-uniform from 16 to 400 pixels and aspect ratios from 1:3 to 3:1 in a 512 × 512 image. For each anchor set at stride 16, it computes the share of objects whose best anchor reaches IoU 0.5 or 0.7:
| anchor set | anchors | covered at 0.5 | at 0.7 | small (under 64²) at 0.5 | large (128² and up) at 0.5 |
|---|---|---|---|---|---|
| 1 scale × 1 ratio | 1,024 | 0.152 | 0.060 | 0.000 | 0.401 |
| 3 scales × 1 ratio | 3,072 | 0.347 | 0.098 | 0.000 | 0.691 |
| 1 scale × 3 ratios | 3,072 | 0.240 | 0.150 | 0.000 | 0.632 |
| 3 scales × 3 ratios (Faster R-CNN) | 9,216 | 0.505 | 0.255 | 0.000 | 1.000 |
| 5 scales × 3 ratios (adds 32, 64) | 15,360 | 0.818 | 0.328 | 0.544 | 1.000 |
- The default anchors cover every large object but cannot reach IoU 0.5 with any object smaller than 64 × 64. These were designed for PASCAL VOC’s mostly large objects.
- Adding 32 and 64 px anchors brings half of the small objects within reach.
- IoU 0.7 is rarely reached (a third of objects at best), because anchors sit on a 16-pixel grid with only three shapes. That is why the RPN also labels each object’s best anchor positive, and why later detectors added feature pyramids (E9-5) and anchor-free heads (E9-11).
Faster R-CNN in AI, robotics and aerospace
- Aerial and satellite detection: Faster R-CNN with FPN is a common baseline for vehicles, ships, aircraft and buildings. Anchors are retuned for small and elongated objects, often rotated boxes for ships.
- Robotics: two-stage detectors provide accurate object locations for grasping when real-time speed is not critical.
- Design rule: match anchor sizes and ratios to the dataset’s object statistics, or use anchor-free methods.
References
- S. Ren, K. He, R. Girshick, J. Sun (2015). “Faster R-CNN: towards real-time object detection with region proposal networks.” Advances in NeurIPS.
- D. Erhan, C. Szegedy, A. Toshev, D. Anguelov (2014). “Scalable object detection using deep neural networks.” Proc. CVPR.
- T.-Y. Lin et al. (2017). “Feature pyramid networks for object detection.” Proc. CVPR.