kaira.losses.image.FocalLoss

Inheritance diagram of FocalLoss

Inheritance diagram for FocalLoss

class kaira.losses.image.FocalLoss(alpha=None, gamma=2.0, reduction='mean')[source]

Bases: BaseLoss

Focal Loss Module for dealing with class imbalance.

This implementation works for both binary and multi-class problems. Focal loss addresses class imbalance by down-weighting well-classified examples, focusing training on difficult examples [Lin et al., 2017].

Methods

__init__

Initialize the FocalLoss module.

forward

Forward pass through the FocalLoss module.

__init__(alpha=None, gamma=2.0, reduction='mean')[source]

Initialize the FocalLoss module.

Parameters:
  • alpha (float or tensor) – Weighting factor for the rare class. Default is None.

  • gamma (float) – Focusing parameter. Default is 2.0.

  • reduction (str) – Specifies the reduction to apply to the output. Default is ‘mean’.

forward(inputs: Tensor, targets: Tensor) Tensor[source]

Forward pass through the FocalLoss module.

Parameters:
Returns:

The focal loss between the input and the target.

Return type:

torch.Tensor