I'm attempting to use this example from Google's TensorFlow GitHub, which builds up a CNN and then trains and tests on the MNIST data set:
https://github.com/tensorflow/tensorflow/blob/r1.4/tensorflow/examples/tutorials/mnist/mnist_deep.py
But instead of testing on the MNIST examples as the above does:
print('test accuracy %g' % accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
I'd like to test on my own images, one at a time. I drew 10 images, the digits 0 - 9 in Paint, here are a few examples:
Making some changes based on this post, which is based on the softmax Google example:
Tensorflow - Testing a mnist neural net with my own images
Here is what I have so far:
I can't seem to work out how to change the above line to use a single .png or .jpg rather than multiple images from the pre-configured MNIST data set. Here's what I have so far for my entire main() function:
#######################################################################################################################
def main():
# import data
print("obtaining MNIST data . . .")
mnistData = mnistDataLib.read_data_sets("C:\\mnist", one_hot=True)
print("building graph . . .")
# create the model
x = tf.placeholder(tf.float32, [None, 784])
# define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 10])
# build the graph for the deep net
y_conv, keep_prob = deepnn(x)
with tf.name_scope('loss'):
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv)
# end with
cross_entropy = tf.reduce_mean(cross_entropy)
with tf.name_scope('adam_optimizer'):
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
# end with
with tf.name_scope('accuracy'):
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
correct_prediction = tf.cast(correct_prediction, tf.float32)
# end with
accuracy = tf.reduce_mean(correct_prediction)
print("training . . .")
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
# ToDo: reduced training size for the moment to save time, restore to 20000 when get program working
#for i in range(20000):
for i in range(150):
batch = mnistData.train.next_batch(50)
if i % 100 == 0:
train_accuracy = accuracy.eval(feed_dict={x: batch[0], y_: batch[1], keep_prob: 1.0})
print("step " + str(i) + ", training accuracy = " + str(train_accuracy))
# end if
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
# end for
print("testing . . .")
# this is the original code by Google to test on the MNIST data:
# testAccuracy = accuracy.eval(feed_dict={x: mnistData.test.images, y_: mnistData.test.labels, keep_prob: 1.0})
# print("test accuracy = " + str(testAccuracy))
image = cv2.imread("3.jpg", cv2.IMREAD_GRAYSCALE)
image = cv2.resize(image, (IMAGE_WIDTH, IMAGE_HEIGHT))
imgFlattened = np.vectorize(lambda x: 255 - x)(np.ndarray.flatten(image))
npaResult = sess.run(tf.argmax(y_, 1), feed_dict={x: [imgFlattened]})
strResult = str(' '.join(map(str, npaResult)))
print("result = " + strResult)
# end with
# write the graph to file so we can view with TensorBoard
print("writing graph to file . . .")
fileWriter = tf.summary.FileWriter(os.getcwd())
fileWriter.add_graph(sess.graph)
fileWriter.close()
print("done !!")
# end main
The other function calls are unchanged from the Google CNN example linked above and I'd rather not post the entire code to keep this post from being even longer, please see the GitHub link above if seeing the entire program is relevant.
Currently I'm getting many errors, but I think the most relevant one is for this line:
npaResult = sess.run(tf.argmax(y_, 1), feed_dict={x: [imgFlattened]})
I'm getting this error:
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,10]
[[Node: Placeholder_1 = Placeholder[dtype=DT_FLOAT, shape=[?,10], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
It seems I need to add more parameters to feed_dict=, but I'm not sure what to add.
If I change the sess.run() line to:
npaResult = sess.run(tf.argmax(y_, 1), feed_dict={x: [imgFlattened], y_: mnistData.test.labels, keep_prob: 1.0})
At least the program doesn't crash but I get this for the output:
result = 7 2 1 0 4 1 4 9 5 9 0 6 9 0 1 5 9 7 3 4 9 6 6 5 4 0 7 4 0 1 3 1 3 4 7 2 7 1 2 1 1 7 4 2 3 5 1 (many more numbers omitted)
When the result line should say
result = 3
I think I have everything correct except for this series of lines:
image = cv2.imread("3.jpg", cv2.IMREAD_GRAYSCALE)
image = cv2.resize(image, (IMAGE_WIDTH, IMAGE_HEIGHT))
imgFlattened = np.vectorize(lambda x: 255 - x)(np.ndarray.flatten(image))
npaResult = sess.run(tf.argmax(y_, 1), feed_dict={x: [imgFlattened]})
strResult = str(' '.join(map(str, npaResult)))
print("result = " + strResult)
How can I change this to successfully test on just the one .jpg image ??