Losses Module#

Losses module for training landmark localization models.

class landmarker.losses.AdaptiveWingLoss[source]#

Adaptive wing loss is a loss function that behaves like a smoothed Wing loss when the target is close to 1 and like the MSE loss when the target is close to 0.

The loss function is defined as:

\[\begin{split}AWing(x, y) = \begin{cases} \omega \log(1 + |\frac{x-y}{\epsilon}|^{\alpha-y} & \text{if } |x - y| < \theta \\ A|x - y| - C & \text{otherwise} \end{cases}})\end{split}\]
where :math:`A = omega (1/(1+(theta/epsilon)^{alpha-y}))(alpha - y)

((theta/epsilon)^(alpha-y-1))(1/epsilon)` and

\(C = (\theta * A - \omega * log(1 + (\theta/\epsilon)^{\alpha-y})))\)

source: β€œAdaptive Wing Loss for Robust Face Alignment via Heatmap Regression” - Wang et al.
Parameters:
  • omega (float, optional) – Adaptive wing loss parameter. Defaults to 5.0.

  • epsilon (float, optional) – Adaptive wing loss parameter. Defaults to 0.5.

  • alpha (float, optional) – Adaptive wing loss parameter. Defaults to 2.1.

  • theta (float, optional) – Adaptive wing loss parameter. Defaults to 0.5.

  • reduction (str, optional) – Specifies the reduction to apply to the output. Defaults to β€˜mean’.

__init__(omega=5, epsilon=0.5, alpha=2.1, theta=0.5, reduction='mean', spatial_dims=2)[source]#

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:
  • omega (float) –

  • epsilon (float) –

  • alpha (float) –

  • theta (float) –

  • reduction (str) –

Return type:

None

forward(pred, target)[source]#
Parameters:
  • pred (torch.Tensor) – Predicted coordinates.

  • target (torch.Tensor) – Target coordinates.

Return type:

Tensor

class landmarker.losses.EuclideanDistanceVarianceReg[source]#

Euclidean distance loss with variance regularization. The regularization term is defined as the squared difference between the fitted/predicted variance and a predefined target variance, as proposed by Nibali et al. (2018). The authors point out that this regularization term does not directly constrain the specif shape of the learned heatmaps.

source: Numerical Coordinate Regression with Convolutional Neural Networks - Nibali et al.
Parameters:
  • alpha (float, optional) – Weight of the regularization term. Defaults to 1.0.

  • var_t (float, optional) – Target variance. Defaults to 1.0.

  • reduction (str, optional) – Specifies the reduction to apply to the output. Defaults to β€˜mean’.

  • eps (float, optional) – Epsilon value to avoid division by zero. Defaults to 1e-6.

__init__(alpha=1.0, var_t=1.0, reduction='mean', eps=1e-06, spatial_dims=2)[source]#

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:
  • alpha (float) –

  • var_t (float) –

  • reduction (str) –

  • eps (float) –

Return type:

None

forward(pred, cov, target)[source]#
Parameters:
  • pred (torch.Tensor) – Predicted coordinates.

  • cov (torch.Tensor) – Related covariance matrix of the predicted coordinates.

  • target (torch.Tensor) – Target coordinates.

Return type:

Tensor

class landmarker.losses.GaussianHeatmapL2Loss[source]#

Loss function for Gaussian heatmap regression. source: http://arxiv.org/abs/2109.09533

__init__(alpha=5, reduction='mean', spatial_dims=2)[source]#

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(heatmap, sigmas, heatmap_target)[source]#

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class landmarker.losses.GeneralizedNormalHeatmapLoss[source]#

Loss function for adaptive generalized normal direct heatmap regression. The loss function is an extension of the loss function proposed by Thaler et al. (2021) for adaptive heatmap regression, where they used a anistropic Gaussian distribution for adaptive heatmap regression. The loss function is defined as the sum of a specified distance function between the predicted heatmap and the target heatmap, additionaly a determinant of the supplied covariance matrix is added as regularization term to penalize the loss of the fitted covariance matrix.

# TODO: add formula.

source: Modeling Annotation Uncertainty with Gaussian Heatmaps in Landmark Localization

Parameters:
  • alpha (float, optional) – Weight of the regularization term. Defaults to 5.0.

  • distance (str, optional) – Distance function to use for the loss calculation. Defaults to β€˜l2’. Possible values are β€˜l2’, β€˜l1’, β€˜smooth-l1’, β€˜bce-with-logits’ and β€˜bce’.

  • reduction (str, optional) – Specifies the reduction to apply to the output. Defaults to β€˜mean’.

  • **kwargs – Additional keyword arguments for the distance function.

__init__(alpha=5.0, distance='l2', reduction='mean', **kwargs)[source]#

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:
  • alpha (float) –

  • distance (str) –

  • reduction (str) –

Return type:

None

forward(heatmap, cov, heatmap_target)[source]#
Parameters:
  • heatmap (torch.Tensor) – Predicted heatmap.

  • cov (torch.Tensor) – Covariance matrix of the fitted heatmap.

  • heatmap_target (torch.Tensor) – Target heatmap.

Returns:

Loss value.

Return type:

torch.Tensor

class landmarker.losses.MultivariateGaussianNLLLoss[source]#

Negative log-likelihood loss for multivariate Gaussian distributions. The loss function is defined as the negative log-likelihood of the predicted coordinates given the predicted covariance matrix. The loss function is defined as:

