FPN Feature Fusion (Simplified)
Implement a Feature Pyramid Network (FPN) fusion module, a crucial component in ConvNext architectures for multi-scale object detection. This module combines low-resolution semantic features with high-resolution spatial features.
The Feature Pyramid Network (FPN) is a technique used to leverage the benefits of both high-resolution and low-resolution feature maps. In a typical ConvNext architecture, feature maps C3​,C4​,C5​ are generated by the backbone, where C5​ is the deepest and smallest map. To fuse these features, the FPN uses a top-down pathway with upsampling and element-wise addition.
Here are the steps to compute P4​:
- Upsample C5​ by 2× using nearest-neighbor interpolation
- Element-wise add upsampled C5​ to C4​
This technique is widely used in object detection tasks, such as those found in autonomous vehicles.
Example:
C4 (8×8, all 2s), C5 (4×4, all 4s)
8×8 matrix, all values 6.0 (upsampled 4 + original 2)
Each 1×1 cell in C5 becomes a 2×2 block with value 4. Adding to C4 (value 2) gives 6.
Constraints:
- C3​ dimensions: H×W (e.g., 16×16)
- C4​ dimensions: H/2×W/2 (e.g., 8×8)
- C5​ dimensions: H/4×W/4 (e.g., 4×4)
- All values are scalar intensities
1. Background Knowledge
Feature Pyramid Networks (FPN) address multi-scale object detection by fusing high-resolution spatial details from shallow layers with low-resolution semantic richness from deep layers. Backbone networks (e.g., ResNet) produce feature maps C3​,C4​,C5​ where depth increases semantic content but reduces spatial resolution: C3​ (e.g., 16×16), C4​ (8×8), C5​ (4×4). The top-down pathway in FPN uses nearest-neighbor upsampling (×2) and element-wise addition (lateral connections) to create pyramid levels like P4​, balancing scale variance without heavy computation.
Prerequisites: Tensor operations (upsampling, addition), CNN feature maps as (H,W,C) tensors, nearest-neighbor interpolation preserves values by replicating pixels.
2. Algorithm Approach
Standard FPN uses a top-down fusion:
- Upsample deepest map (C5​→8×8) via nearest-neighbor (scale=2).
- Add to same-resolution lateral map (C4​): P4​=\text{Upsample}(C5​)+C4​.
In PyTorch/TensorFlow:
import torch.nn.functional as F
# Upsample C5 to C4 size
upsampled_c5 = F.interpolate(C5, scale_factor=2, mode='nearest')
P4 = upsampled_c5 + C4 # Assumes matching channels/spatial dims
Variants (e.g., BiFPN, NAS-FPN) add bottom-up paths or attention, but this task is pure top-down.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.