How to convert tensorflow model to keras model? .pb file to .hdf5?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting a TensorFlow .pb model into a Keras .hdf5 model is not a generic one-step transformation. Whether it is possible depends on what the .pb actually contains: a frozen graph, a SavedModel export, or a model that originally came from Keras. The critical point is that .hdf5 stores Keras-layer structure and weights, while a plain graph export may only contain low-level computation nodes.
First Identify What You Actually Have
People often say ".pb file" when they mean different TensorFlow artifacts.
Common cases:
- frozen graph from older TensorFlow workflows
- SavedModel directory containing a
saved_model.pb - Keras model that was exported after training
These are not equivalent. A frozen graph is often inference-only and does not preserve enough high-level Keras metadata to rebuild a real Keras model automatically.
If You Still Have the Original Keras Model
If the model originally came from Keras and you still have the Python code or the live model object, the simplest answer is to save it again directly in the desired format.
That creates an HDF5 file from the Keras model itself. If you can do this, there is no need to reverse-engineer a graph export.
If You Only Have a Frozen .pb Graph
This is the hard case. In general, there is no reliable automatic conversion from a frozen TensorFlow graph to a full Keras .hdf5 model because Keras needs layer structure, config, and weight mapping in a form the frozen graph may not preserve.
You can still load the graph for inference in TensorFlow, but that is different from recovering a true editable Keras model.
This lets you inspect and use the graph, but it does not magically reconstruct high-level Keras layers.
If You Have a SavedModel Export
A SavedModel is more structured than a standalone frozen graph. You can often load it for inference, but even then it may not load back as a normal Keras training model unless it was saved in a Keras-compatible way.
Example inspection:
This helps you understand inputs and outputs. If the goal is serving or inference, wrapping the SavedModel may be enough. If the goal is a real .h5 Keras model, you still may need to rebuild the architecture manually.
Manual Rebuild Is Often the Real Answer
If you know the original architecture, rebuild it in Keras and then load equivalent weights if you can extract them or still have a checkpoint.
This is not automatic conversion. It is re-creating the model in a format Keras understands.
How to Decide Quickly
Use this decision flow:
- If you have the original Keras model object, save directly to
.h5. - If you have a SavedModel from Keras, try loading through the Keras-compatible path available in your stack.
- If you only have a frozen
.pb, expect inference access, not guaranteed Keras reconstruction. - If retraining or editing is required, rebuild the model architecture explicitly.
That saves time compared with hunting for a universal .pb to .h5 converter that usually does not exist.
Common Pitfalls
The biggest mistake is assuming every .pb file contains enough information to become a Keras .hdf5 model automatically. Another is confusing inference compatibility with model-editing compatibility. Teams also sometimes spend hours trying conversion scripts when the practical solution is to find the original training code or checkpoints. Finally, loading a graph successfully and seeing input-output tensors can create false confidence that the model has been fully reconstructed at the Keras layer level.
Summary
- A
.pbfile is not automatically convertible to.hdf5in the general case. - If you still have the original Keras model, save it directly as
.h5. - Frozen graphs are usually inference artifacts, not full Keras model definitions.
- SavedModel exports are more structured but still may not restore as editable Keras models.
- When exact reconstruction matters, rebuilding the architecture manually is often the correct path.

