LovaszLoss#
- class LovaszLoss(mode: str, class_seen: int | None = None, class_weights: Dict[int, float] | Tensor | list | None = None, per_image: bool = False, ignore_index: int | None = None, loss_weight: float = 1.0, penalize_pred: bool = False)[source]#
Bases:
_LossLovasz loss: a direct surrogate for the IoU (Jaccard) metric.
Optimizes the Lovasz extension of the Jaccard index (Berman et al. 2018).
forward(y_pred, y_true)takesy_predof shape(N, C, ...)(logits; soft-maxed for multiclass) andy_trueof shape(N, ...)(class indices) and returns the loss scaled byloss_weight. Supports binary, multiclass, and multilabel segmentation. Registered asLovaszLoss– use in acriteria=[...]list, typically paired with cross-entropy.- Parameters:
mode (str) – Loss mode, one of
"binary","multiclass","multilabel".class_seen (int | None) – Optional restriction of which classes contribute to the loss. Defaults to
None.class_weights (dict[int, float] | torch.Tensor | list | None) – Optional per-class weights (dict from class index to weight, tensor, or list). Defaults to
None.per_image (bool) – Compute the loss per image then average, instead of over the whole batch. Defaults to
False.ignore_index (int | None) – Label marking ignored pixels. Defaults to
None.loss_weight (float) – Global scale on the returned loss. Defaults to
1.0.penalize_pred (bool) – Weight per-pixel errors by the predicted class (rather than weighting the per-class loss). Defaults to
False.
Example
>>> import torch >>> from pimm.models.losses.builder import build_criteria >>> crit = build_criteria([dict(type="LovaszLoss", mode="multiclass", ... loss_weight=1.0)]) >>> pred = torch.randn(10, 3) # (N=10 points, C=3 classes) logits >>> target = torch.randint(0, 3, (10,)) # class indices >>> crit(pred, target) # scalar Jaccard surrogate tensor(0.7265)