Keras
TensorFlow
model input shape
deep learning
machine learning

TF Keras how to get expected input shape when loading a model?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

When you load a Keras model, the expected input shape is often already stored in the model metadata, but the exact API to inspect it depends on how the model was built. Functional and Sequential models expose shape information more directly than subclassed models, which can stay partially dynamic until they are built or called.

Start with model.inputs and model.input_shape

For ordinary saved Keras models, the first place to look is the loaded model object itself.

python
1import tensorflow as tf
2
3model = tf.keras.models.load_model("classifier.keras")
4
5print(model.input_shape)
6print(model.inputs)
7print(tuple(model.inputs[0].shape))

Typical output might look like:

text
(None, 224, 224, 3)

The leading None is the batch dimension. It means the model can accept any batch size, while the remaining dimensions are fixed.

Multiple Inputs Need model.inputs

If the model has more than one input, model.input_shape may be less convenient than inspecting the full input tensor list.

python
1import tensorflow as tf
2
3model = tf.keras.models.load_model("multi_input.keras")
4
5for tensor in model.inputs:
6    print(tensor.name, tuple(tensor.shape))

This is the safest way to inspect multi-input models because each input tensor can have a different shape and name.

model.summary() Can Help, But It Is Not the First Tool

summary() is useful for a quick overview if the model is already built.

python
1import tensorflow as tf
2
3model = tf.keras.models.load_model("classifier.keras")
4model.summary()

For Functional and Sequential models, this usually prints the input layer and downstream shapes cleanly. If all you want is the expected input shape, model.inputs is still more direct and easier to extract programmatically.

Subclassed Models Are Different

Subclassed models are more dynamic. They do not always carry the same graph-style input metadata until they have been built or called with real input.

Example:

python
1import tensorflow as tf
2
3
4class MyModel(tf.keras.Model):
5    def __init__(self):
6        super().__init__()
7        self.dense = tf.keras.layers.Dense(4)
8
9    def call(self, inputs):
10        return self.dense(inputs)
11
12
13model = MyModel()
14model(tf.ones((1, 8)))
15print(model.summary())

In this case the model learns its shape after seeing a tensor of shape (1, 8). Before that, asking for summary or graph-style shape metadata may be incomplete or fail.

That means the answer to “what input shape does this loaded model expect?” is sometimes “look at how it was saved, or build it first with the intended input signature.”

Input Specification Is Not the Same as Batch Size

A frequent confusion is mixing up sample shape and batch shape.

If the model reports:

text
(None, 28, 28)

That means each sample should be shaped like (28, 28), while the batch dimension is flexible.

When preparing one sample for prediction, you usually still need to add the batch dimension:

python
1import numpy as np
2import tensorflow as tf
3
4model = tf.keras.models.load_model("classifier.keras")
5
6sample = np.zeros((224, 224, 3), dtype=np.float32)
7batch = np.expand_dims(sample, axis=0)
8
9predictions = model.predict(batch)
10print(predictions.shape)

The model wants a batch, even if that batch contains one item.

When the Saved Model Is Not Self-Describing Enough

Most standard saved Keras models are self-describing enough for model.inputs to work. If you are dealing with a less transparent saved artifact, also inspect:

  • the original training code
  • preprocessing code used before training
  • model documentation or serving contract
  • any wrapper class that reshapes data before prediction

The stored tensor shape tells you the raw tensor signature. It does not always tell you the full semantic contract, such as normalization, tokenization, or channel order.

A Reliable Inspection Helper

For normal Keras models, a small helper can make this repeatable:

python
1import tensorflow as tf
2
3
4def describe_inputs(path: str) -> None:
5    model = tf.keras.models.load_model(path)
6    for i, tensor in enumerate(model.inputs):
7        print(f"input {i}: name={tensor.name}, shape={tuple(tensor.shape)}, dtype={tensor.dtype}")
8
9
10describe_inputs("classifier.keras")

This is usually enough to answer practical debugging questions quickly.

Common Pitfalls

The biggest mistake is forgetting that the first None usually means batch dimension, not a missing data dimension.

Another mistake is relying only on summary() when model.inputs would give a more exact and script-friendly answer.

Developers also run into confusion with subclassed models because their input shape is not always materialized until the model is built or called.

Finally, the tensor shape alone does not tell you preprocessing requirements. A model expecting (None, 224, 224, 3) may still require normalized floats, RGB channel order, or other preprocessing steps.

Summary

  • For most loaded Keras models, inspect model.inputs or model.input_shape first.
  • The leading None usually represents flexible batch size.
  • For multi-input models, iterate over model.inputs instead of assuming one shape.
  • Subclassed models may need to be built or called before their shape information is fully available.
  • Tensor shape is only part of the input contract; preprocessing rules still matter.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Practice ML system design

All Rights Reserved.