Sub topic 3
Backbones turn pixels into feature maps
This chapter assumes you know how convolutional layers work, what a stride is, and how to read a tensor shape such as (C, H, W). Every detector in this tutorial starts from the same step. A network called the backbone converts the image into a grid of feature vectors. Anchors, proposals, and DETR's queries all read from that grid.
Why detectors need a backbone
A detector cannot classify raw pixels directly. A single pixel says almost nothing about whether it belongs to a dog or a car. The backbone is a convolutional network, usually pretrained for image classification, that turns the image into a spatial grid of descriptors. Each grid cell summarizes a region of the image.
Classification networks end by pooling the grid into one vector. A detector removes that pooling and keeps the grid. It needs to know where things are, so the spatial layout must survive.
The output of a backbone is a feature map. A feature map has shape (C, H/s, W/s), where:
Cis the number of channels, meaning the length of the descriptor at each cell.sis the stride, the number of input pixels between neighboring cells.
A cell at row i, column j corresponds to roughly the image point ((j + 0.5) * s, (i + 0.5) * s). This mapping is how later chapters place anchors and decode boxes.
Stages, strides, and channels
Most backbones are built from stages. Each stage runs several blocks at one resolution, then the next stage halves the resolution and increases the channels. ResNet-50, a common choice, has these outputs. Detection papers call them C2 to C5.
| Stage | Stride | Channels | Map size for a 480x640 image |
|---|---|---|---|
| C2 | 4 | 256 | 120 x 160 |
| C3 | 8 | 512 | 60 x 80 |
| C4 | 16 | 1024 | 30 x 40 |
| C5 | 32 | 2048 | 15 x 20 |
Two trends run through the table. The grid gets coarser, so there are fewer cells. The channels grow, so each cell carries a richer description. Deeper layers also see more of the image, which gives them more semantic power but less spatial precision.
The stem is the first convolution and max-pool, which together reduce the image by a factor of 4. C2 has the same stride as the stem, so its output is not downsampled again.
Computing shapes and cell locations
Detection code constantly converts between grid cells and image coordinates. The script below computes the output shapes of the four ResNet-50 stages and maps a cell back to the image. It uses only the standard library.
import math
# (name, stride, channels) for the ResNet-50 stage outputs
STAGES = [("C2", 4, 256), ("C3", 8, 512), ("C4", 16, 1024), ("C5", 32, 2048)]
def feature_shape(height, width, stride, channels):
"""Shape (C, H', W'). Padded strided convolutions round up."""
return (channels, math.ceil(height / stride), math.ceil(width / stride))
def cell_center(row, col, stride):
"""Image coordinates (x, y) of the center of a feature-map cell."""
return ((col + 0.5) * stride, (row + 0.5) * stride)
H, W = 480, 640
for name, stride, channels in STAGES:
print(name, feature_shape(H, W, stride, channels))
print(cell_center(row=2, col=3, stride=32))
# C2 (256, 120, 160)
# C3 (512, 60, 80)
# C4 (1024, 30, 40)
# C5 (2048, 15, 20)
# (112.0, 80.0)At stride 32, one cell stands for a 32x32 patch. A C5 cell can therefore locate an object only to within about 32 pixels. The box regression step you will see later has to correct that coarse position.
Receptive field and effective resolution
The receptive field of a cell is the region of the input that can influence its value. It grows with every convolution and every stride. A C5 cell in ResNet-50 has a theoretical receptive field larger than a 400x400 pixel region, so it can use context around an object.
The theoretical size overstates what the network actually uses. Influence inside a receptive field is concentrated near the center and falls off roughly like a Gaussian. The effective receptive field is therefore smaller than the theoretical one. Both numbers matter when you choose which stage should detect which object size.
This explains a recurring detection trade-off:
- Fine maps (C2, C3) keep small objects visible, but their cells have seen little context.
- Coarse maps (C5) carry strong semantics, but a small object may fall inside a single cell or vanish.
Feature pyramids, covered in a later chapter, exist to resolve this trade-off.
Choosing and adapting a backbone
The backbone sets the speed and accuracy ceiling of the detector. Common choices differ along a few axes.
| Family | Typical use in detection | Main trade-off |
|---|---|---|
| ResNet-50 or 101 | Default baseline in Faster R-CNN, RetinaNet, DETR | Well understood, moderate cost |
| ResNeXt | Higher accuracy at similar depth | More compute per block |
| MobileNet, EfficientNet | Mobile and edge deployment | Lower accuracy on small objects |
| Swin Transformer, ViT | High-accuracy detectors | Higher memory, different feature layout |
Several adaptations are standard practice:
- Initialize from classification weights. Pretraining on ImageNet gives useful features and speeds up training considerably. Training a backbone from scratch needs far more data and iterations.
- Freeze early layers. The stem and sometimes C2 are often frozen, because low-level edge and texture filters transfer well. This saves memory and reduces overfitting.
- Freeze or replace batch normalization. Detection uses large images, so batches are often 2 to 16 images per GPU. Batch statistics from so few samples are noisy. Detectors typically freeze batch norm statistics, or use group norm or synchronized batch norm instead.
- Drop the classification head. The global pooling layer and the final fully connected layer are removed. Only the stage outputs are kept.
- Dilate to preserve resolution. Some designs replace the last stride with dilated convolutions, giving a stride-16 C5 with a large receptive field. This helps small objects but costs more compute.
Common errors
- Wrong coordinate mapping. Using
col * strideinstead of(col + 0.5) * strideshifts every anchor by half a cell. The detector still trains, but box offsets must compensate and accuracy drops quietly. - Mixing normalization. Pretrained weights expect the exact input statistics used in pretraining. Feeding images scaled to
[0, 1]into a network trained on mean-subtracted, standard-deviation-scaled inputs degrades the features. - Assuming the last map is enough. Detecting from C5 alone loses objects smaller than about 32 pixels. This is the main motivation for multi-scale designs.
- Unfreezing everything with a tiny batch. Updating batch norm statistics with 2 images per GPU often destabilizes training.
What the rest of the pipeline expects
The backbone's job ends when it hands over one or more feature maps. Later chapters assume this contract:
- Anchor-based detectors place reference boxes at each cell and use the stride to size them.
- Two-stage detectors crop regions out of the map for a second pass.
- DETR flattens the map into a sequence of tokens and lets attention read from it.
Each cell is a descriptor with a known position and a known stride, and those two facts are what detectors build on.
Exercise
An image of size 512x768 passes through ResNet-50. What are the shape of the C4 map, the number of cells it contains, and the image coordinates of the center of the cell at row 5, column 10?
Show answer
C4 has stride 16 and 1024 channels, so its shape is (1024, 32, 48), because 512/16 = 32 and 768/16 = 48. It has 32 x 48 = 1536 cells. The cell at row 5, column 10 has its center at x = (10 + 0.5) 16 = 168 and y = (5 + 0.5) 16 = 88, so (168, 88).
Exercise
A detector reads only the stride-32 map. Why does it struggle with an object that is 20 pixels wide, even though the C5 receptive field is large?
Show answer
One stride-32 cell covers a 32x32 patch, which is larger than the object, so the object shares a cell with its surroundings and its signal is diluted. Position can also be resolved only to about 32 pixels. A large receptive field gives context, but it does not restore spatial detail lost to downsampling. Finer maps such as C3 or a feature pyramid address this.