smckee
smckee

Reputation: 492

ValueError: No variables to save when saving Tensorflow checkpoint

I've been trying to use the freeze_graph function to get a model + weights/biases, but in the process, I found that my inception network does not seem to have any variables, despite being able to correctly classify image. My code is as follows:

#!/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()

The error is:

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

If you don't already have the inception network downloaded, this will get you the .pb file:

$ wget https://storage.googleapis.com/download.tensorflow.org/models/inception_dec_2015.zip -O tensorflow/examples/label_image/data/inception_dec_2015.zip

Also, here's a gist of my full code, if anyone is curious: gist

Does anyone know what's going on here?

Thanks!

Upvotes: 1

Views: 4335

Answers (1)

Sam Abrahams
Sam Abrahams

Reputation: 46

What the freeze_graph tool does is convert all of the Variable nodes in a graph into tf.constant nodes- this allows you to save all the information needed to run inference on a graph in a single protobuf file (as opposed to saving the GraphDef and Variable checkpoint data separately).

What you're experiencing here is the result of a successfully frozen graph (tensorflow_inception_graph.pb): there are no variables to save because they have all been converted into constants!

Upvotes: 3

Related Questions