Keras
TensorFlow
Model Conversion
H5 to PB
Deep Learning

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.

python
1from tensorflow.keras.models import load_model
2
3# Load Keras model
4keras_model = load_model('model.h5')

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.

python
1import tensorflow as tf
2
3# Define the path to save the SavedModel
4saved_model_path = 'saved_model_dir/'
5
6# Save the Keras model as a SavedModel
7tf.saved_model.save(keras_model, saved_model_path)

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.

python
1from tensorflow.python.framework.convert_to_constants import convert_variables_to_constants_v2
2
3# Load the SavedModel as a concrete function
4loaded = tf.saved_model.load(saved_model_path)
5infer = loaded.signatures['serving_default']
6
7# Convert the concrete function to a frozen graph
8frozen_func = convert_variables_to_constants_v2(infer)
9
10# Export frozen graph to .pb
11tf.io.write_graph(graph_or_graph_def=frozen_func.graph,
12                  logdir="/output/path",
13                  name="frozen_model.pb",
14                  as_text=False)

Important Considerations

  • Model Compatibility: Not all Keras models can be directly converted to TensorFlow .pb format 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

StepDescription
Load Keras ModelUse load_model to load .h5 file.
Export to SavedModelUse tf.saved_model.save to export intermediate format.
Convert to .pbUse 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.


Course illustration
Course illustration

All Rights Reserved.