Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
827 views
in Technique[技术] by (71.8m points)

tensorflow - TF save/restore graph fails at tf.GraphDef.ParseFromString()

Based on this converting-trained-tensorflow-model-to-protobuf I am trying to save/restore TF graph without success.

Here is saver:

with tf.Graph().as_default():
    variable_node = tf.Variable(1.0, name="variable_node")
    output_node = tf.mul(variable_node, 2.0, name="output_node")
    sess = tf.Session()
    init = tf.initialize_all_variables()
    sess.run(init)
    output = sess.run(output_node)
    tf.train.write_graph(sess.graph.as_graph_def(), summ_dir, 'model_00_g.pbtxt', as_text=True)
    #self.assertNear(2.0, output, 0.00001)
    saver = tf.train.Saver()
    saver.save(sess, saver_path)

which produces model_00_g.pbtxt with text graph description. Pretty much copy paste from freeze_graph_test.py.

Here is reader:

with tf.Session() as sess:

    with tf.Graph().as_default():
        graph_def = tf.GraphDef()
        graph_path = '/mnt/code/test_00/log/2016-02-11.22-37-46/model_00_g.pbtxt'
        with open(graph_path, "rb") as f:
            proto_b = f.read()
            #print proto_b   # -> I can see it
            graph_def.ParseFromString(proto_b) # no luck..
            _ = tf.import_graph_def(graph_def, name="")

    print sess.graph_def

which fails at graph_def.ParseFromString() with DecodeError: Tag had invalid wire type.

I am on docker container b.gcr.io/tensorflow/tensorflow:latest-devel in case it makes any difference.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The GraphDef.ParseFromString() method (and, in general, the ParseFromString() method on any Python protobuf wrapper) expects a string in the binary protocol buffer format. If you pass as_text=False to tf.train.write_graph(), then the file will be in the appropriate format.

Otherwise you can do the following to read the text-based format:

from google.protobuf import text_format
# ...
graph_def = tf.GraphDef()
text_format.Merge(proto_b, graph_def) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...