Search

Search papers and lessons

Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks

Shaoqing Ren, Kaiming He, Ross Girshick et al. · 2015

Faster R-CNN makes the region proposal step a small convolutional network that shares features with the detector, so finding candidate boxes costs about 10 ms per image.

On this page

  • Colour = topic mix
  • Calculus
  • Computer vision
  • Discrete math
  • Linear algebra
  • Neural network basics
  • Probability
  • ReadReady to readNot yet

TL;DR

Faster R-CNN is an object detector in which one neural network does everything. A small network called a region proposal network reads the same convolutional feature map that the detector uses, and proposes candidate boxes. A second network then labels each box and refines it. Before this paper, the proposal step ran on the CPU and took about 2 seconds per image, which was the bottleneck. Here it costs about 10 ms per image on a GPU, and the whole system runs at 5 frames per second with VGG-16.

Builds on: Fully Convolutional Network, Region Proposal Network, Convolutional Neural Network, Object Detection · Leads to: Mask R-CNN and the two-stage family of detectors

Why it mattered

The paper turned object detection into a single trainable network with one shared set of convolutional features. Its main result is that the proposal step, which had been a separate, slow, hand-designed stage, could be learned and made almost free.

Competitions. 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 the COCO detection and segmentation tracks in 2015. The paper does not claim the entries were won by Faster R-CNN alone. It says the RPN can "easily benefit from deeper and more expressive features", and the first-place systems used the 101-layer residual network, ResNet-101.

Products. The introduction notes that the detector was built into commercial systems, including one at Pinterest, with user engagement improvements reported. The same design became the first stage of Mask R-CNN, which adds a mask prediction branch to each proposed box.

Follow-on work. The authors list later uses of the framework in 3D object detection, part-based detection, instance segmentation and image captioning. Any system that must find many objects of unknown size in one image can use the same two-stage design.

The problem

Before this paper, a detector had two parts with very different costs. The first part proposed a few thousand boxes that might contain an object. The second part classified each box with a convolutional network. The second part had become fast. Fast R-CNN, the detector this paper builds on, runs one convolutional network on the whole image and then classifies every proposal from that shared feature map, instead of running a network once per proposal. The proposal step did not get faster. The two most common proposal methods, Selective Search and EdgeBoxes, ran on the CPU and took about 2 seconds and 0.2 seconds per image. Selective Search greedily merges regions from engineered features, and EdgeBoxes scores boxes from edges in a sliding window. Neither uses the learned features that the detector already computed.

So the proposal step was now the main cost of detection. The paper asks whether proposals can be computed by a deep convolutional network that uses the same features as the detector.

The question has a second part. Objects come in many sizes and shapes. Earlier systems dealt with this by resizing the image several times, which is an Image Pyramid, or by running filters of several sizes. Both are costly. The paper needs one scheme that handles many scales on one feature map.

How it works

The system has two stages that share one backbone of convolutional layers. The walkthrough below follows one image through the pipeline in order. The shapes are small on purpose.

Assume a backbone has already turned a 640×400640 \times 400 image into a feature map of 40×2540 \times 25 cells, with a stride of 16 pixels. Every cell therefore corresponds to a 16 by 16 patch of the image.

  1. Share the backbone. The image goes through the shared convolutional layers once. The output is a feature map. Both stages will read this same map, so the expensive part is paid only once. Check: the backbone runs once per image, not once per proposal.

  2. Slide a small network over the map. The RPN takes a 3×33 \times 3 window of the feature map at each location and turns it into a 256-number vector with ReLU. The same weights are used at every location. Check: an object in the top left and the same object in the bottom right get the same treatment.

  3. Place anchors at each location. At every cell the RPN has k=9k = 9 fixed reference boxes, called anchors: three sizes (areas of 128², 256² and 512² pixels) and three aspect ratios (1:1, 1:2 and 2:1). Each anchor is centred on the cell. The image therefore has 40⋅25⋅9=900040 \cdot 25 \cdot 9 = 9000 anchors. Check: the anchors are fixed in advance, and only the scores and offsets are learned.

  4. Score each anchor. A classification layer outputs two numbers per anchor, an object score and a background score, and softmax turns them into a probability that the anchor contains an object. Check: the classification output has 2k=182k = 18 values per location.

  5. Correct each anchor. A regression layer outputs four offsets per anchor. They shift the centre and rescale the width and height of the anchor, following the parameterisation in Bounding-Box Regression. Check: the regression output has 4k=364k = 36 values per location.

  6. Label anchors for training. During training, an anchor is positive if it has the highest IoU with some ground-truth box, or if its IoU with a ground-truth box is above 0.7. (IoU measures overlap: the shared area of two boxes divided by the area they cover together, from 0 to 1.) An anchor whose IoU is below 0.3 with every ground-truth box is negative. Others are ignored. Anchors that cross the image border are ignored during training as well. Check: an anchor that covers most of an object is positive, and an anchor in empty sky is negative.

  7. Keep the best proposals. After scoring, the RPN decodes the offsets into boxes, clips them to the image, and sorts them by object score. Non-Maximum Suppression with an IoU threshold of 0.7 removes boxes that overlap a higher-scoring box. About 2000 boxes remain for training, and the top 300 are passed on at test time. Check: 9000 candidate boxes become a few hundred proposals.

  8. Pool each proposal. The second stage, a Fast R-CNN detector, takes each proposal and crops its region from the shared feature map with RoI pooling. RoI pooling cuts that region into a fixed grid (for example 7×77 \times 7 cells) and keeps the largest value in each cell, so every proposal becomes a fixed-size feature whatever its shape. A later refinement, RoIAlign, avoids rounding the region's edges. The paper does not use a separate image crop per proposal. Check: the backbone is not run again for each proposal.

  9. Classify and refine. Fully connected layers give each pooled region a softmax over the object classes and a background class, and a second set of offsets refines the box. The output is a labelled box with a score for each detected object. Check: the second stage assigns the class names that the RPN never predicts.

