Search code examples
tensorflowtf-slim

tf.contrib.slim.get_variables_to_restore() does not return value


Running below code tf.contrib.slim.get_variables_to_restore() return empty value [] for all_vars, and then causing failure when calling tf.train.Saver. Detail error message shows below.

Am I missing anything?

>>> import tensorflow as tf
>>> inception_exclude_scopes = ['InceptionV3/AuxLogits', 'InceptionV3/Logits', 'global_step', 'final_ops']
>>> inception_checkpoint_file = '/Users/morgan.du/git/machine-learning/projects/capstone/yelp/model/inception_v3_2016_08_28.ckpt'
>>> with tf.Session(graph=tf.Graph()) as sess:
...     init_op = tf.global_variables_initializer()
...     sess.run(init_op)
...     reader = tf.train.NewCheckpointReader(inception_checkpoint_file)
...     var_to_shape_map = reader.get_variable_to_shape_map()
...     all_vars = tf.contrib.slim.get_variables_to_restore(exclude=inception_exclude_scopes)
...     inception_saver = tf.train.Saver(all_vars)
...     inception_saver.restore(sess, inception_checkpoint_file)
... 
Traceback (most recent call last):
  File "<stdin>", line 7, in <module>
  File "/Users/morgan.du/miniconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1051, in __init__
    self.build()
  File "/Users/morgan.du/miniconda2/lib/python2.7/site-packages/tensorflow/python/training/saver.py", line 1072, in build
    raise ValueError("No variables to save")
ValueError: No variables to save

Solution

  • The problem here seems to be that your graph is empty—i.e. it does not contain any variables. You create a new graph on the line with tf.Session(graph=tf.Graph()):, and none of the following lines creates a tf.Variable object.

    To restore a pre-trained TensorFlow model, you need to do one of three things:

    1. Rebuild the model graph, by executing the same Python graph building code that was used to train the model in the first place.
    2. Load a "MetaGraph" that contains information about how to reconstruct the graph structure and model variables. See this tutorial for more details on how to create and use a MetaGraph. MetaGraphs are often created alongside checkpoint files, and typically have the extension .meta.
    3. Load a "SavedModel", which contains a "MetaGraph". See the documentation here for more details.