mindspore.nn.DiceLoss

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

The Dice coefficient is a set similarity loss, which 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 is 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| + |true| + smooth}\]

\(pred\) represent logits, \(true\) represent labels .

Parameters

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

Inputs:
  • logits (Tensor) - Input predicted value. The data type must be float16 or float32.

  • labels (Tensor) - Input target value. 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 is not a tensor.

Supported Platforms:

Ascend GPU CPU

Examples

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