Home » Tensorflow: converting a GraphDef (.pb) to tflite

Tensorflow: converting a GraphDef (.pb) to tflite

  • by
Want to do a random act of kindness? Share this post.

Update (2019-09-06): the nightly builds will soon start using tensorflow 2. That’s a problem, because TF2 will no longer support converting frozen graphs to tflite.

Please install the following requirements.txt using pip install -r requirements.txt to get an environment that actually succeeds in converting to tflite.

absl-py==0.7.1
adal==1.2.1
asn1crypto==0.24.0
astor==0.7.1
certifi==2019.3.9
cffi==1.12.2
chardet==3.0.4
cryptography==2.6.1
Cython==0.29.7
emoji==0.5.2
gast==0.2.2
google-pasta==0.1.5
grpcio==1.20.0
h5py==2.9.0
idna==2.8
isodate==0.6.0
Keras-Applications==1.0.7
Keras-Preprocessing==1.0.9
Markdown==3.1
mock==2.0.0
msrest==0.6.6
msrestazure==0.6.0
numpy==1.16.2
oauthlib==3.0.1
opencv-python==4.1.0.25
pbr==5.1.3
Pillow==6.0.0
protobuf==3.7.1
pycparser==2.19
PyJWT==1.7.1
python-dateutil==2.8.0
requests==2.21.0
requests-oauthlib==1.2.0
six==1.12.0
tb-nightly==1.14.0a20190417
tensorboard==1.13.1
tensorflow-estimator==1.13.0
termcolor==1.1.0
tf-estimator-nightly==1.14.0.dev2019041601
tf-nightly==1.14.1.dev20190417
urllib3==1.24.1
Werkzeug==0.15.2
wrapt==1.11.1

The Python script can be found below in the old version of this blog post.


In many occasions, I introduce myself as a data scientist. I turn data into value. I wrangle data. I model data. Yet, deployment of models used to be a huge blindspot to me. Problems that are left unsolved often make me feel dumb. One of these problems was the conversion of GraphDef (.pb) to a FlatBuffer (.tflite) file.

The problem is not necessarily that it’s hard, on the contrary. Compatibility is the biggest bottleneck. Finding the right combination of TensorFlow and Protobuf is like the first time you’re making béchamel sauce, where you have to correctly balance butter, flower and milk.

ImportError: DLL load failed: The specified procedure could not be found.

is one of those errors that made me nuts. Many versions of TensorFlow were tried. In a desperate move, I tried running it in Google Cloud Platform on a DataLab notebook, but I ran into the same errors.

The solution?

pip uninstall tensorflow
pip install tf-nightly
pip show protobuf

If protobuf is version 3.6.1, then proceed to installing the pre-release version of 3.7.0.

pip uninstall protobuf
pip install protobuf==3.7.0rc2 

I still couldn’t get the command line version to work. It kept returning the error: “tflite_convert: error: –input_arrays and –output_arrays are required with –graph_def_file” although both parameters were supplied.

It worked in Python, however.

import tensorflow as tf

graph_def_file = "model.pb"
input_arrays = ["model_inputs"]
output_arrays = ["model_outputs"]

converter = tf.lite.TFLiteConverter.from_frozen_graph(
        graph_def_file, input_arrays, output_arrays)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

Finally, if you built a model with an autoML tool such as Microsoft’s custom vision, and you don’t know what the input and output nodes are, I can recommend you the tool Netron, a great neural network visualization tool.

Want to do a random act of kindness? Share this post.