简体   繁体   English

ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到了具有形状的数组

[英]ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape

I have this code that reshapes my image but it yields an error when I get to it: Value Error: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (1, 3072) .我有这段代码可以重塑我的图像,但是当我到达它时它会产生一个错误: Value Error: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (1, 3072) I used modules tensorflow , keras , and cv2 .我使用了模块tensorflowkerascv2

It looks like my input or output is incorrect or I have to input 4 dimensions.看起来我的输入或输出不正确,或者我必须输入 4 个维度。

import tensorflow as tf
import keras
#from keras.models import load_model
from keras.models import load_model
import argparse
import pickle
import cv2
import os
from sklearn.preprocessing import LabelBinarizer
lb = LabelBinarizer()
f = open("simple_multiclass_classifcation_lb.pickle", "wb")
f.write(pickle.dumps(lb))
f.close()

test_image_path = r"E:\classification\test\test\pan26.jpg"

model_path = r"PanModel.model.h5"

label_binarizer_path = "E:\API\simple_multiclass_classifcation_lb.pickle"

image = cv2.imread(test_image_path)
output = image.copy()
image = cv2.resize(image, (32,32))

 #scale the pixel values to [0, 1]
image = image.astype("float") / 255.0
image = image.flatten()
print ("image after flattening",len(image))
image = image.reshape((1, image.shape[0]))
print ("image--reshape",image.shape)

# load the model and label binarizer
print("[INFO] loading network and label binarizer...")
model = tf.keras.models.load_model('PanModel.model.h5')
#model = load_model("PanModel.model.h5")
lb = pickle.loads(open(label_binarizer_path, "rb").read())

# make a prediction on the image
print (image.shape)
preds = model.predict(image)

# find the class label index with the largest corresponding
# probability
print ("preds.argmax(axis=1)",preds.argmax(axis=1))
i = preds.argmax(axis=1)[0]
print (i)
label = lb.classes_[i]
# draw the class label + probability on the output image
text = "{}: {:.2f}%".format(label, preds[0][i] * 100)
cv2.putText(output, text, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)

# show the output image
cv2.imshow("Image", output)
cv2.waitKey(0)

Output:输出:

ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (1, 3072)

Can you help spot the error?你能帮忙找出错误吗?

您需要将输入数组重塑为(number_images, image_width, image_height, channels)

first remove首先删除

image = image.flatten()

and then change然后改变

image = image.reshape((1, image.shape[0]))

to

image = image.reshape((1, 32,32,3))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到的数组具有形状(无,1) - ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (None, 1) 检查输入时出错:预期 conv2d_input 有 4 个维度,但得到了形状为 (28708, 1) 的数组 - Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (28708, 1) Keras model.predict检查输入时出错:预期conv2d_input具有4维,但数组的形状为(128,56) - Keras model.predict Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (128, 56) ValueError:检查输入时出错:预期conv2d_1_input有4个维度,但得到的形状为数组(8020,1) - ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (8020, 1) ValueError:检查输入时出错:预期conv3d_1_input具有5个维,但数组的形状为(7,9,384,1) - ValueError: Error when checking input: expected conv3d_1_input to have 5 dimensions, but got array with shape (7, 9, 384, 1) ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度 - ValueError: Error when checking input: expected conv2d_input to have 4 dimensions 未捕获的错误:检查时出错:预期conv2d_input具有4维,但数组的形状为[275,183,3] - Uncaught Error: Error when checking : expected conv2d_input to have 4 dimension(s), but got array with shape [275,183,3] ValueError:检查输入时出错:预期 conv2d_1_input 有 4 个维度,但得到了形状为 (595、10083) 的数组 - ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (595, 10083) ValueError:检查输入时出错:预期 conv2d_1_input 有 4 个维度,但在与 model 拟合时得到形状为 (999, 12, 1) 的数组 - ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (999, 12, 1) while fitting with model ValueError:检查输入时出错:预期conv2d_1_input具有4维,但数组的形状为(454,512,512) - ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (454, 512, 512)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM