2
votes

I'm following this tutorial:

https://www.tensorflow.org/versions/r0.9/tutorials/mnist/beginners/index.html#mnist-for-ml-beginners

What I want to be able to do is pass in a test image x - as a numpy array, and see the resulting softmax classification values - perhaps as another numpy array. Everything I can find online about testing tensor flow models works by passing in test values and test labels and the outputting the accuracy. In my case, I want to output the model labels just based on the test values.

This is what Im trying: import tensorflow as tf import numpy as np from skimage import color,io

from tensorflow.examples.tutorials.mnist import input_data
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(100)
  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

#so now its trained successfully, and W and b should be the stored "model" 

#now to load in a test image

greyscale_test = color.rgb2gray(io.imread('4.jpeg'))
greyscale_expanded = np.expand_dims(greyscale_test,axis=0)    #now shape (1,28,28)
x = np.reshape(greyscale_expanded,(1,784))     #now same dimensions as mnist.train.images

#initialize the variable
init_op = tf.initialize_all_variables()

#run the graph
with tf.Session() as sess:
    sess.run(init_op) #execute init_op
    print (sess.run(feed_dict={x:x}))    #this is pretty much just a shot in the dark. What would go here?

Right now it results in this:

TypeError                                 Traceback (most recent call last)
<ipython-input-116-f232a17507fb> in <module>()
     36     sess.run(init_op) #execute init_op
---> 37     print (sess.run(feed_dict={x:x}))    #this is pretty much just a shot in the dark. What would go here?

TypeError: unhashable type: 'numpy.ndarray'

So when training, the sess.run is passed a train_step and a feed_dict. When I am trying to evaluate a tensor x, would this go in the feed dict? Would I even use sess.run?(seems I have to), but what would the train_step be? Is there a "test_step" or "evaluate_step"?

2

2 Answers

3
votes

You're getting the TypeError because you are using a (mutable) numpy.ndarray as a key for your dictionary but the key should be a tf.placeholder and the value a numpy array.

The following adjustment fixes this problem:

x_placeholder = tf.placeholder(tf.float32, [None, 784])
# ...
x = np.reshape(greyscale_expanded,(1,784))
# ...
print(sess.run([inference_step], feed_dict={x_placeholder:x})) 

If you just want to perform inference on your model, this will print a numpy array with the predictions.

If you want to evaluate your model (for example compute the accuracy) you also need to feed in the corresponding ground truth labels y as in:

accuracy = sess.run([accuracy_op], feed_dict={x_placeholder:x, y_placeholder:y}

In your case, the accuracy_op could be defined as follows:

correct_predictions = tf.equal(tf.argmax(predictions, 1), tf.cast(labels, tf.int64))
accuracy_op = tf.reduce_mean(tf.cast(correct_predictions, tf.float32))

Here, predictions is the output tensor of your model.

0
votes

your tf.Session.run op needs a fetches tf.Session.run(fetches, feed_dict=None, options=None, run_metadata=None)

https://www.tensorflow.org/versions/r0.9/api_docs/python/client.html#session-management

print (sess.run(train_step,feed_dict={x:x})) but it also needs a feed_dict for y_

what do you mean with:

print the random values that we sample