Back to paper library
arXiv 2017ICCV 2017SegmentationPaper notes

Mask R-CNN

Off by a Hair, Off by a Mile

Original paper

Mask R-CNN

Paper authors

Kaiming He · Georgia Gkioxari · Piotr Dollár · Ross Girshick

Publication
ICCV 2017Conference
First submitted to arXiv
arXiv:1703.06870
Cite this paper · BibTeX

The citation identifies the linked source version. It cites the original paper and its authors.

@misc{segmentationmaskrcnn2017,
  title = {{Mask R-CNN}},
  author = {Kaiming He and Georgia Gkioxari and Piotr Dollár and Ross Girshick},
  year = {2017},
  eprint = {1703.06870},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1703.06870}
}
On this page

Semantic segmentation labels every pixel with a class. If five people stand in a street scene, semantic segmentation paints all five the same color and tells you "these pixels are person," but it can't say which pixels belong to which person.

Instance segmentation asks for the latter. It has to find each object first and then draw an outline for each one, which amounts to doing detection and segmentation together.

In March 2017, Kaiming He, Georgia Gkioxari, Piotr Dollár and Ross Girshick of FAIR published Mask R-CNN. Kaiming He of ResNet, Ross Girshick of the R-CNN series and Piotr Dollár are all also authors of FPN. The paper's approach is very much in their style, adding one branch to Faster R-CNN and then fixing a detail that looks tiny.

Defining the Problem

Instance segmentation methods at the time fell roughly into two camps.

The first camp segments first, then recognizes. Methods like DeepMask generate a pile of candidate segments and then hand them to Fast R-CNN for classification; MNC uses a multi-stage cascade that generates segments from boxes and then classifies them. Segmentation comes before recognition, which is slow and not very accurate.

The second camp starts from semantic segmentation, first classifying every pixel with an FCN and then working out how to split pixels of the same class into separate individuals.

Its latest representative is FCIS, winner of the 2016 COCO segmentation challenge. It uses a set of position-sensitive output channels to handle classes, boxes and masks at once, and it is fast. But the authors point out that FCIS makes systematic errors and produces spurious edges where objects overlap, as we'll see in the experiments shortly.

Meanwhile, detection already had Faster R-CNN and semantic segmentation had FCN, both simple, easy to modify and fast to train. The authors set out to find an equally handy baseline for instance segmentation.

Solving the Problem

A Quick Review of Faster R-CNN

This site doesn't have a Faster R-CNN note yet, so let's cover it in a few sentences.

Faster R-CNN is a two-stage detector. The first stage is the Region Proposal Network (RPN), which slides over the backbone's feature map, judges for a set of preset anchor boxes whether there is an object there, and outputs candidate boxes. The second stage cuts a fixed-size feature, say 7×77\times7, out of the feature map for each candidate box (RoI, Region of Interest), then classifies it with fully connected layers and refines the box position.

The step that cuts the fixed-size feature out of the feature map is called RoIPool. However large or small the candidate box, the extracted features are the same size, so the classifier after it can take them.

Adding a Third Branch

Original Figure 1: the Mask R-CNN framework. On the left is a photo of people playing soccer, which goes through the backbone to give a feature map with a few candidate regions boxed on it; the features of each candidate region are extracted with RoIAlign and split into two paths, the upper one outputting the class and box, the lower one going through two convolution layers to output a mask; at the far right is the result with each player colored separately.
Original Figure 1, He et al., from PDF page 1, arXiv license, scholarly quotation.

Mask R-CNN's first stage is exactly the same as Faster R-CNN's. The second stage originally had two outputs, classification and box regression; now a mask branch is added, and the three are computed in parallel.

This "in parallel" is deliberate. In DeepMask, MNC, FCIS and similar methods, classification depends on the mask results; Mask R-CNN lets the mask and the classification each do their own job, just as Fast R-CNN once made classification and box regression parallel and simplified away R-CNN's multi-stage pipeline.

