ai4med.components.losses package

3.1
class BinaryClassificationLoss

Bases: ai4med.components.losses.loss.Loss

get_loss(predictions, targets, build_ctx=None)

Compute binary classification loss for tf variable

Parameters
  • predictions (Tensor) – outputs of the network

  • targets (Tensor) – target integer labels

  • build_ctx – specified graph context

Returns

cross entropy loss

class MulticlassClassificationLoss

Bases: ai4med.components.losses.loss.Loss

get_loss(predictions, targets, build_ctx=None)

Compute multiclass classification loss for tf variable

Parameters
  • predictions (Tensor) – outputs of the network

  • targets (Tensor) – target integer labels

  • build_ctx – specified graph context

Returns

vector of multiple cross entropy losses

class WeightedMulticlassClassificationLoss(class_weights=None)

Bases: ai4med.components.losses.loss.Loss

Weighted multiclass classification loss :param class_weights (list): weight for each class

Note: Uses tf.losses.sparse_softmax_cross_entropy with weighting. If no weights provided, standard MulticlassClassificationLoss will be used.

get_loss(predictions, targets, build_ctx=None)

Compute multiclass classification loss for tf variable

Parameters
  • predictions (Tensor) – outputs of the network

  • targets (Tensor) – target integer labels

  • build_ctx – specified graph context

Returns

vector of multiple cross entropy losses

class CrossEntropy(data_format='channels_first', skip_background=False, smooth=1e-08, is_onehot_targets=False)

Bases: ai4med.components.losses.loss.Loss

Compute average cross entropy loss between two tensors.

5D tensors (for 3D images) or 4D tensors (for 2D images).

For more details, check hybrid loss for segmentation Wentao Zhu et al., AnatomyNet: Deep Learning for Fast and Fully Automated Whole-volume Segmentation of Head and Neck Anatomy, Medical Physics, 2018.

Parameters
  • data_format (str) – channels_first (default) or channels_last

  • skip_background (bool) – skip dice computation on the first channel of the predicted output (skipping dice on background class) or not

  • smooth (float) – constant to avoid log zero (default 1e-8)

  • is_onehot_targets (bool) – labels with One-Hot format or not

Returns

float tensor of cross entropy loss

get_loss(predictions, targets, build_ctx=None)

Compute cross entropy loss for tf variable

Parameters
  • predictions (Tensor) – outputs of the network

  • targets (Tensor) – target integer labels

  • build_ctx – specified graph context

Returns

cross entropy loss

class CrossEntropyDice(lambda_dice=1, lambda_cross_entropy=1, data_format='channels_first', skip_background=False, squared_pred=False, jaccard=False, dice_smooth=1e-05, top_smooth=0.0, is_onehot_targets=False, cross_entropy_smooth=1e-08, dice_loss_name='dice_loss', print_dice_loss=True, summarize_dice_loss=True, ce_loss_name='ce_loss', print_ce_loss=True, summarize_ce_loss=True)

Bases: ai4med.components.losses.loss.Loss

Hybrid loss between cross entropy loss and dice loss.

5D tensors (for 3D images) or 4D tensors (for 2D images).

For more details, check hybrid loss for segmentation Wentao Zhu et al., AnatomyNet: Deep Learning for Fast and Fully Automated Whole-volume Segmentation of Head and Neck Anatomy, Medical Physics, 2018.

Parameters
  • lambda_dice (float) – trade-off value of dice loss

  • lambda_cross_entropy (float) – trade-off value of cross entropy loss

  • data_format (str) – channels_first (default) or channels_last

  • skip_background (bool) – skip dice computation on the first channel of the predicted output or not

  • 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

  • dice_smooth (float) – denominator constant to avoid zero division (default 1e-5)

  • top_smooth (float) – experimental, nominator constant to avoid zero final loss when targets are all zeros

  • is_onehot_targets (bool) – targets are already One-Hot-encoded or not

  • cross_entropy_smooth (float) – constant to avoid log zero (default 1e-8)

Returns

tensor of hybrid loss

get_loss(predictions, targets, build_ctx=None)

Compute hybrid loss for tf variable

Parameters
  • predictions (Tensor) – outputs of the network

  • targets (Tensor) – target integer labels

  • build_ctx – specified graph context

Returns

hybrid loss

class Dice(data_format='channels_first', skip_background=False, squared_pred=False, jaccard=False, smooth=1e-05, top_smooth=0.0, is_onehot_targets=False)

Bases: ai4med.components.losses.loss.Loss

Compute average Dice loss between two tensors.

5D tensors (for 3D images) or 4D tensors (for 2D images).

Parameters
  • data_format (str) – channels_first (default) or channels_last

  • skip_background (bool) – skip dice computation on the first channel of the predicted output or not

  • 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

  • smooth (float) – denominator constant to avoid zero division (default 1e-5)

  • top_smooth (float) – experimental, nominator constant to avoid zero final loss when targets are all zeros

  • is_onehot_targets (bool) – the targets are already One-Hot-encoded or not

