keras
load model
.pb format
tensorflow
machine learning

How to load a keras model saved as .pb

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When people say they have a Keras model saved as .pb, they usually mean a TensorFlow SavedModel export whose directory contains a saved_model.pb file. The important detail is that modern Keras and TensorFlow do not treat that artifact the same way as a native .keras model file.

Identify What You Actually Have

The most common layout is a SavedModel directory like this:

text
1exported_model/
2  saved_model.pb
3  variables/
4  assets/

In that case, you load the directory, not the raw .pb file path by itself. A standalone protobuf graph file is a different artifact and usually cannot be restored as a normal Keras model.

Load the SavedModel With TensorFlow

If your goal is inference, TensorFlow can load the SavedModel directly:

python
1import tensorflow as tf
2
3model = tf.saved_model.load("exported_model")
4print(list(model.signatures.keys()))

You can then call an exported signature, often named serving_default:

python
infer = model.signatures["serving_default"]
result = infer(tf.constant([[1.0, 2.0, 3.0]]))
print(result)

This is the safest path when you want to serve or run inference from a SavedModel artifact.

Keras 3 Does Not Reload SavedModel With load_model()

This is where many older answers are now outdated. In Keras 3, keras.models.load_model() supports the native .keras format and legacy H5 files, but not TensorFlow SavedModel directories. If you pass a SavedModel path to load_model(), Keras 3 raises a format error.

If you need to use a SavedModel inside Keras 3, wrap it with keras.layers.TFSMLayer:

python
1import keras
2import tensorflow as tf
3
4layer = keras.layers.TFSMLayer(
5    "exported_model",
6    call_endpoint="serving_default"
7)
8
9inputs = tf.constant([[1.0, 2.0, 3.0]])
10outputs = layer(inputs)
11print(outputs)

This gives you a Keras-compatible inference layer around the SavedModel endpoint, but it is not the same as recovering the original high-level Keras model object with all of its internal structure.

Save in the Right Format for Future Use

If you control model export and you know you will want to reload the full model through Keras later, save it in the .keras format instead of relying on SavedModel as the interchange format.

python
1import keras
2
3model.save("classifier.keras")
4restored = keras.models.load_model("classifier.keras")

That workflow is much simpler when you want to keep using Keras APIs such as predict(), evaluate(), or further training.

It also simplifies custom objects. SavedModel endpoints can hide the original training-time class structure, while a native Keras save is designed for later Keras restoration with the right custom object registration. That matters when your model includes custom layers, metrics, or serialization hooks. It also improves portability.

Common Pitfalls

The most common mistake is passing the path to saved_model.pb instead of the SavedModel directory. TensorFlow expects the directory because the variables and assets live alongside the protobuf.

Another issue is following older examples that call keras.models.load_model("saved_model_dir"). That pattern used to appear in older TensorFlow-Keras workflows, but it is no longer the right answer for Keras 3.

It is also easy to confuse inference loading with full model restoration. tf.saved_model.load() and keras.layers.TFSMLayer can run the exported computation, but they do not necessarily restore the original model class, custom methods, or training setup.

Summary

  • A model "saved as .pb" is usually a TensorFlow SavedModel directory, not a single file you load directly.
  • For inference, load the SavedModel directory with tf.saved_model.load().
  • In Keras 3, do not use keras.models.load_model() for SavedModel directories.
  • If you need Keras integration around a SavedModel, use keras.layers.TFSMLayer.
  • If you want full Keras model reloading later, save the model in .keras format in the first place.

Course illustration
Course illustration

All Rights Reserved.