在构建和训练 3D Keras U-NET 时遇到 ValueError

Posted

技术标签:

【中文标题】在构建和训练 3D Keras U-NET 时遇到 ValueError【英文标题】:Getting a ValueError on building and training 3D Keras U-NET 【发布时间】:2020-02-25 08:38:15 【问题描述】:

在训练我使用 keras 为 3D Unet 构建的模型时,我得到 ValueError:conv3d_46 层的输入 0 与该层不兼容:预期 ndim=5,发现 ndim=6。收到的完整形状:[None, 2, 256, 256, 120, 4]。我的数据的形状大小为 (2, 256, 256, 120, 4)。

型号:

data = Input(shape=inp_shape)
flt=32


conv1 = Conv3D(flt, (3, 3, 3), activation='relu', padding='same')(data)
conv1 = Conv3D(flt, (3, 3, 3), activation='relu', padding='same')(conv1)
pool1 = MaxPooling3D(pool_size=(2, 2, 2))(conv1)

conv2 = Conv3D(flt*2, (3, 3, 3), activation='relu', padding='same')(pool1)
conv2 = Conv3D(flt*2, (3, 3, 3), activation='relu', padding='same')(conv2)
pool2 = MaxPooling3D(pool_size=(2, 2, 2))(conv2)

conv3 = Conv3D(flt*4, (3, 3, 3), activation='relu', padding='same')(pool2)
conv3 = Conv3D(flt*4, (3, 3, 3), activation='relu', padding='same')(conv3)
pool3 = MaxPooling3D(pool_size=(2, 2, 2))(conv3)

conv4 = Conv3D(flt*8, (3, 3, 3), activation='relu', padding='same')(pool3)
conv4 = Conv3D(flt*8, (3, 3, 3), activation='relu', padding='same')(conv4)
pool4 = MaxPooling3D(pool_size=(2, 2, 2))(conv4)

conv5 = Conv3D(flt*16, (3, 3, 3), activation='relu', padding='same')(pool4)
conv5 = Conv3D(flt*8, (3, 3, 3), activation='relu', padding='same')(conv5)

up6 = concatenate([Conv3DTranspose(flt*8, (2, 2, 2), strides=(2, 2, 2), padding='same')(conv5), conv4], axis=-1)
conv6 = Conv3D(flt*8, (3, 3, 3), activation='relu', padding='same')(up6)
conv6 = Conv3D(flt*4, (3, 3, 3), activation='relu', padding='same')(conv6)

up7 = concatenate([Conv3DTranspose(flt*4, (2, 2, 2), strides=(2, 2, 2), padding='same')(conv6), conv3], axis=-1)
conv7 = Conv3D(flt*4, (3, 3, 3), activation='relu', padding='same')(up7)
conv7 = Conv3D(flt*2, (3, 3, 3), activation='relu', padding='same')(conv7)

up8 = concatenate([Conv3DTranspose(flt*2, (2, 2, 2), strides=(2, 2, 2), padding='same')(conv7), conv2], axis=4)
conv8 = Conv3D(flt*2, (3, 3, 3), activation='relu', padding='same')(up8)
conv8 = Conv3D(flt, (3, 3, 3), activation='relu', padding='same')(conv8)

up9 = concatenate([Conv3DTranspose(flt, (2, 2, 2), strides=(2, 2, 2), padding='same')(conv8), conv1], axis=4)
conv9 = Conv3D(flt, (3, 3, 3), activation='relu', padding='same')(up9)
conv9 = Conv3D(flt, (3, 3, 3), activation='relu', padding='same')(conv9)


conv10 = Conv3D(2, (1,1,1), activation='sigmoid')(conv9)

model = Model(inputs=[data], outputs=[conv10])

训练模型的代码如下:-

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['binary_accuracy'])

【问题讨论】:

我们需要binary_crossentropy 的损失函数用于 UNet 和 sigmoid 激活。 categorical_crossentropy 用于多类分类。 我改变了,但仍然遇到同样的错误。我认为模型有问题。我想不通。@ShubhamPanchal 任务是执行分割 目标标签的最后一维为2。模型输出的最后一维为1。可能目标标签已经过一次热编码? @ShubhamPanchal 哦,让我编辑和测试 @ShubhamPanchal 感谢更改模型运行的二维后。 【参考方案1】:

目标标签的最后一维为 2。模型的输出的最后一维为 1。感谢@Shubham Panchal

【讨论】:

以上是关于在构建和训练 3D Keras U-NET 时遇到 ValueError的主要内容,如果未能解决你的问题,请参考以下文章

Keras深度学习实战(17)——使用U-Net架构进行图像分割

Keras深度学习实战(18)——语义分割详解

keras 3D UnetCNN训练时出现的segmentation fault的问题

如何在 keras 中正确使用 U-net 批量标准化?

在 Keras 上训练 c3d。训练减慢了中期和ETA攀升

使用 TensorRT 部署语义分割网络(U-Net)(不支持上采样)