pymic.loss.seg package

Submodules

pymic.loss.seg.abstract module

class pymic.loss.seg.abstract.AbstractSegLoss(params=None)

Bases: Module

Abstract class for loss function of segmentation tasks. The parameters should be written in the params dictionary, and it has the following fields:

Parameters:

loss_softmax – (optional, bool) Apply softmax to the prediction of network or not. Default is True.

forward(loss_input_dict)

Forward pass for calculating the loss. The arguments should be written in the loss_input_dict dictionary, and it has the following fields:

Parameters:
  • prediction – (tensor) Prediction of a network, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • ground_truth – (tensor) Ground truth, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • pixel_weight – (optional) Pixel-wise weight map, with the shape of [N, 1, D, H, W] or [N, 1, H, W]. Default is None.

Returns:

Loss function value.

training: bool

pymic.loss.seg.ce module

class pymic.loss.seg.ce.CrossEntropyLoss(params=None)

Bases: AbstractSegLoss

Cross entropy loss for segmentation tasks.

The parameters should be written in the params dictionary, and it has the following fields:

Parameters:

loss_softmax – (optional, bool) Apply softmax to the prediction of network or not. Default is True.

forward(loss_input_dict)

Forward pass for calculating the loss. The arguments should be written in the loss_input_dict dictionary, and it has the following fields:

Parameters:
  • prediction – (tensor) Prediction of a network, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • ground_truth – (tensor) Ground truth, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • pixel_weight – (optional) Pixel-wise weight map, with the shape of [N, 1, D, H, W] or [N, 1, H, W]. Default is None.

Returns:

Loss function value.

training: bool
class pymic.loss.seg.ce.GeneralizedCELoss(params)

Bases: AbstractSegLoss

Generalized cross entropy loss to deal with noisy labels.

  • Reference: Z. Zhang et al. Generalized Cross Entropy Loss for Training Deep Neural Networks with Noisy Labels, NeurIPS 2018.

The parameters should be written in the params dictionary, and it has the following fields:

Parameters:
  • loss_softmax – (bool) Apply softmax to the prediction of network or not.

  • loss_gce_q – (float): hyper-parameter in the range of (0, 1).

  • loss_with_pixel_weight – (optional, bool): Use pixel weighting or not.

  • loss_class_weight – (optional, list or none): If not none, a list of weight for each class.

forward(loss_input_dict)

Forward pass for calculating the loss. The arguments should be written in the loss_input_dict dictionary, and it has the following fields:

Parameters:
  • prediction – (tensor) Prediction of a network, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • ground_truth – (tensor) Ground truth, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • pixel_weight – (optional) Pixel-wise weight map, with the shape of [N, 1, D, H, W] or [N, 1, H, W]. Default is None.

Returns:

Loss function value.

training: bool

pymic.loss.seg.combined module

class pymic.loss.seg.combined.CombinedLoss(params, loss_dict)

Bases: AbstractSegLoss

A combination of a list of loss functions. Parameters should be saved in the params dictionary.

Parameters:
  • loss_softmax – (optional, bool) Apply softmax to the prediction of network or not. Default is True.

  • loss_type – (list) A list of loss function name.

  • loss_weight – (list) A list of weights for each loss fucntion.

  • loss_dict – (dictionary) A dictionary of avaiable loss functions.

forward(loss_input_dict)

Forward pass for calculating the loss. The arguments should be written in the loss_input_dict dictionary, and it has the following fields:

Parameters:
  • prediction – (tensor) Prediction of a network, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • ground_truth – (tensor) Ground truth, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • pixel_weight – (optional) Pixel-wise weight map, with the shape of [N, 1, D, H, W] or [N, 1, H, W]. Default is None.

Returns:

Loss function value.

training: bool

pymic.loss.seg.deep_sup module

class pymic.loss.seg.deep_sup.DeepSuperviseLoss(params)

Bases: AbstractSegLoss

Combine deep supervision with a basic loss function. Arguments should be provided in the params dictionary, and it has the following fields:

Parameters:
  • loss_softmax – (optional, bool) Apply softmax to the prediction of network or not. Default is True.

  • base_loss – (nn.Module) The basic function used for each scale.

  • deep_supervise_weight – (list) A list of weight for each deep supervision scale.

  • deep_supervise_model – (int) Mode for deep supervision when the prediction has a smaller shape than the ground truth. 0: upsample the prediction to the size of the ground truth. 1: downsample the ground truth to the size of the prediction via interpolation. 2: downsample the ground truth via adaptive average pooling.