During training, the loss for each RoI is

L=Lcls+Lbox+LmaskL = L_{cls} + L_{box} + L_{mask}

The first two terms come straight from Fast R-CNN. The mask branch outputs KK masks of m×mm\times m for each RoI, where KK is the number of classes, one mask per class. Each pixel goes through a sigmoid and binary cross-entropy is computed; if the RoI's ground-truth class is kk, the loss is computed only on the kk-th mask, and the other K−1K-1 are ignored.

This differs from FCN's habit in semantic segmentation. FCN applies a KK-class softmax at each pixel, with the classes competing, so a pixel that is a person can't be a car. Each Mask R-CNN mask answers a single yes-or-no question, "If this object is kk, does this pixel belong to it?" Which class the object actually is gets left to the classification branch.

Masks Must Keep Their Spatial Layout

Class and box offsets are just a few numbers, and squeezing them into a vector with fully connected layers is fine. A mask is different; it is an image in its own right.

So the mask branch uses an FCN, convolving from the RoI features all the way to the m×mm\times m output and keeping the spatial structure at every layer. DeepMask and other methods predict masks with fully connected layers; the authors will show in the experiments that the convolutional version has fewer parameters and is more accurate.

The convolutional version, however, requires every position on the RoI features to correspond accurately to a position in the original image. RoIPool can't deliver that.

What's Wrong with RoIPool

Let's work through an example. The backbone's stride is 16, and a candidate box spans 10 to 165 pixels in xx on the original image, 155 pixels wide.

Converted to the feature map, the left and right edges are 10/16=0.62510/16=0.625 and 165/16=10.3125165/16=10.3125. RoIPool's first step rounds them to the integer grid points 1 and 10, which map back to roughly 16 to 176 pixels in the original image. The left edge moves 6 pixels to the right, and the right edge reaches 11 pixels further out.

The second step splits these 10 cells into 7 bins of 1.43 cells each, which again has to be rounded, so some bins get 1 cell and some get 2. Max pooling is then done inside each bin.

For classification this bit of offset is harmless; an object shifted by a few pixels is still the same object. But in the stride-16 C4 architecture, the mask branch outputs only 14×1414\times14, so for this box each mask pixel corresponds to about 11 pixels of the original image; the 6-pixel shift in the left edge puts the mask off by more than half a cell, and the 11 extra pixels on the right are a whole cell.

RoIAlign

The fix is direct. Don't round anywhere.

Original Figure 3: the dashed grid is the feature map, and the solid box is an RoI split into 2×2 bins, each with 4 sampling points; each sampling point's value is bilinearly interpolated from the four surrounding grid points, shown as one point connected to four grid points.
Original Figure 3, He et al., from PDF page 3, arXiv license, scholarly quotation.

RoIAlign takes the floating-point range from 0.6250.625 to 10.312510.3125 as is and divides it evenly into 7 bins, each 9.6875/7≈1.3849.6875/7\approx1.384 cells wide. Each bin gets 4 regularly placed sampling points (2×22\times2); the sampling points mostly fall between grid points, and each one's value is bilinearly interpolated from the four surrounding grid points. The average or maximum of the 4 points is the bin's output.

In the figure, the solid box doesn't line up with the dashed grid and the sampling points aren't on grid points, and that is exactly the point. The authors specifically note that the results are not very sensitive to where the sampling points go or how many there are, as long as nothing is quantized.

MNC's RoIWarp also uses bilinear interpolation, but it quantizes the RoI before that. As the experiments will show, RoIWarp scores about the same as RoIPool; the problem lies in alignment, not interpolation.

Two Kinds of Head

Original Figure 4: on the left is the head for the ResNet-C4 backbone; the RoI first goes through res5 to give 7×7×2048 features, one path average-pools and outputs the class and box, the other deconvolves to 14×14×256 and then outputs a 14×14×80 mask. On the right is the head for the FPN backbone, where class and box go through two 1024-d fully connected layers, and the mask branch goes from 14×14×256 through four convolution layers, deconvolves to 28×28×256, and finally outputs a 28×28×80 mask.
Original Figure 4, He et al., from PDF page 4, arXiv license, scholarly quotation.

