Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.Score the overlap, not the pixels
A segmentation model labels each pixel as object or background. If the object is a thin crack covering 1% of the image, a model that predicts “background everywhere” is 99% right pixel by pixel, and cross-entropy averaged over pixels hardly objects. Overlap measures look only at the object: what fraction of the true region was found, and how much extra was claimed. Dice and IoU do exactly that, so turning them into losses makes small objects count.
| mean BCE over pixels | 0.0747 |
| soft Dice loss | 0.244 |
| soft IoU (Jaccard) loss | 0.391 |
| Tversky loss (α = 0.3, β = 0.7) | 0.246 |
| hard Dice score at 0.5 | 0.769 |
Crack and corrosion segmentation in inspection images, landing-zone and runway masks, cloud and burn-scar masks in satellite imagery, and road and obstacle masks for rovers are all evaluated by Dice or IoU and often involve small regions. Training directly on overlap, or combining it with cross-entropy, is standard practice.
- 1. Dice for a prediction identical to the truth is…
- 2. For a tiny object, mean pixel BCE is dominated by…
- 3. IoU is…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Dice: D = 2|P ∩ T|/(|P| + |T|). For truth (1, 1, 1, 0, 0, 0) and prediction (1, 1, 0, 1, 0, 0): the intersection is 2, so D = 4/6 ≈ 0.667.
- IoU: J = |P ∩ T|/|P ∪ T| = 2/4 = 0.5 for the same masks.
- Relation: J = D/(2 − D) and D = 2J/(1 + J). They rank predictions identically, but IoU is harsher: Dice 0.8 is IoU 0.667.
- Tversky: TI = TP/(TP + α·FP + β·FN). With α = β = 0.5 it is Dice; with α = β = 1, IoU. With α < β, missed object pixels cost more than extra ones, which raises recall.
- Soft versions: use probabilities instead of 0/1 predictions, e.g. soft Dice = 2Σpᵢtᵢ/(Σpᵢ + Σtᵢ). Losses are 1 − score.
- Smoothing: add s (often 1) to numerator and denominator, so empty masks give defined values and gradients stay finite.
- 1. IoU for Dice 0.8 is about…
- 2. Tversky with α = β = 0.5 equals…
- 3. To favour recall, Tversky uses…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Overlap losses in code
Exercise 1.
- For the masks above: Dice 0.667 and IoU 0.500, and the conversion J = D/(2 − D) holds.
- Tversky reduces to Dice and to IoU at the right weights.
- The analytic gradient of the smoothed soft Dice loss matches finite differences.
- With smoothing, an empty prediction of an empty mask scores 1.
Practice.
- Compute soft Dice over a whole batch (or per image, then average). Per-image Dice is noisy for images with tiny or no objects; batch Dice is steadier.
- Dice losses can have erratic gradients early on and ignore calibration. The common recipe is BCE + Dice: BCE gives smooth, per-pixel gradients, Dice gives the overlap objective.
- For very small structures, Tversky (α = 0.3, β = 0.7) or the focal Tversky loss push recall up.
- Report the evaluation metric (hard Dice or IoU at a threshold), not just the loss.
loss = F.binary_cross_entropy_with_logits(z, t) + (1 - (2 * (p * t).sum() + 1) / (p.sum() + t.sum() + 1))- 1. A common, stable segmentation loss is…
- 2. Smoothing in Dice prevents…
- 3. Per-image Dice is noisy for…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Why overlap losses resist imbalance. Mean pixel BCE is a sum over all pixels divided by the image size, so as the object shrinks, its contribution shrinks proportionally. Dice is a ratio of object-related quantities (TP, FP, FN). True negatives do not appear, so the score’s sensitivity to object pixels does not fall with image size. Missing half of a four-pixel object costs as much Dice as missing half of a four-thousand-pixel one.
Gradient. With I = Σpᵢtᵢ and U = Σpᵢ + Σtᵢ, the soft Dice loss gradient is −(2tᵢU − 2I)/U² (unsmoothed). For object pixels (tᵢ = 1) it pushes pᵢ up, and for background pixels it pushes pᵢ down, by the same amount for every pixel of each kind. The scale depends on U, so it is large when predicted and true regions are small, which can make early training erratic.
Metric consistency. Optimising the evaluation metric directly helps when the metric and the per-pixel loss disagree, as with small objects. The Lovász-softmax loss (Berman et al., 2018) is a convex surrogate for IoU with better optimisation properties. Dice losses give miscalibrated pixel probabilities, so they should not be used where calibrated per-pixel uncertainty is needed.
- 1. Which quantity does Dice ignore?
- 2. The Lovász-softmax loss is a surrogate for…
- 3. Dice-trained pixel probabilities are…
Level 4: Expert
★★★★★ Expert⏱ 25 minResearchers: original papers and open problems.Small objects: BCE or Dice?
History.
- L. Dice (1945) and T. Sørensen (1948) defined the coefficient for ecology; P. Jaccard (1901) the index.
- A. Tversky (1977) proposed the asymmetric similarity.
- F. Milletari, N. Navab and S.-A. Ahmadi (2016) introduced the soft Dice loss (V-Net).
- S. Salehi, D. Erdogmus and A. Gholipour (2017) introduced the Tversky loss.
- M. Berman, A. Rannen Triki and M. Blaschko (2018) introduced Lovász-softmax.
Exercise 2. Signals of length 200, each containing one object segment raised by 1 above unit Gaussian noise. Objects cover 2% (4 pixels) or 20% (40 pixels). A per-pixel logistic classifier, using the raw value and moving averages over 3 and 9 pixels, is trained by Adam on 64 signals with BCE, soft Dice, or BCE + Dice, and tested on 256 new signals.
| loss | object share | test hard Dice per seed | predicted size / true size |
|---|---|---|---|
| BCE | 2% | 0.084, 0.075, 0.072 | 0.09, 0.07, 0.06 |
| Dice | 2% | 0.256, 0.261, 0.276 | 0.84, 0.77, 0.88 |
| BCE + Dice | 2% | 0.255, 0.259, 0.267 | 0.73, 0.66, 0.65 |
| BCE | 20% | 0.828, 0.847, 0.837 | 0.90, 0.94, 0.93 |
| Dice | 20% | 0.831, 0.848, 0.837 | 0.94, 1.01, 0.96 |
| BCE + Dice | 20% | 0.831, 0.847, 0.839 | 0.93, 0.98, 0.96 |
- With 2% objects, BCE gave up on them. It flagged less than a tenth as many pixels as the objects contained, because with 98% background the BCE-optimal probability rarely crossed 0.5.
- Dice-based losses tripled the overlap score (0.26–0.28 against 0.07–0.08) by predicting objects of nearly the right size.
- With 20% objects all three losses tied (within 0.01). The overlap losses help specifically when objects are small relative to the image.
- BCE + Dice matched Dice alone on small objects while keeping BCE’s smoother per-pixel gradient. The absolute scores stay low because a 4-pixel object at signal-to-noise 1 is hard to localise exactly.
Overlap losses in AI, robotics and aerospace
- Inspection: crack, corrosion and delamination segmenters for airframes and bridges are trained with BCE + Dice or Tversky losses because defects are thin and rare.
- Remote sensing: cloud, water, burn-scar and building masks from satellites are evaluated by IoU; overlap losses improve small-region recall.
- Navigation: landing-zone and hazard masks for UAVs and planetary landers use Dice-based training when hazards occupy few pixels.
References
- L. R. Dice (1945). “Measures of the amount of ecologic association between species.” Ecology 26(3).
- A. Tversky (1977). “Features of similarity.” Psychological Review 84(4).
- F. Milletari, N. Navab, S.-A. Ahmadi (2016). “V-Net: fully convolutional neural networks for volumetric medical image segmentation.” Proc. 3DV.
- S. S. M. Salehi, D. Erdogmus, A. Gholipour (2017). “Tversky loss function for image segmentation using 3D fully convolutional deep networks.” MLMI Workshop.
- M. Berman, A. Rannen Triki, M. B. Blaschko (2018). “The Lovász-softmax loss.” Proc. CVPR.