Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using TensorFlow to classify Handwritten Digits without validation labels

I have been going through the TensorFlow Tutorial and in general reading up on Machine Learning.

Its my understanding that one of the major benefits to using a neural network is their ability to quickly classify the presented input after training.

To start off I began by stepping through the example code and looking at how the training data is structured and I was able to successfully use the basic example (91% accuracy) to recognize images (only digits) that I have created using the following code snippet:

# Training is already done using the code from the tutorial 
# Do the same for five
...
test_five_image = np.zeros((28, 28), dtype=np.uint8, order='C')
for five_coords in npg.five_coordinates:
    i = int(five_coords[0] / 28)
    j = int((five_coords[1] / 28) + 3) # By Eye Centering
    test_five_image[i][j] = 0xFF
test_five_image = np.rot90(test_five_image, 1)
Image.fromarray(np.uint8(test_five_image)).save(str(5) + '.bmp') 
...
# Images are Four, Five, 0 and 6
test_labels = input_data.dense_to_one_hot(np.array([4, 5, 0, 6], np.int32))
dataset = input_data.DataSet(test_images, test_labels)
print sess.run(accuracy, feed_dict={x: dataset.images, y_: dataset.labels})

Example of an image that is produced from the above code: Bitmap extracted from test data used.

Note: This image is built from a list of points and then scaled down to fit a 28 * 28 array. The colors are inverted as the image is simply converted straight from a numpy array to a bitmap. Each point in the list is set to 0xFF as per the MNIST file format where 0 is white and 255 black.

This snippet above outputs 1.0 (sometimes 0.75 depending on the accuracy from training) and it correctly classifies the inputs to the labels.

So my question is what are the necessary steps involved in using neural network built using TensorFlow to simply classify what the input is so that for example if the input is a '7' the output would for example be:

>>> [0, 0, 0, 0, 0, 0, 0, 1, 0, 0]

I have had a look through the TensorFlow documentation and I have not been able to come up with an solution. I suspect it is probably down to missing something in the tutorial.

Thanks

like image 827
Bigfoot Avatar asked Aug 07 '26 22:08

Bigfoot


1 Answers

Assuming you've followed the MNIST for ML Beginners Tutorial, to get a simple prediction, add an argmax node like so:

prediction = tf.argmax(y, 1)

And then run it, feeding in the data you'd like to classify:

prediction_val = sess.run(prediction, feed_dict={x: dataset.images})

prediction_val will be of shape (batch_size,) and contains the likeliest label for each image in the batch.

Note that the feed_dict only includes x and not y_ because prediction does not depend on y_.

like image 183
Ryan Sepassi Avatar answered Aug 10 '26 10:08

Ryan Sepassi