-3

I am trying to create a simple python script that will allow you to put in picture of a handwritten digit and the NN model will try to make a guess as to what digit it is so far I have successfully made the model as well as tested it but when it comes to testing a single image i get an output like this.

https://i.stack.imgur.com/PQhaU.png

def make_pred():
    Tk().withdraw()
    filename = askopenfilename()
    #the array that will hold the values of image
    image = np.zeros(784)
    #read image
    gray = cv2.imread(filename, cv2.IMREAD_GRAYSCALE )
    #resize image and set background to black
    gray = cv2.resize(255-gray, (28,28))
    #making the image a one dimensional array of 784
    flatten = gray.flatten() / 255.0
    cv2.imshow("heh",gray)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    #saving the image and the correct value
    image = flatten
    prediction = neural_network_model(x)
    n_save = tf.train.Saver()
    with tf.Session() as sess2:        
        n_save.restore(sess2, './nn_test/nnmodel')
        print(sess2.run(prediction,feed_dict={x: [image], y:[7]}))

The y value is 7 because that's the digit i am trying this with.

So how do I get the value that the NN thinks the character is?

nader2929
  • 131
  • 7

1 Answers1

0

It's a bit difficult to tell from the information you give. However, I think it is highly likely that the output you are getting are just the logits, before the softmax output layer.

Feed this output for a softmax layer and then you get a probability distribution over the outputs. In your particular case the softmax output:

[0., 0., 0., 0., 0., 1., 0., 0., 0., 0.]

Now you only need to take the argmax of this tensor. In your example the prediction of the network is 5.

One possible reason why you mistakenly took the logits as the output of the network is that most framework combines softmax output with the loss function. So while the loss function indeed takes logits as input, the real output of your network is only given after a softmax output layer applied on the logits.

mkisantal
  • 644
  • 6
  • 13