Trying the simple MNIST neural network on a self-written image
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?
You need to change the dimensionality of the input image. Reshape it to (1,784),
the neural_net() receive a Tensor of rank 2 instead of rank 1, you should:
print(neural_net(img.reshape((1, 784)))