How to handle variable sized input in CNN with Keras?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
Keras can handle variable-sized inputs in a CNN, but only if the architecture avoids layers that require a fixed flattened size too early. Convolution layers themselves can work with variable spatial dimensions. The constraint usually appears when you add Flatten and dense layers that expect a known number of features. The standard solution is to keep the network fully convolutional until a global pooling layer reduces the spatial dimensions to a fixed-size vector.
What "Variable Sized" Really Means
For images, variable size usually means height and width can differ between examples. In Keras, you can express that with None for the spatial dimensions.
This works because the convolution layers operate locally, and GlobalAveragePooling2D converts any final feature map size into one fixed-length vector per channel.
Why Flatten Usually Breaks It
A model like this does not support variable image sizes cleanly:
Flatten turns the entire spatial map into one long vector. If the image size changes, the vector length changes, and the dense layer no longer knows how many input units it should expect.
That is why global pooling is the usual replacement.
Batching Still Needs a Strategy
Even if the model accepts variable sizes in principle, a normal tensor batch still needs samples of the same shape within that batch. You have three common options:
- resize all images to one fixed size
- pad images to a common size within each batch
- bucket images by similar shapes and batch them together
A padded tf.data pipeline is often the practical middle ground.
The model still sees valid tensors, but padding lets examples with different sizes live in the same batch.
When Resizing Is the Better Choice
For many image-classification problems, resizing inputs to a standard resolution is still the best engineering choice. It simplifies batching, improves throughput, and makes pretrained backbones easier to reuse.
Variable-size support is most valuable when resizing would distort the signal too much, such as in document images, medical scans, or detection-style pipelines.
In other words, do not use variable-sized input just because it is possible. Use it when it preserves important information.
Common Pitfalls
- Declaring
Input(shape=(None, None, 3))and then addingFlattenbefore global pooling. - Forgetting that batches still need compatible tensor shapes unless you pad or bucket them.
- Assuming pretrained models always support arbitrary input sizes for every use case.
- Mixing variable-size inputs with augmentation code that expects one fixed resolution.
- Choosing variable-sized training when simple resizing would be faster and good enough.
Summary
- Convolution layers can handle variable spatial dimensions.
- The usual blocker is
Flattenplus dense layers that require a fixed vector length. - Use
GlobalAveragePooling2DorGlobalMaxPooling2Dto convert variable feature maps into fixed-size outputs. - Plan batching explicitly with resizing, padding, or bucketing.
- Variable-sized input is a design choice, not a default requirement.
Related reading
- How to implement a matrix multiplication in Keras?
- How to implement a neural network with a not-fully-connected layer as the final layer?
- How to implement dropout in Pytorch, and where to apply it
- how to implement early stopping in tensorflow
- How to have predictions AND labels returned with tf.estimator either with predict or eval method?
- How to implement an image2D array sequence sliding window in tensorflow?
- How to implement Grad-CAM on a trained network
- How to implement multi-class semantic segmentation?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.