Keras
machine learning
saved model
prediction
deep learning

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.

Practice ML system design

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.

python
1import numpy as np
2import keras
3
4model = keras.saving.load_model("classifier.keras")
5
6x = np.array([
7    [0.2, 0.4, 0.8, 0.1]
8], dtype="float32")
9
10predictions = model.predict(x)
11print(predictions)

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:

python
1import tensorflow as tf
2import keras
3
4saved_layer = keras.layers.TFSMLayer(
5    "exported_saved_model",
6    call_endpoint="serving_default"
7)
8
9x = tf.constant([[0.2, 0.4, 0.8, 0.1]], dtype=tf.float32)
10outputs = saved_layer(x)
11print(outputs)

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 predict with correctly shaped input.
  • Saved format matters: .keras and .h5 follow 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 predict call itself.

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.