Sub topic 9
Top-down estimation: detect, crop, estimate
This chapter assumes you know what keypoints and heatmaps are, how a heatmap is decoded with an arg-max, and how an object detector returns bounding boxes. It covers the top-down pipeline: how it is built, why it works well, where it breaks, and how to choose it over the alternatives.
The idea in one pass
A top-down pose estimator splits the problem into two stages.
- A person detector finds every person in the image and returns one bounding box per person.
- A single-person pose model runs on each box separately. It outputs K keypoints (K = 17 for COCO) for that one person.
The pose model never has to decide which person a joint belongs to. The crop already answers that question. The cost is that the pose model runs once per detected person.
Stage 1: the detector
Any detector that handles the person class will do. Common choices are Faster R-CNN, YOLO variants and RTMDet. Two settings matter more than the architecture.
- Score threshold. A low threshold keeps partly hidden people but adds false boxes. Each false box produces a skeleton of confident-looking keypoints on a coat rack or a shadow. Pose models are trained on real people and do not reliably reject non-people crops.
- Non-maximum suppression (NMS). NMS removes duplicate boxes for the same person. If the IoU threshold is too high, you get two boxes and two overlapping skeletons. If it is too low, two people standing close together become one box.
Only the detector's person boxes are used. Its recall caps the whole system: a person the detector misses gets no skeleton, whatever the pose model could have done.
Stage 2: crop and normalise
The pose network expects a fixed input size, commonly 256×192 (height × width) or 384×288. A person box has an arbitrary shape, so the crop step has three parts.
- Expand the box. Detector boxes are often tight and may cut off hands or feet. Pose pipelines usually pad the box by about 25%.
- Fix the aspect ratio. Grow the shorter side until the box matches the network's aspect ratio (3:4 for 256×192). This avoids stretching the person, which would distort limb proportions.
- Resize and normalise. Apply an affine warp to the fixed input size, then the same mean and standard-deviation normalisation used in training.
The crop is a similarity or affine transform, so you must keep it. Every keypoint the network predicts is in crop coordinates and has to be mapped back through the inverse transform. Forgetting this step is the most common bug in custom top-down code.
Stage 3: estimate and map back
The network outputs K heatmaps at a lower resolution than the input. With a 256×192 input and a stride of 4, each heatmap is 64×48. Decoding takes the arg-max of each heatmap, which gives coordinates in heatmap cells, and multiplies by the stride to reach crop pixels. The decode_heatmaps function from the previous chapter does exactly this.
Then the inverse crop transform takes the points to the original image. This sketch assumes the crop was made by scaling a box and shifting its origin, with no rotation:
import numpy as np
def decode_heatmaps(heatmaps, stride):
k, h, w = heatmaps.shape
flat = heatmaps.reshape(k, -1)
idx = flat.argmax(axis=1)
scores = flat.max(axis=1)
ys, xs = np.divmod(idx, w)
coords = np.stack([xs, ys], axis=1).astype(np.float32) * stride
return coords, scores
def crop_to_image(coords, box, input_w, input_h):
"""Map (K, 2) crop-pixel coordinates back to the original image.
box: (x0, y0, x1, y1) region that was resized to (input_w, input_h).
"""
x0, y0, x1, y1 = box
sx = (x1 - x0) / input_w
sy = (y1 - y0) / input_h
out = coords.copy()
out[:, 0] = coords[:, 0] * sx + x0
out[:, 1] = coords[:, 1] * sy + y0
return out
# A fake heatmap stack: 17 keypoints, 64x48 cells, stride 4 -> 256x192 input.
rng = np.random.default_rng(0)
heatmaps = rng.random((17, 64, 48)).astype(np.float32)
coords, scores = decode_heatmaps(heatmaps, stride=4)
box = (100, 50, 400, 450) # padded person box in the original image
points = crop_to_image(coords, box, input_w=192, input_h=256)
print(points.shape, scores.shape)The box passed to crop_to_image must be the padded, aspect-corrected region that was actually cropped. It is not the raw detector box. Using the raw box shifts and rescales every keypoint, and the error is largest for the limbs far from the box centre.
Per-keypoint score versus box score
Each skeleton carries two kinds of confidence: the detector's box score and K heatmap peak values. They answer different questions. The box score says "this is a person". The peak values say "this joint is clearly localised". A common final score for a person is the mean of keypoint scores above a small threshold, optionally multiplied by the box score. COCO-style evaluation ranks people by this single number, so the way you combine them affects average precision.
Refinements used in practice
- Flip testing. Run the crop and its mirror image, flip the second set of heatmaps back, swap left and right channels, and average. This adds a second forward pass and typically gives a small accuracy gain.
- Sub-pixel shift. A plain arg-max is accurate only to one heatmap cell, which is 4 input pixels at stride 4. Many implementations shift the peak a quarter of a cell toward the larger neighbouring value.
- Distribution-aware decoding. Better decoders fit a Gaussian around the peak. This matters most for small crops, where one cell covers a large part of the body.
Trade-offs and when to choose top-down
| Property | Top-down | Bottom-up |
|---|---|---|
| Cost with N people | Grows with N (one pose pass each) | Roughly constant |
| Accuracy on isolated people | Higher, since each person gets full network resolution | Lower, since people are small in the full image |
| Grouping joints into people | Free, the crop decides | Needs a separate grouping step |
| Depends on detector | Yes, misses and duplicates propagate | No |
| Crowded scenes | Boxes overlap and include neighbours | Handles crowds more naturally |
Pick top-down when you need the best accuracy, the number of people is small or bounded, and a reliable person detector exists. Consider bottom-up or single-stage models when you must run in real time with unpredictable crowd sizes.
Failure modes specific to top-down
- Overlapping boxes. When two people overlap, the crop for person A contains part of person B. The heatmap for "left wrist" may peak on B's wrist, because the network was trained on one person per crop and has no way to know which one you meant. Training with crops that include neighbours partly addresses this, but not fully.
- Detector misses. Heavy occlusion, unusual poses and very small people often lose the box, so no pose is produced.
- Duplicate skeletons. Two boxes survive NMS for one person, so you see two nearly identical skeletons. Pose-level NMS, which compares skeletons using OKS instead of box IoU, removes these.
- Tiny people. A person 40 pixels tall is upsampled to 256 pixels. The network has little real detail to work with, and the heatmap cells are coarse relative to the person.
- Unbalanced latency. A frame with 30 people costs 30 pose passes. Batch the crops into one tensor to use the GPU well, and cap the number of boxes per frame if latency matters.
Debugging checklist
- Draw the detector boxes alone. If a person has no box, the pose model is not at fault.
- Save and view the actual crop that goes into the network. Check for stretching, cut-off limbs and wrong colour order (RGB versus BGR).
- Plot the raw heatmaps for one failing keypoint. A flat map means the joint is not visible. A map with two peaks suggests a neighbouring person or a left-right ambiguity.
- Convert one keypoint by hand through the inverse transform and compare it with your code's output.
Exercise
A top-down pipeline uses a 256×192 (height × width) input and a stride of 4. The padded crop box in the original image is (200, 100, 440, 400). The arg-max of the left-wrist heatmap is at column 24, row 32 (in heatmap cells). Where is the left wrist in the original image?
Show answer
Crop pixels: x = 24 × 4 = 96, y = 32 × 4 = 128. The box is 240 wide and 300 tall, so the scale factors are 240 / 192 = 1.25 horizontally and 300 / 256 = 1.171875 vertically. Original x = 96 × 1.25 + 200 = 320. Original y = 128 × 1.171875 + 100 = 250. The wrist is at about (320, 250). Note that the box has aspect ratio 4:5 rather than 3:4, so this crop was not aspect-corrected, which would stretch the person.
Exercise
Two people stand close together and a pose model repeatedly places person A's right wrist on person B. Name two causes in a top-down pipeline and one fix for each.
Show answer
Cause 1: the crop for A includes part of B, and the single-person network picks B's wrist. A fix is to train with crops that contain neighbours, or to tighten the box. Cause 2: the detector returned two overlapping boxes or merged boxes. A fix is to tune the NMS threshold or apply pose-level NMS using OKS.