What is the Dice coefficient in image segmentation?
The Dice coefficient (Dice similarity score) measures how closely a predicted segmentation mask matches the ground-truth mask. It is an overlap metric commonly used in medical image segmentation, especially when the target region is small compared with the background.
Dice coefficient formula
`Dice = (2 × overlapping pixels) / (predicted target pixels + ground-truth target pixels)`
Equivalently, `Dice = 2TP / (2TP + FP + FN)`, where:
- TP (true positives): Target pixels correctly included in the predicted mask.
- FP (false positives): Background pixels incorrectly predicted as target.
- FN (false negatives): Target pixels missed by the prediction.
The coefficient ranges from 0 to 1 for non-empty masks: 1 means perfect overlap; 0 means no overlap.
Beginner-friendly numerical example
Imagine a model highlighting a leaf in a photograph:
- Ground-truth leaf region: 80 pixels.
- Predicted leaf region: 70 pixels.
- Correctly overlapping pixels (TP): 60.
- Extra predicted pixels (FP):
70 − 60 = 10. - Missed leaf pixels (FN):
80 − 60 = 20.
`Dice = (2 × 60) / (80 + 70) = 120 / 150 = 0.80`
So the model's Dice coefficient is 0.80 (80%) for the leaf mask. This is a score of agreement between the two regions, not 80% pixel accuracy over the entire photograph.
Why use Dice for segmentation?
- Focus on the target region: Correct background pixels (true negatives) do not appear in the formula. This is helpful when most pixels are background.
- Easy to interpret: Higher scores indicate better overlap between predicted and reference masks.
- Equivalent to pixel-level F1: For a binary target mask, the Dice coefficient equals the F1 score calculated from TP, FP, and FN.
How does Dice relate to IoU?
Both evaluate overlap, but use different denominators. For the same non-empty masks, `Dice = 2 × IoU / (1 + IoU)`. Their values differ, so a Dice score should not be reported as an IoU score. The IoU calculation is covered in the previous question.
Practical caveats: For multiclass segmentation, calculate Dice per class and specify how scores are averaged and whether background is included. If both the predicted and ground-truth masks are empty, the basic formula gives `0 / 0`; libraries may ignore that case or apply a defined convention. State your evaluation policy.
Key takeaway: Dice tells you how strongly two segmentation regions agree by comparing twice their overlap with their combined sizes.
Related: “What is Intersection over Union (IoU) in image segmentation?” The following question explains Dice loss versus cross-entropy loss for training.
References: MathWorks — Dice similarity coefficient, MONAI — DiceMetric, and Metrics for evaluating 3D medical image segmentation (BMC Medical Imaging, 2015).
Sources & Research References
Learning Sequence
Deep Learning Curriculum
Explore Related Questions
Deepen your understanding across related Deep Learning topics.