基于此converting-trained-tensorflow-model-to-protobuf我试图保存/恢复TF图表但没有成功。
这是救星:
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.
我在Docker容器b.gcr.io/tensorflow/tensorflow:latest-devel
上,以防它有任何区别。
答案 0 :(得分:11)
GraphDef.ParseFromString()
方法(以及一般来说,任何Python protobuf包装器上的ParseFromString()
方法)都需要二进制协议缓冲区格式的字符串。如果您将as_text=False
传递给tf.train.write_graph()
,则该文件将采用适当的格式。
否则,您可以执行以下操作来阅读基于文本的格式:
from google.protobuf import text_format
# ...
graph_def = tf.GraphDef()
text_format.Merge(proto_b, graph_def)
答案 1 :(得分:1)
ParseFromString
需要二进制序列化协议缓冲区,对于人类可读的表示,您需要使用text_format.Merge
here
答案 2 :(得分:0)
我试图通过只接受二进制文件的java API加载模型。但是在python中我们使用contrib.Estimator生成文本模型文件格式。 我在网上找到model file converter,似乎工作正常。 如果您有现有的文本格式模型文件,这也可以解决原始问题(使用二进制模型加载器)。