forward(loss_input_dict)

Forward pass for calculating the loss. The arguments should be written in the loss_input_dict dictionary, and it has the following fields:

Parameters:
  • prediction – (tensor) Prediction of a network, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • ground_truth – (tensor) Ground truth, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • pixel_weight – (optional) Pixel-wise weight map, with the shape of [N, 1, D, H, W] or [N, 1, H, W]. Default is None.

Returns:

Loss function value.

training: bool
pymic.loss.seg.deep_sup.match_prediction_and_gt_shape(pred, gt, mode=0)

pymic.loss.seg.dice module

class pymic.loss.seg.dice.DiceLoss(params=None)

Bases: AbstractSegLoss

Dice loss for segmentation tasks. The parameters should be written in the params dictionary, and it has the following fields:

Parameters:

loss_softmax – (bool) Apply softmax to the prediction of network or not.

forward(loss_input_dict)

Forward pass for calculating the loss. The arguments should be written in the loss_input_dict dictionary, and it has the following fields:

Parameters:
  • prediction – (tensor) Prediction of a network, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • ground_truth – (tensor) Ground truth, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • pixel_weight – (optional) Pixel-wise weight map, with the shape of [N, 1, D, H, W] or [N, 1, H, W]. Default is None.

Returns:

Loss function value.

training: bool
class pymic.loss.seg.dice.FocalDiceLoss(params=None)

Bases: AbstractSegLoss

Focal Dice according to the following paper:

  • Pei Wang and Albert C. S. Chung, Focal Dice Loss and Image Dilation for Brain Tumor Segmentation, 2018.

The parameters should be written in the params dictionary, and it has the following fields:

Parameters:
  • loss_softmax – (bool) Apply softmax to the prediction of network or not.

  • FocalDiceLoss_beta – (float) The hyper-parameter to set (>=1.0).

forward(loss_input_dict)

Forward pass for calculating the loss. The arguments should be written in the loss_input_dict dictionary, and it has the following fields:

Parameters:
  • prediction – (tensor) Prediction of a network, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • ground_truth – (tensor) Ground truth, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • pixel_weight – (optional) Pixel-wise weight map, with the shape of [N, 1, D, H, W] or [N, 1, H, W]. Default is None.

Returns:

Loss function value.

training: bool
class pymic.loss.seg.dice.NoiseRobustDiceLoss(params)

Bases: AbstractSegLoss

Noise-robust Dice loss according to the following paper.

  • G. Wang et al. A Noise-Robust Framework for Automatic Segmentation of COVID-19 Pneumonia Lesions From CT Images, IEEE TMI, 2020.

The parameters should be written in the params dictionary, and it has the following fields:

Parameters:
  • loss_softmax – (bool) Apply softmax to the prediction of network or not.

  • NoiseRobustDiceLoss_gamma – (float) The hyper-parameter gammar to set (1, 2).

forward(loss_input_dict)

Forward pass for calculating the loss. The arguments should be written in the loss_input_dict dictionary, and it has the following fields:

Parameters:
  • prediction – (tensor) Prediction of a network, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • ground_truth – (tensor) Ground truth, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • pixel_weight – (optional) Pixel-wise weight map, with the shape of [N, 1, D, H, W] or [N, 1, H, W]. Default is None.

Returns:

Loss function value.

training: bool

pymic.loss.seg.exp_log module

class pymic.loss.seg.exp_log.ExpLogLoss(params)

Bases: AbstractSegLoss

The exponential logarithmic loss in this paper:

  • K. Wong et al.: 3D Segmentation with Exponential Logarithmic Loss for Highly Unbalanced Object Sizes. MICCAI 2018.

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • loss_softmax – (bool) Apply softmax to the prediction of network or not.

  • ExpLogLoss_w_dice – (float) Weight of ExpLog Dice loss in the range of [0, 1].

  • ExpLogLoss_gamma – (float) Hyper-parameter gamma.

forward(loss_input_dict)

Forward pass for calculating the loss. The arguments should be written in the loss_input_dict dictionary, and it has the following fields:

Parameters:
  • prediction – (tensor) Prediction of a network, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • ground_truth – (tensor) Ground truth, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • pixel_weight – (optional) Pixel-wise weight map, with the shape of [N, 1, D, H, W] or [N, 1, H, W]. Default is None.

Returns:

Loss function value.

training: bool

pymic.loss.seg.gatedcrf module

The code is adapted from the original implementation at Github.

class pymic.loss.seg.gatedcrf.GatedCRFLoss

