[英]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
.我使用了模块
tensorflow
、 keras
和cv2
。
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.