Sub topic 8
Training: Gaussian Targets, Losses and Augmentation
This chapter assumes basic Python and NumPy, familiarity with convolutional neural networks, object-detection boxes, and supervised-learning loss functions.
A keypoint model usually predicts one spatial heatmap for each keypoint type. For the COCO layout, the model predicts 17 maps: one for the nose, two for the eyes, two for the ears, and so on. Training requires a target heatmap that describes where each labelled keypoint should appear.
Building Gaussian heatmap targets
A target heatmap places a two-dimensional Gaussian, or smooth hill, at the labelled keypoint location. If the keypoint is at image location (x0, y0), the target value at grid location (x, y) is:
G(x, y) = exp(-((x - x0)^2 + (y - y0)^2) / (2 sigma^2))The centre has a value near 1. Values decrease smoothly as distance from the keypoint increases. The parameter sigma controls the hill's width.
The target is usually built at a lower resolution than the input image. If the input is 256 by 256 pixels and the output heatmap is 64 by 64, coordinates are divided by a stride of 4. A larger sigma represents more positional tolerance. Small keypoints or precise labels may use a narrower Gaussian.
Losses and visibility
A common loss is mean squared error between the predicted and target heatmaps:
L = mean((prediction - target)^2)The loss is calculated across keypoint channels and heatmap pixels. For an unlabelled or invisible keypoint, the target should not contribute to the loss. A visibility mask can set its loss weight to zero.
import numpy as np
def gaussian_target(height, width, x, y, sigma):
yy, xx = np.mgrid[:height, :width]
return np.exp(-((xx - x) ** 2 + (yy - y) ** 2) / (2 * sigma ** 2))
def masked_heatmap_mse(prediction, target, visible):
error = (prediction - target) ** 2
weights = np.asarray(visible, dtype=float)[:, None, None]
weighted_error = error * weights
denominator = weights.sum() * prediction.shape[1] * prediction.shape[2]
return float(weighted_error.sum() / max(denominator, 1.0))
target = gaussian_target(64, 64, x=28.5, y=19.0, sigma=2.0)
prediction = np.zeros((1, 64, 64), dtype=float)
loss = masked_heatmap_mse(prediction, target[None, ...], [1])
print(loss)Some systems use a weighted loss that emphasizes pixels near the Gaussian centre. Other systems use focal-style losses when most heatmap pixels are close to zero. The choice affects how strongly the model learns precise peaks versus broad approximate locations.
Augmentation and coordinate consistency
Augmentation improves generalization by showing varied poses, scales, lighting, and camera views. Common operations include horizontal flips, random scaling, rotation, cropping, and colour changes.
Geometric augmentation must update both the image and every visible keypoint. A horizontal flip changes an x-coordinate from x to image_width minus x. It also swaps semantic pairs such as left wrist and right wrist. After transforming image coordinates, convert them to heatmap coordinates using the output stride.
A frequent bug is applying the image transformation but leaving the keypoint labels unchanged. The model then receives contradictory training data and learns blurred or misplaced peaks.
Exercise
Exercise
An input image is scaled by 0.5 and then represented by a heatmap with stride 4 relative to the scaled image. A keypoint originally at (80, 40) moves to (40, 20) after scaling. What heatmap coordinate should be used?
Show answer
Divide the transformed coordinate by 4, giving (10, 5). The Gaussian centre belongs at heatmap coordinate (10, 5).