Sub topic 11
Feature pyramids for objects of different sizes
This chapter assumes you know how a CNN backbone produces feature maps at several strides, what anchors are, and how single-stage detectors such as RetinaNet use them. It explains why one feature map is a poor basis for detecting objects of very different sizes, and how the Feature Pyramid Network (FPN) fixes that.
The scale problem
A backbone such as ResNet halves the spatial resolution several times. The result is a set of feature maps at different strides. Stride is the number of input pixels that one feature-map cell covers.
| Stage | Typical stride | Resolution for a 640x640 input | Character |
|---|---|---|---|
| C2 | 4 | 160x160 | High resolution, weak semantics |
| C3 | 8 | 80x80 | Mid-level |
| C4 | 16 | 40x40 | Mid-level, stronger semantics |
| C5 | 32 | 20x20 | Low resolution, strong semantics |
Deep layers know what things are, but they have coarse spatial detail. Shallow layers keep fine detail, but they have seen little context, so their class evidence is weak.
Using only C5 causes two problems:
- A 16x16 pixel object occupies a fraction of one cell at stride 32. Its signal is blurred with its neighbors, and several small objects can fall into the same cell.
- A single anchor size cannot fit both a 20-pixel and a 400-pixel object. Matching by IoU fails when the best available anchor has a low IoU with the target.
The older fix was an image pyramid. You resize the image to several scales and run the whole network on each. This works but multiplies compute by the number of scales. A feature pyramid builds the scales from one forward pass.
Two earlier designs
Two designs came before FPN. Each shows what FPN improves.
- Single feature map (Faster R-CNN with C4 or C5). Cheap, but weak for small objects.
- Pyramidal feature hierarchy (SSD). SSD predicts from several backbone layers directly. Shallow layers are used for small objects, but they carry weak semantics, so small-object accuracy stays low. SSD also adds extra layers on top of the backbone for large objects.
FPN keeps the multi-level prediction idea. It adds a path that sends semantic information back down to the high-resolution levels.
How FPN builds the pyramid
FPN adds two paths to the backbone.
- The bottom-up path is the normal backbone forward pass. It produces C2 through C5.
- The top-down path starts at C5 and upsamples it by a factor of 2 (nearest neighbor). Each step is merged with the backbone map of the same resolution.
- Lateral connections are 1x1 convolutions that project each backbone map to the same channel count, usually 256, so the maps can be added.
- A 3x3 convolution is applied to each merged map to reduce aliasing from upsampling. The outputs are called P2 through P5.
Every Pi has the same channel count. That lets one shared prediction head, with the same weights, run on all levels. The head then learns a scale-invariant mapping from features to class scores and box offsets. Sharing the head also keeps the parameter count low.
RetinaNet uses P3 to P7 instead of P2 to P6. It builds P6 and P7 with strided convolutions on top of C5 (or P5) to cover large objects, and it drops P2 because its high resolution is expensive for a dense head.
Assigning objects to levels
Each pyramid level has a stride, and the anchors on it have a matching size. A common pairing is:
| Level | Stride | Anchor base size |
|---|---|---|
| P3 | 8 | 32 |
| P4 | 16 | 64 |
| P5 | 32 | 128 |
| P6 | 64 | 256 |
| P7 | 128 | 512 |
In an anchor-based detector, level assignment follows from the usual IoU matching. A small ground-truth box matches small anchors, and small anchors live only on fine levels. No extra rule is needed.
In a two-stage detector, the RoI head has to pick one level for each proposal. FPN's Faster R-CNN uses this rule, where w and h are the proposal's width and height in input pixels:
import math
def fpn_level(w, h, k0=4, canonical=224, k_min=2, k_max=5):
"""Pick the pyramid level (2..5) used to pool features for a proposal."""
k = math.floor(k0 + math.log2(math.sqrt(w * h) / canonical))
return max(k_min, min(k_max, k))
for size in (32, 64, 112, 224, 448, 900):
print(size, "-> P%d" % fpn_level(size, size))The output is:
32 -> P2
64 -> P2
112 -> P3
224 -> P4
448 -> P5
900 -> P5A proposal of 224x224 pixels, the size ImageNet networks were trained on, maps to P4. Each halving of the size moves the proposal one level finer. The clamp keeps results inside the available levels.
Why the top-down path matters
The gain does not come only from having many resolutions. SSD already had that. It comes from the merge. A P2 cell combines fine location detail from C2 with class evidence that traveled down from C5. In the FPN paper's ablations on COCO, removing the top-down path or the lateral connections reduced average precision, with the largest losses on small objects.
Costs and limits to know:
- Computation grows with the number of levels. P2 has 16 times as many cells as P4, so heads on high-resolution levels dominate cost.
- Nearest-neighbor upsampling plus addition is simple. It assumes the levels align spatially, and it can blur semantics for small objects.
- Features travel in one direction (top-down). Later designs add a second bottom-up path (PANet) or learn fusion weights (BiFPN in EfficientDet). These add accuracy at the price of more computation and complexity.
Pyramids beyond anchors
Feature pyramids are not tied to anchors. Anchor-free detectors such as FCOS use the same levels. They restrict each level to a range of box sizes, so a location on a level is a positive only if the object's regression distances fall in that level's range. The next chapter covers that.
DETR's original design used a single C5 map, which contributed to its weakness on small objects. Later DETR variants return to multi-scale features. That reappears in the last chapter.
Common errors
- Mismatched strides and anchor sizes. If the smallest anchors are larger than your smallest objects, those objects never get positive matches. Check that anchor sizes on each level are about 4 times the stride.
- Forgetting that the head is shared. Because all levels use the same head weights, feature statistics across levels must be similar. This is one reason FPN uses a fixed 256 channels and normalization in many implementations.
- Odd input sizes. If the input size is not divisible by the largest stride, the upsampled map and the lateral map can differ by one cell. Pad the input to a multiple of 32 (or 128 with P7).
- Applying the level rule with the wrong units. The
fpn_levelrule uses box size in input pixels, not in feature-map cells.
Exercise
A proposal is 112x112 input pixels. With canonical size 224 and k0 = 4, which pyramid level does the FPN rule choose, and why does a proposal that is half as large map to a finer level?
Show answer
k = floor(4 + log2(112/224)) = floor(4 - 1) = 3, so P3. Each halving of the box size subtracts 1 from log2, so the proposal moves one level finer. A smaller object needs a higher-resolution feature map, and P3 has stride 8 compared with 16 for P4.
Exercise
Why can a single detection head with shared weights run on every FPN level, and what design choice makes this possible?
Show answer
Every pyramid level has the same channel count (256) because the 1x1 lateral convolutions project all backbone maps to a common width. The top-down merge also gives every level strong semantics. Both facts let one head learn a mapping that works at any scale.