mindspore.nn.DiceLoss

class mindspore.nn.DiceLoss(smooth=1e-05)[source]

The Dice coefficient is a set similarity loss. It is used to calculate the similarity between two samples. The value of the Dice coefficient is 1 when the segmentation result is the best and 0 when the segmentation result is the worst. The Dice coefficient indicates the ratio of the area between two objects to the total area. The function is shown as follows:

\[dice = 1 - \frac{2 * (pred \bigcap true)}{pred \bigcup true}\]
Parameters

smooth (float) – A term added to the denominator to improve numerical stability. Should be greater than 0. Default: 1e-5.

Inputs:
  • logits (Tensor) - Tensor of shape \((N, *)\) where \(*\) means, any number of additional dimensions. The data type must be float16 or float32.

  • labels (Tensor) - Tensor of shape \((N, *)\), same shape as the logits. The data type must be float16 or float32.

Outputs:

Tensor, a tensor of shape with the per-example sampled Dice losses.

Raises
  • ValueError – If the dimension of logits is different from labels.

  • TypeError – If the type of logits or labels are not Tensor.

Supported Platforms:

Ascend GPU CPU

Examples

>>> loss = nn.DiceLoss(smooth=1e-5)
>>> logits = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mstype.float32)
>>> labels = Tensor(np.array([[0, 1], [1, 0], [0, 1]]), mstype.float32)
>>> output = loss(logits, labels)
>>> print(output)
0.38596618