Sub topic 5
Instance segmentation: separating individual objects
Assume you know Python and NumPy, convolutional neural networks, object detection with bounding boxes and confidence scores, and gradient-based losses. Instance segmentation extends object detection by predicting the exact pixels belonging to each detected object.
What the model produces
For an image containing three cars, semantic segmentation may label all car pixels as car. Instance segmentation assigns a separate identity to each car. A typical prediction contains:
- A bounding box for each object.
- A class label, such as
car. - A confidence score.
- A binary mask with one value per image pixel.
The masks have the same height and width as the input image. A value of True means that the pixel belongs to that instance.
| Output | Meaning |
|---|---|
| `boxes` | Rectangles around detected objects |
| `labels` | Predicted class for each object |
| `scores` | Confidence for each prediction |
| `masks` | Pixel-level shape for each object |
Two objects can have the same class but different instance IDs. This distinction matters in robotics, counting, medical imaging, and video tracking.
Mask R-CNN: detect, then segment
Mask R-CNN is a common instance-segmentation architecture. A convolutional backbone extracts features. A region proposal network suggests likely object regions. For each region, separate heads predict the class, refine the bounding box, and generate a mask.
The mask head usually predicts a small mask for each proposed object. The system places that mask back into the object’s image region and resizes it to image resolution. This design is efficient because the model does not predict a full mask independently for every possible class at every pixel.
The following example runs a pretrained model and keeps predictions above a confidence threshold:
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()
image = torch.rand(3, 480, 640) # replace with a float image in [0, 1]
with torch.no_grad():
prediction = model([image])[0]
keep = prediction["scores"] > 0.5
masks = prediction["masks"][keep, 0] > 0.5
labels = prediction["labels"][keep]
boxes = prediction["boxes"][keep]
print("instances:", len(labels))
print("mask shape:", tuple(masks.shape))
print("labels:", labels.tolist())Overlapping objects and evaluation
Instance masks may overlap in the model’s raw predictions. Postprocessing commonly uses non-maximum suppression or mask-aware filtering to remove duplicate detections. When two real objects overlap, their masks should remain separate.
Instance segmentation is often evaluated with mask average precision, or mask AP. A predicted mask matches a ground-truth mask when their intersection over union (IoU) exceeds a selected threshold and their classes agree. AP then summarizes precision and recall across confidence thresholds.
Exercise
Why can two instances have the same class label but different masks?
Show answer
The class identifies what each object is, while the separate masks identify which pixels belong to each individual object.