TensorFlow – Deep MNIST for Experts tutorial

The second step in my quest for deep learning for 3D point clouds was to try recognizing MNIST digits using a deep convolutional network (The first step can be found here).

I followed google’s tutorial. I found it very well explained. My final code can be downloaded here.

Using this code I managed to get a recognition accuracy of about 99.2%.

However, the accuracy wasn’t the thing that troubled me the most. At this point, I realized there are several things I wish to know how to do

  1. Export/import the final model
  2. Track the training progress
  3. Visualize the neural net

For these tasks, I did not find a single good tutorial. Therefore I decided to summarize my findings. In this post, I will cover export/import the final model and the other two will be covered in future posts.

Export the final model 

It turns out that exporting the model is rather simple. It requires about 3 lines of code:
Two lines before the training loop

saver = tf.train.Saver(max_to_keep=3)
CeckPointFilename = "path to where you wish to save the checkpoints"

I defined to keep the model of the last 3 training iteration (more precisely 300 iterations because I saved once every 100 iterations) because there are a lot of these files and I don’t want it to take too much memory on my hard drive.

Another line in the training loop (or after it, depends if you want the final model or several models throughout the training process):

saver.save(sess, CeckPointFilename , global_step = i )

However, this turned out to be not quite enough. If we wish to make use of our model we should also insert the input and output into a collection so we can use them once the model is restored.

tf.add_to_collection("keep_prob", keep_prob)
tf.add_to_collection("x", x)
tf.add_to_collection("y_", y_)
tf.add_to_collection("y_conv", y_conv)

Loading the model

I found this Stack-overflow thread useful. It referenced this Tensorflow documentation.

In order to load the model you should change the following lines of code to include the name and path to your model.

sess = tf.Session()
new_saver = tf.train.import_meta_graph("PathToModelDir/ModelName.meta")
new_saver.restore(sess, "PathToModelDir/ModelName")

Once you imported the model you can extract the input and output which we added to the collection

y_conv = tf.get_collection("y_conv")[0]
x = tf.get_collection("x")[0]
y_ = tf.get_collection("y_")[0]
keep_prob =  tf.get_collection("keep_prob")[0]

Now we just need to apply the model to an input image and make a class prediction

ImageIndex = 3
InputImage = mnist.test.images[ImageIndex].reshape(1,784)
CorrectLabel = mnist.test.labels[ImageIndex].reshape(1,10)

logit = sess.run(y_conv,feed_dict={ x: InputImage, y_: CorrectLabel, keep_prob: 1.0})
prediction = sess.run(tf.argmax(logit,1))
digit = sess.run(tf.argmax(CorrectLabel,1))
print("Prediction : %d, Actual : %d"% (prediction, digit))

I’m not sure if this is the best way of doing this but it works. The final version (with exporting) can be found here and the importing code can be found here.