TL;DR
A region proposal network, or RPN, is a small neural network that looks at a convolutional feature map and proposes where objects might be. At every position it scores a fixed set of anchor boxes as "object" or "not object" and predicts a small correction to each one. Because it reuses the features that the detector computes anyway, the proposals cost almost nothing extra.
Builds on: Anchor Box, Convolutional Neural Network, Bounding-Box Regression, Non-Maximum Suppression · Leads to: Faster R-CNN, Mask R-CNN, and the two-stage family of detectors
Where it's used
The Faster R-CNN detector. The paper that introduced the RPN built a complete detector around it. The RPN proposes boxes, and a second network classifies them and refines them. On the PASCAL VOC 2007 test set, the RPN version ran at about 5 frames per second with a VGG-16 backbone on a Tesla K40 GPU, while the earlier pipeline that used Selective Search for proposals ran at about 0.5 frames per second on the same card.
Competition detectors in 2015. The authors report that Faster R-CNN and the RPN were the basis of several first-place entries in the ImageNet detection and localization tracks and in the COCO detection and segmentation tracks that year. Any system that must find many objects of unknown size in one image can use the same design.
Instance segmentation. Mask R-CNN keeps the RPN as its first stage and adds a branch that predicts a pixel mask inside each proposed box. A robot picking parts from a bin, or a camera counting cells under a microscope, needs both a box and an outline for each item, and the RPN supplies the boxes.
How it works
Trace one tiny instance. The backbone has produced a feature map, and we look at one location on it, which corresponds to pixel centre (88, 120) in the input image. We use three square anchors at that location, instead of the usual nine, to keep the example short. Their sides are 32, 64 and 128 pixels. The ground-truth object is a 60 by 60 square centred at (90, 118). Boxes are written as centre x, centre y, and side length.
-
Slide a small window. The RPN looks at a 3 by 3 patch of the feature map centred on this location. A learned layer turns that patch into a short vector of numbers, here 256 of them, with ReLU after it. One new idea: the same small network is applied at every location, so it does not matter where in the image the object sits. Check: the same weights are used at every position, so the output is the same for the same patch.
-
Read two outputs. Two small layers read the vector. The classification layer outputs two scores per anchor, one for object and one for background. The regression layer outputs four numbers per anchor, the offsets from Bounding-Box Regression. With three anchors that is 6 scores and 12 offsets. Check: the output size is set by the number of anchors per location, not by the image.
-
Measure each anchor's overlap with the object. Use Intersection over Union.
- The 32-pixel anchor covers [72, 104] by [104, 136]. It overlaps the object on a 32 by 32 square, 1024 pixels. The union is 1024 + 3600 − 1024 = 3600 pixels. IoU is 1024/3600 ≈ 0.28.
- The 64-pixel anchor covers [56, 120] by [88, 152]. The overlap is the full 60 by 60 object, 3600 pixels. The union is 4096 pixels. IoU is 3600/4096 ≈ 0.88.
- The 128-pixel anchor covers [24, 152] by [56, 184]. The overlap is again 3600 pixels, and the union is 16384. IoU is about 0.22. Check: only the middle anchor is close in size to the object, and only it overlaps well.
-
Label each anchor. The 64-pixel anchor has IoU 0.88, above the 0.7 cut-off, so it is positive. The 32-pixel anchor has IoU 0.28 and the 128-pixel anchor has IoU 0.22, both below the 0.3 cut-off, so they are negative. The paper also makes the single best-overlapping anchor for each object positive, so an object is never left without one. Check: one positive, two negatives, and no ignored anchors in this tiny case.
-
Compute the regression target for the positive anchor. The offsets from the anchor to the object are and . The size targets are for both width and height. Only positive anchors get these targets. Check: the object is slightly smaller than the anchor, so both size targets are negative.
-
Decode a prediction. Suppose the regression layer outputs for the 64-pixel anchor. The centre becomes . The side becomes pixels. Check: the predicted box is close to the true 60 pixel square and a little right of it.
-
Keep the best boxes. Suppose the classification layer gives this anchor an object score of 0.92. Every anchor across the image gets a score and a box. Thousands of them overlap, so Non-Maximum Suppression keeps the best and removes the rest. The output is a short ranked list of proposals, which the second stage classifies.
flowchart LR
A["Feature map from the backbone"] --> B["3 by 3 sliding window, 256-d, ReLU"]
B --> C["Classification: object or not, per anchor"]
B --> D["Regression: 4 offsets per anchor"]
C --> E["Decode anchors into boxes"]
D --> E
E --> F["Non-maximum suppression, keep top proposals"]
The key idea is in steps 1 and 2. The RPN does not search the image pixel by pixel. It reads one shared feature map, and at each location it answers two questions about a fixed set of templates: is one of them an object, and how should it move?
The math
An RPN is a fully convolutional network that takes a feature map of size and, at each of its locations, predicts for each of anchors a score that the anchor contains an object and four offsets that map the anchor onto a nearby box. The paper uses anchors, made of 3 scales and 3 aspect ratios, so the whole RPN produces scores and offsets.
Step 1 in symbols: the sliding window
Let be the feature map. A learned Convolution maps each patch of to a 256-dimensional vector, followed by a ReLU. Two convolutions then read that vector: one outputs values, the other . A convolution applies the same weights at every location, which is why the design is translation invariant. The paper notes that the effective Receptive Field on the input is 171 pixels for the ZF backbone and 228 for VGG-16, which is large compared with the 3 by 3 window on the feature map.
Step 2 in symbols: the outputs
The classification layer gives a two-way Softmax over object and background for each anchor, so is the object probability. The regression layer gives . Both are per anchor, and the number of outputs is fixed by , not by the image size.
Step 3 in symbols: the overlap
For an anchor and a ground-truth box ,
In the example, for the 64-pixel anchor, and , so the IoU is .
Step 4 in symbols: the labels
Let and . An anchor is positive, with , if it has the highest IoU with some ground-truth box or if its IoU with any ground-truth box is at least . It is negative, with , if its IoU with every ground-truth box is below . Anchors in between do not contribute to the loss.
Step 5 in symbols: the regression target
For a positive anchor with ground-truth box , the target is
Centres are measured in units of the anchor's size, and sizes on a log scale.
Step 6 in symbols: the loss
Over a mini-batch of sampled anchors, the paper minimises
The classification term is the log loss over object and not-object, which is cross-entropy for two classes. The regression term is the smooth L1 loss applied to each offset, which equals when and otherwise. It is switched on only for positive anchors by the factor . In the paper , for a feature map of about 2,400 locations, and , which makes the two terms roughly equal in size.
Sampling the mini-batch
Most anchors are negative, so training on all of them would teach the network to say "background" everywhere. The paper samples 256 anchors per image, using up to 128 positives and filling the rest with negatives. If an image has fewer than 128 positives, the batch is padded with negatives.
Worked examples
A feature map is and the RPN uses anchors per location.
- The map has locations.
- Each location has 9 anchors, so there are anchors in total.
- The classification layer therefore outputs scores per location and the regression layer outputs numbers per location.
- Over the whole map that is scores and offsets.
Interpretation: the number of candidate boxes grows with the size of the feature map, but the number of learned weights in the RPN does not, because the same small head is reused at every location.
A backbone reduces the image by a factor of 16, so the feature map is wide and high.
- The map has locations.
- With anchors per location there are anchors.
- Suppose the image has one object, and the best-overlapping anchor has IoU 0.85 with it. That anchor is positive, and so is any other anchor with IoU at least 0.7.
- Suppose 40 anchors are positive and 10,000 are negative. A mini-batch of 256 keeps at most 128 positives, so it has 40 positives and 216 negatives, a ratio of about 1 to 5.
- The positive anchors each get regression targets and contribute to . The negatives contribute only to the classification term.
- After training, the RPN keeps the top scoring boxes, runs Non-Maximum Suppression, and passes a short list, typically a few hundred boxes, to the second stage.
Interpretation: the RPN does most of its work on the cheap feature map, and the expensive second stage only sees a few hundred boxes instead of ten thousand.
A street image is pixels, which the backbone reduces by a factor of 16 to a feature map. A car is in the image with a true box . The location at feature cell that maps to pixel centre has a anchor, which is a plausible car shape, centred at .
- The anchor has , , and the car has , .
- The shared area is the car's area clipped to the anchor. The anchor's x-range is , the car's is , and the shared x-range is the whole anchor, width 128. The anchor's y-range is and the car's is , so the shared y-range is the whole anchor, height 64.
- Shared area is . Union is .
- IoU is . That lies in the ignore band, so this anchor is not used for training.
- Consider a second anchor, , at the same location. Its x-range is and y-range is . The car's top edge at lies just outside it, so the shared area is . The union is . The IoU is . This anchor is positive.
- For the positive anchor, and . The network must learn to predict these two numbers, which is an easier task than predicting and directly.
Try it: a 128 by 64 anchor is centred on the car's centre, and the car is 150 by 90. What is the regression target for this anchor? Answer: , which is positive because the car is wider than the anchor.
Common mistakes and tradeoffs
It is tempting to think the RPN finds object classes. In fact it only answers "object or not" for each anchor. Naming the class, such as car or dog, is the job of the second stage, which is why the RPN can be trained on one shared kind of label.
It is tempting to think the RPN output is the final detection. In fact its boxes are proposals. They overlap heavily, carry no class, and still need Non-Maximum Suppression and a classifier. The RPN buys speed by narrowing the search, not by making the final answer.
It is tempting to think the RPN needs a window of every size. In fact it uses one window size on one feature map, and the size variety comes from the anchors rather than from resizing the image as in an Image Pyramid.
It is tempting to think the anchors are learned. In fact they are fixed templates chosen in advance. The network learns only the scores and the offsets from each anchor. A badly chosen set of sizes and shapes cannot be repaired by training, so the anchor design matters.
It is tempting to think a shared RPN and detector can be trained in one simple pass. In fact the two networks need the same convolutional layers, and the paper trains them with a pragmatic four-step alternating scheme, which it describes as a way to learn shared features. Sharing saves computation at test time, but the training procedure is more involved than training either network alone.
When to use it: when you need a two-stage detector that spends most of its time on a shared feature map, and when a few hundred good candidate boxes are enough. When not to use it: when a single-stage detector that predicts boxes directly is fast enough and simpler to deploy, which is the trade made by the YOLO and SSD families. The main alternative is Selective Search, a CPU method that the paper measures at about 2 seconds per image, against about 0.2 seconds for the RPN on the same GPU.
History
Before 2015, region proposals were produced by bottom-up methods that ran on the CPU. Selective Search and EdgeBoxes were the common choices, and the Faster R-CNN paper reports that Selective Search took about 2 seconds per image on a CPU. Fast R-CNN, by Ross Girshick, cut the cost of classifying proposals by sharing convolutions across them, which left the proposal step as the main bottleneck.
Shaoqing Ren, Kaiming He, Ross Girshick and Jian Sun answered that bottleneck in 2015 by making the proposals a learned network on the same feature map. Its design caught on because it removed the separate proposal stage, so one GPU pass produced both boxes and classes, and because anchors gave a simple way to handle many object sizes with one feature map.
Recap
- A feature map is shared by the whole detector, so the proposal step reuses work already done.
- A small sliding window with two heads outputs an object score and four offsets for each of anchors at each location.
- Anchors are fixed templates. Each is labelled positive, negative or ignored by its IoU with the ground truth, and positives are regressed toward their object.
- The loss combines a log loss on object or not with a smooth L1 loss on the offsets of positive anchors, and training samples 256 anchors per image.
- After scoring, Non-Maximum Suppression keeps a short list of proposals for the second stage.
Read next: Faster R-CNN, which puts the RPN in front of a detector, and Mask R-CNN, which adds a mask head to the same design.
Check yourself
QuestionRecall: what does the RPN's classification layer predict for each anchor, and how many outputs does it produce per location with ?Show answer
It predicts a two-way score, object or not object, for each anchor. With anchors there are outputs per location.
QuestionComputation: a feature map is and . How many anchors are there in total?Show answer
There are locations. With 9 anchors each, the total is anchors.
QuestionWhy: why does the RPN sample 256 anchors per image instead of training on every anchor?Show answer
Most anchors are negative, because an image has far more background than objects. Training on all of them would bias the classifier toward background. Sampling 256 anchors, with at most 128 positives, keeps the two kinds balanced enough to learn from.
QuestionApplied: a colleague's detector outputs thousands of nearly identical boxes around each object and no class names. Which stage is missing, and what does each part contribute?Show answer
The colleague has a region proposal network without non-maximum suppression, and without a classification stage after it. The RPN proposes boxes, which overlap heavily. Non-maximum suppression keeps one box per object, and the second stage gives each box a class.
Sources
- Ren, He, Girshick and Sun, "Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks" (2015): https://arxiv.org/abs/1506.01497
- Girshick, "Fast R-CNN" (2015): https://arxiv.org/abs/1504.08083
- Object detection, Wikipedia: https://en.wikipedia.org/wiki/Object_detection