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.
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.
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.
| Task | Output | Example model |
|---|---|---|
| Classification | One label per image | ResNet |
| Object detection | Boxes + classes | YOLO, Faster R-CNN |
| Semantic segmentation | Per-pixel class (no instances) | U-Net, FCN |
| Instance segmentation | Per-object mask + box + class | Mask 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
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.
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
- Short Answer: What task does Mask R-CNN perform? Answer: Instance segmentation.
- True/False: Mask R-CNN extends Faster R-CNN. Answer: True.
- Multiple Choice: The new branch Mask R-CNN adds predicts: (a) captions, (b) a per-object binary mask, (c) audio, (d) depth. Answer: (b).
- Short Answer: What operation replaced RoIPool and why? Answer: RoIAlign—avoids coordinate quantization, preserving alignment.
- True/False: Semantic segmentation distinguishes individual instances of a class. Answer: False (that’s instance segmentation).
- Multiple Choice: Which proposes candidate regions? (a) RPN, (b) softmax, (c) NMS, (d) dropout. Answer: (a).
- Short Answer: Name the three parallel heads in Mask R-CNN. Answer: Class, box regression, and mask.
- True/False: Mask R-CNN is typically faster than one-stage YOLO. Answer: False—it is a two-stage, slower model.
- Multiple Choice: The backbone commonly used is: (a) LSTM, (b) ResNet+FPN, (c) plain MLP, (d) LeNet. Answer: (b).
- 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.
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.