tf.train.write_graph用法

I am not a producer of knowledge, I'm just a tiny porter, we all stand on the shoulders of giants


Explore the usage of this stuff afternoon, finally it will be used, in this instance attached child

We must first understand the principles he saved map, which was about this in great detail, please fine chemicals

https://zhuanlan.zhihu.com/p/31308381

tf.train.write_graph This function can be retained node, op, constant, but do not save variable, if you want to save the variable, then they would turn constant

import tensorflow as tf
import numpy as np
from tensorflow.python.platform import gfile

#生成图
input1= tf.placeholder(tf.int32,name="input")
b = tf.constant([3])
output1= tf.add(input1, b, name="output")

#保存图
with tf.Session() as sess:
    tf.train.write_graph(sess.graph_def, "./", "test.pb", False)
    print(sess.run(output1,feed_dict={input1:1}))

#读取图
with tf.Session() as sess:
    with gfile.FastGFile("./test.pb",'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        tf.import_graph_def(graph_def, name='')

#查看图中信息,填充运行图
with tf.Session() as sess:
    input_x1 = sess.graph.get_tensor_by_name("input:0")  
    print (input_x1)   #可以看到这个placeholder的属性
    output = sess.graph.get_tensor_by_name("output:0")
    print (output)
    data1 = int(3)
    print(sess.run(output,feed_dict={input_x1:data1}))  #填充placeholder,然后运行图

#或者也可以直接读入图,运行
data1 = int(3)
with tf.Session() as sess:
    with gfile.FastGFile("./test.pb",'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        output = tf.import_graph_def(graph_def, input_map={'input:0':data1},             
        return_elements=['output:0'], name='a') 
        print(sess.run(output))  

Explain a few questions:

1.sess.graph.get_tensor_by_name ( "input: 0") is doing, why it is input: 0?

   A: Yes, to help you get tensor, input is the node name, input: 0 is the first output of the tensor expression node

2. If there is a variable figure, but also want to save, how to do?

   A: Conversion of FIG save time constant to save, graph = convert_variables_to_constants (sess, sess.graph_def, [ "out"])

Finally: there are problems please correct me Contact

 

 

Published 15 original articles · won praise 30 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_40778406/article/details/104554897