\(NLL = 0.5 * (log(det(Cov)) + (x - mu)^T * Cov^{-1} * (x - mu))\)

As proposed in: β€œUGLLI Face Alignment: Estimating Uncertainty with Gaussian Log-Likelihood

Loss” - Kumar et al. (2019)

Parameters:
  • reduction (str, optional) – Specifies the reduction to apply to the output. Defaults to β€˜mean’.

  • eps (float, optional) – Epsilon value to avoid division by zero. Defaults to 1e-6.

__init__(reduction='mean', eps=1e-06, spatial_dims=2)[source]#

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:
  • reduction (str) –

  • eps (float) –

  • spatial_dims (int) –

forward(pred, cov, target)[source]#
Parameters:
  • pred (torch.Tensor) – Predicted coordinates.

  • cov (torch.Tensor) – Related covariance matrix of the predicted coordinates.

  • target (torch.Tensor) – Target coordinates.

Return type:

Tensor

class landmarker.losses.NLLLoss[source]#

Negative log-likelihood loss for 2D/3D heatmaps. Assumes that the input is a probability distribution and calculates the negative log-likelihood of the predicted heatmap given the target heatmap.

Parameters:
  • spatial_dims (int, optional) – Spatial dimension of the heatmaps. Defaults to 2.

  • reduction (str, optional) – Specifies the reduction to apply to the output. Defaults to β€˜mean’.

__init__(spatial_dims=2, apply_softmax=True, reduction='mean')[source]#

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:
  • spatial_dims (int) –

  • apply_softmax (bool) –

  • reduction (str) –

forward(output, target)[source]#

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class landmarker.losses.StackedLoss[source]#

Stacked loss function. Applies a specified loss function to each list of predictions. This loss function is used to calculate the loss for each heatmap in the stacked heatmap regression, suchs as stacked hourglass and U-Net networks.

Parameters:
  • loss_fn (nn.Module) – Loss function to use.

  • reduction (str, optional) – Specifies the reduction to apply to the output. Defaults to β€˜mean’.

__init__(loss_fn, reduction='mean', **kwargs)[source]#

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:
  • loss_fn (Module) –

  • reduction (str) –

Return type:

None

forward(preds, target)[source]#
Parameters:
  • preds (list[torch.Tensor]) – List of predicted heatmaps or coordinates.

  • target (torch.Tensor) – Target heatmap.

Return type:

Tensor

class landmarker.losses.StarLoss[source]#

Self-adapTive Ambiguity Reduction (STAR) loss. Star loss takes into account the ambuigity (uncertainty) of the intermediate heatmap predictions by using the covariance matrix of the heatmap predictions, and extracting the eigenvectors and eigenvalues of the covariance matrix.

source: β€œSTAR Loss: Reducing Semantic Ambiguity in Facial Landmark Detection” - Zhou et al.
Parameters:
  • omega (float, optional) – Weight of the regularization term. Defaults to 1.0.

  • distance (str, optional) – Distance function to use for the loss calculation. Defaults to β€˜l2’. Possible values are β€˜l2’, β€˜l1’, β€˜smooth-l1’.

  • reduction (str, optional) – Specifies the reduction to apply to the output. Defaults to β€˜mean’.

  • **kwargs – Additional keyword arguments for the distance function.

__init__(omega=1.0, distance='l2', epsilon=1e-05, reduction='mean', spatial_dims=2, **kwargs)[source]#

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:
  • omega (float) –

  • distance (str) –

  • epsilon (float) –

  • reduction (str) –

Return type:

None

forward(pred, cov, target)[source]#
Parameters:
  • pred (torch.Tensor) – Predicted coordinates.

  • cov (torch.Tensor) – Related covariance matrix of the predicted coordinates.

  • target (torch.Tensor) – Target coordinates.

Return type:

Tensor

class landmarker.losses.WingLoss[source]#

Wing loss, proposed for facial landmark detection by Feng et al. (2018), is a piece-wise loss function that focusses more attention on small and medium range erros compared to L2, L1, and smooth L1. It has large gradient when the error is small and a constant gradient when the error is large. The loss function is defined as:

\[\begin{split}Wing(x, y) = \begin{cases} \omega * log(1 + \frac{|x - y|}{\epsilon}) & \text{if } |x - y| < \omega \\ |x - y| - C & \text{otherwise} \end{cases}\end{split}\]
where \(C = \omega - \omega * log(1 + \frac{\omega}{\epsilon})\)
source: β€œWing Loss for Robust Facial Landmark Localisation With Convolutional Neural

Networks” - Feng et al. (2018)

Parameters:
  • omega (float, optional) – Wing loss parameter. Defaults to 5.0.

  • epsilon (float, optional) – Wing loss parameter. Defaults to 0.5.

  • reduction (str, optional) – Specifies the reduction to apply to the output. Defaults to β€˜mean’.

__init__(omega=5.0, epsilon=0.5, reduction='mean', spatial_dims=2)[source]#

Initialize internal Module state, shared by both nn.Module and ScriptModule.

Parameters:
  • omega (float) –

  • epsilon (float) –

  • reduction (str) –

forward(pred, target)[source]#
Parameters:
  • pred (torch.Tensor) – Predicted coordinates.

  • target (torch.Tensor) – Target coordinates.

Return type:

Tensor