Skip to content

lovasz

Lovász-Softmax loss.

Classes:

  • LovaszLoss –

    Lovász-Softmax loss: a smooth surrogate for the mean-IoU objective.

LovaszLoss [source] ¶

LovaszLoss(
    ignore_index: int = -1,
    classes: Literal["present", "all"] = "present",
)

Bases: Module

Lovász-Softmax loss: a smooth surrogate for the mean-IoU objective.

Optimizes segmentation overlap directly, and is typically summed with cross-entropy. See The Lovász-Softmax loss: A tractable surrogate for the optimization of the intersection-over-union measure in neural networks (Berman et al., 2018). The function form on probabilities is lovasz_softmax.

Parameters:

  • ignore_index (int, default: -1 ) –

    Label value excluded from the loss.

  • classes (Literal['present', 'all'], default: 'present' ) –

    "present" averages only classes present in the targets; "all" averages every class.

Methods:

  • forward –

    Compute the loss from per-point logits \((N, C)\) and labels \((N,)\).

forward [source] ¶

forward(logits: Tensor, labels: Tensor) -> Tensor

Compute the loss from per-point logits \((N, C)\) and labels \((N,)\).

lovasz_softmax [source] ¶

lovasz_softmax(
    probas: Tensor,
    labels: Tensor,
    *,
    classes: Literal["present", "all"] = "present",
    ignore_index: int = -1,
) -> Tensor

Multi-class Lovász-Softmax loss over per-point class probabilities.

The smooth surrogate of the mean-IoU objective of The Lovász-Softmax loss: A tractable surrogate for the optimization of the intersection-over-union measure in neural networks (Berman et al., 2018); the function form of LovaszLoss, which takes logits.

Parameters:

  • probas (Tensor) –

    Per-point class probabilities (post-softmax), shape \((N, C)\).

  • labels (Tensor) –

    Per-point ground-truth labels, shape \((N,)\).

  • classes (Literal['present', 'all'], default: 'present' ) –

    "present" averages only classes present in labels; "all" averages every class.

  • ignore_index (int, default: -1 ) –

    Label value excluded from the loss.

Returns:

  • Tensor –

    The scalar loss (zero when no point or no class contributes).

Shape
  • probas: \((N, C)\)
  • labels: \((N,)\)
  • output: scalar
Example
>>> labels = torch.tensor([0, 1, 2])
>>> probas = torch.nn.functional.one_hot(labels, 3).float()
>>> lovasz_softmax(probas, labels).item()
0.0