简体   繁体   English

ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度

[英]ValueError: Error when checking input: expected conv2d_input to have 4 dimensions

hey write this code for reshaping my image but when i run the code it give me this error Value Error: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape (1, 3072).嘿编写此代码来重塑我的图像,但是当我运行该代码时,它给了我这个错误值错误:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到了形状为 (1, 3072) 的数组。 It's look like my input or output is incorrect or i have to input 4 dimension.看起来我的输入或输出不正确,或者我必须输入 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)

replace代替

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

with

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

暂无
暂无

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

相关问题 ValueError:检查输入时出错:预期 conv2d_input 有 4 个维度,但得到了具有形状的数组 - ValueError: Error when checking input: expected conv2d_input to have 4 dimensions, but got array with shape 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) 预期的conv2d_input具有shape(1,1)的4个维度 - expected conv2d_input to have 4 dimensions with shape(1, 1) Keras转换神经网络问题:ValueError:检查输入时出错:预期conv2d_1_input具有4维 - Keras conv-neural-network issue: ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM