Losses

class InnerEye.ML.models.losses.cross_entropy.CrossEntropyLoss(class_weight_power: Optional[float] = None, smoothing_eps: float = 0.0, focal_loss_gamma: Optional[float] = None, ignore_index: int = - 100)[source]
forward_minibatch(output: Tensor, target: Tensor, **kwargs: Any) Tensor[source]

Wrapper for multi-class cross entropy function implemented in PyTorch. The implementation supports tensors with arbitrary spatial dimension. Input logits are normalised internally in F.cross_entropy function.

Parameters:
  • output – Class logits (unnormalised), e.g. in 3D : BxCxWxHxD or in 1D BxC

  • target – Target labels encoded in one-hot representation, e.g. in 3D BxCxWxHxD or in 1D BxC

get_focal_loss_pixel_weights(logits: Tensor, target: Tensor) Tensor[source]

Computes weights for each pixel/sample inversely proportional to the posterior likelihood.

Parameters:
  • logits – Logits tensor.

  • target – Target label tensor in one-hot encoding.

training: bool
class InnerEye.ML.models.losses.ece.ECELoss(n_bins: int = 15, activation: ~typing.Callable = <function ECELoss.<lambda>>)[source]

Calculates the Expected Calibration Error of a model. Confidence outputs are divided into equally-sized interval bins. In each bin, we compute the confidence gap as: bin_gap = l1_norm(avg_confidence_in_bin - accuracy_in_bin) A weighted average of the gaps is then returned based on the number of samples in each bin.

forward(logits: Tensor, labels: Tensor) Tensor[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class InnerEye.ML.models.losses.mixture.MixtureLoss(components: List[Tuple[float, SupervisedLearningCriterion]])[source]
forward_minibatch(output: Tensor, target: Tensor, **kwargs: Any) Tensor[source]

Wrapper for mixture loss function implemented in PyTorch. Arguments should be suitable for the component loss functions, typically:

Parameters:
  • output – Class logits (unnormalised), e.g. in 3D : BxCxWxHxD or in 1D BxC

  • target – Target labels encoded in one-hot representation, e.g. in 3D BxCxWxHxD or in 1D BxC

training: bool
class InnerEye.ML.models.losses.soft_dice.SoftDiceLoss(eps: float = 1e-10, apply_softmax: bool = True, class_weight_power: Optional[float] = None)[source]

Implementation of Soft-Dice Loss. Reference: Milletari, F., Navab, N., & Ahmadi, S. (2016). V-Net: Fully Convolutional Neural Networks for Volumetric

Medical Image Segmentation. In International Conference on 3D Vision (3DV).

eps

Small value to avoid division by zero errors.

forward_minibatch(output: Tensor, target: Tensor, **kwargs: Any) Tensor[source]

Computes the forward pass of soft-dice loss. It assumes the output and target have Batch x Classes x … dimensions, with the last dimensions being an arbitrary number of spatial dimensions.

Parameters:
  • output – The output of the network.

  • target – The target of the network.

Returns:

The soft-dice loss.

Raises:
  • ValueError – If the shape of the tensors is incorrect.

  • TypeError – If output or target are not torch.tensors.

training: bool