How to export Keras .h5 to tensorflow .pb?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Keras, a high-level neural networks API, runs on top of TensorFlow and enables fast experimentation with deep learning models. One of the key aspects of utilizing Keras is saving models in the .h5 format. However, deploying these models in a production environment often requires converting them to the TensorFlow .pb (Protocol Buffers) format. This article outlines the steps necessary to export a Keras model in the .h5 format to a TensorFlow .pb file, examining the technical details and offering practical examples.
Understanding Keras .h5 and TensorFlow .pb Formats
The Keras .h5 file format is based on HDF5, a data model, library, and file format for storing multidimensional data. This format retains the model architecture, weights, and other crucial components.
In contrast, the TensorFlow .pb file format, also known as Protocol Buffers, is a binary serialization format used for faster deserialization in production environments. It contains the computation graph of the model and is optimized for deployment, enabling integration with other systems.
Conversion Process
1. Load the Keras Model
Begin by loading your trained model from the .h5 file.
2. Export to TensorFlow's SavedModel Format
The first step in the conversion process requires exporting the Keras model into TensorFlow's SavedModel format. This intermediary step facilitates the conversion to a .pb file.
3. Exporting to .pb Format
The SavedModel format will have subdirectories and files, including saved_model.pb. This file contains the serialized graph and is what you will use for deployment. If you specifically need a .pb file without other artifacts, you can convert it directly, but for most purposes, the SavedModel directory is sufficient for TensorFlow serving.
4. Optimize for Deployment (Optional)
After exporting to a .pb file, you may want to optimize the model for production use. This can include techniques like quantization to reduce the file size and increase inference speed.
Important Considerations
- Model Compatibility: Not all Keras models can be directly converted to TensorFlow
.pbformat if they use custom layers or operations unsupported by TensorFlow. - Version Issues: Ensure the versions of Keras and TensorFlow are compatible, as APIs change over time.
- Optimization: Additional tools like TensorFlow’s Model Optimization Toolkit can further optimize the model for deployment.
Summary Table
| Step | Description |
| Load Keras Model | Use load_model to load .h5 file. |
| Export to SavedModel | Use tf.saved_model.save to export intermediate format. |
Convert to .pb | Use the directory to access saved_model.pb directly. |
| Optimize for Deployment (Optional) | Use optimization techniques like quantization. |
Conclusion
The conversion of a Keras model from .h5 to a TensorFlow .pb file format is essential for deploying models into production. This guide simplifies the process through clear steps and considerations. By exporting to TensorFlow's SavedModel format, you pave the way for further optimization and a more efficient deployment process in high-performance environments.

