How to predict from saved model in 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
Predicting from a saved Keras model is straightforward once you know which format was saved and which Keras API you are using. The essential steps are always the same: load the model or inference artifact, preprocess new input exactly the same way as during training, then call the prediction entry point with data that matches the expected input shape.
The Standard Keras Loading Path
For models saved in the native Keras format or the legacy HDF5 format, load the model and call predict.
This is the normal path for .keras files and still works for older .h5 model files when supported by the runtime.
Why Input Shape Matters
Most prediction failures happen before the first numeric result appears. The model expects the same feature order, dtype, normalization, and batch shape it saw during training.
If the model was trained on batches of shape (batch, 4), a single example usually still needs shape (1, 4), not just (4,). Likewise, if training normalized images to the range 0.0 through 1.0, the prediction path must do the same transformation.
Keras 3 And TensorFlow SavedModel
This is where version awareness matters. In the current Keras ecosystem, standalone Keras loading is mainly for .keras and legacy .h5 files. TensorFlow SavedModel is a different inference artifact and may be used through a TensorFlow-specific flow or an inference layer wrapper.
For inference-only use with a TensorFlow SavedModel export, a TFSMLayer can be appropriate:
The exact endpoint name depends on how the SavedModel was exported. That is one reason saved-format details matter when you move a model between training and serving environments.
Keep Preprocessing With The Model Contract
A saved model only preserves learned parameters and model structure. It does not automatically fix upstream feature engineering mistakes in your caller.
That means prediction code should answer these questions clearly:
- What order are features expected in?
- What dtype is required?
- Was the input normalized, tokenized, padded, or resized during training?
- Does the output need post-processing into labels or probabilities?
If those steps are missing, loading the model successfully still will not produce trustworthy predictions.
Post-processing matters too. A classification model may return logits or class probabilities rather than final labels, while a regression model may need inverse scaling before the numbers are meaningful to users. The saved model gets you back to the raw model output, not necessarily to the final business-facing answer.
Keeping a tiny prediction fixture alongside the saved model is often worth the effort. One known input and one expected output shape can confirm very quickly that loading, preprocessing, and endpoint selection still line up after a deployment or library upgrade.
Common Pitfalls
One common mistake is trying to load every saved artifact with the same API. .keras, .h5, and TensorFlow SavedModel do not all behave identically across Keras versions.
Another mistake is passing a single sample with the wrong batch shape. Models usually expect batched input, even when predicting one example.
A third issue is forgetting to reproduce training-time preprocessing. The model can load correctly and still return garbage if the new data is scaled or encoded differently.
Summary
- For native Keras files, load the model and call
predictwith correctly shaped input. - Saved format matters:
.kerasand.h5follow the normal Keras loading path, while TensorFlow SavedModel may use a different inference flow. - Match prediction-time preprocessing to training-time preprocessing exactly.
- Most prediction bugs come from input mismatch, not from the
predictcall itself.
Related reading
- How to predict input image using trained model in Keras?
- how to predict my own image using cnn in keras after training on MNIST dataset
- How to predict values with a trained Tensorflow model
- How to prepare a dataset for Keras?
- How to prefetch data using a custom python function in tensorflow
- How to prevent tensorflow from allocating the totality of a GPU memory?
- How to predict time series in scikit-learn?
- How to preprocess audio data for input into a Neural Network
.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.