Inside a detector: the R-CNN family and YOLO

Let's open up a detector. Follow a two-stage detector (Faster R-CNN) one step at a time as it makes proposal boxes from one cell of the feature map, thins them out and classifies them. Then click the grid of a one-stage detector (YOLO) to open the numbers each cell outputs. IoU, which scores a box, and NMS, which removes overlapping boxes, are on the object detection page.

Short answer. A detector works on the feature map made by the backbone of a classification network, and predicts "is there an object here?" and "how should the box be corrected?" together. A two-stage detector (the R-CNN family) picks candidate places first and looks at them again; a one-stage detector (YOLO) predicts everything for each grid cell in one pass.

Definitions

The text below explains with diagrams and the lab. These are the textbook definitions. They define the parts and approaches that make up a detector, not the details of one model.

backbone
The front part of a detector: a convolutional neural network that turns the image into a feature map. It is usually the front part of an image classification network with the last classification layer removed.
head
The part that takes the feature map (or features cut out of it) and gives the final answer. A detector can have several side by side, such as a classification head that predicts the class, a box head that refines the box, and a mask head that gives the mask.
anchor
A reference box set in advance at each position of the feature map, with different sizes and width-to-height ratios. The detector predicts an objectness score and box corrections for each anchor.
bounding box regression
Predicting, by regression, how far to move the center and how much to stretch the width and height (the corrections) so that a reference box (an anchor or a region proposal) fits the object. The move is set as a fraction of the reference box's width and height.
two-stage detector
A detector that first picks region proposals where an object may be (with a region proposal network, RPN), then cuts the features of each proposal to the same size (RoI pooling) to predict the class and refine the box again.
one-stage detector
A detector with no step that picks proposals. One pass of the network directly gives boxes and class scores at fixed positions (grid cells or anchors).

1. What does a detector predict?

The front of a detector is the same as the backbone of a network that classifies pictures. A backbone of stacked convolution layers (CNN architectures) turns the picture into a feature map. The feature map has fewer cells than the picture, and each cell looks at one patch of the picture. A classification network pools this map into one name; a detector predicts two things at each place.

The Faster R-CNN paper describes its region proposal network (RPN) as "a fully convolutional network that simultaneously predicts object bounds and objectness scores at each position" (source ①). The two kinds of detector differ in where they start from and how they narrow things down.

2. Two-stage: pick candidates, then look again

Faster R-CNN lets the RPN and the detection network share the full-image convolutional features, and merges the two into one network. In the paper's words, the RPN "tells the unified network where to look" (source ①). The flow looks like this.

picture backbone (convolutions) feature map region proposal network (RPN) proposal boxes RoI pooling crop each to one size class head what is it box head refine the box
The backbone runs only once. The RPN and RoI pooling share the feature map (green arrows). The RPN's proposals only say where to cut from the feature map; the two heads take the cropped, pooled features, name the class and refine the box.

The demo below follows this path, starting from one cell of the feature map.

Press "Next" to move one step. Gray lines are the feature map cells, the orange fill is the cell being looked at, purple dashed boxes are anchors, blue boxes are proposals, green (✓) marks proposals NMS keeps, gray dashed (✕) marks removed ones, and the black arrow is the path of a box's center. Picture coordinates are in feature map cells, and cells are written (column, row), counting from 0. The picture, anchors, scores, corrections and proposals are all small values made up for the demo. The rule for removing overlapping proposals is NMS on the object detection page; the idea of pooling cells is on the pooling page.

3. Building the formula: box correction

This is the math in step 5 of the demo. Anchor A1 has center (1.5, 2.5), width 2 and height 2, and the RPN's corrections are (dx, dy, dw, dh) = (0.2, −0.2, 1.1, 1.1).

  1. Move by grid units: cx′ = cx + dx. The simplest form. But the same dx = 0.2 is a fifth of the width for an anchor 1 wide, and a twentieth for an anchor 4 wide. The same number means different things for different anchors.
  2. Multiply by the anchor size: cx′ = cx + dx × w. Now dx = 0.2 means "0.2 widths to the right" for every anchor. For A1: 1.5 + 0.2 × 2 = 1.9, and vertically 2.5 + (−0.2) × 2 = 2.1.
  3. Scale width and height: w′ = w × dw. With addition (w + dw), a negative correction could make the width 0 or less. Multiplying by a factor above 0 keeps the width above 0. For A1: 2 × 1.1 = 2.2.

cx′ = cx + dx × w
cy′ = cy + dy × h
w′ = w × dw
h′ = h × dh

Turning it into corners, for example x1 = cx′ − w′ ÷ 2 = 1.9 − 1.1 = 0.8, gives proposal P1 = (0.8, 1.0) to (3.0, 3.2).

About this formula. This is a simple form this page chose for the demo. Real detectors' formulas have the same shape (move the center, stretch the width and height), but the details can differ. When you use a library, check the formula in its documentation. The correction values are made up too.

4. One-stage: every grid cell at once

YOLO frames detection "as a regression problem to spatially separated bounding boxes". A single network predicts boxes and class probabilities directly from the full picture in one evaluation (source ②). There is no separate step that picks candidates. Put an S × S grid on the picture; the cell that contains an object's center is responsible for that object. Each cell gives B boxes and C class probabilities, so the output has this size.

