Python 3.x 在我自己训练的Keras模型中预测我的图像的问题

Python 3.x 在我自己训练的Keras模型中预测我的图像的问题,python-3.x,tensorflow,opencv,keras,computer-vision,Python 3.x,Tensorflow,Opencv,Keras,Computer Vision,我使用迁移学习来训练我的模型。现在,当我在Colab中预测我的图像时,它显示了一个错误: WARNING:tensorflow:Model was constructed with shape (None, 128, 128, 3) for input Tensor("xception_input:0", shape=(None, 128, 128, 3), dtype=float32), but it was called on an input with incompat

我使用迁移学习来训练我的模型。现在,当我在Colab中预测我的图像时,它显示了一个错误:

WARNING:tensorflow:Model was constructed with shape (None, 128, 128, 3) for input Tensor("xception_input:0", shape=(None, 128, 128, 3), dtype=float32), but it was called on an input with incompatible shape (None, 275, 3).
WARNING:tensorflow:Model was constructed with shape (None, 128, 128, 3) for input Tensor("input_1:0", shape=(None, 128, 128, 3), dtype=float32), but it was called on an input with incompatible shape (None, 275, 3).

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-7-142a5ca8cbef> in <module>()
      1 import numpy as np
----> 2 classes = np.argmax(model.predict(img), axis=-1)
      3 print(classes)
.
.
.


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

警告:tensorflow:模型是用输入张量的形状(None,128,128,3)构造的(“Exception_输入:0”,shape=(None,128,128,3),dtype=float32),但它是在具有不兼容形状(None,275,3)的输入上调用的。
警告:tensorflow:为输入张量(“input_1:0”,shape=(None,128,128,3),dtype=float32)构造了形状(None,128,128,3)的模型,但在形状不兼容的输入(None,275,3)上调用了该模型。
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
1作为np导入numpy
---->2类=np.argmax(模型预测(img),轴=-1)
3印刷品(类)
.
.
.
ValueError:layer block1\u conv1的输入0与layer::expected min\u ndim=4不兼容,found ndim=3。收到完整形状:[无,275,3]

基本上,在培训期间,您向网络输入了一批图像,测试/评估时也需要这些图像。因此,简单的解决方案是将img张量的维数扩展到
[1,img.shape]

img\u测试=tf.展开尺寸(img,轴=0)

这条消息说,您使用的形状是

shape=(None, 128, 128, 3)
但当你试图从模型中预测时,你提供了

[None, 275, 3]
显然,您的模型不能使用此选项。首先,您提供了一个3dim维度输入,但您应该提供一个4dim维度输入。通常,图像是
(高度、宽度,3)
,如果您分批提供,则会变成
(批量大小、高度、宽度,3)
,如果您只有一个图像,则会变成:

(1, height, width, 3)
因此,您应该检查为模型提供的输入。对于numpy,您通常会使用

np.expand_dims(original_image, axis=0)

从3dim到4dim输入。

如何将我的3dim输入转换为4dim?如前所述,您可以使用numpy或tensorflow方法(基本相同的语法),使用
expand\u dims