【问题标题】:is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape [None, 256, 256, 3]与图层不兼容:输入形状的预期轴 -1 的值为 1,但接收到的输入形状为 [None, 256, 256, 3]
【发布时间】:2020-09-13 06:33:57
【问题描述】:

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

Model: "sequential_4"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_170 (Conv2D)          (None, 256, 256, 32)      320       
_________________________________________________________________
batch_normalization_169 (Bat (None, 256, 256, 32)      128       
_________________________________________________________________
activation_166 (Activation)  (None, 256, 256, 32)      0         
_________________________________________________________________
conv2d_171 (Conv2D)          (None, 256, 256, 32)      9248      
_________________________________________________________________
batch_normalization_170 (Bat (None, 256, 256, 32)      128       
_________________________________________________________________
activation_167 (Activation)  (None, 256, 256, 32)      0         
_________________________________________________________________
max_pooling2d_35 (MaxPooling (None, 128, 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, 256, 256, 3]

我的图片的属性:

print(imm.dtype)   # float32
print(imm.ndim)    # 3
print(imm.shape)   # (256, 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, y = train_y,
      5     #batch_size=32,
      6     #epochs=epochs,

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

【问题讨论】:

    标签: tensorflow keras tensorflow2.0 keras-layer


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      此模型缺少输入层。使用输入层启动模型序列。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-17
        • 2021-08-05
        • 2021-09-15
        • 2021-10-23
        • 2021-01-18
        • 2021-04-23
        • 2021-10-30
        • 2021-09-03
        相关资源
        最近更新 更多