Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow - How to freeze a .pb from the SavedModel to be used for inference in TensorFlowInferenceInterface?

According to this answer, I can extract the MetaGraph from a SavedModel, then freeze the MetaGraph's GraphDef, and THEN run the freeze_graph.py script on that GraphDef to get the .pb usable in Android. My question: how exactly do I extract the MetaGraph (and then its GraphDef)? Because tf.saved_model.loader.load(sess, [tag_constants.SERVING], <model_path>) returns a MetaGraphDef instead of a MetaGraph.

like image 344
Gensoukyou1337 Avatar asked Oct 31 '17 06:10

Gensoukyou1337


1 Answers

I just got it. Turns out, after removing the Tensorflow version I got from conda and replacing it with the one from pip, I could just do this:

from tensorflow.python.tools import freeze_graph
from tensorflow.python.saved_model import tag_constants

input_saved_model_dir = "F:/python_machine_learning_codes/estimator_exported_model/1509418513"
output_node_names = "softmax_tensor"
input_binary = False
input_saver_def_path = False
restore_op_name = None
filename_tensor_name = None
clear_devices = False
input_meta_graph = False
checkpoint_path = None
input_graph_filename = None
saved_model_tags = tag_constants.SERVING

freeze_graph.freeze_graph(input_graph_filename, input_saver_def_path,
                            input_binary, checkpoint_path, output_node_names,
                              restore_op_name, filename_tensor_name,
                              output_graph_filename, clear_devices, "", "", "",
                              input_meta_graph, input_saved_model_dir,
                            saved_model_tags)

The one from conda-forge was incomplete, and even with the pip install, I had to copy the freeze_graph.py and the saved_model_utils from tensorflow-master. Also, the code from above is mostly copied from the freeze_graph_test.py.

like image 179
Gensoukyou1337 Avatar answered Oct 13 '22 00:10

Gensoukyou1337