Search

Search papers and lessons

Mask R-CNN

Kaiming He, Georgia Gkioxari, Piotr Dollár et al. · 2017

He, Gkioxari, Dollár and Girshick (2017) add a mask branch to Faster R-CNN and align region features exactly, which gives top instance segmentation results while running at about 5 fps.

On this page

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

TL;DR

Mask R-CNN finds every object in an image, draws a box around it, and predicts a pixel mask for it, all in one network. It extends Faster R-CNN with a third output that predicts the mask, and it fixes a rounding step in the feature extraction so that the mask lines up with the object. It sets top single-model results on the COCO benchmark and runs at about 5 frames per second.

Builds on: Faster R-CNN, Region Proposal Network, Fully Convolutional Network, RoIAlign · Leads to: Object Detection and Image Segmentation systems that predict per-object masks, and pose estimation with one mask per keypoint.

Why it mattered

The paper's central claim is that instance segmentation can be built from simple parts. Its code was released as Detectron, and the authors describe the method as a solid baseline for later work on instance-level recognition. The same framework was also extended to human pose estimation, where each keypoint is treated as a one-pixel mask.

The numbers that made it stick: on the COCO instance segmentation benchmark, the ResNeXt-101 version reaches 37.1 mask AP on test-dev, ahead of the 2015 and 2016 challenge winners. On COCO object detection, a ResNet-101 model with a feature pyramid reaches 38.2 box AP, and the paper's ResNeXt model reaches 39.8.

The problem

Object detection says where each object is with a box. Semantic segmentation says which class each pixel belongs to, but not which object it belongs to. Instance segmentation needs both: a separate mask for each object, even when two objects are the same class and overlap.

The obvious route was to extend Faster R-CNN, which already detects objects in two stages and outputs a class and a box for each candidate. But the authors found a specific obstacle. The standard operation for pulling a region's features out of a feature map, RoIPool, first rounds the region's coordinates to the grid of the feature map, then rounds the bins inside it. Rounding is harmless for a class label, which tolerates small shifts. It is fatal for a mask, which must match the object's edge pixel by pixel. The paper's answer is RoIAlign, a layer that removes the rounding.

A second design question is how to handle classes. Earlier mask systems let the masks of different classes compete, using a softmax over classes at each pixel. The authors decouple the two tasks: the box branch decides the class, and the mask branch predicts one binary mask per class with no competition between classes.

How it works

Trace one image through the system. The steps below follow the order the pipeline runs at test time.

  1. Input and backbone. The image is resized so its shorter side is 800 pixels, then passed through a convolutional backbone such as ResNet-101 with a feature pyramid. The output is a set of feature maps, one per scale.
  2. Proposals. The region proposal network scans the feature map and proposes candidate boxes, each with an objectness score. Non-maximum suppression removes near-duplicate boxes. The paper uses 1000 proposals with the feature pyramid.
  3. RoIAlign. For each proposal, the layer takes the region of the feature map under the box, divides it into a small grid, samples four points in each cell with bilinear interpolation, and takes the average. No coordinate is rounded. The output is a small fixed-size feature map, for example 7 by 7 for the box head.
  4. Box head. The box branch turns the region's features into a class score and a corrected box, as in Faster R-CNN.
  5. Mask head. In parallel, a small fully convolutional network turns the same region features into a mask for every class. For a region of class kk, only mask kk is used.
  6. Loss. During training the three losses are added: classification, box, and mask. The mask loss is computed only on positive regions, and only on the mask of the true class.
  7. Output. At test time the mask branch runs only on the 100 highest-scoring boxes. The predicted mask for the chosen class is resized to the box and set to foreground where its value is at least 0.5.

Check: after step 3 every region has the same shape, so the rest of the network can treat every object alike. After step 5 every region has one mask per class, and the box branch picks which one to show.

The diagram shows the path from image to output.

flowchart LR
  I["Image"] --> B["Backbone: ResNet with feature pyramid"]
  B --> R["Region proposal network"]
  R --> A["RoIAlign: boxes to fixed grids, no rounding"]
  B --> A
  A --> C["Box head: class and box"]
  A --> M["Mask head: one mask per class"]
  C --> S["Pick mask of the predicted class"]
  M --> S
  S --> O["Boxes and masks"]

