The Dice score (also called the Dice coefficient, or F1 score for segmentation) is IoU's close mathematical cousin, most commonly used to evaluate pixel-level segmentation masks rather than bounding boxes.
Formula
\(A\) is the predicted segmentation mask, \(B\) is the ground-truth mask. Compare directly to IoU's formula (\(\frac{|A\cap B|}{|A\cup B|}\)): Dice doubles the intersection and divides by the sum of both regions' sizes, rather than their union.
The Exact Relationship to IoU
These two metrics are monotonically related โ Dice is always \(\ge\) IoU for the same pair of regions, and a model that improves one will always improve the other. Which one to report is often a matter of the specific field's convention rather than a meaningful difference in what's being measured (medical imaging tends to favor Dice; general computer vision benchmarks tend to favor IoU).
Numerical Example
Reusing the IoU worked example: two 40ร40 regions (area 1600 each), overlap area 400.
Compare to the earlier IoU value of \(\approx0.143\) for the same regions โ confirming Dice \(>\) IoU here, consistent with the general relationship above.
Code
def dice_score(maskA, maskB):
intersection = (maskA & maskB).sum()
return 2 * intersection / (maskA.sum() + maskB.sum())
import numpy as np
mask_pred = np.array([[1,1,0],[1,0,0],[0,0,0]], dtype=bool)
mask_true = np.array([[1,0,0],[1,1,0],[0,0,0]], dtype=bool)
print(dice_score(mask_pred, mask_true))
Why Dice Is Popular in Medical Imaging
Medical segmentation tasks (like tumor or organ boundary detection) often involve small structures relative to the full image โ the regions being segmented are frequently a small fraction of total pixels. Dice's formula, weighting the intersection more heavily relative to the union, tends to be somewhat more forgiving and interpretable for these small, imbalanced-region segmentation tasks, which partly explains its strong association with medical imaging benchmarks specifically.
Common Mistakes
- Treating Dice and IoU as measuring fundamentally different things โ they're monotonically related transformations of the same underlying overlap information, not independent metrics capturing different aspects of quality.
- Comparing a Dice score directly against an IoU threshold (or vice versa) without converting between them first, given their different numeric scales for the same underlying overlap.
Interview Relevance
Q: "Are Dice score and IoU measuring different things, or the same thing differently?" The same underlying overlap information, expressed with different formulas โ they're monotonically related (\(\text{Dice}=\frac{2\times\text{IoU}}{1+\text{IoU}}\)), so a model that improves one always improves the other. The choice between them is largely a matter of field convention (Dice in medical imaging, IoU in general object detection) rather than a meaningful difference in what's actually being evaluated.
Practice Question
Given an IoU of 0.6, compute the corresponding Dice score using the conversion formula.