2017-03-03 135 views
0

在代碼tf.contrib.slim.get_variables_to_restore()下運行all_vars返回空值[],然後在調用tf.train.Saver時導致失敗。詳細錯誤信息如下所示。tf.contrib.slim.get_variables_to_restore()不返回值

我錯過了什麼?

>>> 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 

回答

0

這裏的問題似乎是你的圖是空的 - 即。它不包含任何變量。您在行with tf.Session(graph=tf.Graph()):上創建一個新圖形,並且以下任一行都不會創建一個tf.Variable對象。

要恢復預訓練TensorFlow模型,你需要做的三兩件事之一:

  1. 重建模型圖,通過執行該用於訓練的模型相同的Python構建圖形碼第一名。
  2. 加載一個「MetaGraph」,其中包含有關如何重構圖結構和模型變量的信息。有關如何創建和使用MetaGraph的更多詳細信息,請參閱this tutorial。 MetaGraphs通常與檢查點文件一起創建,並且通常具有擴展.meta
  3. 加載一個「SavedModel」,其中包含一個「MetaGraph」。有關更多詳細信息,請參見文檔here