Is there an example on how to generate protobuf files holding trained TensorFlow graphs

前端 未结 6 1859
情话喂你
情话喂你 2020-11-28 19:45

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:

6条回答
  •  渐次进展
    2020-11-28 20:00

    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:

    • It freezes the weights by replacing variables with constants
    • It removes nodes which are not related to feedforward prediction

    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)
    

提交回复
热议问题