The brand new TensorFlow 2.0 has a lot of features which unify reduntencies inherited from TF 1.0. Here’s my conclusion of converting old TensorFlow models all the way to ready-to-serve, mobile-friendly FP16 TensorFlow Lite model in *.tflite format.
- Check your model
First off, you should have these files in check before dealing with your models:
1 | checkpoint |
These are typical checkpoint files genereated by tf.train.Saver() helper during steps of training, in the purpose of saving current variables from nodes and help recover training process with them. As we no longer need further training, let’s convert it to a SavedModel file with freeze_model.py under Windows PowerShell:
1 | py .\freeze_model --model_dir "./your/model/directory" --output_node_names "output_node_1,output_node_2" |
where --model_dir
is your model directory, and --output_node_names
are the names of your output nodes. The names of output nodes can be easily found by simply reviewing the original code, or typing the following code during a TensorFlow session after your model’s graph is loaded:
1 | print([n.name for n in tf.get_default_graph().as_graph_def().node]) |
- Add tags to MetaGraphDef
After freezing your model, you will obtain a file called frozen_model.pb
, but it still can’t be served because lack of MetaGraphDef
tagged with serve
. To get a properly labeled SavedModel, you need to utilize simple_save to add these meta tags:
1 | import tensorflow as tf |
- Conversion
Now your SavedModel has these MetaGraphDef
tagged with serve
. The next step is to convert it into *.tflite for simple and fast model serving on almost any platforms:
1 | ... |
If you encounter an error complaining tf.lite.Optimize is not found, and your are using TensorFlow version 1.13 or lower, try the solution here.