Calculate the Dice coefficient to evaluate overlap in image segmentation tasks.
The Dice coefficient (F1 for sets) measures overlap between predicted and ground truth segmentation masks: Dice = 2×|Intersection| / (|Prediction| + |Ground Truth|). It ranges from 0 (no overlap) to 1 (perfect overlap) and is the standard metric for medical image segmentation tasks.
Dice Coefficient
Dice = 2 * |X ∩ Y| / (|X| + |Y|)
Dice = 2*IoU / (1 + IoU). Dice is always ≥ IoU for the same prediction. Both measure overlap, but Dice gives more credit for partial overlap and is preferred in medical imaging where class imbalance is severe.
In segmentation, the background class often dominates (>95% of pixels), making pixel accuracy misleading. Dice focuses exclusively on the overlap of the foreground class of interest.