---
kind: concept
slug: dice-loss
title: Dice loss
summary: >-
  One minus an overlap score between predicted and true masks, computed on probabilities. Five cards cover soft Dice, Dice added to cross-entropy or binary cross-entropy, and a soft-Jaccard loss that is a sibling, not Dice. Overlap alone can behave badly on small objects, so the cards pair it with cross-entropy. No card ablates the Dice term.
tags: [loss-function, segmentation, class-imbalance]
papers: ["1803.01207", "1910.07655", "2108.00882", "2207.08518", "2307.02694"]
related: [focal-loss, semantic-segmentation, segmentation-evaluation]
status: draft
updated_at: 2026-11-01
---

## Overview

Dice loss scores how much a predicted mask overlaps the true mask and
turns that overlap into a loss. The Dice coefficient for a binary mask pair is
$2\,TP / (2\,TP + FP + FN)$ [arxiv:1910.07655 §9.1]. The loss is one minus
the coefficient. The review that defines the formula prints the coefficient
and calls it the loss; the loss is $1 - \text{coefficient}$
[arxiv:1910.07655 §4.4.1]. A second survey gives the same set-form definition
and the same $1 - \text{DSC}$ loss, but does not write the probability form
[arxiv:2307.02694 §6.1.2].

To train with it, the cards replace the hard counts with predicted
probabilities, so the score is differentiable. Each pixel contributes its
probability times its target, and the denominator is the sum of the two masks
[arxiv:1910.07655 §4.4.1]. Because the score is taken over the whole region,
it does not weight each pixel by how confident it is. This is the reason it
helps with class imbalance, and also the reason it can misbehave on small
objects [arxiv:1910.07655 §4.4] [arxiv:1910.07655 §10.3].

