2016-02-11 2480 views
2

基於此converting-trained-tensorflow-model-to-protobuf我試圖保存/恢復TF圖沒有成功。TF保存/恢復圖失敗tf.GraphDef.ParseFromString()

這裏是保護程序:

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) 

,其與文字圖形描述產生model_00_g.pbtxt。從freeze_graph_test.py幾乎複製粘貼。

這裏是讀者:

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 

其在graph_def.ParseFromString()DecodeError: Tag had invalid wire type.

我失敗的碼頭工人容器b.gcr.io/tensorflow/tensorflow:latest-devel的情況下,這有什麼差別。

回答

9

GraphDef.ParseFromString()方法人類可讀表示(並且,在一般情況下,在任何的Python protobuf的包裝紙的ParseFromString()方法)預計在字符串二進制協議緩衝區格式。如果您通過as_text=Falsetf.train.write_graph(),那麼文件將採用適當的格式。

否則,您可以執行以下操作來讀取基於文本的格式:

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

感謝 - 這兩種方式解決眼前的問題。然而,主要任務不在我這裏 - 我想存儲圖+變量,然後加載,然後在其他地方評估。這[[tensorflow/tensorflow/python/tools/freeze_graph.py'](https://github.com/tensorflow/tensorflow/tree/00440e99ffb1ed1cfe4b4ea650e0c560838a6edc/tensorflow/python/tools)看起來非常符合我的需要,但它是不是在我的碼頭圖像中,並且使用不是很清晰,只有一半的參數 - 我想我會等待0.7這個,現在切換到其他任務。 – rgr

1

ParseFromString需要二進制序列化協議緩衝,對於需要使用text_format.Merge所用here

0

我試圖通過Java API,它僅接受二進制加載模型。但是在我們使用contrib.pystimator生成文本模型文件格式的python中。 我在網上發現了一個model file converter,看起來工作正常。 如果您有現有的文本格式模型文件,這也可能解決原始問題(使用二進制模型加載器)。