Sub topic 5
Heatmaps: Representing Each Keypoint as a Spatial Map
Assume you know basic Python and NumPy, convolutional neural networks, image coordinates, and object-detection bounding boxes. This chapter focuses on how a pose model represents keypoints during prediction and training.
From coordinates to maps
A keypoint is a named body location, such as the left wrist. A model could predict its coordinate directly as (x, y). A heatmap is an alternative: it is a two-dimensional grid in which every cell stores how likely that cell is to contain one keypoint.
A pose model usually produces one heatmap per keypoint type. For the COCO layout, the output has 17 channels:
- Channel 0 represents the nose.
- Channel 1 represents the left eye.
- Other channels represent ears, shoulders, elbows, wrists, hips, knees, and ankles.
Each channel is spatially aligned with the input image, but often at a lower resolution. If the input is 256 × 192 pixels and the model stride is 4, the heatmap may be 64 × 48 cells. A stride of 4 means that one heatmap cell corresponds to four input pixels in each direction.
| Representation | Output for one keypoint | Main strength | Main weakness |
|---|---|---|---|
| Coordinate regression | Two numbers, `x` and `y` | Compact output | Loses spatial uncertainty |
| Heatmap | A two-dimensional score grid | Preserves location information | Uses more memory and needs decoding |
The highest-valued cell in a heatmap is the model's estimated keypoint location. Nearby high values indicate uncertainty or spatial tolerance.
Gaussian target heatmaps
During training, the target is usually not a single cell containing 1. Instead, the target keypoint is drawn as a two-dimensional Gaussian, which looks like a smooth hill.
For a keypoint centered at (cx, cy), the target value at cell (x, y) can be written as:
H(x, y) = exp(-((x - cx)^2 + (y - cy)^2) / (2σ^2))
The center has a value near 1. Values decrease as the distance from the center increases. The parameter σ, called the standard deviation, controls the hill's width.
A Gaussian target gives the model useful gradients around the labelled position. A one-cell target would make nearly every other cell exactly zero and would make small labelling or quantization errors less forgiving.
The model is trained by comparing its predicted heatmap with the target heatmap. A common choice is mean squared error, computed over heatmap cells. The loss penalizes incorrect high values and rewards a peak in the correct region.
Resolution and uncertainty
Higher-resolution heatmaps can locate joints more precisely, but they require more computation and memory. Lower-resolution heatmaps are cheaper but may merge nearby joints or produce quantization error when a peak falls between cells. Some systems refine the peak with local offsets or use a higher-resolution prediction head.
The heatmap's maximum value is often treated as a confidence score. This is useful, but it is not automatically a calibrated probability. A model can produce a high peak for an incorrect joint, especially under occlusion.
Exercise
An input image uses a heatmap stride of 4. The wrist peak is at heatmap cell (x=12, y=20). What approximate input-pixel coordinate does this represent before crop correction?
Show answer
(48, 80), found by multiplying both heatmap coordinates by 4.