The cards use the loss in four shapes: plain soft Dice
[arxiv:1910.07655 §4.4.1], Dice added to binary cross-entropy in a polyp
segmenter [arxiv:2108.00882 §3.4], Dice combined with cross-entropy in a
hybrid CNN-transformer for organ segmentation, where the paper does not state
the loss and the released code sets the weights [code: trainer.py#L129-L131],
and a soft-Jaccard term combined with cross-entropy for surgical-tool
segmentation [arxiv:1803.01207 §2.3]. The last one is an intersection-over-union
score, not Dice. The Tversky and exponential-logarithmic variants are
described in the surveys, but no card here trains with them
[arxiv:1910.07655 §4.4.2] [arxiv:1910.07655 §4.4.3] [arxiv:2307.02694 §6.1].

Dice and the Jaccard index are both overlap scores, and they are monotone in
each other. From the two set formulas, $D = 2J / (1 + J)$ and
$J = D / (2 - D)$. This is derived on this page from the definitions above;
no card states it.

## Variants

### Soft Dice on probabilities

The basic form. Sum the product of prediction and target over pixels, and
divide by the sum of both masks. The survey writes it without a smoothing
term [arxiv:1910.07655 §4.4.1]. The review's exponential-logarithmic variant
adds a smoothing constant to the numerator and the denominator
[arxiv:1910.07655 §4.4.3]. The second survey gives the set form only, so it
does not settle how to compute the score on probabilities
[arxiv:2307.02694 §6.1.2]. Card: [Deep Semantic Segmentation of Natural and Medical Images: A Review](/papers/1910.07655).

### Dice with binary cross-entropy

The polyp segmenter trains on the sum of binary cross-entropy and Dice loss,
with both weights set to 1 [arxiv:2108.00882 eq. 5] [arxiv:2108.00882 §3.4].
The paper does not define the Dice formula. The code computes it per image on
sigmoid probabilities, with a $+1$ term in the denominator only, and averages
the per-image scores [code: src/train.py#L61-L66] [arxiv:2108.00882 §3.4]. Card:
[Shallow Attention Network for Polyp Segmentation](/papers/2108.00882).

### Dice with cross-entropy, squared-denominator form

The hybrid transformer segments 9 classes and reports Dice and Hausdorff
distance on Synapse [arxiv:2207.08518 tab. 2]. The paper does not state its
training loss. The released code uses 0.4 times cross-entropy plus 0.6 times
Dice [code: trainer.py#L131], and its Dice squares the predictions and targets
in the denominator, with a $10^{-5}$ term [code: utils.py#L566-L570]. The
background class is included in the average [code: utils.py#L583-L587]. Card:
[HiFormer: Hierarchical Multi-scale Representations Using Transformers for Medical Image Segmentation](/papers/2207.08518).

### Soft Jaccard with cross-entropy (sibling, not Dice)

The surgical-tool segmenter does not use Dice in its training loss. It trains
on $H - \log J$, where $H$ is the cross-entropy and $J$ is a soft Jaccard
index, both weighted 1 [arxiv:1803.01207 eq. 3] [arxiv:1803.01207 §2.3]. The
per-pixel Jaccard is $\frac{1}{n}\sum_i y_i\hat{y}_i / (y_i + \hat{y}_i -
y_i\hat{y}_i)$ [arxiv:1803.01207 eq. 2]. Its code uses $(1-a)H - a\log J$,
takes one ratio of sums over the batch on sigmoid probabilities, and sets $a$
to 0.5 by default [code: loss.py#L18] [code: loss.py#L25-L28]. The paper
reports Dice as a metric in its table, not as a loss [arxiv:1803.01207 tab. 1].
Card: [Robot Instrument Segmentation with Cross-Entropy and Jaccard Losses](/papers/1803.01207).

### Tversky-type overlap

The survey describes a generalisation with a parameter $\beta$ that weights
false negatives against false positives, and notes that $\beta = 0.5$ reduces
to Dice [arxiv:1910.07655 §4.4.2]. The survey's plots use $\beta = 0.3$
[arxiv:1910.07655 §4.4]. The card's formula is typeset with mismatched
brackets, so do not implement it from the survey text alone
[arxiv:1910.07655 §4.4.2]. A combined cross-entropy and Dice loss, with
weights that control false positives and false negatives, is also named there
[arxiv:1910.07655 §5]; its weights are not stated. The second survey also
covers a Tversky loss in set form [arxiv:2307.02694 §6.1].

## When to use which

| Situation | Use | Why |
|---|---|---|
| Foreground is a large share of the image, and the cross-entropy loss is slow to learn region shape | Dice, or Dice plus cross-entropy | The survey presents overlap losses as a way to handle class imbalance [arxiv:1910.07655 §4] [arxiv:1910.07655 §10.3]. |
| Small foreground objects | Dice combined with cross-entropy, not Dice alone | With one false negative on a small object, the survey's example drops Dice to 0.66, against 0.96 on a large object; cross-entropy gives 0.83 for both. These are illustrative numbers [arxiv:1910.07655 §4.4] [arxiv:1910.07655 §10.3]. |
| Binary segmentation with a per-pixel head, and you want a fixed recipe | Binary cross-entropy plus Dice, both weights 1 | The polyp card uses this recipe [arxiv:2108.00882 §3.4]. It gives no ablation of the Dice term alone. |
| Multi-class medical segmentation, matching a published baseline | Cross-entropy plus Dice, with the code's 0.4 / 0.6 split if you need to match the reported numbers | The paper does not state the loss; the code's weights are the only ones in the cards [code: trainer.py#L129-L131]. |
| Binary or few-class instrument segmentation, and you want an overlap term that is an IoU | Cross-entropy plus soft Jaccard, with the paper's equal weights or the code's $a$ | The card reports this recipe for surgical tools [arxiv:1803.01207 §2.3]. The card gives no comparison against Dice. |
| Penalising missed foreground more than false alarms | A Tversky-type overlap, or a weighted cross-entropy plus Dice | The survey says $\beta$ controls the trade-off, but gives no value for the combo loss [arxiv:1910.07655 §5]. Tune $\beta$ on validation data. |
| Need a metric that counts pixels, not overlap | Not Dice; use cross-entropy or a pixel-level loss | The survey notes that overlap and pixel-count measures are different [arxiv:1910.07655 §9.1]. |

## Implementation sketch

The common core is one minus a smoothed overlap ratio on probabilities. The
polyp and hybrid cards differ only in the smoothing term and the denominator.
The soft-Jaccard card uses a different denominator, $\sum(y + \hat{y} -
y\hat{y})$, and is not the Dice form.

```pseudocode
# Soft Dice loss, one class  [arxiv:1910.07655 §4.4.1]
# probs: predicted foreground probabilities (N,) in [0, 1]
# target: 0/1 ground-truth mask (N,)
# eps: smoothing; 0 reproduces the plain formula  [§4.4.1] [§4.4.3]

def soft_dice_loss(probs, target, eps=0.0):
    # Overlap: sum of prediction times target over pixels  [§4.4.1]
    intersection = sum(target * probs)
    # Denominator: the two masks' sums added together  [§4.4.1]
    total = sum(target) + sum(probs)
    # The loss is one minus the coefficient, not the coefficient  [§4.4.1]
    return 1 - (2 * intersection + eps) / (total + eps)
```

```pseudocode
# Combined loss used in the polyp card  [arxiv:2108.00882 §3.4] [eq. 5]
# logits, mask: (H, W) per image; mask holds 0 or 1
# Smoothing of 1 in the denominator is the code's choice  [code: src/train.py#L66]

def bce_plus_dice(logits, mask, weight_bce=1.0, weight_dice=1.0):
    # Binary cross-entropy on the raw logits  [eq. 5]
    bce = binary_cross_entropy_with_logits(logits, mask)
    # Dice on the sigmoid probabilities  [code: src/train.py#L61-L66]
    probs = sigmoid(logits)
    dice = soft_dice_loss(probs, mask, eps=1.0)
    # Weighted sum; the cards use equal weights  [eq. 5]
    return weight_bce * bce + weight_dice * dice
```

## Contradictions

None of the numeric results conflict, because no two cards report the same
setting. The weights differ: the polyp card uses 1 to 1 [arxiv:2108.00882
§3.4], and the hybrid transformer's code uses 0.4 to 0.6 [code: trainer.py#L131].
The hybrid transformer's paper does not state its loss, so its weights come
from code alone [code: trainer.py#L129-L131]. The two smoothing choices also
differ, a $+1$ in the denominator only [code: src/train.py#L66] against a
$10^{-5}$ term in a squared denominator [code: utils.py#L566-L570]. The
soft-Jaccard card's own paper and code disagree on the weights: the paper
weights cross-entropy and $-\log J$ equally [arxiv:1803.01207 eq. 3], while the
code's default $a = 0.5$ and the released script's $a = 0.3$ give other
weights [code: loss.py#L18] [code: train.sh#L12]. Its paper gives a threshold
of 0.3, which the code does not use [arxiv:1803.01207 §2.3]. The survey's own
worked example is the only evidence on small objects, and it is illustrative
[arxiv:1910.07655 §4.4].

## Papers

- [Automatic Instrument Segmentation in Robot-Assisted Surgery Using Deep Learning](/papers/1803.01207): cross-entropy plus a soft-Jaccard term for surgical tools; a sibling of Dice, with Dice reported only as a metric.
- [Deep Semantic Segmentation of Natural and Medical Images: A Review](/papers/1910.07655): survey definition of the Dice coefficient and loss, the Tversky and exponential-logarithmic variants, and the small-object example.
- [Shallow Attention Network for Polyp Segmentation](/papers/2108.00882): binary cross-entropy plus Dice with equal weights; the Dice formula is taken from the code.
- [HiFormer: Hierarchical Multi-scale Representations Using Transformers for Medical Image Segmentation](/papers/2207.08518): cross-entropy plus Dice in the code, with the paper not stating the loss.
- [Loss Functions and Metrics in Deep Learning](/papers/2307.02694): set-form Dice and Tversky losses in the segmentation section, with the Dice coefficient given without a probability form.