Bases: Module

Gated CRF Loss for Weakly Supervised Semantic Image Segmentation. This loss function promotes consistent label assignment guided by input features, such as RGBXY.

  • Reference: Anton Obukhov, Stamatios Georgoulis, Dengxin Dai and Luc Van Gool: Gated CRF Loss for Weakly Supervised Semantic Image Segmentation. CoRR 2019.

forward(y_hat_softmax, kernels_desc, kernels_radius, sample, height_input, width_input, mask_src=None, mask_dst=None, compatibility=None, custom_modality_downsamplers=None, out_kernels_vis=False)

Performs the forward pass of the loss.

Parameters:
  • y_hat_softmax – A tensor of predicted per-pixel class probabilities of size NxCxHxW

  • kernels_desc – A list of dictionaries, each describing one Gaussian kernel composition from modalities. The final kernel is a weighted sum of individual kernels. Following example is a composition of RGBXY and XY kernels: kernels_desc: [{‘weight’: 0.9,’xy’: 6,’rgb’: 0.1},{‘weight’: 0.1,’xy’: 6}]

  • kernels_radius – Defines size of bounding box region around each pixel in which the kernel is constructed.

  • sample – A dictionary with modalities (except ‘xy’) used in kernels_desc parameter. Each of the provided modalities is allowed to be larger than the shape of y_hat_softmax, in such case downsampling will be invoked. Default downsampling method is area resize; this can be overriden by setting. custom_modality_downsamplers parameter.

  • height_input (width_input,) – Dimensions of the full scale resolution of modalities

  • mask_src – (optional) Source mask.

  • mask_dst – (optional) Destination mask.

  • compatibility – (optional) Classes compatibility matrix, defaults to Potts model.

  • custom_modality_downsamplers – A dictionary of modality downsampling functions.

  • out_kernels_vis – Whether to return a tensor with kernels visualized with some step.

Returns:

Loss function value.

training: bool

pymic.loss.seg.mse module

class pymic.loss.seg.mse.MAELoss(params=None)

Bases: AbstractSegLoss

Mean Absolute Loss for segmentation tasks. The arguments should be written in the params dictionary, and it has the following fields:

Parameters:

loss_softmax – (bool) Apply softmax to the prediction of network or not.

forward(loss_input_dict)

Forward pass for calculating the loss. The arguments should be written in the loss_input_dict dictionary, and it has the following fields:

Parameters:
  • prediction – (tensor) Prediction of a network, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • ground_truth – (tensor) Ground truth, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • pixel_weight – (optional) Pixel-wise weight map, with the shape of [N, 1, D, H, W] or [N, 1, H, W]. Default is None.

Returns:

Loss function value.

training: bool
class pymic.loss.seg.mse.MSELoss(params=None)

Bases: AbstractSegLoss

Mean Sequare Loss for segmentation tasks. The parameters should be written in the params dictionary, and it has the following fields:

Parameters:

loss_softmax – (bool) Apply softmax to the prediction of network or not.

forward(loss_input_dict)

Forward pass for calculating the loss. The arguments should be written in the loss_input_dict dictionary, and it has the following fields:

Parameters:
  • prediction – (tensor) Prediction of a network, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • ground_truth – (tensor) Ground truth, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • pixel_weight – (optional) Pixel-wise weight map, with the shape of [N, 1, D, H, W] or [N, 1, H, W]. Default is None.

Returns:

Loss function value.

training: bool

pymic.loss.seg.mumford_shah module

class pymic.loss.seg.mumford_shah.MumfordShahLoss(params=None)

Bases: Module

Implementation of Mumford Shah Loss for weakly supervised learning.

  • Reference: Boah Kim and Jong Chul Ye: Mumford–Shah Loss Functional for Image Segmentation With Deep Learning. IEEE TIP, 2019.

The oringial implementation is availabel at Github. Currently only 2D version is supported.

The parameters should be written in the params dictionary, and it has the following fields:

Parameters:
  • loss_softmax – (bool) Apply softmax to the prediction of network or not.

  • MumfordShahLoss_penalty – (optional, str) l1 or l2. Default is l1.

  • MumfordShahLoss_lambda – (optional, float) Hyper-parameter lambda, default is 1.0.

forward(loss_input_dict)

Forward pass for calculating the loss. The arguments should be written in the loss_input_dict dictionary, and it has the following fields:

Parameters:
  • prediction – (tensor) Prediction of a network, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • image – (tensor) Image, with the shape of [N, C, D, H, W] or [N, C, H, W].

