Keras to TensorFlow

General code to convert a trained keras model into an inference tensorflow model
Under MIT License
By amir-abdi

python deep-learning machine-learning tensorflow keras

Keras to TensorFlow

The keras_to_tensorflow is a tool that converts a trained keras model into a ready-for-inference TensorFlow model. The tool is NOT tailored for TensorFlow 2.0 it SEEMS to be working fine.


How to use

Keras models can be saved as a single [.hdf5 or h5] file, which stores both the architecture and weights, using the function.
This model can be then converted to a TensorFlow model by calling this tool as follows:


Keras models can also be saved in two separate files where a [.hdf5 or h5] file stores the weights, using the model.save_weights() function, and another .json file stores the network architecture using the model.to_json() function.
In this case, the model can be converted as follows:



python --help

to learn about other supported flags (quantize, output_nodes_prefix, save_graph_def).


Legacy code

The code on how to freeze and save keras models in previous versions of tensorflow is also available. Back then, the freeze_graph tool (/tensorflow/python/tools/ was used to convert the variables into constants. This functionality is now handled by graph_util.convert_variables_to_constants