Tensorflow - Testing a mnist neural net with my own images
Asked Answered
D

1

10

I'm trying to write a script that will allow me to draw an image of a digit and then determine what digit it is with a model trained on MNIST.

Here is my code:

import random
import image
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np
import scipy.ndimage

mnist = input_data.read_data_sets( "MNIST_data/", one_hot=True )

x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder(tf.float32, [None, 10])

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

train_step = tf.train.GradientDescentOptimizer(0.5).minimize (cross_entropy)

init = tf.initialize_all_variables()

sess = tf.Session()
sess.run(init)

for i in range( 1000 ):
    batch_xs, batch_ys = mnist.train.next_batch( 1000 )
    sess.run(train_step, feed_dict= {x: batch_xs, y_: batch_ys})

print ("done with training")

data = np.ndarray.flatten(scipy.ndimage.imread("im_01.jpg", flatten=True))
result = sess.run(tf.argmax(y,1), feed_dict={x: [data]})
print (' '.join(map(str, result)))

For some reason the results are always wrong but has a 92% accuracy when I use the standard testing method.

I think the problem might be how I encoded the image:

data = np.ndarray.flatten(scipy.ndimage.imread("im_01.jpg", flatten=True))

I tried looking in the tensorflow code for the next_batch() function to see how they did it, but I have no idea how I can compare against my approach.

The problem might be somewhere else too.

Any help to make the accuracy 80+% would be greatly appreciated.

Digestive answered 21/10, 2016 at 14:26 Comment(1)
As for image encoding, try use .png. From my tests, .jpg format is bad as long as it leave artifacts (grey pixels) on image.Rachitis
D
7

I found my mistake: it encoded the reverse, blacks were at 255 instead of 0.

 data = np.vectorize(lambda x: 255 - x)(np.ndarray.flatten(scipy.ndimage.imread("im_01.jpg", flatten=True)))

Fixed it.

Digestive answered 21/10, 2016 at 18:47 Comment(3)
Can you please mention the dimensions of the image used?Malefactor
@Malefactor Here is one of the images I tested againstDigestive
Thanks a lot, @VincentCloutier. My accuracy was 96% and my model was predicting wrong on my own image too. But your answer helped me to know that I have to inverse the image array.Lanielanier

© 2022 - 2024 — McMap. All rights reserved.