Sub topic 11
Mask R-CNN: Detect, Then Segment
This chapter assumes that you know basic Python, convolutional neural networks, and object detection. You should also understand bounding boxes and confidence scores.
Mask R-CNN is an instance segmentation model. It detects each object with a bounding box and class label, then predicts a pixel mask for each detected object. If an image contains three cars, the output contains three separate car masks.
From detection to a mask
Mask R-CNN extends Faster R-CNN. Its main stages are:
- A backbone network extracts feature maps from the image.
- A region proposal network suggests regions that might contain objects.
- RoIAlign extracts features for each proposed region.
- Classification and box-regression heads predict the class and refine the box.
- A mask head predicts a binary mask for each region.
RoIAlign is important because it preserves spatial alignment between the region and its feature values. Earlier RoI pooling methods rounded coordinates, which could shift object boundaries by several pixels.
The mask head usually produces a low-resolution mask, such as 28 by 28 pixels. The model resizes that mask to the detected bounding box and thresholds its probabilities. Therefore, the mask boundary is not automatically perfect. Small objects, thin structures, and overlapping objects are difficult cases.
Running a pretrained model
Torchvision provides pretrained Mask R-CNN weights. The model returns a dictionary containing boxes, labels, scores, and masks. Each mask is a probability map with shape (1, H, W).
import torch
from torchvision.models.detection import maskrcnn_resnet50_fpn
model = maskrcnn_resnet50_fpn(weights="DEFAULT")
model.eval()
image = torch.rand(3, 480, 640) # replace with a float tensor in [0, 1]
with torch.no_grad():
output = model([image])[0]
keep = output["scores"] > 0.5
masks = output["masks"][keep, 0] > 0.5
labels = output["labels"][keep]
boxes = output["boxes"][keep]
print(masks.shape, labels.tolist())The confidence threshold is a decision rule, not a property of the image. A higher threshold reduces false positives but can remove real objects. A lower threshold finds more objects but may include incorrect detections.
| Output | Meaning |
|---|---|
| `boxes` | Coordinates of detected object regions |
| `labels` | Predicted class IDs |
| `scores` | Detection confidence |
| `masks` | One probability mask per detection |
Common failure modes
Mask R-CNN can fail when objects are heavily occluded, very small, or unlike the training data. Two nearby objects may be merged, or one object may receive several detections. Non-maximum suppression usually removes overlapping boxes, but it cannot correct a poor mask.
Use validation images that reflect deployment conditions. Inspect masks visually, measure mask quality separately from box quality, and tune the confidence and mask thresholds on validation data rather than on the test set.
Exercise
An image contains two overlapping people. Why is semantic segmentation insufficient if an application must count and track each person separately?
Show answer
Semantic segmentation gives both people the same person label, so their pixels are not separated. Mask R-CNN predicts a separate mask and detection for each instance.