Skip to main content
This section covers instance segmentation with Mask R-CNN. Semantic segmentation labels every pixel with a class; instance segmentation also separates the individual objects, so two people standing side by side get two different masks. Mask R-CNN is an extension of Faster R-CNN that adds a mask head to the detector, in parallel with the box and class heads. For each detected box, RoIAlign pools the backbone features inside the box into a small fixed-size grid, and the mask head, a small fully convolutional network, predicts a low-resolution binary mask for that object (28×28 in the paper, one per class). The mask is then resized to the box and pasted into the image, so the pixels of the object are marked as 1 and the rest as 0. In the object detection section we saw R-CNN that simply cropped proposals, generated externally to the detector, from the input image and classifies those proposals. Since the proposals were typically overlapping, CNN computations that extracted features per proposal were wasted and the detector was very slow. Fast R-CNN improved this by passing the whole input image once via a CNN feature extractor and used a feature map internally to this CNN to elect proposals, therefore avoiding the feature extraction per proposal. Faster R-CNN removed the external dependency on proposal generation and introduced a Region Proposal Network (RPN) internally to the detector. For the RPN to generate proposals, prior (or anchor) boxes were defined uniformly across the input image and the RPN was trained to predict the class of each anchor and by how much the anchor needs to shift to match the ground truth bounding box. The inspection sections below show the internals of both Faster R-CNN and the mask head that marks the pixels of each detected object.

Demo

Sections

Inside Mask R-CNN

The two sections below take a COCO-trained Mask R-CNN apart. The first follows COCO images through the training pipeline, from instance masks to mask targets and anchors. The second follows one image through the trained network and rebuilds each stage by hand, from anchor matching and proposals to per-class non-maximum suppression and mask prediction, then looks at activations, weights and mean average precision.

Training data

COCO masks and boxes, resizing and batching, augmentation, 28x28 mask targets and anchors.

Model internals

RPN targets and proposals, proposal classification, box refinement, NMS, masks, activations, weights and mAP.

Libraries

There are two main PyTorch implementations of Mask R-CNN. Detectron2 is aimed at research projects: it is more flexible but harder to learn. The model shipped with torchvision, used in the two sections above, is simpler to use and less configurable.

Detectron2 Inference

Shows how an existing pretrained model can be used to do instance segmentation on new classes and how video can be processed via a relevant pipeline.

TorchVision Inference

Runs a pretrained Mask R-CNN on a single image and draws the predicted masks and boxes.

PyTorch reference

Key references: (Ren et al., 2015; He et al., 2017; Chen et al., 2018; Peng et al., 2017)

References

  • Chen, L., Zhu, Y., Papandreou, G., Schroff, F., Adam, H. (2018). Encoder-decoder with atrous separable convolution for semantic image segmentation. arXiv [cs.CV]. Link
  • He, K., Gkioxari, G., Dollár, P., Girshick, R. (2017). Mask R-CNN. arXiv [cs.CV]. Link
  • Peng, C., Xiao, T., Li, Z., Jiang, Y., Zhang, X., et al. (2017). MegDet: A Large Mini-Batch Object Detector. arXiv [cs.CV]. Link
  • Ren, S., He, K., Girshick, R., Sun, J. (2015). Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks. arXiv [cs.CV]. Link

Found an error on this page? Report an issue.