How to use a previously trained model to get labels of images - TensorFlow
<p>After training a model (according to the <a href="https://www.tensorflow.org/versions/r0.11/tutorials/mnist/pros/index.html" rel="nofollow">mnist tutorial</a>), and saving it using this code:</p>

<pre><code>saver = tf.train.Saver()
save_path = saver.save(sess,'/path/to/model.ckpt')
</code></pre>

<p>I want to use the saved model in order to find labels for a new batch of images. I succeeded to load the model and test it with an already exist database, as follows:</p>

<pre><code># load MNIST data
folds = build_database_tuple.load_data(data_home_dir='/path/to/database')

# starting the session. using the InteractiveSession we avoid build the entiee comp. graph before starting the session
sess = tf.InteractiveSession()

# start building the computational graph
...

BUILD AND DEFINE ALL THE LAYERS

...

y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

# TRAIN AND EVALUATION:
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


sess.run(tf.initialize_all_variables())
saver = tf.train.Saver()
# Restore variables from disk.
savepath = '/path/to/model.ckpt'
saver.restore(sess, save_path=savepath)
print("Model restored.")

print("test accuracy %g"%accuracy.eval(feed_dict={x: folds.test.images, y_: folds.test.labels, keep_prob: 1.0}))
</code></pre>

<p>Although I succeed to load and test the model, <strong>I don't know how to get y'</strong> array (the model's predictions for the database images).</p>

<p>I scanned the web and found a lot of answers for this question, but I couldn't project those answers for this particular case (e.g. <a href="http://stackoverflow.com/questions/37578876/how-to-feed-cifar10-trained-model-with-my-own-image-and-get-label-as-output">this answer</a> that given about the CIFAR10 tutorial, which really different from the MNIST tutorial).</p>

<p>A detailed answer will highly appreciated (I'm new with tensorflow and still learning the terminology). Thanks a lot for your help!</p>

<p>Define an OP for performing classification, like</p>

<pre><code>predictor = tf.argmax(y_conv,1)
</code></pre>

<p>and then run it on trained model with new inputs</p>

<pre><code>print(sess.run(predictor, feed_dict={ x = new_data }))
</code></pre>

<p>since "predictor" does not depend on <code>y</code> you do not have to provide it and this will still execute.</p>

<p>If you just want to see predictions on test images you can also do both things in one run call by removing your accuracy eval call and doing</p>

<pre><code>acc, predictions = sess.run([accuracy, predictor],
                            feed_dict={x: folds.test.images,
                                       y_: folds.test.labels,
                                       keep_prob: 1.0}))

print('Accuracy', acc)
print('Predictions', predictions)
</code></pre>

python python-2.7 machine-learning neural-network tensorflow