output size = S × S × (B × 5 + C)

This demo outputs 4 × 4 × (2 × 5 + 2) = 4 × 4 × 12 = 192 numbers. This page chose x, y as the place inside the cell (0 at the cell's top left, 1 at its bottom right) and w, h as fractions of the whole picture. The values 4, 2, 2 for S, B, C are also small values picked for the demo.

Click a grid cell (or focus the picture and use the arrow keys). The cell's 12 numbers open up in the row above, and its two boxes are drawn on the picture. In the row above, the two blue groups are (x, y, w, h, confidence) for boxes 1 and 2, and the orange group is the class probabilities (ball, bottle). On the picture the solid blue box is box 1 and the dashed one is box 2; thicker lines mean higher confidence. Blue dots are box centers and black dots are object centers. A green fill marks a cell containing an object's center, and a gray fill marks a cell with no object to handle. All numbers are made up; the cells with no object use a fixed-seed random generator.

Try this. ① The starting cell (1, 1) contains the ball's center (1.9, 2.1). Box 1 has confidence 0.9, and the class probability for ball is 0.92. ② Cell (2, 2) contains the bottle's center (4.2, 3.6) and is responsible for the bottle: box 1 has confidence 0.85, and bottle has 0.94. ③ Click cell (0, 1), which the ball covers but whose center it does not contain: the box confidences are 0.15 or less. Only one cell handles the ball. ④ Whichever cell you click, the blue dots (box centers) stay inside that cell, because x and y are places inside the cell.

5. The two kinds side by side

Two-stage (Faster R-CNN)One-stage (YOLO)
How many looks at the pictureThe backbone runs once; the RPN and the detection network share its features (①). Then the heads take another look at each proposalOne network gives boxes and class probabilities in one evaluation (②)
What gives the candidatesThe RPN gives objectness scores and boxes at each position (①)Nothing separate. The grid cells themselves are the places
How boxes are predictedAnchors are corrected into proposals, and the box head refines them once moreEach cell predicts its boxes directly; detection is treated as a regression problem (②)
Strengths and weaknesses the paper statesThe YOLO paper says that, compared with the best detectors of the time, YOLO makes more localization errors but is far less likely to predict false detections (②).

Which one to use for which job is in the "When to use what" table on the object detection page. Both kinds still have many boxes at the end and clean them up with NMS.

6. One more head for masks

Mask R-CNN adds to Faster R-CNN a branch that predicts an object mask, in parallel with the existing branch for bounding box recognition (source ③). Because each proposal now also gives the object's shape (a mask) besides its class and box, the result is instance segmentation, where each object is painted separately.

pooled features of each proposal class head → class box head → box mask head → shape
A dashed head next to the two heads from section 2. The backbone, feature map, RPN and RoI pooling stay the same; the mask head is attached in parallel.

Labeling every pixel, and how to score it, is on the image segmentation page.

Where it is used

Both kinds are detectors, so they are used in the same places as in "Where it is used" on the object detection page: self-driving cars and robots, factory inspection, drone and satellite photos, medical images, and checking labels. As the section 5 table shows, the two-stage kind has the heads take another look at each proposal, while in the one-stage kind one network gives boxes and class probabilities in one evaluation. The YOLO paper says YOLO makes more localization errors but is far less likely to predict false detections (②). Which one to use for which job is in the "When to use what" table.

FAQ

Does a two-stage detector run the picture through the backbone twice?

No. Faster R-CNN lets the region proposal network (RPN) share the full-image convolutional features with the detection network, and merges the two into one network. The backbone runs once; what gets a second look is each proposal's place, cut out of the feature map.

How is the objectness score different from a class probability?

The objectness score only asks "is there any object here?". A class probability asks "if so, what is it?". In a two-stage detector the RPN handles the first question and the class head the second. The one-stage demo on this page gives one confidence per box and C class probabilities per cell.

Does a one-stage detector need NMS too?

Yes. Each cell gives B boxes, so there are S × S × B boxes, and several overlapping boxes can stay around one object. Both kinds of detector output many boxes and clean them up with NMS.

Words

Word used hereTextbook term
backbonebackbone
feature mapfeature map
region proposal networkRPN (region proposal network)
proposal boxregion proposal
reference boxanchor, anchor box
objectness scoreobjectness score
box correctionbounding box regression
crop each to one sizeRoI pooling (RoI = region of interest)
headhead
confidenceconfidence score
one-stage, two-stageone-stage detector, two-stage detector
mask, painting each object separatelymask, instance segmentation

Sources

  1. Shaoqing Ren, Kaiming He, Ross Girshick, Jian Sun, Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks, arXiv:1506.01497, 2015. The RPN shares full-image convolutional features with the detection network, predicts object bounds and objectness scores at each position, the two are merged into one network, and the RPN "tells the unified network where to look".
  2. Joseph Redmon, Santosh Divvala, Ross Girshick, Ali Farhadi, You Only Look Once: Unified, Real-Time Object Detection, arXiv:1506.02640, 2015. Detection framed as regression, boxes and class probabilities in one evaluation, localization errors and false detections.
  3. He, Gkioxari, Dollár, Girshick, Mask R-CNN, arXiv:1703.06870, 2017. A mask branch added in parallel with the existing branch for bounding box recognition.

← Object detectionImage segmentation →