Worked examples

The four worked examples below use the paper's formulas on small numbers. RoIPool and RoIAlign are compared on a row of feature values, then the mask loss is computed on one pixel, then the speed figures are checked.

Example
1. RoIPool and RoIAlign on one row of features

The feature map has one row with values at integer positions 0,1,2,30, 1, 2, 3: f(0)=10f(0) = 10, f(1)=20f(1) = 20, f(2)=30f(2) = 30, f(3)=40f(3) = 40. The region of interest spans positions 0.40.4 to 2.22.2, and we split it into two bins.

  1. RoIPool first rounds the region to whole positions: 0.40.4 becomes 00 and 2.22.2 becomes 22. The region is now [0,2][0, 2], width 2.
  2. RoIPool splits it into bins [0,1][0, 1] and [1,2][1, 2], and takes the maximum value in each bin. Bin one has max⁡(f(0),f(1))=20\max(f(0), f(1)) = 20. Bin two has max⁡(f(1),f(2))=30\max(f(1), f(2)) = 30. The output is (20,30)(20, 30).
  3. RoIAlign keeps the true region. Bin one is [0.4,1.3][0.4, 1.3] with centre 0.850.85. Bin two is [1.3,2.2][1.3, 2.2] with centre 1.751.75.
  4. Bilinear interpolation between two neighbouring integer positions pp and p+1p + 1 gives f(x)=(1−a)f(p)+af(p+1)f(x) = (1 - a) f(p) + a f(p + 1), where a=x−pa = x - p. At 0.850.85, a=0.85a = 0.85, so f(0.85)=0.15⋅10+0.85⋅20=18.5f(0.85) = 0.15 \cdot 10 + 0.85 \cdot 20 = 18.5.
  5. At 1.751.75, a=0.75a = 0.75, so f(1.75)=0.25⋅20+0.75⋅30=27.5f(1.75) = 0.25 \cdot 20 + 0.75 \cdot 30 = 27.5. The output is (18.5,27.5)(18.5, 27.5).

Interpretation: RoIPool moved the region by up to half a cell, and the output values come from the wrong place. RoIAlign returns values that match the feature map at the true location. On a mask, that shift shows up as an edge that is a cell off.

Example
2. The mask loss on one pixel

Each mask pixel gets a sigmoid of a raw score zz, which gives a probability s=1/(1+e−z)s = 1 / (1 + e^{-z}). The binary cross-entropy loss for a pixel with label yy (1 for foreground, 0 for background) is −[yln⁡s+(1−y)ln⁡(1−s)]-[y \ln s + (1 - y) \ln (1 - s)].

  1. Take z=0z = 0, label y=1y = 1. Then s=1/(1+e0)=1/2=0.5s = 1 / (1 + e^0) = 1 / 2 = 0.5.
  2. The loss is −ln⁡0.5=ln⁡2≈0.693-\ln 0.5 = \ln 2 \approx 0.693.
  3. Now take z=2z = 2, label y=1y = 1. Then s=1/(1+e−2)≈1/1.1353≈0.8808s = 1 / (1 + e^{-2}) \approx 1 / 1.1353 \approx 0.8808.
  4. The loss is −ln⁡0.8808≈0.127-\ln 0.8808 \approx 0.127. The loss is smaller because the score agrees with the label.
  5. Keep z=2z = 2 but change the label to y=0y = 0. The loss is −ln⁡(1−0.8808)=−ln⁡0.1192≈2.127-\ln(1 - 0.8808) = -\ln 0.1192 \approx 2.127. A confident wrong answer costs much more.

Interpretation: the mask loss averages this quantity over all m×mm \times m pixels of the true-class mask. Because each pixel uses its own sigmoid, a pixel can be foreground for one class without lowering the score for another class.

Example
3. Speed budget for a real system

