TensorFlow How to predict from a SavedModel?
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 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.
Related reading
- Tensorflow How to randomly crop input images and labels in the same way?
- Tensorflow How to reduce memory footprint for inference only models?
- Tensorflow How to replace a node in a calculation graph?
- Tensorflow How to replace or modify gradient?
- tensorflow how to rotate an image for data augmentation?
- TensorFlow how to safely terminate training manually KeyboardInterrupt
- Tensorflow How to switch channels of a tensor from RGB to BGR?
- Tensorflow How to use a trained model in a application?
.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.