Neural networks, deep learning, NLP · insight-0438
Compute CNN feature-map sizes on paper before writing the classifier head
- Evidence type
- documented fact
- Difficulty
- easy
- Roles
- ML engineer
- Industries
- manufacturing
Summary: situation, action, reason, boundary
Situation
You stack convolution and pooling layers for an image task and hard-code the input size of the first linear layer.
Action
Write out per layer, assert it with a dummy forward, and end the feature extractor with adaptive pooling so the head does not depend on input resolution.
Reason
The output size is fixed arithmetic of input size, kernel, padding, stride and dilation, so it can be checked before training.
Boundary
CNNs, pooling layers and 1D convolutions over sequences.
Symptom or risk
A size mismatch crashes at the first forward, or worse, a different input resolution later breaks serving or forces silent resizing.
Exceptions
Pretrained backbones already document their output shapes; still verify with a dummy input.
Trade-offs
Adaptive pooling discards spatial detail that some tasks need; segmentation keeps spatial maps.
Pitfalls
Forgetting that floor division drops a pixel at odd sizes, so a decoder no longer aligns with the encoder.
Example
224 x 224 input, kernel 7, stride 2, padding 3: floor((224 + 6 - 6 - 1)/2) + 1 = 112.
Lessons and concepts
Claims and their sources
PyTorch Conv2d documents output height as floor((H_in + 2*padding - dilation*(kernel_size - 1) - 1)/stride + 1).
Dumoulin and Visin derive the relationships between input shape, kernel, padding, stride and output shape for convolution and pooling.