3

I trained a model (per the MNIST tutorial) and saved it:

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

I want to use the saved model in order to find labels for a new batch of images. I load the model and test it with a database:

# 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}))

Although I can load and test the model, how do I get the y' array that contains the predictions for the database images?

I scanned the web and found a lot of answers for this question, but I couldn't fit those answers to this particular case. For example, I found this answer about the CIFAR10 tutorial but it's very different from the MNIST tutorial.

CDspace
  • 2,639
  • 18
  • 30
  • 36
roishik
  • 515
  • 2
  • 9
  • 19

2 Answers2

6

Define an OP for performing classification, like

predictor = tf.argmax(y_conv,1)

and then run it on trained model with new inputs

print(sess.run(predictor, feed_dict={ x = new_data }))

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

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

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)
lejlot
  • 64,777
  • 8
  • 131
  • 164
-1

Another option to what lejlot just answered(and this option is primarily for learning and understanding what the network is doing ) could be:You can make predictions using feedforward using the weights and biases that your network already learned that means that al the computations you defined in your "BUILD AND DEFINE ALL THE LAYERS" should be applied to a new dataset ,for example asuming that your network is of the shape inputs-> relu layer -> softmax layer. You would compute:

relu_layer_oututs = tf.nn.relu(tf.matmul(input_data,weights_layer1)+biases_layer1 );
prediction = tf.nn.softmax(tf.matmul(relu_layer_outputs,weights_layer2)+biases_layer2);
Luis Leal
  • 3,388
  • 5
  • 26
  • 49