在构建和训练 3D Keras U-NET 时出现 ValueError
Getting a ValueError on building and training 3D Keras U-NET
在训练我使用 keras 为 3D Unet 构建的模型时,我得到 ValueError: Input 0 of layer conv3d_46 is incompatible with the layer: expected ndim=5, found 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'])
目标标签的最后一个维度为 2。模型的输出最后一个维度为 1。谢谢@Shubham Panchal
在训练我使用 keras 为 3D Unet 构建的模型时,我得到 ValueError: Input 0 of layer conv3d_46 is incompatible with the layer: expected ndim=5, found 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'])
目标标签的最后一个维度为 2。模型的输出最后一个维度为 1。谢谢@Shubham Panchal