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β.
- 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
- 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.
- 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.
- 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.
- 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β.
- 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.
- 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β.