层顺序的输入0与层不兼容

时间:2020-07-24 08:13:20

标签: python numpy tensorflow machine-learning keras

我创建了一个模型,然后将其加载到另一个脚本中,并尝试从中进行预测,但是我不明白为什么传递给函数的形状不正确。

这是创建模型的方式:

batch_size = 1232
epochs = 5
IMG_HEIGHT = 400
IMG_WIDTH = 400

model = tf.keras.models.load_model('test')

test_1 = cv2.imread('./Data/Images/test_no.jpg')
test_1 = cv2.resize(test_1, (IMG_HEIGHT, IMG_WIDTH))


prediction = model.predict([test_1])[0]
print(prediction)

这就是我要做出的预测:

ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 400, 3]

打印测试图像的形状时,输出为:(400,400,3)

在通过测试图像进​​行预测时,我还尝试使用numpy操作重塑形状。但是错误始终是:

NSPhotoLibraryUsageDescription

1 个答案:

答案 0 :(得分:0)

为您的输入添加额外的维度 [n_items,400,400,3]

import tensorflow as tf
X_train = tf.expand_dims(X_train, axis =-1)