Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow ValueError: No variables to save from

I have written a tensorflow CNN and it is already trained. I wish to restore it to run it on a few samples but unfortunately its spitting out:

ValueError: No variables to save

My eval code can be found here:

import tensorflow as tf

import main
import Process
import Input

eval_dir = "/Users/Zanhuang/Desktop/NNP/model.ckpt-30"
checkpoint_dir = "/Users/Zanhuang/Desktop/NNP/checkpoint"

init_op = tf.initialize_all_variables()
saver = tf.train.Saver()

def evaluate():
  with tf.Graph().as_default() as g:
    sess.run(init_op)

    ckpt = tf.train.get_checkpoint_state(checkpoint_dir)

    saver.restore(sess, eval_dir)

    images, labels = Process.eval_inputs(eval_data = eval_data)

    forward_propgation_results = Process.forward_propagation(images)

    top_k_op = tf.nn.in_top_k(forward_propgation_results, labels, 1)

    print(top_k_op)

def main(argv=None):
    evaluate()

if __name__ == '__main__':
  tf.app.run()
like image 755
Zan Huang Avatar asked Jul 28 '16 03:07

Zan Huang


1 Answers

The tf.train.Saver must be created after the variables that you want to restore (or save). Additionally it must be created in the same graph as those variables.

Assuming that Process.forward_propagation(…) also creates the variables in your model, adding the saver creation after this line should work:

forward_propgation_results = Process.forward_propagation(images)

In addition, you must pass the new tf.Graph that you created to the tf.Session constructor so you'll need to move the creation of sess inside that with block as well.

The resulting function will be something like:

def evaluate():
  with tf.Graph().as_default() as g:
    images, labels = Process.eval_inputs(eval_data = eval_data)
    forward_propgation_results = Process.forward_propagation(images)
    init_op = tf.initialize_all_variables()
    saver = tf.train.Saver()
    top_k_op = tf.nn.in_top_k(forward_propgation_results, labels, 1)

  with tf.Session(graph=g) as sess:
    sess.run(init_op)
    saver.restore(sess, eval_dir)
    print(sess.run(top_k_op))
like image 60
mrry Avatar answered Sep 16 '22 14:09

mrry