Skip to main content
Image segmentation assigns a class label to every pixel in an image, producing a dense output map rather than a single prediction. UNet, originally designed for biomedical image segmentation, has become a standard architecture across many visual domains.

Segmentation types

UNet architecture

UNet follows an encoder-decoder design with skip connections that copy feature maps from the encoder directly to the corresponding decoder level.

Encoder path (contracting)

The encoder applies repeated blocks of:
  1. Two 3×33 \times 3 convolutions + ReLU
  2. 2×22 \times 2 max pooling (stride 2) — halves spatial dimensions, doubles channels

Bottleneck

The deepest layer captures the most abstract, high-level features without spatial pooling.

Decoder path (expanding)

The decoder mirrors the encoder:
  1. 2×22 \times 2 transposed convolution (upsampling) — doubles spatial dimensions
  2. Concatenation with the skip connection from the corresponding encoder level
  3. Two 3×33 \times 3 convolutions + ReLU

Output layer

A 1×11 \times 1 convolution maps the 64-channel feature maps to num_classes channels, followed by softmax (or sigmoid for binary segmentation).

PyTorch implementation

Loss functions for segmentation

Cross-entropy loss

The standard choice for multi-class segmentation. Applied pixel-wise: LCE=−1HW∑h,w∑cyh,w,clog⁡p^h,w,c\mathcal{L}_{\text{CE}} = -\frac{1}{HW} \sum_{h,w} \sum_{c} y_{h,w,c} \log \hat{p}_{h,w,c} Can be weighted per class to handle class imbalance (background vs. small objects).

Dice loss

Dice loss directly optimizes the Dice coefficient (F1 score at pixel level), which is more robust to class imbalance: LDice=1−2∑ipigi∑ipi2+∑igi2\mathcal{L}_{\text{Dice}} = 1 - \frac{2 \sum_i p_i g_i}{\sum_i p_i^2 + \sum_i g_i^2} where pip_i are predicted probabilities and gig_i are ground truth labels.
A combined loss L=LCE+LDice\mathcal{L} = \mathcal{L}_{\text{CE}} + \mathcal{L}_{\text{Dice}} is common in practice and often outperforms either alone.

Training and evaluation

Evaluation metrics: mean IoU (mIoU) across classes is the standard benchmark. mIoU=1C∑c=1CTPcTPc+FPc+FNc\text{mIoU} = \frac{1}{C} \sum_{c=1}^{C} \frac{\text{TP}_c}{\text{TP}_c + \text{FP}_c + \text{FN}_c}

Resources

UNet Segmentation Examples

Colab notebook with UNet segmentation on real datasets.

Exercise E08: Segmentation with UNet

Hands-on exercise: train UNet for image segmentation.

Original UNet Paper

Ronneberger et al. (2015) — the original UNet paper for biomedical image segmentation.

Video: UNet, GAN & Anomaly Detection

Recorded lecture covering UNet, GANs, and anomaly detection.