Object Detection: Faster R-CNN
Faster R-CNN (Region-based Convolutional Neural Network) is a two-stage object detection framework introduced by Ren et al. (2015) that unifies region proposal generation and object classification into a single end-to-end trainable network, achieving near real-time detection speeds by sharing convolutional features between both stages. It matters because it established the dominant paradigm for high-accuracy detection on benchmarks like COCO and PASCAL VOC, and its architectural ideas — anchor boxes, ROI pooling, and shared backbones — remain foundational to virtually every subsequent detector. The counterintuitive fact: the "fast" in Faster R-CNN comes not from a fundamentally different algorithm, but from replacing an external, CPU-bound proposal method (Selective Search) with a tiny neural network that shares 99% of its computation with the detector — a parasitic insight rather than a redesign.
The central breakthrough of Faster R-CNN is the Region Proposal Network (RPN): instead of spending hundreds of milliseconds generating region proposals with hand-crafted methods like Selective Search, you attach a lightweight sliding-window network directly on top of the shared convolutional feature map, so proposals become almost free. The RPN and the detection head look at exactly the same learned feature representation, meaning the backbone only runs once per image regardless of how many proposals are generated. This shared computation is the "aha moment" — detection and proposal generation are no longer sequential bottlenecks but parallel consumers of a single feature extraction pass.
How It Works
Faster R-CNN processes an image in two sequential stages that share a convolutional backbone.
Stage 1: Backbone + Region Proposal Network
A CNN backbone (e.g., ResNet-50 with FPN) maps an input image to a feature map , where , for stride . The RPN slides a small network over every spatial location of . At each location, anchors of different scales and aspect ratios are defined. For each anchor, the RPN predicts:
- An objectness score: probability that the anchor contains any foreground object
- 4 box deltas: offsets to refine the anchor into a proposal
Explanation: The RPN loss is a weighted sum of classification loss (objectness) over all sampled anchors and regression loss (box refinement) only over positive anchors, where balances their relative importance.
Symbols:
- : predicted objectness probability for anchor
- : ground-truth label (1 if IoU ≥ 0.7 with any GT box, 0 if IoU < 0.3)
- : predicted box delta vector
- : ground-truth box delta vector
- : binary cross-entropy
- : smooth-L1 loss
- : normalization factors (mini-batch size and anchor count)
Anchor Parameterization
Proposals are encoded as offsets relative to anchor boxes, not as absolute coordinates:
Explanation: Log-space encoding for width and height prevents negative predicted sizes and makes the regression target scale-invariant relative to the anchor dimensions.
Symbols:
- : predicted box center and dimensions
- : anchor box center and dimensions
- Log encoding ensures for any real-valued
Stage 2: ROI Pooling + Detection Head
After Non-Maximum Suppression (NMS), the top- proposals (typically ) are fed into ROI Pooling, which extracts a fixed-size feature grid from for each proposal regardless of its spatial size.
Explanation: Each ROI is divided into a fixed grid and max-pooling is applied within each bin, collapsing variable-size proposals into a uniform feature tensor.
Symbols:
- : a region proposal in feature-map coordinates
- : the set of feature map cells falling in the -th pooling bin
- : output pooling size (typically )
The pooled features pass through two FC layers, then two parallel heads:
Explanation: The final loss combines softmax classification across all classes with class-specific bounding-box regression, where regression is only penalized for non-background ROIs.
Symbols:
- : pooled and flattened ROI feature vector
- : predicted class probabilities (softmax over categories)
- : predicted box deltas for the predicted class
- , : ground-truth class and box delta
- : Iverson bracket suppressing regression loss on background ROIs
```python import torch import torchvision from torchvision.models.detection import fasterrcnn_resnet50_fpn from torchvision.models.detection.faster_rcnn import FastRCNNPredictor
# Load pretrained Faster R-CNN (ResNet-50 + FPN backbone) model = fasterrcnn_resnet50_fpn(weights="DEFAULT")
# Replace head for custom number of classes (e.g., 5 classes + background) num_classes = 6 in_features = model.roi_heads.box_predictor.cls_score.in_features model.roi_heads.box_predictor = FastRCNNPredictor(in_features, num_classes) model.train() optimizer = torch.optim.SGD(model.parameters(), lr=0.005, momentum=0.9)
# Forward pass returns a dict of losses when targets are provided images = [torch.rand(3, 600, 800)] targets = [{ "boxes": torch.tensor(50., 30., 200., 180.), "labels": torch.tensor([1]) }] loss_dict = model(images, targets)
# {'loss_classifier': ..., 'loss_box_reg': ...,
# 'loss_objectness': ..., 'loss_rpn_box_reg': ...} total_loss = sum(loss_dict.values()) total_loss.backward() optimizer.step() ```
Shaoqing Ren completed his PhD at the University of Science and Technology of China and conducted the Faster R-CNN work as a research intern at Microsoft Research Asia under Jian Sun. His specific contribution was the core insight that region proposals could be generated by a network that shares convolutional layers with the detector, making proposals effectively free — reducing proposal generation from ~2000ms (Selective Search) to ~10ms. Surprisingly, Ren later became a key engineer at Face++/Megvii, where the same anchoring principles underpinned one of the world's most deployed face-recognition systems.
Intuition
Imagine you're a security guard reviewing CCTV footage from a warehouse. The old approach (R-CNN) was: look at 2000 highlighted patches one by one, inspect each under a magnifying glass. The improved approach (Fast R-CNN) was: hold up one big magnifying glass over the whole scene, then crop regions from the already-magnified image. Faster R-CNN adds a third insight: instead of hiring an external analyst to tell you where to look, you train your own eyes to simultaneously scan for interesting regions while you're examining the scene — the region-finding is just a reflex, not a separate job. More concretely: think of the RPN as sticky notes attached to a whiteboard (the feature map). A tiny worker (3×3 conv) slides across the whiteboard and slaps down multiple sticky notes of different shapes at each position — "something tall here," "something wide there" — then prioritizes the most plausible ones. The main detector then inspects only those shortlisted spots, not every possible patch. The clever trick is that both the sticky-note worker and the main detector are reading off the same whiteboard, so you only had to draw the whiteboard once.