how to freeze save model pb

Solutions on MaxInterview for how to freeze save model pb by the best coders in the world

showing results for - "how to freeze save model pb"
Cressida
05 Mar 2018
1def frozen_graph_maker(export_dir,output_graph):
2        with tf.Session(graph=tf.Graph()) as sess:
3                tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], export_dir)
4                output_nodes = [n.name for n in tf.get_default_graph().as_graph_def().node]
5                output_graph_def = tf.graph_util.convert_variables_to_constants(
6                        sess, # The session is used to retrieve the weights
7                        sess.graph_def,
8                        output_nodes# The output node names are used to select the usefull nodes
9               )       
10        # Finally we serialize and dump the output graph to the filesystem
11        with tf.gfile.GFile(output_graph, "wb") as f:
12                f.write(output_graph_def.SerializeToString())
13def main():
14        export_dir='/dir/of/pb/and/variables'
15        output_graph = "frozen_graph.pb"
16        frozen_graph_maker(export_dir,output_graph)
17