Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Deep Learning Notes
Topic #347

Dice Score

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

\[ \text{Dice} = \frac{2|A\cap B|}{|A|+|B|} \]

\(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

\[ \text{Dice} = \frac{2\times\text{IoU}}{1+\text{IoU}}, \qquad \text{IoU} = \frac{\text{Dice}}{2-\text{Dice}} \]

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.

\[ \text{Dice} = \frac{2\times400}{1600+1600} = \frac{800}{3200} = 0.25 \]

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.

Related DL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →