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')[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.EuclideanDistanceJSDivergenceReg[source]#

Euclidean distance loss with Jensen-Shannon divergence regularization. The regularization term imposes a Gaussian distribution on the predicted heatmaps and calculates the Jensen-Shannon divergence between the predicted and the target heatmap. The Jensen-Shannon divergence is a method to measure the similarity between two probability distributions. It is a symmetrized and smoothed version of the Kullback-Leibler divergence, and is defined as the average of the Kullback-Leibler divergence between the two distributions and a mixture M of the two distributions:

\(JSD(P||Q) = 0.5 * KL(P||M) + 0.5 * KL(Q||M)\) where \(M = 0.5 * (P + Q)\)

Generalization of regularization term proposed by Nibali et al. (2018), which only considers Multivariate Gaussian distributions, to a generalized Gaussian distribution. (However, now we only consider the Gaussian and the Laplace distribution.)

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

  • sigma_t (float, optional) – Target sigma value. Defaults to 1.0.

  • rotation_t (float, optional) – Target rotation value. Defaults to 0.0.

  • nb_landmarks (int, optional) – Number of landmarks. Defaults to 1.

  • heatmap_fun (str, optional) – Specifies the type of heatmap function to use. Defaults to β€˜gaussian’. Possible values are β€˜gaussian’ and β€˜laplacian’.

  • heatmap_size (tuple[int, int], optional) – Size of the heatmap. Defaults to (512, 512).

  • gamma (Optional[float], optional) – Gamma value for the Laplace distribution. 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, sigma_t=1.0, rotation_t=0.0, nb_landmarks=1, heatmap_fun='gaussian', heatmap_size=(512, 512), gamma=1.0, reduction='mean', eps=1e-06)[source]#

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

Parameters:
  • alpha (float) –

  • sigma_t (float | Tensor) –

  • rotation_t (float | Tensor) –

  • nb_landmarks (int) –

  • heatmap_fun (str) –

  • heatmap_size (tuple[int, int]) –

  • gamma (float | None) –

  • reduction (str) –

  • eps (float) –

Return type:

None

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

  • pred_heatmap (torch.Tensor) – Predicted heatmap.

  • 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)[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')[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)[source]#

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

Parameters:
  • reduction (str) –

  • eps (float) –

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.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')[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', **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')[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