I want to use my drawing for my neural network. I trained it on MNIST. I can’t prepare from any way. I drew a figure in a 28x28 picture resolution. I try to run it through the neural network, but it doesn't work. I don't understand how to fix this problem.
CODE
#norm_image = np.asarray(pixels, dtype=np.float32) / 255
img_size=28
img = cv2.resize(img, (img_size,img_size))
x=img
x=np.asarray(img)
x = img.astype('float32')
x /= 255.0
x = np.expand_dims(x, axis=0)
images = np.vstack([x])
print(images.shape)
images = np.reshape(images,(1, 28, 28))
#print(pixels)
#print(x.shape)
#res=model.predict(x)
#print (res)
#print(f"Распознанная цифра: {np.argmax(res)}")
#x = np.expand_dims(x, axis=0)
#plt.imshow(img, cmap=plt.cm.binary)
#plt.show()
Errore
ValueError Traceback (most recent call last)
<ipython-input-167-3fe66f8f6d63> in <module>()
13 images = np.vstack([x])
14 print(images.shape)
---> 15 images = np.reshape(images,(1, 28, 28))
16
17 #print(pixels)
<__array_function__ internals> in reshape(*args, **kwargs)
1 frames
/usr/local/lib/python3.7/dist-packages/numpy/core/fromnumeric.py in _wrapfunc(obj, method, *args, **kwds)
56
57 try:
---> 58 return bound(*args, **kwds)
59 except TypeError:
60 # A TypeError occurs if the object does have such a method in its
ValueError: cannot reshape array of size 2352 into shape (1,28,28)
CodePudding user response:
The problem is that you read your image in color mode instead of grayscale (BGR
in OpenCV), but the order of channel is not of essence here (ofc 2352 // 3 = 784
).
As you have an image read of 28x28x3
= 2352
, you want to reshape it into 28x28x1
= 784
, which of course does not work as it the error suggests.
The problem lies in the way you read the image: img = cv2.imread("t7.png")
. Instead use img = cv2.imread("t7.png",0)
, the parameter 0
being the default flag for grayscale image.