The paper reports 195 ms per image on an Nvidia Tesla M40 for a ResNet-101-FPN model that shares features between the proposal and mask stages. It adds about 15 ms of CPU time to resize the outputs to the original image size.

  1. Total time per image: 195+15=210195 + 15 = 210 ms.
  2. Frames per second: 1000/210≈4.761000 / 210 \approx 4.76.
  3. The paper rounds this to about 5 fps, which matches the abstract.
  4. The C4 variant takes about 400 ms, which is roughly half the speed. That is why the paper does not recommend it in practice.

Interpretation: a system with a 210 ms budget per image can process about 4.8 images every second on that hardware. The frame rate is an engineering number for a given GPU, not a property of the method alone.

Try it: a version of the model takes 250 ms per image on the GPU and 20 ms on the CPU. What is its frame rate?

Answer: total time is 250+20=270250 + 20 = 270 ms, so the rate is 1000/270≈3.71000 / 270 \approx 3.7 fps.

The math

Definition (the multi-task loss). During training each sampled region rr has a true class kk, a true box, and a true mask. The loss is

L=Lcls+Lbox+Lmask.L = L_{cls} + L_{box} + L_{mask}.

The first two terms are the same as in Fast R-CNN. The third term is defined as follows.

Step 1 in symbols (the mask output). For a region with KK classes, the mask head produces KK masks of resolution m×mm \times m, so Km2K m^2 numbers in total. Call the raw score for class cc at pixel (i,j)(i, j) zijcz^{c}_{ij}. Each raw score goes through a sigmoid:

sijc=11+e−zijc.s^{c}_{ij} = \frac{1}{1 + e^{-z^{c}_{ij}}}.

Here ss is between 0 and 1 and can be read as the probability that the pixel is foreground for class cc.

Step 2 in symbols (the mask loss). Let yijy_{ij} be 1 if the pixel is inside the true mask and 0 otherwise, for the true class kk. The mask loss averages the binary cross-entropy over the pixels of mask kk only:

Lmask=−1m2∑i=1m∑j=1m[yijln⁡sijk+(1−yij)ln⁡(1−sijk)].L_{mask} = -\frac{1}{m^2} \sum_{i=1}^{m} \sum_{j=1}^{m} \left[ y_{ij} \ln s^{k}_{ij} + (1 - y_{ij}) \ln \left(1 - s^{k}_{ij}\right) \right].

The other K−1K - 1 masks do not contribute. This is why the classes do not compete: no term pushes one class's mask down because another class's mask was raised.

Step 3 in symbols (RoIAlign). Let the feature map have stride σ\sigma, so one feature cell covers σ\sigma image pixels. A continuous coordinate xx in image space maps to x/σx / \sigma in feature space, with no rounding. RoIPool replaces x/σx / \sigma with the rounded value ⌊x/σ⌉\lfloor x / \sigma \rceil. For a sampled position p+ap + a, where pp is an integer and 0≤a<10 \le a < 1, bilinear interpolation gives

f(p+a)=(1−a) f(p)+a f(p+1).f(p + a) = (1 - a)\, f(p) + a\, f(p + 1).

In two dimensions the same rule is applied along each axis, with four neighbouring grid values. Each bin of the region averages a fixed number of such samples (the paper uses four), and the result does not depend on exactly where the samples fall, as long as no rounding happens.

Step 4 in symbols (the output at test time). For the chosen class k^\hat{k}, the mask Mk^M^{\hat{k}} is resized to the box size and binarized:

pixel is foreground  ⟺  sijk^≥0.5.\text{pixel is foreground} \iff s^{\hat{k}}_{ij} \ge 0.5.

Results

COCO instance segmentation, test-dev (Table 1). Mask AP for single models: MNC (ResNet-101-C4, the 2015 winner) 24.6, FCIS with OHEM (the 2016 winner, dilated ResNet-101) 29.2, FCIS+++ 33.6. Mask R-CNN with ResNet-101-C4 reaches 33.1, with ResNet-101-FPN 35.7, and with ResNeXt-101-FPN 37.1. The paper says Mask R-CNN beats FCIS+++, which uses several test-time tricks that the paper does not.

