ValueError:检查输入时出错:预期conv2d_input具有4维 - python

嘿,编写此代码来重塑我的图像,但是当我运行代码时,它给了我这个错误值错误:检查输入时出错:预期conv2d_input具有4个维,但是数组的形状为(1,3072)。看来我的输入或输出不正确,或者我必须输入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)

参考方案

更换

image = image.flatten()
print ("image after flattening",len(image))
image = image.reshape((1, image.shape[0]))
print ("image--reshape",image.shape)

image = np.expand_dims(image, axis=0)

Python uuid4,如何限制唯一字符的长度 - python

在Python中,我正在使用uuid4()方法创建唯一的字符集。但是我找不到将其限制为10或8个字符的方法。有什么办法吗?uuid4()ffc69c1b-9d87-4c19-8dac-c09ca857e3fc谢谢。 参考方案 尝试:x = uuid4() str(x)[:8] 输出:"ffc69c1b" Is there a way to…

Python-crontab模块 - python

我正在尝试在Linux OS(CentOS 7)上使用Python-crontab模块我的配置文件如下:{ "ossConfigurationData": { "work1": [ { "cronInterval": "0 0 0 1 1 ?", "attribute&…

Python:检查是否存在维基百科文章 - python

我试图弄清楚如何检查Wikipedia文章是否存在。例如,https://en.wikipedia.org/wiki/Food 存在,但是https://en.wikipedia.org/wiki/Fod 不会,页面只是说:“维基百科没有此名称的文章。”谢谢! 参考方案 >>> import urllib >>> prin…

具有Java异常的Python等效原因 - python

Python中是否有一种引发错误的原因,该错误以另一个错误为原因?在Java中,您可以创建一个原因异常的实例,例如以下代码try { throw new IOException(); } catch (IOException e) { throw new RuntimeException("An exception occurred while t…

Python Pandas导出数据 - python

我正在使用python pandas处理一些数据。我已使用以下代码将数据导出到excel文件。writer = pd.ExcelWriter('Data.xlsx'); wrong_data.to_excel(writer,"Names which are wrong", index = False); writer.…