focal
Focal losses: the sigmoid focal loss of the anchor / point / query heads, the Gaussian focal loss of the heatmap heads and the Poly-1 focal loss.
Classes:
-
SigmoidFocalLossβModule form of
sigmoid_focal_loss. -
GaussianFocalLossβModule form of
gaussian_focal_loss. -
Poly1FocalLossβModule form of
poly1_focal_loss, taking class indices.
SigmoidFocalLoss [source] ΒΆ
SigmoidFocalLoss(
alpha: float = 0.25,
gamma: float = 2.0,
reduction: Literal["none", "sum", "mean"] = "mean",
)
Bases: Module
Module form of sigmoid_focal_loss.
Parameters:
-
alpha(float, default:0.25) βPositive/negative balance \(\alpha\).
-
gamma(float, default:2.0) βFocusing exponent \(\gamma\).
-
reduction(Literal['none', 'sum', 'mean'], default:'mean') β"none"returns the per-element loss,"sum"its sum,"mean"its mean.
Example
Methods:
-
forwardβCompute the focal loss from logits \((\ldots, C)\), binary targets \((\ldots, C)\) and row weights \((\ldots)\).
GaussianFocalLoss [source] ΒΆ
Bases: Module
Module form of gaussian_focal_loss.
Takes heatmap logits and applies the sigmoid itself.
Parameters:
-
alpha(float, default:2.0) βFocusing exponent on the prediction.
-
gamma(float, default:4.0) βDown-weighting exponent on the soft negatives.
Example
Methods:
-
forwardβCompute the loss from heatmap logits and the Gaussian target of the same shape.
Poly1FocalLoss [source] ΒΆ
Poly1FocalLoss(
epsilon: float = 1.0,
alpha: Optional[float] = 0.25,
gamma: float = 2.0,
reduction: Literal["none", "sum", "mean"] = "mean",
)
Bases: Module
Module form of poly1_focal_loss, taking class indices.
The targets are either class indices \((\ldots)\), one-hot encoded over the \(C\) logits, or binary targets of the logits' shape. reduction="mean" averages over every element of the \((\ldots, C)\) loss.
Parameters:
-
epsilon(float, default:1.0) βWeight \(\epsilon\) of the Poly-1 term.
-
alpha(Optional[float], default:0.25) βPositive/negative balance \(\alpha\);
Nonedisables it. -
gamma(float, default:2.0) βFocusing exponent \(\gamma\).
-
reduction(Literal['none', 'sum', 'mean'], default:'mean') β"none"returns the per-element loss,"sum"its sum,"mean"its mean.
Example
Methods:
-
forwardβCompute the loss from logits \((\ldots, C)\) and class indices \((\ldots)\) or binary targets \((\ldots, C)\).
sigmoid_focal_loss [source] ΒΆ
sigmoid_focal_loss(
logits: Tensor,
targets: Tensor,
weights: Optional[Tensor] = None,
*,
alpha: float = 0.25,
gamma: float = 2.0,
) -> Tensor
Sigmoid focal loss between per-class logits and binary targets (no reduction).
The classification term of Focal Loss for Dense Object Detection (Lin et al., 2017), shared by the anchor, point and query heads: binary cross-entropy on the sigmoid of each logit, scaled by \(\alpha_t (1 - p_t)^\gamma\) so confident predictions contribute little. Same math as torchvision.ops.sigmoid_focal_loss, plus an optional per-row weight.
Parameters:
-
logits(Tensor) βPer-class logits, shape \((\ldots, C)\).
-
targets(Tensor) βBinary (one-hot) targets of the same shape.
-
weights(Optional[Tensor], default:None) βOptional weight of every row, shape \((\ldots)\), broadcast over the class dimension.
-
alpha(float, default:0.25) βPositive/negative balance \(\alpha\).
-
gamma(float, default:2.0) βFocusing exponent \(\gamma\).
Returns:
-
TensorβThe weighted per-element loss, shape \((\ldots, C)\).
Shape
- logits: \((\ldots, C)\)
- targets: \((\ldots, C)\)
- weights: \((\ldots)\)
- output: \((\ldots, C)\)
gaussian_focal_loss [source] ΒΆ
gaussian_focal_loss(
pred: Tensor,
target: Tensor,
*,
alpha: float = 2.0,
gamma: float = 4.0,
eps: float = 0.0001,
) -> Tensor
Penalty-reduced focal loss over a Gaussian heatmap, normalized by the number of peaks.
The heatmap objective of Objects as Points (Zhou et al., 2019), used by the center-based detection heads (CenterPoint, VoxelNeXt, TransFusion). Cells whose target is exactly \(1\) are positives with the \((1 - p)^\alpha \log p\) term; every other cell is a soft negative whose \(p^\alpha \log(1 - p)\) term is down-weighted by \((1 - y)^\gamma\), so cells near a peak barely count. The sum is divided by the number of positives (or left as is when there is none).
Parameters:
-
pred(Tensor) βPredicted probabilities (post-sigmoid) of any shape; clamped to \([\varepsilon, 1 - \varepsilon]\).
-
target(Tensor) βGaussian heatmap target of the same shape, values in \([0, 1]\).
-
alpha(float, default:2.0) βFocusing exponent on the prediction.
-
gamma(float, default:4.0) βDown-weighting exponent on the soft negatives.
-
eps(float, default:0.0001) βClamp keeping the logarithms finite.
Returns:
-
TensorβThe scalar loss.
Shape
- pred: \((\ldots)\)
- target: \((\ldots)\)
- output: scalar
one_hot_foreground [source] ΒΆ
One-hot encode per-row class labels, dropping the background column.
Ignored (\(-1\)) and background (\(0\)) rows map to an all-zero row; a foreground row with label \(\ell \ge 1\) maps to a one-hot row on class \(\ell - 1\). The target format of sigmoid_focal_loss for the anchor and point heads.
Parameters:
-
labels(Tensor) βClass labels (\(-1\) ignore, \(0\) background, \(\ge 1\) foreground), shape \((\ldots)\).
-
num_classes(int) βNumber of foreground classes \(C\).
Returns:
-
TensorβOne-hot foreground targets, shape \((\ldots, C)\).
Shape
- labels: \((\ldots)\)
- output: \((\ldots, C)\)
poly1_focal_loss [source] ΒΆ
poly1_focal_loss(
logits: Tensor,
targets: Tensor,
*,
epsilon: float = 1.0,
alpha: Optional[float] = 0.25,
gamma: float = 2.0,
) -> Tensor
Poly-1 focal loss between per-class logits and binary targets (no reduction).
The focal loss plus its first Poly-1 correction term of PolyLoss: A Polynomial Expansion Perspective of Classification Loss Functions (Leng et al., 2022), with \(p\) the sigmoid of the logit, \(p_t = p\) on the positive entries and \(1 - p\) elsewhere,
Parameters:
-
logits(Tensor) βPer-class logits, shape \((\ldots, C)\).
-
targets(Tensor) βBinary (one-hot) targets of the same shape.
-
epsilon(float, default:1.0) βWeight \(\epsilon\) of the Poly-1 term.
-
alpha(Optional[float], default:0.25) βPositive/negative balance \(\alpha\);
Nonedisables it. -
gamma(float, default:2.0) βFocusing exponent \(\gamma\).
Returns:
-
TensorβThe per-element loss, shape \((\ldots, C)\).
Shape
- logits: \((\ldots, C)\)
- targets: \((\ldots, C)\)
- output: \((\ldots, C)\)