与图层不兼容:输入形状的预期轴-1的值为1,但接收到形状为[None,256,256,3]的输入

问题描述

我有一个模型,看起来像这样:

Model: "sequential_4"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_170 (Conv2D)          (None,256,32)      320       
_________________________________________________________________
batch_normalization_169 (Bat (None,32)      128       
_________________________________________________________________
activation_166 (Activation)  (None,32)      0         
_________________________________________________________________
conv2d_171 (Conv2D)          (None,32)      9248      
_________________________________________________________________
batch_normalization_170 (Bat (None,32)      128       
_________________________________________________________________
activation_167 (Activation)  (None,32)      0         
_________________________________________________________________
max_pooling2d_35 (MaxPooling (None,128,32)      0         


..............

但这给了我

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

我的图像的属性

print(imm.dtype)   # float32
print(imm.ndim)    # 3
print(imm.shape)   # (256,3)

错误出现在:

history = model.fit(
    x = train_x,y = train_y,#batch_size=32,#epochs=epochs,#verbose=1,#shuffle=True,#validation_split=0.2
)

踪迹:

ValueError                                Traceback (most recent call last)
<ipython-input-36-bf5138504d79> in <module>()
      2 
      3 history = model.fit(
----> 4     x = train_x,5     #batch_size=32,6     #epochs=epochs,

当我从模型拟合中删除单个注释时,错误向下移了一行。

解决方法

图像具有 3 个通道,但是第一层具有 32 个通道。第一层应与输入图像具有相同的通道。

请尝试在模型的开头添加一个新的输入层(我的意思是在conv2d_170层之前)。

keras.Input(shape=(256,256,3))

,

此模型缺少输入层。从输入层开始模型序列。

keras.layers.InputLayer(input_shape=(256,3))