Loss Functions

class fgvc.losses.BCEWithLogitsLoss(weight: Tensor | None = None, reduction: str = 'mean', pos_weight: Tensor | None = None)

A wapper class for torch.nn.BCEWithLogitsLoss that aligns shapes and dtypes of inputs.

forward(logits: Tensor, targs: Tensor) Tensor

Evaluate Binary Cross Entropy Loss.

class fgvc.losses.BinaryDiceLoss(eps: float = 0.001)
forward(logits: Tensor, targs: Tensor) Tensor

Compute Dice Loss.

Thanks to: https://arxiv.org/abs/1606.04797 And: https://github.com/hubutui/DiceLoss-PyTorch/blob/master/loss.py

Parameters:
  • logits – Tensor with predicted values.

  • targs – Tensor with target values.

Return type:

loss

class fgvc.losses.ComposeLoss(criterions: List[Module], weights: List[float] | None = None)
forward(logits: Tensor, targs: Tensor) Tensor

Compute individual losses and combine them together.

class fgvc.losses.DiceLoss(eps: float = 0.001)
forward(logits: Tensor, targs: Tensor) Tensor

Compute Dice Loss.

Thanks to: https://arxiv.org/abs/1606.04797 And: https://github.com/hubutui/DiceLoss-PyTorch/blob/master/loss.py

Parameters:
  • logits – Tensor with predicted values.

  • targs – Tensor with target values.

Return type:

loss

class fgvc.losses.FocalLossWithLogits(weight: Tensor | None = None, gamma: float = 2.5)
forward(logits: Tensor, targs: Tensor) Tensor

Evaluate Focal Loss.

class fgvc.losses.SeesawLossWithLogits(class_counts, p: float = 0.8)

An unofficial implementation for Seesaw loss.

The loss was proposed in the technical report for LVIS workshop at ECCV 2020. For more detail, please refer https://arxiv.org/pdf/2008.10032.pdf.

Parameters:
  • class_counts – The list which has number of samples for each class. Should have same length as num_classes.

  • p – Scale parameter which adjust the strength of punishment. Set to 0.8 as a default by following the original paper.

forward(logits: Tensor, targs: Tensor) Tensor

Evaluate Seesaw Loss.