ValueError: cannot reshape array of size 2352 into shape (1,28,28)
ValueError: cannot reshape array of size 2352 into shape (1,28,28)
我想将我的绘图用于我的神经网络。我在 MNIST 上训练它。我无法从任何方面进行准备。我以 28x28 的图片分辨率绘制了一个图形。我尝试通过神经网络 运行 它,但它不起作用。我不明白如何解决这个问题。
代码
#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()
错误
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)
问题是您以彩色模式而不是灰度模式读取图像(OpenCV 中的 BGR
),但通道的顺序在这里并不重要(ofc 2352 // 3 = 784
)。
当你读到 28x28x3
= 2352
的图像时,你想将其重塑为 28x28x1
= 784
,这当然行不通错误提示。
问题在于您阅读图片的方式:img = cv2.imread("t7.png")
。而是使用 img = cv2.imread("t7.png",0)
,参数 0
是灰度图像的默认标志。
我想将我的绘图用于我的神经网络。我在 MNIST 上训练它。我无法从任何方面进行准备。我以 28x28 的图片分辨率绘制了一个图形。我尝试通过神经网络 运行 它,但它不起作用。我不明白如何解决这个问题。
代码
#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()
错误
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)
问题是您以彩色模式而不是灰度模式读取图像(OpenCV 中的 BGR
),但通道的顺序在这里并不重要(ofc 2352 // 3 = 784
)。
当你读到 28x28x3
= 2352
的图像时,你想将其重塑为 28x28x1
= 784
,这当然行不通错误提示。
问题在于您阅读图片的方式:img = cv2.imread("t7.png")
。而是使用 img = cv2.imread("t7.png",0)
,参数 0
是灰度图像的默认标志。