Skip to main content
Open In Colab

The core dimensional constraint

Let xRB×Cin×H×Wx \in \mathbb{R}^{B \times C_{in} \times H \times W}. A residual unit computes y=F(x)+S(x)y = F(x) + \mathcal{S}(x) and addition requires identical tensor shapes: F(x),S(x)RB×Cout×H×W.F(x), \mathcal{S}(x) \in \mathbb{R}^{B \times C_{out} \times H' \times W'}. Hence the skip connection must handle two mismatches:
  • channel mismatch: CinCoutC_{in} \neq C_{out}
  • spatial mismatch: (H,W)(H,W)(H,W) \neq (H',W') (typically caused by stride-2 downsampling)

Residual block, skip connection options

Residual unit data flow. Input x [B, Cᵢₙ, H, W] passes through the residual branch Conv 3×3 stride s, BN + ReLU, Conv 3×3 stride 1, BN into an addition node. In parallel, the skip connection takes one of two routes: an Identity branch when Cᵢₙ=Cₒᵤₜ and s=1, otherwise a 1×1 Conv with stride s (Option B). Both routes feed the same addition node, whose output goes through ReLU to produce y [B, Cₒᵤₜ, H/s, W/s]. Editable Mermaid source: images/residual-block-skip.mermaid.md Addition requires identical tensor shapes: both the residual branch and the skip connection must produce [B,Cout,H,W][B, C_{out}, H', W'].

ResNet-style block with correct skip connection dimensioning

We implement a standard BasicBlock with:
  • residual branch: 3×3 conv → BN → ReLU → 3×3 conv → BN
  • skip connection:
    • identity if stride=1 and Cin=CoutC_{in}=C_{out}
    • otherwise a 1×1 conv (projection), with the same stride as the residual branch’s downsampling

Option A vs. Option B (ResNet paper terminology)

In the ResNet paper’s discussion:
  • Option A: downsample the skip connection (stride 2) and zero-pad channels to match CoutC_{out}.
  • Option B: downsample and project with 1×1 conv to match dimensions.
For FPN-style backbones, Option B is the preferred practical choice because:
  • the feature hierarchy is consumed downstream (e.g., lateral merges), so having a learned projection at stage transitions is robust,
  • and it matches the canonical ResNet-{50,101,152} “option B” design in the CVPR paper.
Below is a small functional illustration of “Option A-like” padding for the channel mismatch (spatial downsample uses strided slicing for simplicity).

A minimal ResNet-like backbone that exposes {C2, C3, C4, C5}

FPN (Lin et al.) uses the outputs of each ResNet stage’s last block: {C2, C3, C4, C5} with strides {4, 8, 16, 32} relative to the input. We build a small backbone that mirrors this structure (conceptually like a tiny ResNet-18).

Backbone stage layout, strides and channel widths

Linear backbone pipeline. Image 3×224×224 feeds the Stem (Conv7 s2 + MaxPool s2) producing 64×56×56. Stage 1 outputs 64×56×56 as C2 at stride 4. A stride-2 transition leads to Stage 2 outputting 128×28×28 as C3 at stride 8, then Stage 3 outputting 256×14×14 as C4 at stride 16, then Stage 4 outputting 512×7×7 as C5 at stride 32. Editable Mermaid source: images/backbone-stages.mermaid.md Each stage transition uses a stride-2 first block with a 1×1 projection skip connection (Option B) to match dimensions.
Output from cell 4

FPN module implementation

Canonical FPN design choices (as in Lin et al.):
  • 1×1 lateral conv to unify channels to d=256d=256
  • top-down upsample by factor 2 (nearest neighbor is typical)
  • element-wise addition (requires same H×WH \times W and same dd)
  • 3×3 conv “smoothing” on each merged map
  • optional P6P6 via stride-2 3×3 conv on P5P5 (common in detection systems)

FPN top-down pathway, lateral merges and channel unification

FPN top-down pathway in three columns. Left column: backbone features C5 512×7×7, C4 256×14×14, C3 128×28×28, C2 64×56×56. Each connects via a 1×1 lateral convolution to the middle column of merged maps M5, M4, M3, M2, all unified to 256 channels at the matching spatial size. The middle column flows top-down: M5 is upsampled ×2 and added (⊕) into M4, M4 into M3, M3 into M2. Each merged map passes through a 3×3 convolution to the right column producing pyramid levels P5, P4, P3, P2. P5 is further downsampled by a stride-2 3×3 convolution to produce P6. Editable Mermaid source: images/fpn-top-down.mermaid.md The 1×1 lateral convolutions unify heterogeneous backbone channels (64/128/256/512) to a uniform d=256d=256 before the element-wise additions. The additions require strict spatial and channel alignment, which the lateral convolutions and upsample guarantee.

What “preferred approach for FPN” means (operationally)

In a modern featurizer intended for FPN-style consumption, the pragmatic default is:
  1. Backbone (ResNet-style):
    • Identity skip connection if (Cin,H,W)(C_{in}, H, W) matches (Cout,H,W)(C_{out}, H', W')
    • 1×1 projection skip connection (with stride=2 when downsampling) otherwise
      This matches the ResNet paper’s “projection to match dimensions” guidance and the widespread “option B” practice in deep variants.
  2. FPN neck:
    • 1×1 lateral convs to unify all C2..C5C2..C5 to d=256d=256 channels
    • top-down nearest-neighbor upsample by 2
    • elementwise addition
    • 3×3 smoothing conv
    • optional P6P6 from P5P5 via stride-2 3×3 conv
The key theme is the same in both ResNet and FPN: addition enforces strict shape equality, so dimensioning is not a detail, it is the design constraint.

References (primary sources)

  • Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun. Deep Residual Learning for Image Recognition. CVPR 2016. arXiv:1512.03385.
  • Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun. Identity Mappings in Deep Residual Networks. ECCV 2016. arXiv:1603.05027.
  • Tsung-Yi Lin, Piotr Dollár, Ross Girshick, Kaiming He, Bharath Hariharan, Serge Belongie. Feature Pyramid Networks for Object Detection. CVPR 2017. arXiv:1612.03144.
Key references: (Wightman et al., 2021; Zagoruyko & Komodakis, 2016; Tan & Le, 2019; Dong et al., 2017; He et al., 2016)

PyTorch reference

References

  • Dong, X., Wu, J., Zhou, L. (2017). How deep learning works -The geometry of deep learning.
  • He, K., Zhang, X., Ren, S., Sun, J. (2016). Identity mappings in deep residual networks.
  • Tan, M., Le, Q. (2019). EfficientNet: Rethinking Model Scaling for Convolutional Neural Networks.
  • Wightman, R., Touvron, H., Jégou, H. (2021). ResNet strikes back: An improved training procedure in timm.
  • Zagoruyko, S., Komodakis, N. (2016). Wide Residual Networks.