我一直在尝试使用 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
如果您尚未下载 inception 网络,这将为您提供 .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
有人知道这是怎么回事吗?
谢谢!
最佳答案
freeze_graph
工具的作用是将图表中的所有 Variable
节点转换为 tf.constant
节点 - 这允许您保存在单个 protobuf 文件中对图运行推理所需的所有信息(而不是分别保存 GraphDef 和 Variable 检查点数据)。
您在这里遇到的是成功卡住图(tensorflow_inception_graph.pb
)的结果:没有要保存的变量,因为它们已全部转换为常量!
关于python - 值错误: No variables to save when saving Tensorflow checkpoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42123196/