dice coefficient
The Dice coefficient measures how well two masks overlap, on a scale from 0 (no overlap) to 1 (identical). The intuition: take the area both masks share, and compare it to their average size. Formally, for a predicted mask A and a ground-truth mask B, Dice = 2·|A∩B| / (|A| + |B|), where |·| is the number of pixels in a region and ∩ is intersection. The factor of 2 is there so that a perfect match gives exactly 1.
Dice is the same number as the F1 score when you view segmentation as per-pixel binary classification. Writing TP, FP, FN for true positives (correctly predicted foreground pixels), false positives, and false negatives, Dice = 2·TP / (2·TP + FP + FN), which is exactly F1 = harmonic mean of precision and recall. It is also tightly tied to Intersection-over-Union: Dice = 2·IoU/(1+IoU) and IoU = Dice/(2−Dice). The two always agree on ranking (whenever one is higher, so is the other), but Dice is more forgiving, always reporting a higher number than IoU except at the endpoints 0 and 1.
Beyond a metric, a smooth version of Dice is widely used as a loss function (soft Dice loss), especially in medical imaging. Its great virtue is robustness to class imbalance: because it depends only on the foreground overlap and ignores the sea of correctly classified background, a tiny lesion contributes as much as a large organ, unlike pixel-wise cross-entropy which the background can swamp. A common recipe combines cross-entropy and Dice loss to get stable gradients early and overlap-aware optimization later.
Truth = 100 px, prediction = 80 px, overlap = 70 px. Dice = 2·70/(100+80) = 0.78; IoU = 70/(100+80−70) = 0.64. Check: 2·0.64/(1+0.64) = 0.78. Dice flatters the same result.