im2sim.losses.DiceLoss

Contents

im2sim.losses.DiceLoss#

class DiceLoss(epsilon=1e-05, average='macro', class_weights=None, reduction='mean')[source]#

Bases: TverskyLoss

Dice loss, also known as F1 loss.

The Dice loss is:

\[L = \left( 1 - \frac{\mathrm{TP} + \epsilon} {\mathrm{TP} + 0.5 * \mathrm{FP} + 0.5 * \mathrm{FN} + \epsilon} \right)\]
Parameters:
  • epsilon (float) – Smoothing factor. Default is 1e-5.

  • average (str) – Class averaging strategy. Default is "macro". One of "micro", "macro", or "weighted". "micro": Calculate metrics globally by counting the total true positives, false negatives and false positives. "macro": Calculate metrics for each class, and find their unweighted mean. This does not take class imbalance into account. "weighted": Calculate metrics for each class, and find their average weighted by support (the number of true instances for each class).

  • class_weights (Tensor) – Optional class weights. Default is None. If provided, should be a 1D tensor of shape [C] or a 2D tensor of shape [B, C].

  • reduction (str) – Batch reduction strategy. Default is “mean”. One of "none", "mean", or "sum".

Note

Inputs y_true and y_pred are expected to have shape [Batch, Channels, *Spatial], with channel i containing labels/predictions for class i. y_true[:, i, ...] is 1 if the element represented by y_true[...] is a member of class i and 0 otherwise. y_pred[:,i,...] is the predicted probability, in the range [0.0, 1.0], that the element represented by y_pred[...] is a member of class i.

The loss is computed for each batch element y_true[i, ...] and y_pred[i, ...], and then reduced over this dimension as specified by argument reduction.

This loss works for binary, multiclass and multilabel classification and/or segmentation. In multiclass/multilabel problems, the different classes are combined according to the average and class_weights arguments.