Sub topic 3
Mask R-CNN: detect, then segment
Prerequisites: Python, NumPy, convolutional networks, bounding-box detection, and IoU (intersection over union, the overlap between a predicted region and a true region).
Mask R-CNN: detect, then segment
Mask R-CNN is a two-stage model. It proposes regions, classifies each one and refines its box, then predicts a binary mask inside each box. Every detection carries a class, a score, a box, and a mask.
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 real float image in [0, 1]
with torch.no_grad():
output = model([image])[0]
keep = output["scores"] > 0.5
masks = output["masks"][keep, 0] > 0.5 # (N, H, W) boolean masks
print(masks.shape, output["labels"][keep].tolist())Evaluating instances with mask AP
Mask AP matches predicted masks to ground-truth masks by IoU, usually at 0.5 (AP50) or averaged over thresholds from 0.5 to 0.95 (COCO AP). For each class, precision and recall are traced as the score threshold drops. Average precision is the area under that curve, and the mean over classes is mask AP.
Merging semantic and instance predictions into panoptic output
A panoptic result gives every pixel exactly one label. Stuff pixels come from the semantic map. Thing pixels come from instance masks. When masks overlap, the higher-scoring instance keeps the pixel. Thing pixels that no mask claims become void.
Scoring panoptic results with PQ, SQ, and RQ
Panoptic quality is PQ = SQ × RQ. A predicted segment matches a ground-truth segment when their IoU exceeds 0.5, which allows at most one match per segment.
- SQ (segmentation quality) is the mean IoU of matched pairs.
- RQ (recognition quality) is TP / (TP + 0.5·FP + 0.5·FN), an F1-style detection score.
Unified mask classification with transformers
MaskFormer and Mask2Former replace per-pixel classification with a fixed set of N queries. Each query outputs one class distribution and one mask. Hungarian matching assigns queries to ground-truth segments during training, and the same output format serves semantic, instance, and panoptic tasks.
Datasets and annotation formats
COCO stores instance masks as polygons or run-length encodings. COCO panoptic stores one PNG per image, where each pixel encodes a segment ID, plus a JSON file that maps segment IDs to category IDs. Check which format your data loader expects before training.
Promptable segmentation models
Promptable models such as SAM take a point or a box and return the mask of the object it indicates. They do not output a fixed class list, so class names must come from another model or from the user.
Common pitfalls and practical checks
- Confirm that label IDs match the training class mapping. Off-by-one errors are common.
- Resize predicted masks back to the original image size before scoring.
- Exclude ignore labels (such as 255) from IoU calculations.
- Use the same score threshold when comparing models.
Exercise
Exercise
Why does panoptic matching require IoU above 0.5?
Show answer
An IoU above 0.5 guarantees at most one match per ground-truth segment, so each match is unambiguous.