flowchart LR
    I["Image"] --> B["Shared convolutional backbone"]
    B --> R["RPN: 3 by 3 window, 256-d, ReLU"]
    B --> P["RoI pooling"]
    R --> N["Score and offset 9 anchors per cell, then NMS"]
    N --> P
    P --> H["Fast R-CNN head: class and refined box"]
    H --> O["Detections"]

The key design is in steps 2 to 5. The RPN answers two questions at every location, for a fixed set of templates: is one of them an object, and how should it move? Because the answers come from the shared feature map, the marginal cost of proposals is small.

The math

The RPN loss, from the paper, is written for one image with anchors indexed by ii:

L({pi},{ti})=1Ncls∑iLcls(pi,pi∗)+λ1Nreg∑ipi∗ Lreg(ti,ti∗).L(\{p_i\}, \{t_i\}) = \frac{1}{N_{\mathrm{cls}}} \sum_i L_{\mathrm{cls}}(p_i, p^*_i) + \lambda \frac{1}{N_{\mathrm{reg}}} \sum_i p^*_i \, L_{\mathrm{reg}}(t_i, t^*_i).

In words: pip_i is the predicted object probability for anchor ii, and pi∗p^*_i is 1 for a positive anchor and 0 for a negative one. tit_i are the four predicted offsets and ti∗t^*_i are the target offsets of the matched ground-truth box.

Step 4 in symbols. LclsL_{\mathrm{cls}} is the log loss over two classes, object and not object, which is cross-entropy with two outcomes.

Step 5 in symbols. The offsets are

tx=x−xawa,ty=y−yaha,tw=ln⁡wwa,th=ln⁡hha,t_x = \frac{x - x_a}{w_a}, \quad t_y = \frac{y - y_a}{h_a}, \quad t_w = \ln\frac{w}{w_a}, \quad t_h = \ln\frac{h}{h_a},

with the same formulas for the ground-truth box, written with starred symbols. Here (xa,ya,wa,ha)(x_a, y_a, w_a, h_a) describe the anchor.

Step 6 in symbols. LregL_{\mathrm{reg}} is the smooth L1 loss applied to each offset. It equals 0.5x20.5x^2 when ∣x∣<1|x| \lt 1 and ∣x∣−0.5|x| - 0.5 otherwise. The factor pi∗p^*_i switches it on only for positive anchors. The paper uses Ncls=256N_{\mathrm{cls}} = 256 (the mini-batch size), Nreg≈2400N_{\mathrm{reg}} \approx 2400 (the number of anchor locations) and λ=10\lambda = 10, which makes the two terms roughly equal in size. The paper reports that results change little over a wide range of λ\lambda.

Sampling. The paper samples 256 anchors per image, with at most 128 positives. If fewer than 128 are positive, the batch is padded with negatives.

Sharing features. The paper uses a four-step alternating scheme. Step 1 trains the RPN from an ImageNet-pretrained backbone. Step 2 trains a separate Fast R-CNN detector on those proposals. Step 3 initialises the RPN from the detector, freezes the shared convolutions and fine-tunes only the RPN layers. Step 4 freezes the shared convolutions and fine-tunes the detector layers. After step 3 the two networks share their convolutions. The paper also describes approximate joint training, which trains the merged network in one pass and ignores the gradient through the proposal coordinates. It reports results close to alternating training, with 25 to 50 percent less training time.

