Segmentation
Every pixel gets a label — FCN, U-Net, Mask R-CNN and Segment Anything
Segmentation asks the finest question in vision — which pixels belong to what — and it powers medical imaging, autonomous driving, satellite analysis and photo editing. Its workhorse, the U-Net, is also the denoising backbone inside Stable Diffusion (8.9 Generative models II: Diffusion and flow matching), so its encoder–decoder-with-skips design is worth knowing cold. Segment Anything showed that promptable foundation models apply to pixels too, foreshadowing the multimodal systems of 8.10 Multimodal models.
You'll be able to
- Distinguish semantic, instance and panoptic segmentation and their metrics (mIoU, Dice, PQ)
- Explain FCN upsampling, U-Net skip connections and why they recover localization
- Derive the soft Dice loss, relate Dice to IoU, and justify combined losses for imbalanced masks
- Describe Mask R-CNN's mask head and RoIAlign, and Segment Anything's promptable design
- Choose a segmentation model and dataset format for a practical task
Key ideas
- Semantic = class per pixel; instance = mask per object; panoptic = both. Metrics: mIoU, per-class Dice, PQ = SQ × RQ.
- FCNs replace FC layers with 1×1 convs and upsample; skip connections from shallow layers restore boundaries (deep = what, shallow = where).
- U-Net mirrors an encoder with a decoder and concatenates encoder features at each resolution; it trains from tiny datasets and is the backbone of diffusion models.
Show 4 more key ideas
- Dice = 2|A∩B|/(|A|+|B|) = 2·IoU/(1+IoU); soft Dice loss 1 − (2Σp·g + ε)/(Σp + Σg + ε) is immune to background imbalance; BCE + Dice is the standard recipe.
- Mask R-CNN = Faster R-CNN + per-class sigmoid mask head; RoIAlign's bilinear sampling (no quantization) is what makes masks accurate.
- Segment Anything: one heavy image embedding, cheap prompt encoder and mask decoder, trained on 1.1 B masks via a data engine; zero-shot, promptable, class-agnostic.
- Label maps must use nearest-neighbour interpolation and identical geometric augmentations to the image.
Suggested path through this lesson
Three flavors of segmentation
Semantic segmentation assigns every pixel a class (road, car, sky) without distinguishing instances. Instance segmentation finds each object and its mask (car 1, car 2) but ignores amorphous "stuff" like road. Panoptic segmentation does both: every pixel gets a class and, for countable "things", an instance id. Each flavor has its benchmarks (Cityscapes, ADE20K, COCO-Stuff/Panoptic) and losses, but they share one engineering problem: producing output at full image resolution from features that a classifier would have downsampled 32×.
Fully convolutional networks
FCN (2015) made the first move: take a classification CNN, replace the fully connected layers with convolutions (5.2 CNN building blocks) so the network outputs a coarse class-score map, then upsample it to the input size with bilinear interpolation or learned transposed convolutions. Upsampling a 32×-downsampled map alone gives blobby boundaries, so FCN-16s and FCN-8s add skip connections: fuse predictions from earlier, higher-resolution layers. The idea generalizes: deep layers know what, shallow layers know where; combine them.
U-Net: the encoder–decoder with skips
U-Net (2015, Ronneberger et al.) made the combination systematic. A contracting encoder (conv–conv–pool, channels doubling) is mirrored by an expanding decoder (upsample–conv–conv, channels halving), and at every resolution the encoder's feature map is concatenated onto the decoder's — the "U" shape with horizontal skips. The decoder thus gets semantic context from below and precise localization from the side; gradients also flow through short paths, which is why U-Nets train quickly even from a few dozen annotated biomedical images with heavy elastic augmentation. Modern variants pad convolutions so output size equals input size, swap the encoder for a pretrained ResNet/ConvNeXt (via libraries like segmentation_models.pytorch), and add attention or residual blocks. The same architecture, with time and text conditioning injected, is the noise predictor of DDPM and Stable Diffusion (8.9 Generative models II: Diffusion and flow matching) — the encoder/decoder vocabulary from 4.10 Autoencoders, embeddings and representation learning in its most literal form.
DeepLab took another route to resolution: keep the feature map at output stride 8 or 16 using dilated convolutions (5.2 CNN building blocks) and gather multi-scale context with Atrous Spatial Pyramid Pooling (parallel dilated convs with different rates). Transformer-based segmenters (SegFormer, Mask2Former) now lead benchmarks, but U-Net remains the default for medical and small-data problems.
Losses for masks
Per-pixel cross-entropy (4.3 Loss functions catalog) is the natural loss, but masks are often tiny: a tumor may be 1% of the pixels, so a model predicting "background everywhere" scores 99% pixel accuracy and a low cross-entropy. Overlap-based losses optimize what we actually measure. The Dice coefficient between predicted mask and ground truth :
Worked: pixels with 60 overlapping. IoU ; Dice ; check: . ✓ Dice is always at least IoU and less harsh on partial overlaps.
To train with it, make it differentiable using predicted probabilities and binary targets — the soft Dice loss:
with for numerical stability when a class is absent. Because it is a ratio over the whole mask, it is insensitive to how many background pixels exist — exactly the imbalance fix we wanted. Its gradients can be noisy for small structures, so the standard recipe is BCE + Dice (or focal + Dice, 5.5 Object detection); the Lovász-softmax loss optimizes a convex surrogate of IoU directly; boundary losses sharpen edges.
Metrics
Pixel accuracy is nearly meaningless under imbalance. Report mean IoU over classes (mIoU, the Cityscapes/ADE20K standard) or per-class Dice (the medical standard). Panoptic quality combines segmentation and recognition: , with matches defined by IoU .
Instance segmentation: Mask R-CNN
Mask R-CNN (2017) adds a third branch to Faster R-CNN (5.5 Object detection): for each detected region, a small FCN predicts a binary mask per class, using a sigmoid rather than a softmax across classes so mask prediction does not compete with classification. Its enabling detail is RoIAlign: RoI pooling snapped region boundaries to the feature grid (quantization errors of several input pixels), which barely matters for boxes but ruins masks; RoIAlign samples features at exact fractional positions with bilinear interpolation, improving mask AP by 10–50% relative. Runs at about 5 fps; extended to keypoints by predicting one-hot masks per joint.
Segment Anything: promptable segmentation
SAM (2023) reframed segmentation as a promptable task, like a language model for pixels. A heavy ViT-H image encoder (5.7 Vision Transformers, pretrained with MAE, 5.8 Self-supervised and contrastive learning) embeds the image once; a lightweight prompt encoder embeds points, boxes or rough masks (and text via CLIP); a small mask decoder (two-way attention) outputs several candidate masks with confidence scores in milliseconds — interactive segmentation in the browser. The training data came from a data engine: annotate with model assistance, retrain, repeat, producing SA-1B with 11 million images and 1.1 billion masks. SAM transfers zero-shot to new domains, accelerates annotation enormously, and SAM 2 (2024) extended it to video with a streaming memory. It does not know class names — pair it with a detector or a vision-language model to name what it segments (8.10 Multimodal models).
In practice
Semantic: torchvision.models.segmentation (DeepLabV3, FCN), segmentation_models.pytorch (U-Net with 500+ encoders), Hugging Face transformers (SegFormer, Mask2Former). Instance: Mask R-CNN in torchvision, YOLO-seg in Ultralytics. Promptable: SAM/SAM 2 via transformers or the official repo. Datasets store masks as PNG label maps (semantic) or COCO RLE polygons (instance). Augment masks and images with the same geometric transform (Albumentations does this), never interpolate label maps (use nearest-neighbour), and evaluate per class — the rare classes are where models fail.
Where this shows up later
- U-Net as the denoiser in diffusion models — 8.9 Generative models II: Diffusion and flow matching; ViT encoders in SAM — 5.7 Vision Transformers; MAE pretraining — 5.8 Self-supervised and contrastive learning.
- Promptable, multimodal segmentation and open-vocabulary detection — 8.10 Multimodal models.
- Dice/IoU as overlap metrics connect to IoU in detection — 5.5 Object detection and to retrieval metrics — 9.5 Retrieval quality: hybrid search, reranking, query transforms and metrics.
Common pitfalls (4)
- Reporting pixel accuracy on imbalanced masks — 99% accuracy can mean the model found nothing.
- Resizing label maps with bilinear interpolation, which invents non-existent class ids at boundaries.
- Using softmax across classes in the mask head of an instance segmenter (Mask R-CNN decouples class and mask with per-class sigmoids for a reason).
- Expecting SAM to output class labels — it segments; naming requires a detector or vision-language model.
Recap: all 7 key points
- Semantic = class per pixel; instance = mask per object; panoptic = both. Metrics: mIoU, per-class Dice, PQ = SQ × RQ.
- FCNs replace FC layers with 1×1 convs and upsample; skip connections from shallow layers restore boundaries (deep = what, shallow = where).
- U-Net mirrors an encoder with a decoder and concatenates encoder features at each resolution; it trains from tiny datasets and is the backbone of diffusion models.
- Dice = 2|A∩B|/(|A|+|B|) = 2·IoU/(1+IoU); soft Dice loss 1 − (2Σp·g + ε)/(Σp + Σg + ε) is immune to background imbalance; BCE + Dice is the standard recipe.
- Mask R-CNN = Faster R-CNN + per-class sigmoid mask head; RoIAlign's bilinear sampling (no quantization) is what makes masks accurate.
- Segment Anything: one heavy image embedding, cheap prompt encoder and mask decoder, trained on 1.1 B masks via a data engine; zero-shot, promptable, class-agnostic.
- Label maps must use nearest-neighbour interpolation and identical geometric augmentations to the image.
Watch
The first 20 minutes cover semantic segmentation, FCNs and learnable upsampling; the end covers Mask R-CNN.
Read
Exercises
- Dice vs IoU by hand
For masks with , and overlaps of 20, 60 and 80 pixels, compute IoU and Dice, then confirm each time. Then compute pixel accuracy for a image where is a square and the model predicts all background — expected: 99.4% accuracy, Dice 0.
- Train a U-Net on a small dataset
Using
segmentation_models.pytorchwith a pretrained ResNet-34 encoder, train on a small public mask dataset (e.g., a pet or skin-lesion set) with BCE + Dice for 20 epochs. Report per-class IoU and Dice, and show the 8 worst predictions. Expected: Dice above 0.85 on foreground after a few epochs; failures at thin boundaries. - Prompt SAM
Run Segment Anything (via
transformers) on three of your own photos: click points, draw boxes, compare the three candidate masks and their scores. Then combine it with a detector's boxes to produce labeled instance masks. Note where SAM over- or under-segments.
Quiz
Flashcards
These cards are now in your spaced-repetition queue. Review all due cards →