繁体   English   中英

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

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

我有这段代码可以重塑我的图像,但是当我到达它时它会产生一个错误: Value Error: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (1, 3072) 我使用了模块tensorflowkerascv2

看起来我的输入或输出不正确,或者我必须输入 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)

输出:

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

你能帮忙找出错误吗?

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

首先删除

image = image.flatten()

然后改变

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

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM