tensorflow
machine learning
deep learning
model deployment
saved model

how to load and use a saved model on tensorflow?

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

Loading a saved TensorFlow model correctly is a critical step between training and production inference. Teams often train a model successfully, then run into deployment issues because input signatures changed, preprocessing is inconsistent, or they confuse Keras and low-level SavedModel loading paths. A reliable loading workflow needs to preserve three things: model architecture, trained weights, and callable signatures.

TensorFlow supports multiple formats, but modern workflows usually rely on the SavedModel directory format (and .keras for Keras-native serialization). This guide covers loading patterns for inference, inspecting signatures, and avoiding version-related breakage.

Core Sections

1. Save and load with Keras APIs

If your model is built with tf.keras, the simplest round-trip is model.save() and tf.keras.models.load_model().

python
1import tensorflow as tf
2import numpy as np
3
4# Train or define model
5model = tf.keras.Sequential([
6    tf.keras.layers.Input(shape=(4,)),
7    tf.keras.layers.Dense(16, activation="relu"),
8    tf.keras.layers.Dense(1)
9])
10model.compile(optimizer="adam", loss="mse")
11
12X = np.random.rand(128, 4).astype("float32")
13y = np.random.rand(128, 1).astype("float32")
14model.fit(X, y, epochs=2, verbose=0)
15
16# Save and reload
17model.save("artifacts/regressor.keras")
18loaded = tf.keras.models.load_model("artifacts/regressor.keras")
19
20pred = loaded.predict(np.array([[0.1, 0.2, 0.3, 0.4]], dtype="float32"))
21print(pred)

Use the same preprocessing pipeline at inference that you used during training, or predictions will drift.

2. Load SavedModel and call signatures explicitly

For serving systems, SavedModel signatures are often the contract.

python
1import tensorflow as tf
2
3# Export SavedModel directory
4loaded.save("artifacts/saved_model")
5
6# Low-level load
7tf_model = tf.saved_model.load("artifacts/saved_model")
8print(tf_model.signatures.keys())
9
10infer = tf_model.signatures["serving_default"]
11output = infer(tf.constant([[0.1, 0.2, 0.3, 0.4]], dtype=tf.float32))
12print(output)

Inspecting signature names and tensor keys is important when integrating with TF Serving, batch jobs, or cross-language clients.

3. Production checks: schema, versioning, and reproducibility

Before promoting a loaded model, run schema and behavior checks.

python
1import json
2import numpy as np
3
4expected_shape = (None, 4)
5actual_shape = loaded.inputs[0].shape
6assert tuple(actual_shape.as_list()) == expected_shape, (actual_shape, expected_shape)
7
8# Smoke test for deterministic pipeline stage
9sample = np.array([[0.5, 0.5, 0.5, 0.5]], dtype="float32")
10_ = loaded(sample, training=False)
11
12# Store metadata for traceability
13metadata = {
14    "model_path": "artifacts/regressor.keras",
15    "tf_version": tf.__version__,
16    "input_shape": str(actual_shape),
17}
18print(json.dumps(metadata, indent=2))

In CI/CD, compare current model outputs against baseline tolerances to detect accidental serialization or preprocessing regressions.

Common Pitfalls

  • Loading a model successfully but forgetting to apply the same feature scaling or tokenization used during training.
  • Assuming SavedModel signature names and tensor keys without inspecting them, causing serving-time input mismatches.
  • Mixing incompatible TensorFlow/Keras versions across training and inference environments.
  • Saving custom layers without registering them, which breaks deserialization in clean environments.
  • Treating model load success as proof of correctness without smoke tests on known input-output examples.

Summary

To load and use a saved TensorFlow model reliably, pick a consistent format, inspect signatures, and verify inference behavior with schema checks and smoke tests. tf.keras.models.load_model is ideal for Keras workflows, while tf.saved_model.load provides explicit control for serving contracts. Robust deployment depends less on the load call itself and more on disciplined compatibility and validation practices around it.

For production deployment, treat model loading as one stage in a broader inference contract. Alongside the model artifact, version and store preprocessing code, expected input schema, output postprocessing rules, and sample payloads used for smoke tests. Many deployment failures are not serialization failures; they are contract mismatches where data types, field order, or normalization steps differ between training and serving. Keeping these components versioned together makes rollbacks and audits much simpler.

Another useful practice is to benchmark cold-start load time and first-inference latency. Large SavedModel directories can introduce startup delays that matter in autoscaled services. If startup becomes expensive, preload models during service boot and expose readiness only after an inference smoke test passes. This ensures traffic reaches only healthy replicas with validated model state.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the 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.