Loss functions

Segmentation Losses

DiceLoss

class DiceLoss(include_background=True, to_onehot_y=False, do_sigmoid=False, do_softmax=False, squared_pred=False, jaccard=False)[source]

Compute average Dice loss between two tensors. It can support both multi-classes and multi-labels tasks. Input logits pred (BNHW[D] where N is number of classes) is compared with ground truth ground’ (BNHW[D]). Axis N of `pred is expected to have logit predictions for each class rather than being image channels, while the same axis of ground can be 1 or N(one-hot format). The smooth parameter is a value added to the intersection and union components of the inter-over-union calculation to smooth results and prevent divide by 0, this value should be small. The include_background class attribute can be set to False for an instance of DiceLoss to exclude the first category (channel index 0) which is by convention assumed to be background. If the non-background segmentations are small compared to the total image size they can get overwhelmed by the signal from the background so excluding it in such cases helps convergence.

Parameters
  • include_background (bool) – If False channel index 0 (background category) is excluded from the calculation.

  • to_onehot_y (bool) – whether to convert y into the one-hot format. Defaults to False.

  • do_sigmoid (bool) – If True, apply a sigmoid function to the prediction.

  • do_softmax (bool) – If True, apply a softmax function to the prediction.

  • squared_pred (bool) – use squared versions of targets and predictions in the denominator or not.

  • jaccard (bool) – compute Jaccard Index (soft IoU) instead of dice or not.

forward(pred, ground, smooth=1e-05)[source]
Parameters
  • pred (tensor) – the shape should be BNH[WD].

  • ground (tensor) – the shape should be BNH[WD].

  • smooth (float) – a small constant to avoid nan.

GeneralizedDiceLoss

class GeneralizedDiceLoss(include_background=True, to_onehot_y=False, do_sigmoid=False, do_softmax=False, w_type='square')[source]

Compute the generalised Dice loss defined in:

Sudre, C. et. al. (2017) Generalised Dice overlap as a deep learning loss function for highly unbalanced segmentations. DLMIA 2017.

Adapted from:

https://github.com/NifTK/NiftyNet/blob/v0.6.0/niftynet/layer/loss_segmentation.py#L279

Parameters
  • include_background (bool) – If False channel index 0 (background category) is excluded from the calculation.

  • to_onehot_y (bool) – whether to convert y into the one-hot format. Defaults to False.

  • do_sigmoid (bool) – If True, apply a sigmoid function to the prediction.

  • do_softmax (bool) – If True, apply a softmax function to the prediction.

  • w_type ('square'|'simple'|'uniform') – type of function to transform ground truth volume to a weight factor.

forward(pred, ground, smooth=1e-05)[source]
Parameters
  • pred (tensor) – the shape should be BNH[WD].

  • ground (tensor) – the shape should be BNH[WD].

  • smooth (float) – a small constant to avoid nan.