Building YOLO-Pose from Scratch, Part 1: From Pixels to Skeletons
I’ve used a pretrained pose model in an app. I could take a camera frame and draw joints on the screen. If someone had asked me what happened between those two steps, I would have had trouble explaining it.
So I’m building a small YOLO-style pose model from scratch. The goal is to train it from randomly initialized weights and run it offline on an iPhone. This first part is a design walkthrough. We’ll follow a single camera frame from pixels to skeletons and account for every dimension along the way.
What are we asking the model to predict?
For each person in a frame, we want a bounding box around the body and a set of keypoints for locations such as the nose, shoulders, and wrists. Connecting some of those keypoints with lines gives us the skeleton we can draw. A frame might contain two people, ten people, or nobody.
In a top-down approach, a person detector finds each person first. A pose model then works on each person’s crop. The single-stage design I’m building reads the whole frame once and predicts boxes and keypoints together. That raises a question I got wrong at first. If three people appear, does the model’s output get three times bigger?
Why the raw output has a fixed shape
Take a 320 × 320 RGB image. With PyTorch’s channel-first convention, its shape is (3, 320, 320). The first dimension holds the red, green, and blue channels. A batch is a group of images passed through the model together. Eight images would have shape (8, 3, 320, 320).
A convolution moves a small filter across the image. The kernel size is the size of the filter, the stride is the spacing between filter positions, and padding adds pixels around the boundary. With a 3 × 3 filter, stride 2, and one pixel of padding on each side, we get 160 output positions along each axis.
floor((320 + 2 × 1 − 3) / 2) + 1 = 160
With 32 filters, the output shape for one image is (32, 160, 160). I initially read the 32 in Conv2d(3, 32, …) as a 32 × 32 kernel. It’s the number of output channels. The values in those channels change with the image, while the number of positions follows from the input size and layer settings. The same reasoning applies to our pose head.
One candidate per grid cell
Think of the head’s output as a grid of slots. Each slot can propose a person. The slot’s position tells us where to start looking, and its predicted values say what might be there.
I first thought I could count the cells covered by a person. One close-up person might cover four cells, while two smaller people might cover two cells each. The count is four in either image. For the first training design, I’ll choose a grid scale for each labeled person and assign the cell that contains the center of their bounding box. That cell gets a positive person target. If two centers land in the same cell at the same scale, there’s a conflict the assignment rule will have to handle.
At inference, every cell can still emit a candidate. Training assignment tells the model which slots should learn from each labeled person. Score filtering and duplicate removal decide what to keep when the model runs on a new image.
What goes into one candidate?
- 1 person score. How strongly the candidate indicates a person.
- 4 box values. After decoding, a box can be described by its center
(cx, cy), width, and height. - 51 keypoint values. Seventeen points, with an x coordinate, a y coordinate, and a confidence value for each.
That gives 1 + 4 + 17 × 3 = 56 values per cell in this teaching design. The exact encoding of the raw box and keypoint numbers is still a design decision.
Why give a keypoint its own confidence value? Imagine someone turns their head and their left eye is hidden. The model can still put that eye at a plausible-looking location. A simple distance check against nearby joints might accept it. A separate confidence output gives the drawing code another signal when deciding whether to show the point or its connecting lines.
The COCO dataset labels keypoints with (x, y, v). A value of v = 0 means the point has no label. v = 1 means its position is labeled even though it is hidden, and v = 2 means it is labeled and visible. Both 1 and 2 supply coordinates for our coordinate loss. We still need to decide how the dataset’s visibility flags become training targets for the model’s confidence output. COCO annotation format.
Why use three grids?
On a coarse grid, two nearby people can have centers in the same cell. A finer grid gives them more chances to land in different cells. This design uses three grids whose neighboring slots are spaced 8, 16, or 32 pixels apart in the 320 × 320 model input.
- Stride 8. 40 × 40 grid, output
(56, 40, 40), 1,600 candidates. - Stride 16. 20 × 20 grid, output
(56, 20, 20), 400 candidates. - Stride 32. 10 × 10 grid, output
(56, 10, 10), 100 candidates.
Added together, that’s 2,100 candidates per image, even for an empty room. The stride gives the spacing between cells. A predicted box can extend well beyond that spacing, so a stride-8 candidate can still describe a person much larger than 8 × 8 pixels.
How 2,100 candidates become a few skeletons
First, decode converts the head’s raw numbers into box and keypoint coordinates. The exact formula depends on how the model encodes them. Next, we discard candidates whose person score falls below a threshold. Several strong candidates can still describe the same person, so we apply non-maximum suppression (NMS).
NMS keeps a high-scoring box and suppresses lower-scoring boxes that overlap it too much. It measures overlap using Intersection over Union (IoU), the shared area divided by the total area covered by either box. If two boxes each have area 4 and overlap by 2, their IoU is 2 / (4 + 4 − 2) ≈ 0.33.
Suppose candidates A, B, C, and D score 0.90, 0.80, 0.70, and 0.20. A and B describe the same person and have an IoU of 0.90. C describes a second person and has an IoU of 0.10 with A.
- A score threshold of 0.50 drops D.
- NMS keeps A, the highest-scoring remaining box. With an IoU threshold of 0.50, it suppresses B.
- C’s overlap with A is low enough, so C stays. We draw the skeletons attached to A and C.
This is where my original intuition about the changing output size fits. The dense head returns a fixed shape. The list of surviving detections has a length that varies with the image. Setting the NMS threshold too low can hide a second person who stands close to the first. Setting it too high can leave two skeletons on one person.
Getting coordinates back onto the camera frame
A phone frame might be 1920 × 1080, while our model expects 320 × 320. Stretching the frame to a square would also stretch the people in it. Letterboxing preserves the aspect ratio. Here we resize both axes by 1/6, giving 320 × 180, then fill the extra space with 70 pixels above and 70 below.
Take a point at (480, 180) in the camera frame. Resizing gives (80, 30). The top padding moves it down to (80, 100) in the model input. To draw that point on the camera preview, undo the operations in reverse order. Subtract 70 from y, then multiply both coordinates by 6.
A missed padding correction can make every joint appear shifted by the same amount. Square inputs can hide the problem because they may need no padding. A practical check is to choose a point with known coordinates, transform it into model coordinates, then pass it through the same inverse function used for drawing. The original point should come back.
What changes during training?
During inference, we work forward from an image to a list of detected people. During training, we also have ground-truth boxes and keypoints. When we letterbox the image, we apply the same forward transform to those labels. The assignment rule selects which slots learn from each person. We compare the predictions against their targets using losses for the person score, box, and keypoints, then use backpropagation and an optimizer update to improve the model’s weights.
The picture I wanted to be able to explain is now concrete. A frame becomes a fixed set of candidate poses, a few candidates survive, and their coordinates are mapped back onto the original frame. The next implementation milestone is to read out the COCO labels and draw them over real images, so coordinate mistakes are visible before they reach training.
In the next installment I’ll cover the Apple M4 CPU/MPS setup and a small CNN benchmark. The 56 values and 2,100 candidates in this article are design calculations. Full-model accuracy and iPhone performance are still to be measured.