【问题标题】:ValueError: No variables to save when saving Tensorflow checkpointValueError:保存 TensorFlow 检查点时没有要保存的变量
【发布时间】:2017-02-11 13:41:13
【问题描述】:

我一直在尝试使用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

有人知道这里发生了什么吗?

谢谢!

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    freeze_graph 工具的作用是将图形中的所有 Variable 节点转换为 tf.constant 节点 - 这允许您将在图形上运行推理所需的所有信息保存在单个 protobuf 文件中(如反对分别保存GraphDef 和Variable 检查点数据)。

    您在这里遇到的是成功冻结图形 (tensorflow_inception_graph.pb) 的结果:没有要保存的变量,因为它们都已转换为常量!

    【讨论】:

    • 我在这里,认为我已经将我的实际问题简化为一个更容易测试的问题。谢谢!
    • 好的。那么,我们如何保存冻结的图表呢?
    猜你喜欢
    • 2016-12-02
    • 1970-01-01
    • 2018-11-07
    • 1970-01-01
    • 2018-11-17
    • 2016-03-10
    • 1970-01-01
    • 2019-11-15
    • 1970-01-01
    相关资源
    最近更新 更多