ValueError:无法将大小为189000的数组转化为形状(28,28,1)

时间:2018-01-27 19:19:51

标签: python numpy

代码:

import deepneuralnet as net
import random 
import tflearn.datasets.mnist as mnist
import numpy as np
from skimage import io
model = net.model
path_to_model = 'final-model.tflearn'
path_to_image = 'unlucky-13.jpg' # Change this to the file path/name of the image file you want to use
model.load(path_to_model)
# Load image (normalized)
print ('hello')
print (io.imread(path_to_image).shape)
x = io.imread(path_to_image).reshape((28, 28, 1)).astype(np.float) / 255
print ('hello')
print (x)
print ('hello')
result = model.predict([x])[0] # Predict
prediction = result.index(max(result)) # The index represents the number predicted in this case
print("Prediction", prediction)

当我运行此代码错误是

x = io.imread(path_to_image).reshape((28, 28, 1)).astype(np.float) / 255
ValueError: cannot reshape array of size 189000 into shape (28,28,1)

1 个答案:

答案 0 :(得分:0)

好像你试图将图像输入到一个28x28x1图像的分类器。

但实际输入图像bar具有不同的形状。您需要调整图像大小并将其转换为黑白(1通道)图像,然后才能将其传递给分类器。

要具体回答有关错误,您会看到错误,因为您正在尝试将更大的矩阵(300,210,3)重塑为(28,28,1),这是不可能的。