Python ValueError:无法将大小为 50176 的数组重塑为形状 (1,224,224,3)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50306863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: cannot reshape array of size 50176 into shape (1,224,224,3)
提问by Madhi
I am doing image classification and I trained a model and saved a model. When I try to predict the model, it shows input error. I am building a classifier using ResNet Architecture and initially declared input_size as 224 x 224. Now I need to predict the class of the test image.
我正在做图像分类,我训练了一个模型并保存了一个模型。当我尝试预测模型时,它显示输入错误。我正在使用 ResNet 架构构建一个分类器,并最初将 input_size 声明为224 x 224。现在我需要预测测试图像的类别。
I converted the image into 224x224 numpy array. when I try the below code
我将图像转换为224x224 numpy array。当我尝试以下代码时
#plot the figure
fig = plt.figure()
for num,data in enumerate(test_data):
img_num = data[1]
img_data = data[0]
y = fig.add_subplot(9,3,num+1)
orig = img_data
data = img_data.reshape(1,IMG_SIZ,IMG_SIZ,3)
#predict the model
model_out = model.predict_classes([orig])[0]
if np.argmax(model_out) == 1: str_label='Dog'
else: str_label='Cat'
y.imshow(orig,cmap = 'gray')
plt.title(str_label)
y.axes.get_xaxis().set_visible(False)
y.axes.get_yaxis().set_visible(False)
plt.show()
plt.savefig('test_labeled.jpg')
It shows me the following error
它向我显示以下错误
ValueError: cannot reshape array of size 50176 into shape (1,224,224,3)
ValueError:无法将大小为 50176 的数组重塑为形状 (1,224,224,3)
in what size I have to reshape the correct dimension?
我必须以什么尺寸重塑正确的尺寸?
Thanks!
谢谢!
回答by vijay m
Seems your input is of size [224, 224, 1]
instead of [224, 224, 3]
. Looks like you converting your inputs to gray scale
in process_test_data()
似乎您的输入是 size[224, 224, 1]
而不是[224, 224, 3]
. 看起来你转换你的投入gray scale
在process_test_data()
you may need to change:
您可能需要更改:
img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img,(IMG_SIZ,IMG_SIZ))
to:
到:
img = cv2.imread(path)
img = cv2.resize(img,(IMG_SIZ,IMG_SIZ),3)
回答by Vadim
In my case function was expecting RGB
image and it has failed cause it was RGBA
one what automatically means it's got 4 channels instead of 3.
so I've refurbished their function in order to be able to swallow RGBA
在我的情况下,函数期待RGB
图像并且它失败了,因为它是RGBA
一个自动意味着它有 4 个通道而不是 3 个通道。所以我翻新了它们的功能以便能够吞下 RGBA
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
if image.getdata().mode == "RGBA":
image = image.convert('RGB')
np_array = np.array(image.getdata())
reshaped = np_array.reshape((im_height, im_width, 3))
return reshaped.astype(np.uint8)