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 loss — 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 :
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.