Returns:

Loss function value.

get_gradient_loss(pred, penalty='l2')
get_levelset_loss(output, target)

Get the level set loss value.

Parameters:
  • output – (tensor) softmax output of a network.

  • target – (tensor) the input image.

Returns:

the level set loss.

training: bool

pymic.loss.seg.slsr module

class pymic.loss.seg.slsr.SLSRLoss(params=None)

Bases: AbstractSegLoss

Spatial Label Smoothing Regularization (SLSR) loss for learning from noisy annotatins. This loss requires pixel weighting, please make sure that a pixel_weight field is provided for the csv file of the training images.

The pixel wight here is actually the confidence mask, i.e., if the value is one, it means the label of corresponding pixel is noisy and should be smoothed.

  • Reference: Minqing Zhang, Jiantao Gao et al.: Characterizing Label Errors: Confident Learning for Noisy-Labeled Image Segmentation, MICCAI 2020.

The arguments should be written in the params dictionary, and it has the following fields:

Parameters:
  • loss_softmax – (bool) Apply softmax to the prediction of network or not.

  • slsrloss_epsilon – (optional, float) Hyper-parameter epsilon. Default is 0.25.

forward(loss_input_dict)

Forward pass for calculating the loss. The arguments should be written in the loss_input_dict dictionary, and it has the following fields:

Parameters:
  • prediction – (tensor) Prediction of a network, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • ground_truth – (tensor) Ground truth, with the shape of [N, C, D, H, W] or [N, C, H, W].

  • pixel_weight – (optional) Pixel-wise weight map, with the shape of [N, 1, D, H, W] or [N, 1, H, W]. Default is None.

Returns:

Loss function value.

training: bool

pymic.loss.seg.ssl module

class pymic.loss.seg.ssl.EntropyLoss(params=None)

Bases: AbstractSegLoss

Entropy Minimization for segmentation tasks. The parameters should be written in the params dictionary, and it has the following fields:

Parameters:

loss_softmax – (bool) Apply softmax to the prediction of network or not.

forward(loss_input_dict)

Forward pass for calculating the loss. The arguments should be written in the loss_input_dict dictionary, and it has the following fields:

Parameters:

prediction – (tensor) Prediction of a network, with the shape of [N, C, D, H, W] or [N, C, H, W].

Returns:

Loss function value.

training: bool
class pymic.loss.seg.ssl.TotalVariationLoss(params=None)

Bases: AbstractSegLoss

Total Variation Loss for segmentation tasks. The parameters should be written in the params dictionary, and it has the following fields:

Parameters:

loss_softmax – (bool) Apply softmax to the prediction of network or not.

forward(loss_input_dict)

Forward pass for calculating the loss. The arguments should be written in the loss_input_dict dictionary, and it has the following fields:

Parameters:

prediction – (tensor) Prediction of a network, with the shape of [N, C, D, H, W] or [N, C, H, W].

Returns:

Loss function value.

training: bool

pymic.loss.seg.util module

pymic.loss.seg.util.get_classwise_dice(predict, soft_y, pix_w=None)

Get dice scores for each class in predict (after softmax) and soft_y.

Parameters:
  • predict – (tensor) Prediction of a segmentation network after softmax.

  • soft_y – (tensor) The one-hot segmentation ground truth.

  • pix_w – (optional, tensor) The pixel weight map. Default is None.

Returns:

Dice score for each class.

pymic.loss.seg.util.get_soft_label(input_tensor, num_class, data_type='float')

Convert a label tensor to one-hot label for segmentation tasks.

Parameters:
  • input_tensor – (tensor) Tensor with shae [B, 1, D, H, W] or [B, 1, H, W].

  • num_class – (int) The class number.

  • data_type – (optional, str) Type of data, float (default) or double.

Returns:

A tensor with shape [B, num_class, D, H, W] or [B, num_class, H, W]

pymic.loss.seg.util.reshape_prediction_and_ground_truth(predict, soft_y)

Reshape input variables two 2D.

Parameters:
  • predict – (tensor) A tensor of shape [N, C, D, H, W] or [N, C, H, W].

  • soft_y – (tensor) A tensor of shape [N, C, D, H, W] or [N, C, H, W].

Returns:

Two output tensors with shape [voxel_n, C] that correspond to the two inputs.

pymic.loss.seg.util.reshape_tensor_to_2D(x)

Reshape input tensor of shape [N, C, D, H, W] or [N, C, H, W] to [voxel_n, C]

Module contents