TensorFlow How to predict from a SavedModel?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Predicting from a TensorFlow SavedModel starts with knowing how the model was exported. If it was saved from Keras, tf.keras.models.load_model usually gives the simplest inference path. If it was exported as a generic SavedModel, you may instead use tf.saved_model.load and call one of its signatures directly.
Predict with a Keras SavedModel
For Keras models saved with model.save(...), loading and predicting is straightforward.
This works because the Keras export keeps the architecture, weights, and enough metadata for inference.
Use the Same Preprocessing as Training
Loading the model is only part of the job. The input data must be shaped and scaled the same way it was during training.
If training used normalization, tokenization, image resizing, or feature ordering, inference must do the same. Many "wrong predictions" are really preprocessing mismatches.
Predict from a Generic SavedModel Signature
If the model was exported for serving or is not a normal Keras object, use tf.saved_model.load.
The signatures dictionary tells you which callable entry points were exported. serving_default is common, but not guaranteed. Inspecting the keys first is safer than assuming the name.
That quick inspection step also helps when the model was exported by another team or by TensorFlow Serving tooling you did not write yourself.
Know the Input Names
Serving signatures can expect named tensors. When that happens, inference looks more like this:
The required keyword names come from the exported signature. If you pass unnamed tensors to a signature that expects named inputs, TensorFlow raises an argument error.
Batch Prediction with tf.data
For larger inference jobs, a dataset pipeline is often cleaner than calling predict on ad hoc arrays repeatedly.
Calling the model directly with training=False is often a good fit when you already have the loaded Keras model object in memory.
That pattern also makes it easier to keep preprocessing, batching, and postprocessing in one explicit pipeline instead of scattering small predict calls throughout an application.
Common Pitfalls
The biggest mistake is mixing up tf.keras.models.load_model and tf.saved_model.load without checking how the model was exported. Both load SavedModel directories, but they return different types and support different inference patterns.
Another issue is ignoring signatures. Generic SavedModels may expect named inputs, and their output keys may not match your assumptions.
Preprocessing mismatch is another major source of confusion. If training normalized features or resized images, the inference path must reproduce that logic exactly.
Finally, do not forget batch shape. Many models expect a leading batch dimension, so a single example often still needs shape (1, feature_count) rather than just (feature_count,).
Summary
- Use
tf.keras.models.load_modelfor normal Keras SavedModel inference. - Use
tf.saved_model.loadand exported signatures for generic or serving-oriented SavedModels. - Inspect signature names and input keys instead of guessing them.
- Keep preprocessing identical between training and prediction.
- Remember that inference inputs usually need a batch dimension.

