[英]ValueError: No variables to save when saving Tensorflow checkpoint
我一直在嘗試使用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
此外,這里是我的完整代碼的要點,如果有人好奇: 要點
有誰知道這里發生了什么?
謝謝!
freeze_graph
工具所做的是將圖形中的所有Variable
節點轉換為tf.constant
節點 - 這允許您在單個protobuf文件中保存在圖形上運行推理所需的所有信息(與保存GraphDef
和Variable
相反)檢查點數據分開)。
你在這里遇到的是成功凍結圖形的結果( tensorflow_inception_graph.pb
):沒有要保存的變量,因為它們都被轉換為常量!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.