Ablations on minival (Table 2). The paper's ablations change one thing at a time.

  • Backbones: ResNet-50-C4 gets 30.3 mask AP, ResNet-101-FPN gets 35.4, and ResNeXt-101-FPN gets 36.7.
  • Decoupling: a per-pixel softmax over classes, as in FCN, loses 5.5 mask AP against the sigmoid version.
  • Class-agnostic masks (one mask for all classes) get 29.7 against 30.3 for class-specific masks, which shows that the box branch does most of the work of classification.
  • RoIAlign: on ResNet-50-C4 it improves AP by about 3 points over RoIPool. On a stride-32 C5 backbone it improves mask AP by 7.3 points and AP75 by 10.5 points. RoIWarp, which bilinearly samples but still rounds, performs about as badly as RoIPool.
  • Mask branch: a convolutional mask head beats an MLP head by 2.1 mask AP.

Box detection (Table 3). Using only the box output, ResNet-101-FPN Mask R-CNN reaches 38.2 box AP, against 36.2 for Faster R-CNN with FPN. The gain comes from RoIAlign (+1.1), multi-task training (+0.9) and the stronger backbone (ResNeXt adds +1.6 for the same model family). ResNeXt-101-FPN reaches 39.8.

Human keypoints (Table 4). A ResNet-50-FPN model reaches 62.7 keypoint AP on test-dev, 0.9 points above the 2016 winner, and runs at about 5 fps.

What the numbers mean. The mask gain over the previous best is large, and most of it comes from the alignment fix and from the decoupled classes, not from a bigger network. The box gain shows that training the mask branch alongside the boxes also helps detection, as the authors claim.

Limits and tradeoffs. The mask head adds about 20 percent to the cost of Faster R-CNN, and the C4 variant is much slower than the FPN version. The paper trains the proposal network separately in its ablations, so the full system has more moving parts than the story suggests. Results are on COCO, with ResNet and ResNeXt backbones. The paper does not study other datasets, and it says the mask branch's architecture is kept simple on purpose.

Reading guide

Read the paper in this order. Each section names the lessons it needs.

Check yourself

QuestionRecall: what does RoIAlign change compared with RoIPool, and why does that matter more for masks than for class labels?Show answer

RoIAlign removes the rounding of region coordinates and bin boundaries, and samples the feature map at exact positions with bilinear interpolation. RoIPool's rounding shifts features by up to half a cell. A class label tolerates such a shift, but a mask must match the object's edge pixel by pixel, so the shift produces misaligned masks.

QuestionCompute: a mask head predicts a single pixel with raw score z=0z = 0, and the true label is background (y=0y = 0). What is the binary cross-entropy loss for that pixel?Show answer

The sigmoid gives s=1/(1+e0)=0.5s = 1 / (1 + e^0) = 0.5. The loss is −[0⋅ln⁡0.5+1⋅ln⁡(1−0.5)]=−ln⁡0.5=ln⁡2≈0.693-[0 \cdot \ln 0.5 + 1 \cdot \ln(1 - 0.5)] = -\ln 0.5 = \ln 2 \approx 0.693.

QuestionWhy: the paper finds that a per-pixel softmax over classes loses 5.5 mask AP against a per-pixel sigmoid. Why does the sigmoid version work better?Show answer

The box branch has already decided the class, so the mask branch only needs to decide whether each pixel belongs to the object. With a sigmoid and a binary loss, each class's mask is trained on its own, and no mask is pushed down because another class's mask was raised. The softmax couples the classes and makes the task harder.

QuestionApplied: a team builds a system on Mask R-CNN with a ResNet-101-C4 backbone and asks whether to switch to the FPN version for a deployment that needs about 5 fps. What does the paper suggest?Show answer

Switch to FPN. The paper reports that the ResNet-101-C4 variant takes about 400 ms per image, while the ResNet-101-FPN version takes about 195 ms on the same GPU, and it advises against the C4 variant in practice. The FPN version also has higher accuracy on test-dev (35.7 mask AP against 33.1). The team should measure the speed on its own hardware, since the paper's timing is for one GPU type.

Sources

Read the full paper

Kaiming He, Georgia Gkioxari, Piotr Dollár et al., 2017. arXiv:1703.06870