预测经过训练的 DNN 模型 类 时出错

Error while predicting classes of an trained DNN model

我使用以下程序为我的图像预测 类。

from tensorflow.keras.preprocessing.image import load_img, img_to_array

x = load_img("8-SignLanguageMNIST/test1.jpg", target_size = (28, 28))

x = img_to_array(x)

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

x = np.vstack([x])

classes = model.predict(x)
print(classes[0])

我用来训练的图片是(28, 28, 1)的形状。

这里我上传了一张形状为 (28, 28, 3) 的 RGB 图像,我尝试将该图像转换为灰度,然后进行预测,但一直出现以下错误。

ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape [None, 28, 28, 3]

任何人都可以告诉我我做错了什么,并帮助我解决这个问题。

您需要像下面那样应用 conversion to grayscale

load_img(path, color_mode='grayscale')