Heatmap Module#
Heatmap module.
- class landmarker.heatmap.GaussianHeatmapGenerator[source]#
Gaussian heatmap generator for generating heatmaps from landmarks.
- Parameters:
nb_landmarks (int) β number of landmarks
sigmas (float or list[float] or torch.Tensor or np.ndarray) β sigmas of the gaussian heatmap function
gamma (float or None) β scaling factor of the gaussian heatmap function
rotation (float or list[float] or torch.Tensor or np.ndarray) β rotation of the gaussian heatmap function
heatmap_size (tuple[int, int]) β size of the heatmap
full_map (bool) β whether to return the full heatmap or only the part around the landmark
learnable (bool) β whether the sigmas and rotation are learnable
background (bool) β whether to add a background channel to the heatmap
all_points (bool) β whether to add a channel with the sum of all the landmarks
continuous (bool) β whether to use continuous or discrete landmarks
device (str) β device to use for the heatmap generator
- __init__(nb_landmarks, sigmas=1.0, gamma=None, rotation=0, heatmap_size=(512, 512), full_map=True, learnable=False, background=False, all_points=False, continuous=True, device='cpu')[source]#
- Parameters:
nb_landmarks (int) β
sigmas (float | list[float] | Tensor | ndarray) β
gamma (float | None) β
rotation (float | list[float] | Tensor | ndarray) β
heatmap_size (tuple[int, int]) β
full_map (bool) β
learnable (bool) β
background (bool) β
all_points (bool) β
continuous (bool) β
device (str | device) β
- Return type:
None
- heatmap_fun(landmark_t, coords, covariance, gamma)[source]#
Gaussian heatmap function
- Parameters:
landmark_t (torch.Tensor) β coordinates of the landmark (y, x)
coords (torch.Tensor) β coordinates of the pixel (y, x)
covariance (torch.Tensor) β covariance matrix (y, x)
gamma (float or None) β scaling factor of the heatmap function
- Returns:
value of the gaussian heatmap function for the given pixel
- Return type:
torch.Tensor
- class landmarker.heatmap.LaplacianHeatmapGenerator[source]#
Laplacian heatmap generator for generating heatmaps from landmarks.
- Parameters:
nb_landmarks (int) β number of landmarks
sigmas (float or list[float] or torch.Tensor or np.ndarray) β sigmas of the Laplacian heatmap function
gamma (float or None) β scaling factor of the Laplacian heatmap function
rotation (float or list[float] or torch.Tensor or np.ndarray) β rotation of the laplacian heatmap function
heatmap_size (tuple[int, int]) β size of the heatmap
full_map (bool) β whether to return the full heatmap or only the part around the landmark
learnable (bool) β whether the sigmas and rotation are learnable
background (bool) β whether to add a background channel to the heatmap
all_points (bool) β whether to add a channel with the sum of all the landmarks
continuous (bool) β whether to use continuous or discrete landmarks
device (str) β device to use for the heatmap generator
- __init__(nb_landmarks, sigmas=1.0, gamma=None, rotation=0, heatmap_size=(512, 512), full_map=True, learnable=False, background=False, all_points=False, continuous=True, device='cpu')[source]#
- Parameters:
nb_landmarks (int) β
sigmas (float | list[float] | Tensor | ndarray) β
gamma (float | None) β
rotation (float | list[float] | Tensor | ndarray) β
heatmap_size (tuple[int, int]) β
full_map (bool) β
learnable (bool) β
background (bool) β
all_points (bool) β
continuous (bool) β
device (str | device) β
- Return type:
None
- heatmap_fun(landmark_t, coords, covariance, gamma=None)[source]#
Laplacian heatmap function
- Parameters:
landmark_t (torch.Tensor) β coordinates of the landmark (y, x)
coords (torch.Tensor) β coordinates of the pixel (y, x)
covariance (torch.Tensor) β covariance matrix (y, x)
gamma (float or None) β scaling factor of the heatmap function
- Returns:
value of the gaussian heatmap function for the given pixel
- Return type:
torch.Tensor
- landmarker.heatmap.coord_argmax(heatmap)[source]#
Returns the coordinates of the maximum value of the heatmap for each batch and channel (landmark).
- Parameters:
heatmap (torch.Tensor) β heatmap of shape (B, C, H, W)
- Returns:
coordinates of shape (B, C, 2)
- Return type:
(torch.Tensor)
- landmarker.heatmap.coord_cov_from_gaussian_ls(heatmap, gamma=None, ls_library='scipy')[source]#
Returns the modal coordinates and covariance matrix from a heatmap through fitting the heatmap on Gaussian distribution with a specicic scaling factor gamma with help of least squares optimization.
- Parameters:
heatmap (torch.Tensor) β heatmap of shape (B, C, H, W)
gamma (float) β gamma parameter of the gaussian heatmap generator
ls_library (str) β library to use for least squares optimization. (scipy or pytorch)
- Returns:
coordinates of shape (B, C, 2) (torch.Tensor): covariance matrix of shape (B, C, 2, 2)
- Return type:
(torch.Tensor)
- landmarker.heatmap.coord_cov_windowed_weigthed_sample_cov(heatmap)[source]#
Calculate the covariance matrix from a heatmap by calculating the mean of the heatmap values weighted by the heatmap values. The window is determined by the distance to the closest edge.
- Parameters:
heatmap (torch.Tensor) β heatmap of shape (B, C, H, W)
coords (torch.Tensor) β coordinates of shape (B, C, 2)
- Returns:
coordinates of shape (B, C, 2) (torch.Tensor): covariance matrix of shape (B, C, 2, 2)
- Return type:
(torch.Tensor)
- landmarker.heatmap.coord_local_soft_argmax(heatmap, window=3, t=10.0)[source]#
- Returns coordiantes through applying the local soft-argmax function on the heatmaps.
Source: βSubpixel Heatmap Regression for Facial Landmark Localizationβ - Bulat et al. (2021)
- Parameters:
heatmap (torch.Tensor) β heatmap of shape (B, C, H, W)
idx_pred (torch.Tensor) β predicted indices of shape (B, C, 2)
window (int) β local window size
t (float) β temperature that controls the resulting probability map
- Returns:
coordinates of shape (B, C, 2)
- Return type:
(torch.Tensor)
- landmarker.heatmap.coord_soft_argmax(heatmap, eps=1e-06, logit_scale=False, require_grad=False)[source]#
Returns the spatial mean over the softmax distribution of the heatmap. Source: β2D/3D Pose Estimation and Action Recognition using Multitask
Deep Learningβ - Luvizon et al. (2018)
- Parameters:
heatmap (torch.Tensor) β heatmap of shape (B, C, H, W)
eps (float) β epsilon to avoid division by zero
logit_scale (bool) β whether the input is logit scaled
require_grad (bool) β whether to require gradient for the coordinates
- Return type:
Tensor
- landmarker.heatmap.coord_soft_argmax_cov(heatmap, eps=1e-06, logit_scale=True, require_grad=True)[source]#
Returns the spatial mean over the softmax distribution of the heatmap and the covariance matrix. The covariance matrix is the weigthed sample covariance matrix.
- Parameters:
heatmap (torch.Tensor) β heatmap of shape (B, C, H, W)
eps (float) β epsilon to avoid division by zero
logit_scale (bool) β whether the input is logit scaled
require_grad (bool) β whether to require gradient for the coordinates
- Returns:
coordinates of shape (B, C, 2) (torch.Tensor): covariance matrix of shape (B, C, 2, 2)
- Return type:
(torch.Tensor)
- landmarker.heatmap.coord_weighted_spatial_mean(heatmap, eps=1e-05)[source]#
Returns the spatial weighted mean of the possitive elements of the heatmap by the heatmap values. Source: βUGLLI Face Alignment: Estimating Uncertainty with
Gaussian Log-Likelihood Lossβ - Kumar et al. (2019)
- Parameters:
heatmap (torch.Tensor) β heatmap of shape (B, C, H, W)
eps (float) β epsilon to avoid division by zero
- Returns:
coordinates of shape (B, C, 2)
- Return type:
(torch.Tensor)
- landmarker.heatmap.coord_weighted_spatial_mean_cov(heatmap, eps=1e-05)[source]#
Returns the spatial weighted mean and the weighted sample covariance of the possitive elements of the heatmap by the heatmap values.
- Parameters:
heatmap (torch.Tensor) β heatmap of shape (B, C, H, W)
eps (float) β epsilon to avoid division by zero
- Returns:
coordinates of shape (B, C, 2) (torch.Tensor): covariance matrix of shape (B, C, 2, 2)
- Return type:
(torch.Tensor)
- landmarker.heatmap.cov_from_gaussian_ls(heatmap, coords, gamma=None, ls_library='scipy')[source]#
Returns covariance matrix from a heatmap through fitting the heatmap on Gaussian distribution with a specicic scaling factor gamma and specified coordinates with help of least squares optimization.
- Parameters:
heatmap (torch.Tensor) β heatmap of shape (B, C, H, W)
coords (torch.Tensor) β coordinates of shape (B, C, 2)
gamma (float) β gamma parameter of the gaussian heatmap generator
ls_library (str) β library to use for least squares optimization. (scipy or pytorch)
- Returns:
covariance matrix of shape (B, C, 2, 2)
- Return type:
(torch.Tensor)
- landmarker.heatmap.heatmap_coord_to_weighted_sample_cov(heatmap, coords, eps=1e-06, apply_softmax=False)[source]#
- Returns the weighted sample covariance matrix from a heatmap and coordinates.
source: https://en.wikipedia.org/wiki/Weighted_arithmetic_mean#Weighted_sample_covariance
- Parameters:
heatmap (torch.Tensor) β heatmap of shape (B, C, H, W)
coords (torch.Tensor) β coordinates of shape (B, C, 2)
eps (float) β epsilon to avoid division by zero
apply_softmax (bool) β whether to apply softmax on the heatmap
- Returns:
covariance matrix of shape (B, C, 2, 2)
- Return type:
(torch.Tensor)
- landmarker.heatmap.heatmap_to_coord(heatmap, offset_coords=0, method='argmax')[source]#
Returns the retrieved coordinates via specified method from a heatmap. The offset_coords is used to remove the first offset_coords coordinates from the heatmap. This is used to remove the background class (if present).
- Parameters:
heatmap (torch.Tensor) β heatmap of shape (B, C, H, W)
offset_coords (int) β number of coordinates to remove
method (str) β method to retrieve the coordinates
- Returns:
coordinates of shape (B, C, 2)
- Return type:
(torch.Tensor)
- landmarker.heatmap.heatmap_to_coord_cov(heatmap, method='soft_argmax', eps=1e-06, logit_scale=True, require_grad=True)[source]#
Returns the modal coordinates and covariance matrix from a heatmap.
- Parameters:
heatmap (torch.Tensor) β heatmap of shape (B, C, H, W)
method (str) β method to retrieve the coordinates
eps (float) β epsilon to avoid division by zero
logit_scale (bool) β whether the input is logit scaled
require_grad (bool) β whether to require gradient for the coordinates
- Returns:
coordinates of shape (B, C, 2) (torch.Tensor): covariance matrix of shape (B, C, 2, 2)
- Return type:
(torch.Tensor)
- landmarker.heatmap.heatmap_to_coord_enlarge(heatmap, offset_coords=0, method='argmax', enlarge_factor=1, enlarge_dim=None, enlarge_mode='bilinear')[source]#
Returns the retrieved coordinates via specified method from an enlarged heatmap. The offset_coords is used to remove the first offset_coords coordinates from the heatmap. This is used to remove the background class (if present).
- Parameters:
heatmap (torch.Tensor) β heatmap of shape (B, C, H, W)
offset_coords (int) β number of coordinates to remove
method (str) β method to retrieve the coordinates
enlarge_factor (int) β factor to enlarge the heatmap
enlarge_dim (tuple[int, int] or None) β dimensions to enlarge the heatmap to
enlarge_mode (str) β interpolation mode to enlarge the heatmap
- Returns:
coordinates of shape (B, C, 2)
- Return type:
(torch.Tensor)
- landmarker.heatmap.heatmap_to_multiple_coord(heatmaps, window=5, threshold=None, method='argmax')[source]#
Returns the multiple coordinates of the maximum value of the heatmap for each batch and channel. Additionally, the scores of the maxima are returned. If threshold is not None, only the maxima with a score higher than the threshold are returned.
- Parameters:
heatmap (torch.Tensor) β heatmap of shape (B, C, H, W)
window (tuple) β window size
threshold (float) β threshold to remove points. If None, no threshold is applied. Default None.
method (str) β method to retrieve the coordinates
heatmaps (Tensor) β
- Returns:
- list of coordinates of the maxima for each batch and
channel, with the first list the batch, the second list the channel and the third list the coordinates
- (list(list(list(float)))): list of scores of the maxima for each batch and channel, with
the first list the batch, the second list the channel and the third list the scores.
- Return type:
(list(list(list(torch.Tensor))))
- landmarker.heatmap.non_maximum_surpression(heatmap, window=3)[source]#
Non-Maximum Surpression (NMS)
source: Efficient Non-Maximum Suppression - Neubeck and Van Gool (2006)
- Parameters:
heatmap (torch.Tensor) β heatmap of shape (H, W)
window (int) β window size
- Returns:
list of coordinates of the maxima
- Return type:
(list(tuple(int, int)))
- landmarker.heatmap.non_maximum_surpression_local_soft_argmax(heatmap, window=3)[source]#
Returns the coordinates of the maximum value of the heatmap for each batch and channel (landmark), with the soft-argmax function applied on the heatmaps.
- Parameters:
heatmap (torch.Tensor) β heatmap of shape (H, W)
window (int) β window size
- Returns:
list of coordinates of the maxima
- Return type:
(list(tuple(int, int)))
- landmarker.heatmap.weighted_sample_cov(heatmap, coords, eps=1e-06)[source]#
Calculate the covariance matrix from a heatmap by calculating the mean of the heatmap values weighted by the heatmap values. source: https://en.wikipedia.org/wiki/Weighted_arithmetic_mean#Weighted_sample_covariance
- Parameters:
heatmap (torch.Tensor) β heatmap of shape (B, C, H, W)
coords (torch.Tensor) β coordinates of shape (B, C, 2)
eps (float) β epsilon to avoid division by zero
- Returns:
covariance matrix of shape (B, C, 2, 2)
- Return type:
(torch.Tensor)