CSC3104 · Module 6 of 9

Segmentation

Predict pixel-level regions

~56 min to study properly

Segmentation is dense prediction: instead of one label per image or one box per object, the model assigns a class to every single pixel.

Checkpoint · the examiner asks

Why are skip connections indispensable in U-Net? What happens to the output mask if you remove them?

In one minute

Segmentation labels every pixel. Semantic = class per pixel, instance = separate objects, panoptic = both. U-Net's encoder learns WHAT is there but loses WHERE; the decoder upsamples and skip connections bring back high-resolution detail for sharp boundaries. Evaluate with per-class IoU / mIoU or Dice = 2·overlap / (|A| + |B|).

Traps examiners look for
  • Thinking Dice and IoU are the same number — Dice = 2·IoU / (1 + IoU) is always ≥ IoU.
  • Removing skip connections and expecting crisp edges.
  • Reporting pixel accuracy when the background dominates the image.
Read

Study notes

01Dense prediction

Segmentation assigns a label to every pixel. The output is a mask with the same height and width as the image.

  • Semantic segmentation — a class per pixel; all sheep are just "sheep".
  • Instance segmentation — separates individual objects: sheep 1, sheep 2. Mask R-CNN adds a small mask head to Faster R-CNN that predicts a mask inside each detected box.
  • Panoptic segmentation — combines both: countable things (people, cars) get instance ids; amorphous stuff (sky, road, grass) gets semantic labels.

Applications range from medical imaging (tumour boundaries) to agriculture (field and crop mapping) to self-driving (drivable area).

02Encoder–decoder and U-Net

A segmentation network has to answer two questions: what is in the image (needs large receptive fields and deep, semantic features) and where exactly (needs high resolution).

  • The encoder repeatedly convolves and downsamples: features become more semantic, but spatial detail is lost — a 256 × 256 input may be 16 × 16 at the bottleneck.
  • The decoder upsamples back to full resolution, using transposed convolutions ("learned upsampling") or interpolation + convolution.

U-Net's key idea is the skip connection: copy the encoder's high-resolution feature maps across and concatenate them with the decoder's upsampled maps at each level. The decoder then combines "what" from deep layers with "where" from shallow ones. Remove the skips and masks come out blurry, with poor boundaries.

03IoU and Dice for masks

With predicted mask P and ground truth G (sets of pixels):

  • IoU (Jaccard) = |P ∩ G| / |P ∪ G| = TP / (TP + FP + FN).
  • Dice (F1) = 2|P ∩ G| / (|P| + |G|) = 2TP / (2TP + FP + FN).

They are linked: Dice = 2·IoU / (1 + IoU), so Dice ≥ IoU (IoU 0.5 ↔ Dice 0.667). Both punish false positives (painting outside) and false negatives (missing parts).

Report per-class IoU and mean IoU (mIoU) across classes — pixel accuracy is misleading when background dominates: a model that predicts "background" everywhere can get 95% pixel accuracy on small lesions.

04Losses and class imbalance

The standard loss is per-pixel cross-entropy. When the object of interest is tiny (a lesion covering 2% of the image), cross-entropy is dominated by easy background pixels.

Remedies:

  • Dice loss (1 − Dice) optimises overlap directly and is insensitive to how much background there is — popular in medical imaging.
  • Combined loss: cross-entropy + Dice, for stable gradients plus good overlap.
  • Class weights or focal loss to emphasise rare classes.

Data augmentation must transform the image and mask identically (same crop, flip, rotation) — otherwise labels no longer line up with pixels.

Watch it done

Worked examples

Worked example 1

IoU and Dice from pixel counts

A predicted lesion mask has TP = 1,800 pixels, FP = 400, FN = 600. Compute IoU and Dice and check the identity.

    Worked example 2

    Why pixel accuracy lies

    In a 512 × 512 scan, the lesion covers 1% of pixels. A model predicts all background. What are its pixel accuracy and lesion IoU?

      Vocabulary

      Key terms

      Segment Anything, now with words

      SAM (2023) segmented any object from a click. SAM 2 (2024) extended this to video with a streaming memory that follows the object across frames. SAM 3 (late 2025) adds concept prompts — type ‘yellow school bus’ and it segments every instance.

      Practise

      Quick check

      0 of 8 correct so far. Wrong answers come with an explanation — and the buddy can unpack them.

      Q1Counting individual cars in a parking lot needs…

      Q2A mask has IoU = 0.5 with ground truth. Its Dice score is…

      Q3Labelling 'sheep 1' and 'sheep 2' separately is…

      Q4In panoptic segmentation, 'sky' and 'road' are…

      Q5What do U-Net's skip connections provide to the decoder?

      Q6IoU = 0.5. Dice = ?

      Q7Why is Dice loss popular for small medical structures?

      Q8When you random-crop a training image, what must happen to its mask?

      Exam-style

      Written practice

      Short answer5 marks

      Explain why skip connections are essential in U-Net and what happens without them.

      Short answer5 marks

      Define IoU and Dice for segmentation masks and explain why pixel accuracy can be misleading.

      Long answer12 marks

      Design a system to segment cassava fields from satellite images for district planning. Choose the segmentation type and architecture, describe training (loss, augmentation, imbalance) and evaluation, and explain how you'd turn masks into field areas.

      Watch

      Videos

      Fresh picks are chosen by AI every day.
      Hands-on

      Paint a mask: IoU vs Dice

      4 guided tasks: predict, experiment, explain — then challenge yourself. Plus 1 coding exercise you run right in the browser.

      Start the lab