TensorFlow-Examples icon indicating copy to clipboard operation
TensorFlow-Examples copied to clipboard

Trying the simple MNIST neural network on a self-written image

Open josephernest opened this issue 7 years ago • 2 comments

I'm using https://github.com/aymericdamien/TensorFlow-Examples/blob/master/notebooks/3_NeuralNetworks/neural_network_raw.ipynb, it works.

I have written a digit myself and saved it into a 28x28 PNG file, I would like to apply the neural network to it, to see how it would recognize it.

from PIL import Image
import numpy as np
img = Image.open('mydigit.png').convert("L")  # convert to grayscale
img = np.array(img, dtype=np.float32).ravel()  # convert to numpy 1D array
print(img.shape)  # (784L,)
print(img)  # [255.  255.  255.  255.  183.   32.    0.    0.    0.    0.    0.    0.  
    0.    0.    0.    0.    0.    0.    0.   32.  159.  255.  255.  255. ...]

Now the problems comes here:

print(neural_net(img))

ValueError: Shape must be rank 2 but is rank 1 for 'MatMul_3' (op: 'MatMul') with input shapes: [784], [784,256].

Question: How to apply a 1D numpy array of size (784=28x28) to the neural_net function?

josephernest avatar Nov 09 '18 00:11 josephernest

You need to change the dimensionality of the input image. Reshape it to (1,784),

mansiag avatar Jan 12 '19 17:01 mansiag

the neural_net() receive a Tensor of rank 2 instead of rank 1, you should: print(neural_net(img.reshape((1, 784)))

songt96 avatar Jan 14 '19 08:01 songt96