Returns

tensor of one minus average dice loss

get_loss(predictions, targets, build_ctx=None)

Compute dice loss for tf variable

Parameters
  • predictions (Tensor) – outputs of the network

  • targets (Tensor) – target integer labels

  • build_ctx – specified graph context

Returns

tensor of dice loss

class Focal(data_format='channels_first', skip_background=False, smooth=1e-08, is_onehot_targets=False, alpha=0.25, gamma=2, use_sigmoid=False, use_for_segmentation=True, use_for_classification=False)

Bases: ai4med.components.losses.loss.Loss

Compute average Focal loss between two tensors.

5D tensors (for 3D images) or 4D tensors (for 2D images).

For more details, check the focal loss for segmentation: Wentao Zhu et al., AnatomyNet: Deep Learning for Fast and Fully Automated Whole-volume Segmentation of Head and Neck Anatomy, Medical Physics, 2018.

Parameters
  • data_format (str) – channels_first (default) or channels_last

  • skip_background (bool) – skip dice computation on the first channel of the predicted output or not

  • smooth (float) – denominator constant to avoid zero division (default 1e-5)

  • is_onehot_targets (bool) – targets are already One-Hot-encoded or not

  • alpha (float) – fl = -alpha * (1-p)^gamma * log(p)

  • gamma (float) – indicating the power of 1-p

  • use_sigmoid (bool) – add sigmoid layer on predictions or not

  • use_for_segmentation (bool) – use this Focal loss for segmentation task or not

  • use_for_classification (bool) – use this Focal loss for classification task or not

Returns

tensor of focal loss

get_loss(predictions, targets, build_ctx=None)

Compute hybrid loss for tf variable

Parameters
  • predictions (Tensor) – outputs of the network

  • targets (Tensor) – target integer labels

  • build_ctx – specified graph context

Returns

hybrid loss

class FocalDice(lambda_dice=1, lambda_focal=2, data_format='channels_first', skip_background=False, squared_pred=False, jaccard=False, dice_smooth=1e-05, top_smooth=0.0, is_onehot_targets=False, focal_smooth=1e-08, focal_alpha=0.25, focal_gamma=2, summarize_focal_loss=True, print_focal_loss=True, focal_loss_key='focal_loss', summarize_dice_loss=True, print_dice_loss=True, dice_loss_key='dice_loss')

Bases: ai4med.components.losses.loss.Loss

Hybrid loss between focal loss and dice loss.

5D tensors (for 3D images) or 4D tensors (for 2D images).

For more details, check the hybrid loss for segmentation: Wentao Zhu et al., AnatomyNet: Deep Learning for Fast and Fully Automated Whole-volume Segmentation of Head and Neck Anatomy, Medical Physics, 2018.

Parameters
  • lambda_dice (float) – trade-off value of dice loss

  • lambda_focal (float) – trade-off value of focal loss

  • data_format (str) – channels_first (default) or channels_last

  • skip_background (bool) – skip dice computation on the first channel of the predicted output or not

  • 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

  • dice_smooth (float) – denominator constant to avoid zero division (default 1e-5)

  • top_smooth (float) – experimental, nominator constant to avoid zero final loss when targets are all zeros

  • is_onehot_targets (bool) – targets are already One-Hot-encoded or not

  • focal_smooth (float) – constant to avoid log zero (default 1e-8)

  • focal_alpha (float) – fl = -alpha * (1-p)^gamma * log(p)

  • focal_gamma (float) – indicating the power of 1-p

  • summarize_focal_loss – whether to summarize focal loss during fitting

  • print_focal_loss – whether to print focal loss during fitting

  • focal_loss_key – key name for focal loss in summary and printing. If empty, not summarized or printed

  • summarize_dice_loss – whether to summarize dice loss during fitting

  • print_dice_loss – whether to print dice loss during fitting

  • dice_loss_key – key name for dice loss in summary and printing. If empty, not summarized or printed

Returns

tensor of hybrid loss

get_loss(predictions, targets, build_ctx=None)

Compute hybrid loss for tf variable

Parameters
  • predictions (Tensor) – outputs of the network

  • targets (Tensor) – target integer labels

  • build_ctx – specified graph context

Returns

hybrid loss

class Loss

Bases: ai4med.common.graph_component.GraphComponent

Base class of loss component.

A loss component is also a GraphComponent that builds part of the computation graph.

build(build_ctx: ai4med.common.build_ctx.BuildContext)

Builds the loss part of the computation graph.

Note

A loss component implementation must not override this method. Instead, it must implement the get_loss method.

Parameters

build_ctx – the build context

Returns: a loss tensor.

get_loss(predictions, targets, build_ctx=None)

This is the required method that a loss component must implement. It must return a loss tensor.

Parameters
  • predictions (Tensor) – outputs of the network

  • targets (Tensor) – target ground truth labels

  • build_ctx (BuildContext) – the build context that contains contextual info.

Returns

loss function

© Copyright 2020, NVIDIA. Last updated on Feb 2, 2023.