Results

The main results are on PASCAL VOC 2007 and 2012, and on MS COCO. The score is mean average precision (mAP): for each class, the area under the precision-recall curve, averaged over classes, so higher is better. A detection counts as correct when its box has IoU of at least 0.5 with a true object, the PASCAL VOC standard. ZF is an earlier, smaller backbone network than VGG-16, used for comparison. All numbers below are the paper's own.

  • Proposal method comparison, ZF backbone. Fast R-CNN trained on Selective Search gets 58.7 mAP on PASCAL VOC 2007. With EdgeBoxes it gets 58.6. With shared RPN proposals, 300 per image, it gets 59.9.
  • Speed, VGG-16 on a K40 GPU. Selective Search with Fast R-CNN takes 1830 ms per image (0.5 fps). The RPN takes 10 ms for proposals and 198 ms for the whole system (5 fps). With ZF, the whole system takes 59 ms (17 fps).
  • Main VOC 2007 result with VGG-16. Shared RPN gets 69.9 mAP trained on VOC 2007 trainval, against 66.9 for Selective Search. Trained on VOC 2007 and 2012 trainval, it reaches 73.2. Adding COCO pretraining reaches 78.8.
  • VOC 2012 test. The shared RPN model reaches 70.4 mAP trained on the 07++12 union, against 68.4 for Selective Search.
  • COCO test-dev. Trained on the COCO training set, Faster R-CNN reaches 42.1 mAP at IoU 0.5 and 21.5 at the averaged IoU range. These are 2.8 and 2.2 points above the Fast R-CNN baseline under the same protocol.

What the numbers mean. The gain over Selective Search comes from two places. The proposals are better, and the proposal cost is nearly zero. The ablations show that the top-ranked RPN boxes are accurate. Using only the top 100 proposals gives 55.1 mAP, and removing the object score makes the top 100 much worse, at 44.6. This shows that the score, not just the box position, decides which proposals matter.

Limits. The paper's experiments use single-scale images with a shorter side of 600 pixels. The authors note that multi-scale features might help, but cost time. The 4-step training scheme is complicated, and the paper says the exact end-to-end solution needs an RoI warping layer that it leaves to later work. The results are on a few datasets with a few backbones, all from 2015. Later one-stage detectors such as YOLO trade some of this accuracy for speed and simplicity.

Reading guide

The paper's sections, in order, and the lessons each one needs.

Check yourself

QuestionRecall: what does the RPN predict for each anchor, and what is an anchor?Show answer

An anchor is a fixed reference box at a feature-map location, with a set size and aspect ratio. For each anchor the RPN predicts an object score, a two-way probability of object or background, and four offsets that move and rescale the anchor toward a nearby object.

QuestionComputation: a feature map is 40×2540 \times 25 with k=9k = 9 anchors per location. How many anchors are there, and how many classification and regression outputs does the RPN produce per location?Show answer

There are 40⋅25=100040 \cdot 25 = 1000 locations, and 1000⋅9=90001000 \cdot 9 = 9000 anchors in total. Per location, the classification layer outputs 2⋅9=182 \cdot 9 = 18 values and the regression layer outputs 4⋅9=364 \cdot 9 = 36 values.

QuestionWhy: why does the paper ignore anchors that cross the image border during training, and why does it sample 256 anchors per image instead of using all of them?Show answer

Border-crossing anchors have partial objects and produce large, hard-to-correct regression errors, and the paper reports that training does not converge if they are included. Sampling is needed because most anchors are negative. Using all of them would bias the classifier toward background, so the paper samples 256 with at most 128 positives.

QuestionApplied: a team wants to replace the RPN with a CPU method like Selective Search to make the detector simpler. Using the paper's numbers, what do they lose in speed, and what does the paper say about accuracy?Show answer

Selective Search takes about 2 seconds per image on a CPU, against about 10 ms for the RPN on a GPU. The whole VGG-16 system goes from about 5 fps to about 0.5 fps. On accuracy, the paper's Table II shows that shared RPN proposals with Fast R-CNN reach 59.9 mAP on VOC 2007 with ZF, against 58.7 for Selective Search. So the switch costs about 2 seconds per image in place of 10 ms, and in the paper's ZF setting it also loses about 1.2 mAP (58.7 against 59.9). The RPN is both faster and more accurate there, which is the paper's point.

Sources

Read the full paper

Shaoqing Ren, Kaiming He, Ross Girshick et al., 2015. arXiv:1506.01497