toolbelt

‘Classical’ losses imported from https://github.com/BloodAxe/pytorch-toolbelt and adapted to our projects.

class mfai.pytorch.losses.toolbelt.DiceLoss(mode, classes=None, log_loss=False, from_logits=True, smooth=0.0, ignore_index=None, eps=1e-07)[source]

Bases: _Loss

Parameters:
aggregate_loss(loss)[source]
Return type:

Tensor

Parameters:

loss (Tensor)

compute_score(output, target, smooth=0.0, eps=1e-07, dims=None)[source]

Code from /segmentation_models_pytorch/losses/_functional.py.

Return type:

Tensor

Parameters:
forward(y_pred, y_true)[source]

Define the computation performed at every call.

Should be overridden by all subclasses. :rtype: Tensor

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.

Parameters:
Return type:

Tensor

class mfai.pytorch.losses.toolbelt.SoftBCEWithLogitsLoss(weight=None, ignore_index=-100, reduction='mean', smooth_factor=None, pos_weight=None)[source]

Bases: Module

Parameters:
  • weight (Tensor | None)

  • ignore_index (int | None)

  • reduction (str)

  • smooth_factor (float | None)

  • pos_weight (Tensor | None)

forward(y_pred, y_true)[source]
Parameters:
  • y_pred (Tensor) – torch.Tensor of shape (N, C, H, W).

  • y_true (Tensor) – torch.Tensor of shape (N, H, W) or (N, 1, H, W).

Returns:

torch.Tensor

Return type:

loss

class mfai.pytorch.losses.toolbelt.SoftCrossEntropyLoss(reduction='mean', smooth_factor=0.0, ignore_index=-100, dim=1)[source]

Bases: Module

Parameters:
forward(y_pred, y_true)[source]

Define the computation performed at every call.

Should be overridden by all subclasses. :rtype: Tensor

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.

Parameters:
Return type:

Tensor

label_smoothed_nll_loss(lprobs, target, epsilon, ignore_index=None, reduction='mean', dim=-1)[source]

NLL loss with label smoothing.

References

https://github.com/pytorch/fairseq/blob/master/fairseq/criterions/label_smoothed_cross_entropy.py

Parameters:
  • lprobs (Tensor) – Log-probabilities of predictions (e.g after log_softmax).

  • target (Tensor) – Ground truth labels.

  • epsilon (float) – Smoothing factor.

  • ignore_index (Optional[int]) – Index to ignore.

  • reduction (str) –

    Reduction method. “none”: No reduction will be applied, “mean”: The sum of the output will be divided by the number of

    elements in the output,

    ”sum”: The output will be summed.

  • dim (int) – Dimension along which to compute the loss.

Returns:

Loss values.

Return type:

Tensor