Image Segmentation

Classify every pixel: semantic, instance and panoptic segmentation with encoder-decoder models like U-Net and promptable models like SAM.

Advanced · about 10 hours · difficulty 4/5

Learn first: Transfer Learning and Fine-Tuning

Segmentation produces a label for every pixel instead of a box. It is essential in medical imaging, autonomous driving, satellite analysis and photo editing tools.

Flavours

  • Semantic segmentation: each pixel gets a class (road, car, sky), but two cars share one label.
  • Instance segmentation: separate masks for each object (car 1, car 2), as in Mask R-CNN.
  • Panoptic segmentation: combines both for a complete scene description.

Architectures

  • U-Net: an encoder downsamples to capture context, a decoder upsamples back to full resolution, and skip connections carry fine spatial detail across. Still the default in medical imaging.
  • DeepLab uses dilated (atrous) convolutions to widen the receptive field without losing resolution.
  • Segment Anything (SAM) and its successors segment any object from a click or box prompt and are excellent for speeding up labelling.

Losses and metrics

Pixel-wise cross-entropy is the baseline. Dice loss and focal loss help when the foreground is tiny compared to the background. Evaluate with IoU (Jaccard) and the Dice coefficient per class, not plain pixel accuracy, which looks great when a model predicts background everywhere.

Practical notes

Masks must receive the same geometric augmentations as images, but use nearest-neighbour resizing so labels stay integers. Libraries such as segmentation_models_pytorch give you pretrained encoders plugged into U-Net style decoders.

Good enough to move on when you can train a U-Net with a pretrained encoder, evaluate it with Dice and IoU, and explain the difference between semantic and instance segmentation.

Key concepts

  • Semantic vs instance vs panoptic
  • U-Net encoder-decoder
  • Dice and focal loss
  • IoU and Dice metrics
  • Mask R-CNN
  • Segment Anything (SAM)
  • Mask-aware augmentation

Mini project

Train a U-Net with a pretrained encoder on a public segmentation dataset (for example pets or road scenes), report per-class IoU, and overlay predicted masks on 10 validation images.

Check yourself

Why can pixel accuracy be misleading for segmentation, and what does Dice loss do differently from cross-entropy?

Free resources