← Master Index
Vol. 07 Module 7.3 Lecture

Mask R-CNN

Popular Vision Models

How This Lesson Fits the Module

YOLO gives fast bounding boxes, but a box is coarse—it cannot outline an object’s exact shape. Mask R-CNN (He et al., Facebook AI, 2017) extends the two-stage detector to produce a per-pixel segmentation mask for every detected object. It is the reference model for instance segmentation—the segmentation task from Module 7.2, but object-aware.

Faster R-CNN — region proposals → box + class Mask R-CNN (2017) — adds a parallel mask branch + RoIAlign Result: box + class + pixel-accurate mask per instance

Learning Objectives

By the end of this lesson, students should be able to:

  • Distinguish detection, semantic segmentation, and instance segmentation.
  • Explain how Mask R-CNN extends Faster R-CNN with a mask branch.
  • Describe RoIAlign and why it replaced RoIPool.
  • Outline the backbone → RPN → RoIAlign → heads pipeline.
  • Run Mask R-CNN inference from torchvision.models.detection.
  • Recognize where instance segmentation is used in practice.
Definition — Instance Segmentation

Instance segmentation detects each object and labels every pixel belonging to it, separating overlapping instances of the same class (e.g., three distinct people, each with its own mask). Mask R-CNN achieves this by adding a small fully convolutional mask branch to Faster R-CNN that predicts a binary mask per region of interest.

The Pipeline

A CNN ResNet+FPN backbone extracts features. A Region Proposal Network (RPN) proposes candidate boxes. RoIAlign crops fixed-size features for each proposal, then three parallel heads predict: (1) class, (2) refined box, (3) binary mask. Running the heads in parallel keeps it efficient.

TaskOutputExample model
ClassificationOne label per imageResNet
Object detectionBoxes + classesYOLO, Faster R-CNN
Semantic segmentationPer-pixel class (no instances)U-Net, FCN
Instance segmentationPer-object mask + box + classMask R-CNN

Why RoIAlign Matters

The earlier RoIPool quantized region coordinates to the feature grid, misaligning masks by a pixel or two—fatal for pixel-accurate output. RoIAlign uses bilinear interpolation to sample features at exact fractional locations, preserving spatial alignment. This single fix gave Mask R-CNN a large jump in mask quality.

Mask R-CNN in PyTorch

import torch from torchvision.models.detection import ( maskrcnn_resnet50_fpn, MaskRCNN_ResNet50_FPN_Weights) weights = MaskRCNN_ResNet50_FPN_Weights.DEFAULT model = maskrcnn_resnet50_fpn(weights=weights) model.eval() preprocess = weights.transforms() img = preprocess(read_image_tensor) # [3, H, W] float tensor with torch.no_grad(): outputs = model([img]) # list of dicts, one per image out = outputs[0] for box, label, score, mask in zip( out["boxes"], out["labels"], out["scores"], out["masks"]): if score > 0.5: name = weights.meta["categories"][label] binary_mask = mask[0] > 0.5 # [H, W] boolean per instance print(name, float(score), box.tolist())
Module 7.1 / 7.2 Tie-In The backbone is a ResNet built from convolutions; the task refines segmentation into instance-aware masks and detection boxes together.
Common Misconception: “Mask R-CNN and semantic segmentation are the same thing.”

Reality: Semantic segmentation labels every pixel by class but merges all cars into one “car” blob. Mask R-CNN separates each car as a distinct instance with its own mask—that is instance segmentation.

Critical Mistake — Treating Mask Output as Final

The mask head outputs soft probabilities at low resolution per RoI. You must threshold (e.g., >0.5) and resize the mask back to the box’s location in the full image. Skipping the resize/paste step yields masks that don’t align with the objects.

Knowledge Check

  1. Short Answer: What task does Mask R-CNN perform? Answer: Instance segmentation.
  2. True/False: Mask R-CNN extends Faster R-CNN. Answer: True.
  3. Multiple Choice: The new branch Mask R-CNN adds predicts: (a) captions, (b) a per-object binary mask, (c) audio, (d) depth. Answer: (b).
  4. Short Answer: What operation replaced RoIPool and why? Answer: RoIAlign—avoids coordinate quantization, preserving alignment.
  5. True/False: Semantic segmentation distinguishes individual instances of a class. Answer: False (that’s instance segmentation).
  6. Multiple Choice: Which proposes candidate regions? (a) RPN, (b) softmax, (c) NMS, (d) dropout. Answer: (a).
  7. Short Answer: Name the three parallel heads in Mask R-CNN. Answer: Class, box regression, and mask.
  8. True/False: Mask R-CNN is typically faster than one-stage YOLO. Answer: False—it is a two-stage, slower model.
  9. Multiple Choice: The backbone commonly used is: (a) LSTM, (b) ResNet+FPN, (c) plain MLP, (d) LeNet. Answer: (b).
  10. Short Answer: Give one real-world use of instance segmentation. Answer: Medical imaging, autonomous driving, photo editing (any).

Key Takeaways

  • Mask R-CNN adds a mask branch to Faster R-CNN for instance segmentation.
  • RoIAlign preserves spatial alignment, enabling pixel-accurate masks.
  • Pipeline: backbone → RPN → RoIAlign → class/box/mask heads.
  • Instance segmentation separates each object, unlike semantic segmentation.
  • Next: architectures move beyond convolution—the Vision Transformer.
Trainer’s Guide

Demo: Run pretrained Mask R-CNN on a crowd photo and overlay masks—students see overlapping people separated into distinct instances.

Discussion: Show a RoIPool vs RoIAlign diagram and ask why a one-pixel misalignment matters far more for masks than for classification.

Progress CNN-based vision is complete—classification, detection, and segmentation. Next we question convolution itself. Continue to the Vision Transformer.