The authors treat the backbone (features for the whole image) and the head (the work done for each RoI) separately, and experiment with two combinations.

ResNet-C4: the backbone uses ResNet only up to the fourth stage, and the fifth stage (res5) moves into the head, where it runs once for every RoI. This is how Faster R-CNN originally used ResNet, and it is computationally heavy.

FPN: res5 is already in the backbone, each RoI takes features from a pyramid level chosen by its size, and the head is much lighter. The mask branch starts from 14×14×25614\times14\times256 RoI features, applies four 3×33\times3 convolutions and one 2×22\times2, stride-2 deconvolution up to 28×2828\times28, and finally a 1×11\times1 convolution outputs one mask for each of the 80 (COCO) classes.

The authors say both mask branches are deliberately kept simple; more elaborate designs might do better, but they aren't the focus of this paper.

Training and Inference

The hyperparameters are mostly carried over from Faster R-CNN and FPN. An RoI with IoU of at least 0.5 against a ground-truth box is positive, and the mask loss is computed only on positives; the mask target is the intersection of the RoI and the ground-truth mask. Images are resized so the short side is 800, and training runs on 8 GPUs with 2 images each for 160k iterations, at a learning rate of 0.02 divided by 10 at iteration 120k.

Inference differs somewhat from training. In training, the three branches run in parallel on all RoIs; at inference, the box branch and NMS run first, and only the 100 highest-scoring boxes go through the mask branch. The mask for the predicted class is taken, its m×mm\times m probability map is resized to the box size and binarized at a threshold of 0.5.

Because only 100 boxes are processed, the mask branch adds about 20% to Faster R-CNN's running time. ResNet-101-FPN takes 195 ms per image on a Tesla M40, plus 15 ms of CPU time to resize the outputs back to the original image.

Discussion

All experiments are on COCO. The training set is 80k train images plus 35k val images (trainval35k), ablations are evaluated on the remaining 5k (minival), and the main results are submitted to test-dev. AP is averaged over IoU 0.5 to 0.95, AP50 and AP75 use fixed thresholds, and unless stated otherwise IoU is computed on masks.

Main Results

Table 1 of the paper, mask AP on COCO test-dev:

MethodBackboneAPAP50AP75
MNC (2015 winner)ResNet-101-C424.644.324.8
FCIS+OHEM (2016 winner)ResNet-101-C5-dilated29.249.5–
FCIS+++ +OHEMResNet-101-C5-dilated33.654.5–
Mask R-CNNResNet-101-C433.154.934.8
Mask R-CNNResNet-101-FPN35.758.037.8
Mask R-CNNResNeXt-101-FPN37.160.039.4

FCIS+++ used multi-scale training and testing, horizontal-flip testing and OHEM. Mask R-CNN adds none of these, and its ResNet-101-FPN version still wins by 2.1 points.

Original Figure 6: the top row is FCIS+++, the bottom row Mask R-CNN (ResNet-101-FPN), on six COCO images: four people holding umbrellas, a row of people sitting together, several people playing soccer, two giraffes, three people side by side, and two trains. Where objects overlap, FCIS blends colors together or draws edges that aren't there, while Mask R-CNN keeps every individual's outline cleanly apart.
Original Figure 6, He et al., from PDF page 6, arXiv license, scholarly quotation.

Look at the four people with umbrellas in the first column; where two neighbors meet, FCIS draws a boundary that belongs to neither of them. In the two trains at the far right, FCIS's colors run together at the junction. Mask R-CNN finds boxes first and then draws a mask inside each box, so neighboring individuals come out separate by construction.

Ablations

Table 2 of the paper breaks things down into five groups, all mask AP on minival:

  1. Backbone: ResNet-50-C4 30.3, ResNet-101-C4 32.7, ResNet-50-FPN 33.6, ResNet-101-FPN 35.4, ResNeXt-101-FPN 36.7. Depth, FPN and ResNeXt all help.
  2. Softmax or sigmoid: with the same ResNet-50-C4, a per-pixel softmax gets only 24.8, while an independent sigmoid per class gets 30.3, a 5.5-point gap. The authors' reading is that the classification branch has already judged the object as a whole, so the mask only needs to separate foreground from background, without competing against other classes again.
  3. One mask per class or one shared: a class-agnostic mask that outputs just one map gets AP 29.7, barely different from 30.3 with one per class. This further shows that classification and segmentation are already decoupled in Mask R-CNN.
  4. FCN or MLP: on ResNet-50-FPN, both fully connected mask branches get 31.5, and the four-layer convolutional FCN gets 33.6.
  5. RoIAlign: see the next section.

What Alignment Is Actually Worth

Comparison on ResNet-50-C4 (stride 16):

RoI opAlignBilinearAPAP50AP75
RoIPool26.948.826.4
RoIWarp✓27.249.227.1
RoIAlign✓✓30.351.231.5

RoIAlign adds 3.4 points, most of it at AP75, the threshold that demands tightly fitting masks. RoIWarp adds interpolation without alignment and barely improves.

The larger the stride, the worse the misalignment. Switching to ResNet-50-C5 with stride 32, RoIPool drops to 23.6 while RoIAlign gets 30.9, a 7.3-point gap, and the gap at AP75 is 10.5. Stride 32 with RoIAlign at 30.9 even beats stride 16 with RoIAlign at 30.3. The authors consider RoIAlign to have largely solved the long-standing problem that "large-stride features can't be used for precise localization."

Even on FPN, whose features are already fine, RoIAlign still adds 1.5 points of mask AP.

Detection Got Better Too

Drop the mask output and look only at boxes (Table 3 of the paper, box AP on test-dev), and Faster R-CNN with FPN on the same backbone gets 36.2, while Mask R-CNN gets 38.2.

The authors break these 2 points down. Switching to RoIAlign gives 37.3, a gain of 1.1; the remaining 0.9 comes from multi-task training, where learning masks during training makes the boxes more accurate too. Switching to ResNeXt-101 adds another 1.6, reaching 39.8.

Switching to Human Keypoints

Finally, the authors show how general the framework is. Each human keypoint (left shoulder, right elbow and so on) is treated as an m×mm\times m mask with a single pixel set to 1; the keypoint branch outputs 56×5656\times56 and applies a softmax over the m2m^2 positions.

The ResNet-50-FPN version reaches a keypoint AP of 63.1 on COCO test-dev, above the 61.8 of CMU-Pose+++, the 2016 winner, at 5 images per second. Even though FPN's finest level has a stride of only 4 pixels, RoIAlign still beats RoIPool by 4.4 points on keypoints (Table 6 of the paper), because keypoints need precise positions even more than masks do.

The appendix also runs instance segmentation on Cityscapes. With fine annotations only, ResNet-50-FPN reaches 26.2 AP on the test set, and the biggest gains are on person and car, the classes with the most same-class overlap; fine-tuning from a COCO-pretrained model raises it to 32.0 AP.

Conclusion

Mask R-CNN's changes add up to just three things, an extra FCN mask branch, one sigmoid mask per class, and RoIAlign to remove the two roundings. It beats FCIS+++, which piled on several extra tricks, picks up 2 points of box AP along the way, and with a different output can even do human pose.

Of the three, RoIAlign has had the longest influence. On the same ResNet-50-C4, swapping out RoIPool lifts mask AP from 26.9 to 30.3, and the two-stage models in Detectron2 and MMDetection all use it by default today.

When you write your own RoI feature extraction, first check whether the coordinates get quietly rounded at some step. That is a better use of your time than tuning any hyperparameter.