Image Segmentation
Pixel-wise understanding
Segmentation classifies every pixel: semantic assigns each pixel a class, instance separates individual objects. U-Net-style encoder-decoder or FCN heads over backbones are the workhorses.
What you will learn
- Distinguish semantic, instance and panoptic segmentation
- Explain the encoder-decoder = segmentation architecture
- Pick the right loss (pixel CE vs Dice) for your task
What is this?
Segmentation predicts per-pixel labels, producing masks instead of boxes. Semantic segmentation labels each pixel with a class (all roads 'road', all hands 'hand'); instance segmentation separates individual objects (hand #1 vs #2) with masks; panoptic combines both. Encoders (CNN/transformer) compress context, decoders upsample back to full resolution with Skip paths (U-Net) to retain fine detail.
Why do you need it?
- Fine-grained spatial decisions (surgical boundaries, drivable area) need pixel truth, not boxes.
- Mask outputs power areas from precision, autonomous driving, satellite crop mapping, and medical anatomy delineation.
- An encoder-decoder sharing one backbone extends any classification model to dense prediction.
- It's the recognition task where evaluation (mAoU, Dice) matches the deployment metric most directly.
Mental model
Coloring a photo by regions of meaning
Semantic segmentation
An adult colors 'sky', 'sand', 'ocean' each with one color (semantic). Coloring the same photo once per person (this swimmer vs that swimmer) is instance segmentation. Panoptic does both maps at once.
How it works
- Encoder (backbone) downsamples to a feature hierarchy, keeping context.
- Decoder upsamples back to input resolution with skip connections from encoder stages (preserves edges).
- Head: per-pixel logits over classes → softmax gives a class probability map.
- Instance segmentation adds a mask branch (detect instances, then predict a mask per box).
- Loss: pixel-wise cross-entropy, or Dice/IoU-style — Dice handles class imbalance better.
Encoder-decoder with skip connections (U-Net structure).
In production
- Medical: precise organ/tumor region masks (U-Net spines) analyzed by radiology workflows.
- Autonomous vehicles: drivable-area and object-boundary masks over camera feeds.
- Earth observation: crop type / flooding area masks from satellite imagery.
Best practices
- Freeze/extend a pretrained encoder (ResNet/ViT) and add a U-Net-style decoder.
- Resample masks with the same interpolation/precision as labels (nearest for int labels).
- Use Dice/Jaccard loss or Dice+CE blend when classes are imbalanced (tiny tumors).
- Evaluate on your task's metric (Dice, mIoU) and correlate with pixel accuracy — class-imbalance can fool both.
Common mistakes
- Using pixel accuracy as the headline metric while tiny rare classes are basically ignored.
- Resizing/distorting masks separately from images — label/map misalignment.
- Ignoring sparse-boundary thickness: pooling destroys thin structures without skip detail.
- Treating a detection problem (boxes) as segmentation because it 'visually looks' like one.
Common issues
Borders systematically shrunk (thin structures missing)Pooling/stride destroyed high-frequency detail before the decode
Add skip connections from early encoder stages and don't pool too aggressively early.
Regions get matched to the wrong classLabel-encoding mismatch or class weights missing for rare classes
Re-map class ids, add weighting/Dice and inspect a confusion matrix per class.
Empty/fragmentary masksWeak loss on sparse structures — CE-dominated gradients
Switch to Dice or Dice+CE and add boundary-aware terms.
Debugging tips
- Overlay predicted masks on inputs to spot off-by-one class shifts, class bleed and border shrinkage.
- Compute per-class Dice — top-class accuracy can hide a dead rare class.
- Check the decoder's first upsampling shape; the skips must match encoder stage spatial sizes.
- Run the network on ONE image and iterate on that until masks are visually sane.
Interview checkpoint
Quick quiz
Which task separates individual objects as distinct masks?
Pixel accuracy is a reliable metric for heavily imbalanced segmentation.