← Back to curriculum

Module 5 — Segmentation & instance masks

Segmentation losses & metrics

Cross-entropy vs Dice vs focal loss, IoU and mIoU, boundary-aware losses, class imbalance, and qualitative vs quantitative eval.

~70 min read + exercises

Segmentation losses & metrics

Before we begin

Segmentation models optimize per-pixel losses and report overlap metrics. Choosing the wrong loss for imbalanced data is a common failure mode.


Learning objectives

  • Compare cross-entropy, Dice, and focal loss.
  • Compute IoU and mIoU.
  • Interpret boundary quality qualitatively.
  • Handle class imbalance and ignore regions.

Cross-entropy

Standard per-pixel CE — works when classes are roughly balanced. For rare foreground (tumor, defect), CE alone may ignore small regions.

Weighted CE: multiply rare class loss by higher weight.


Dice loss

Dice=2ABA+B\text{Dice} = \frac{2|A \cap B|}{|A| + |B|}

Dice loss =1Dice= 1 - \text{Dice} — directly optimizes overlap; popular in medical imaging.


Focal loss

Down-weights easy pixels — focuses on hard negatives and hard positives. Useful for dense detection/segmentation with extreme imbalance.


mIoU

For each class cc:

IoUc=TPcTPc+FPc+FNc\text{IoU}_c = \frac{TP_c}{TP_c + FP_c + FN_c}

mIoU = mean of IoU over classes (often excluding void). Primary benchmark metric on Cityscapes, ADE20K, etc.


Qualitative review

Always overlay predictions on hard images — thin structures, shadows, and rare classes often fail before mIoU drops noticeably.


What's next

Module 5 quiz — then pet segmentation project.