Dice loss scores how badly a predicted segmentation mask overlaps the true mask: 0 means perfect overlap and 1 means none at all. It is a standard training loss for medical image segmentation and for any task where the object of interest covers only a small part of the image. This page gives the formula, a calculator that shows every step, a worked example by hand, and how the measure relates to the F1 score and IoU.
What is Dice loss?
It is one minus the soft Dice coefficient. For each pixel \( i \), let \( p_i \) be the predicted probability that the pixel belongs to the object (a number between 0 and 1, usually the output of a sigmoid) and let \( g_i \) be the ground-truth label (0 or 1). Then:
\[ D = \frac{2\sum_i p_i g_i + \epsilon}{\sum_i p_i + \sum_i g_i + \epsilon}, \qquad L_{\text{Dice}} = 1 – D \]
The numerator is twice the soft intersection, the overlap between the prediction and the mask. The denominator is the total size of both. \( \epsilon \) is a small smoothing constant that prevents 0/0 when the prediction and the mask are both empty. The value used in papers and libraries varies (1, \( 10^{-5} \) and \( 10^{-6} \) are all common), so check the code you are copying.
With hard 0/1 predictions and \( \epsilon = 0 \), the intersection is just the count of true positives and the formula becomes \( D = \frac{2TP}{2TP + FP + FN} \). That is exactly the F1 score computed from the confusion matrix of pixels. The coefficient is also tied to Intersection over Union (the Jaccard index): \( J = D / (2 – D) \) and \( D = 2J / (1 + J) \), so ranking models by Dice or by IoU gives the same order. The coefficient itself comes from ecology; see the Sørensen–Dice coefficient on Wikipedia.
Soft Dice calculator
Paste the predicted probabilities and the true mask, one value per pixel in the same order. The defaults are the five-pixel example worked through below.
Worked example: five pixels by hand
Take a tiny image of five pixels. Three belong to the object (\( g = 1 \)) and two are background. The model outputs these probabilities:
| Pixel | p | g | p × g |
|---|---|---|---|
| 1 | 0.9 | 1 | 0.9 |
| 2 | 0.8 | 1 | 0.8 |
| 3 | 0.3 | 0 | 0 |
| 4 | 0.1 | 0 | 0 |
| 5 | 0.6 | 1 | 0.6 |
| Sum | 2.7 | 3 | 2.3 |
- Soft intersection: \( \sum p_i g_i = 0.9 + 0.8 + 0.6 = 2.3 \). Pixels 3 and 4 add nothing because their label is 0.
- Sizes: \( \sum p_i = 2.7 \) and \( \sum g_i = 3 \). The 0.3 on background pixel 3 still counts here, which is how false positives are penalised.
- Coefficient with \( \epsilon = 1 \): \( D = \frac{2 \times 2.3 + 1}{2.7 + 3 + 1} = \frac{5.6}{6.7} = 0.8358 \).
- Loss: \( L = 1 - 0.8358 = 0.1642 \).
Set the smoothing to 0 and the answer moves: \( D = 4.6 / 5.7 = 0.8070 \), so the loss is 0.1930. On five pixels \( \epsilon = 1 \) is a big nudge; on a large mask it barely registers. The matching soft IoU (with \( \epsilon = 0 \)) is \( 0.8070 / (2 - 0.8070) = 0.6765 \), the same as computing it directly: \( 2.3 / (2.7 + 3 - 2.3) = 2.3 / 3.4 \).
When to use Dice loss and when not to
Use it for segmentation where the foreground is small. In a brain scan a tumour may cover a tiny fraction of the voxels. Pixel-wise binary cross-entropy averages over every pixel, so a model that predicts background everywhere already earns a low loss. The Dice formula ignores true negatives entirely: background predicted as background never appears in it. The loss only falls when the predicted region and the true region overlap, which handles class imbalance without hand-tuned weights. It was popularised for volumetric segmentation by V-Net (Milletari et al., 2016), whose version squares the terms in the denominator: \( \sum p_i^2 + \sum g_i^2 \). Both forms are used, and they give different numbers for soft predictions.
Avoid it, or combine it with something else, when:
- You need calibrated probabilities. Dice rewards overlap, not honest confidence, so outputs tend to be pushed towards 0 or 1.
- Many images have empty or tiny masks. One missed pixel can swing the loss from near 0 to near 1, which makes gradients noisy.
- The task is ordinary classification with one label per example. Use cross-entropy loss instead.
A common compromise is to add the two: \( L = L_{\text{BCE}} + L_{\text{Dice}} \). Cross-entropy gives smooth per-pixel gradients early in training, and the overlap term stops small structures from being ignored. Focal loss is another way to deal with imbalance.
Dice loss vs cross-entropy and IoU loss
| Binary cross-entropy | Dice loss | IoU (Jaccard) loss | |
|---|---|---|---|
| Formula | \( -\frac{1}{N}\sum_i [g_i \ln p_i + (1-g_i)\ln(1-p_i)] \) | \( 1 - \frac{2\sum p_i g_i}{\sum p_i + \sum g_i} \) | \( 1 - \frac{\sum p_i g_i}{\sum p_i + \sum g_i - \sum p_i g_i} \) |
| Works on | Each pixel, then averaged | The whole mask | The whole mask |
| Counts true negatives | Yes | No | No |
| Small foreground | Struggles without class weights | Handles it well | Handles it well |
| Gradients | Smooth and stable | Noisy when masks are tiny | Noisy when masks are tiny |
| Matching metric | Log-likelihood | Dice score / F1 | IoU / Jaccard index |
| Range | 0 to infinity | 0 to 1 | 0 to 1 |
Dice and IoU losses move together, because one coefficient is an increasing function of the other, but for partial overlap the IoU loss is always the larger (0.3235 against 0.1930 in the example). The chart near the top shows this for a single foreground pixel: the Dice curve is \( (1-p)/(1+p) \) and the IoU curve is \( 1 - p \). For a wider map of options, see the guide to loss functions.
Common mistakes
- Passing logits instead of probabilities. The formula expects values from 0 to 1. Apply a sigmoid for binary masks, or a softmax across classes, first.
- Dropping the smoothing term. An empty mask with an all-zero prediction gives 0/0, and the resulting NaN ruins training. Libraries handle this case differently; the calculator treats it as a perfect match (loss 0).
- Mixing up batch and per-image Dice. Summing over the whole batch before dividing gives a different number from computing Dice per image and averaging. Neither is wrong, but compare like with like.
- Averaging classes carelessly. For multi-class segmentation, compute the coefficient per class and average. Including the large background class inflates the score.
- Reporting soft Dice as the evaluation metric. For evaluation, threshold the predictions (often at 0.5) and compute the hard Dice score, which equals the F1 score built from pixel-level precision and recall.
Frequently asked questions
Is Dice loss the same as 1 minus the F1 score?
With hard 0/1 predictions, yes: the Dice coefficient equals the F1 score. The soft version replaces counts with probabilities so that it is differentiable and can be trained with gradient descent.
What smoothing value should I use?
There is no single standard. A value of 1 is common in tutorials, while many library implementations use something tiny such as \( 10^{-5} \). A large value softens the loss on small masks; a tiny one only prevents division by zero.
Can Dice loss be negative or above 1?
Not with probabilities between 0 and 1 and labels of 0 or 1. The coefficient then lies between 0 and 1, so the loss does too. Values outside that range usually mean raw logits were passed in.
Why does my Dice loss stay near 1 early in training?
If the network predicts almost nothing for a small object, the intersection is close to zero and so is the coefficient. Adding a cross-entropy term usually gets training moving, because it gives every pixel a useful gradient from the first step.
How do I convert a Dice score to IoU?
Use \( J = D / (2 - D) \). A Dice score of 0.8 corresponds to an IoU of about 0.6667, and an IoU of 0.5 corresponds to a Dice score of about 0.6667.