Sub topic 7
Backbone, neck and head: where the work happens
This chapter assumes you know what a convolutional neural network (CNN) is, how a convolution produces a feature map, and the basic YOLO idea from earlier chapters: one forward pass that outputs boxes, scores and classes. Here we open the model and look at its three parts.
Why split a detector into three parts
A detector has to answer two questions about an image at once: what is in it, and where. Classification networks answer only the first. They shrink the image step by step into a compact description and discard position detail. A detector needs that compact description, but it also needs spatial detail to place boxes precisely.
YOLO-style models handle this with three stages:
- Backbone: a CNN that turns the image into feature maps at several resolutions.
- Neck: layers that mix those feature maps so that fine detail and high-level meaning end up in the same place.
- Head: layers that turn the mixed feature maps into box coordinates and class scores.
Backbone: extracting features
The backbone is a stack of convolution layers. Each time the image passes through a layer with stride 2, the feature map halves in width and height and usually gains channels. Early layers respond to edges and textures. Later layers respond to parts and whole objects.
The stride of a feature map is how many input pixels one cell covers. A cell on a stride-32 map summarizes a 32x32 pixel region of the input. For a 640x640 input, the maps have these sizes:
| Level | Stride | Grid size | Best for |
|---|---|---|---|
| P3 | 8 | 80x80 | Small objects |
| P4 | 16 | 40x40 | Medium objects |
| P5 | 32 | 20x20 | Large objects |
Deep layers have a large receptive field, the region of the input that can influence one cell. That is why they capture context and whole objects. They also lose fine position detail, which is why a small object can vanish at stride 32. A 16-pixel object covers only half a cell on that map.
Recent YOLO backbones are built from CSP-style blocks (Cross Stage Partial). A CSP block splits the channels into two paths. One path goes through a few bottleneck convolutions, and the other skips them. The two are then concatenated. This cuts computation and gives gradients short routes back to early layers. The exact block (C3, C2f, C3k2 and so on) differs by version. The role is the same.
Backbones are usually pretrained, commonly on a large dataset such as COCO. This is why fine-tuning works with modest data: the backbone already knows generic visual features.
Neck: merging scales
The backbone's three outputs are complementary but mismatched:
- P3 has fine location detail but weak semantics, since it sits early in the network.
- P5 has strong semantics but coarse location.
The neck fixes this by passing information between levels. The common design is a feature pyramid with two passes:
- Top-down: upsample P5 and merge it into P4, then upsample the result and merge it into P3. Fine maps gain semantic information.
- Bottom-up: downsample P3 and merge it into P4, then merge that into P5. Coarse maps gain precise location information.
This two-pass pattern is called FPN (Feature Pyramid Network) for the top-down part and PAN (Path Aggregation Network) for the bottom-up part. Merging is typically done by concatenating channels and then applying a convolution, not by adding values.
Some versions add a small module before the neck to enlarge the receptive field further. SPPF (Spatial Pyramid Pooling, Fast) is one: it applies max-pooling several times in a row and concatenates the results. It sits at the end of the backbone and costs little.
Head: producing predictions
The head runs on each of the three neck outputs. Every grid cell on every level produces one prediction. For a 640x640 input the total number of predictions is:
strides = [8, 16, 32]
size = 640
cells = [(size // s) ** 2 for s in strides]
print(cells) # [6400, 1600, 400]
print(sum(cells)) # 8400That 8400 is why raw YOLOv8 output has 8400 candidates per image. Non-maximum suppression (NMS), covered earlier, trims them to the final detections.
Each prediction holds two kinds of information:
- A box: where the object is, relative to the cell.
- Class scores: one number per class.
Older YOLO versions used a coupled head, one set of layers predicting everything, plus an extra objectness score. Newer versions use a decoupled head: separate convolution branches for box regression and for classification. Box regression and classification want different features, and sharing layers makes them compete. Splitting them generally trains faster and gives better accuracy. YOLOv8 also dropped the separate objectness score. The class score serves as the confidence.
Many modern heads are anchor-free. They predict the distance from the cell centre to the four box edges instead of offsets from preset anchor boxes. This removes the need to tune anchor sizes for each dataset.
You can inspect the structure of a real model yourself:
from ultralytics import YOLO
model = YOLO("yolov8n.pt")
model.info() # prints layer count, parameter count and GFLOPs
# Run a dummy forward pass to see the raw output shape.
import torch
x = torch.zeros(1, 3, 640, 640)
out = model.model.eval()(x)
raw = out[0] if isinstance(out, (list, tuple)) else out
print(raw.shape) # torch.Size([1, 84, 8400]) for 80 classesThe shape [1, 84, 8400] reads as: one image, 84 values per candidate (4 box values plus 80 class scores), and 8400 candidates.
How the parts map to model sizes
Variants of one YOLO version, such as n, s, m, l and x, usually keep the same three-part layout. They change only two scaling factors: depth (how many blocks are repeated) and width (how many channels each layer has). The nano model is fast and small. The extra-large model is slower and more accurate. Choosing a size is a trade between speed, memory and accuracy, not a change of design.
Edge cases and gotchas
- Input size must be divisible by the largest stride. With a maximum stride of 32, use sizes such as 320, 640 or 1280. Ultralytics adjusts other values, but you should choose a multiple of 32 on purpose.
- Small objects suffer first. If objects are only a few pixels wide, P3 may still be too coarse. Raising
imgszor tiling the image helps more than a larger model. - Changing the number of classes only changes the head. When you fine-tune on your own classes, the final classification layers are re-initialized. The backbone and neck weights are reused. This is why the "transferred weights" message appears at the start of training.
- Freezing layers. For very small datasets, you can freeze the backbone with
freeze=10inmodel.train(...)so only later layers update. This lowers the risk of overfitting but limits adaptation. - Output shape differs by version and export format. Some versions emit
[1, 84, 8400], others[1, 8400, 84]or already-filtered boxes. Check the shape before writing your own decoding code.
Summary of responsibilities
| Part | Input | Output | Main job |
|---|---|---|---|
| Backbone | Image | Feature maps at 3 strides | Extract visual features |
| Neck | Backbone maps | Merged maps at 3 strides | Combine detail and meaning |
| Head | Merged maps | Boxes and class scores per cell | Predict objects |
Exercise
A model takes a 1280x1280 input and uses strides 8, 16 and 32. How many candidate predictions does the head produce per image, and which level contributes the most?
Show answer
The grids are 160x160 = 25600, 80x80 = 6400 and 40x40 = 1600. The total is 33600 candidates. The stride-8 level (P3) contributes the most, 25600 of them, because it has the finest grid.
Exercise
You fine-tune a detector and find that it misses very small objects but detects large ones well. Name two changes related to the architecture or input that could help, and explain why.
Show answer
Increase the input size (for example from 640 to 1280), so small objects cover more cells on the fine P3 map. Alternatively, tile the image into overlapping crops so each small object is larger relative to the input. A model with a stride-4 output level would also help, but that is a different architecture. A larger model size alone helps less, because the limit is the resolution of the feature map, not the capacity.