我一直在尝试使用freeze_graph函数来获取模型+权重/偏差,但在此过程中,我发现我的初始网络似乎没有任何变量,尽管能够正确分类图像。我的代码如下:
#!/usr/bin/python
import tensorflow as tf
import numpy as np
def outputCheckpoint(sess):
with sess.as_default():
print("Saving to checkpoint")
saver = tf.train.Saver()
# Fails on this line: 'ValueError: No variables to save'
saver.save(sess, '/path/to/tensorflow/graph_saver/save_checkpoint')
def main():
with tf.device('/cpu:0'):
with open("tensorflow_inception_graph.pb", mode='rb') as f:
fileContent = f.read()
graph_def = tf.GraphDef()
graph_def.ParseFromString(fileContent)
images = tf.placeholder("float", [ None, 299, 299, 3])
tf.import_graph_def(graph_def, input_map={ "Mul": images })
print "graph loaded from disk"
graph = tf.get_default_graph()
with tf.Session() as sess:
init = tf.global_variables_initializer()
sess.run(init)
outputCheckpoint(sess)
main()
错误是:
graph loaded from disk
Saving to checkpoint
Traceback (most recent call last):
File "./inception_benchmark.py", line 28, in <module>
main()
File "./inception_benchmark.py", line 24, in main
saver = tf.train.Saver()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1067, in __init__
self.build()
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1088, in build
raise ValueError("No variables to save")
ValueError: No variables to save
如果您还没有下载初始网络,这将获得.pb文件:
$ wget https://storage.googleapis.com/download.tensorflow.org/models/inception_dec_2015.zip -O tensorflow/examples/label_image/data/inception_dec_2015.zip
此外,如果有人好奇的话,这里是我的完整代码的要点:gist
有谁知道这里发生了什么?
谢谢!
答案 0 :(得分:3)
freeze_graph
工具的作用是将图表中的所有Variable
节点转换为tf.constant
个节点 - 这样您就可以保存在图表上运行推理所需的所有信息单个protobuf文件(而不是单独保存GraphDef
和Variable
检查点数据)。
您在此处遇到的是成功冻结图表(tensorflow_inception_graph.pb
)的结果:没有要保存的变量,因为它们都已转换为常量!