Building inference

Now we will create an inference code which loads the latest checkpoints and then makes predictions on the basis of learned parameters. For that, we need to create a saver operation which will pick the latest checkpoints and load the metadata. Metadata contains the information regarding the variables and the nodes that we created in the graph:

# Pointing the model checkpoint
checkpoint_file = tf.train.latest_checkpoint(os.path.join(hy_param.checkpoint_dir, 'checkpoints'))
saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))

We know the importance of this because we want to load the similar variables and operations back from the stored checkpoint. We load them into memory using tf.get_default_graph().get_operation_by_name() by passing the operation name in the param which we defined in the model:

# Load the input variable from the model
input_x = tf.get_default_graph().get_operation_by_name("input_x").outputs[0]

# Load the Prediction operation
prediction = tf.get_default_graph().get_operation_by_name("prediction").outputs[0]

Now we need to initialize the session and pass data for a test image to the operation that makes the prediction:

# Load the test data
test_data = np.array([mnist.test.images[0]])

with tf.Session() as sess:
# Restor the model from the checkpoint
saver.restore(sess, checkpoint_file)

# Execute the model to make predictions
data = sess.run(prediction, feed_dict={input_x: test_data })

print("Predicted digit: ", data.argmax() )

And we are done with our first project which predictions the digit class provided the handwritten image! Here are some of the results that model predicted when provided the test image from the mnist dataset:

Figure 2.4: Output of the model which depicts the prediction of the model and the input image.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.44.182