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