ValueError:エラー入力をチェック:4次元を持つことが期待conv2d_input

sk_1995:

ちょっと私のイメージを再形成するために、このコードを書いたが、私は、コードを実行すると、それは私に、このエラー値エラーを与える:形状(1、3072)との配列を4次元を持つことが期待conv2d_input、しかしだ:入力をチェックする際にエラーが。私の入力または出力のようなそれの外観が間違っているか、私は入力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)

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=347348&siteId=1