I am looking at Google\'s example on how to deploy and use a pre-trained Tensorflow graph (model) on Android. This example uses a .pb
file at:
Alternatively to my previous answer using freeze_graph()
, which is only good if you call it as a script, there is a very nice function that will do all the heavy lifting for you and is suitable to be called from your normal model training code.
convert_variables_to_constants() does two things:
Assuming sess
is your tf.Session()
and "output"
is the name of your prediction node, the following code will serialize your minimal graph both into textual and binary protobuf.
from tensorflow.python.framework.graph_util import convert_variables_to_constants
minimal_graph = convert_variables_to_constants(sess, sess.graph_def, ["output"])
tf.train.write_graph(minimal_graph, '.', 'minimal_graph.proto', as_text=False)
tf.train.write_graph(minimal_graph, '.', 'minimal_graph.txt', as_text=True)