# Image Segmentation

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

Canonical: https://www.internhack.in/roadmaps/tracks/machine-learning/image-segmentation
Last updated: 2026-11-28

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](https://www.internhack.in/roadmaps/tracks/machine-learning/transfer-learning-vision)

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

- [PyTorch tutorials](https://pytorch.org/tutorials/) (PyTorch)
- [Hugging Face Transformers documentation](https://huggingface.co/docs/transformers/index) (Hugging Face)
- [Dive into Deep Learning](https://d2l.ai/) (d2l.ai)

Previous: [Object Detection](https://www.internhack.in/roadmaps/tracks/machine-learning/object-detection) · Next: [Text Preprocessing and Representations](https://www.internhack.in/roadmaps/tracks/machine-learning/text-representation)

## More in Computer Vision

- [Convolutional Neural Networks](https://www.internhack.in/roadmaps/tracks/machine-learning/cnns)
- [Image Data Pipelines and Augmentation](https://www.internhack.in/roadmaps/tracks/machine-learning/image-data-pipelines)
- [Transfer Learning and Fine-Tuning](https://www.internhack.in/roadmaps/tracks/machine-learning/transfer-learning-vision)
- [Object Detection](https://www.internhack.in/roadmaps/tracks/machine-learning/object-detection)
- [Image Segmentation](https://www.internhack.in/roadmaps/tracks/machine-learning/image-segmentation)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Machine Learning Engineer Roadmap](https://www.internhack.in/roadmaps/tracks/machine-learning)
