python - 在构建和训练 3D Keras U-NET 时出现 ValueError

标签 python tensorflow keras unet-neural-network

在训练我使用 keras 为 3D Unet 构建的模型时,我得到了 ValueError: 层 conv3d_46 的输入 0 与该层不兼容:预期 ndim=5,发现 ndim=6。收到的完整形状:[无、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'])

最佳答案

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

关于python - 在构建和训练 3D Keras U-NET 时出现 ValueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58618723/

相关文章:

Tensorflow Autoencoder - 如何计算重构误差?

python - `tf.multiply` 和 `*` 和有什么区别?

tensorflow - 层密集需要 1 个输入,但它收到 2 个输入张量,我该如何更改它

python - 如何将re.findall应用于文件为空列表

python - 快照电影

python - 在 Python 中评估符号函数的参数导数

python - 测试 Django 对 Stripe Webhook 的响应

python-3.x - Tensorflow中是否可以采用张量的模式?

python - keras model.fit 函数打印的准确率与验证集还是训练集有关?

python - 使用 Keras np_utils.to_categorical 的问题