预期conv2d具有4个维度,但具有形状的数组

时间:2019-04-18 16:43:44

标签: python keras medical unity3d-unet

我正在尝试使用 Keras 在某些具有 nifti 格式的医学图像上进行卷积网络。当我尝试像这样拟合模型时:

model.fit(X_train, Y_train, 
      batch_size=batch_size, 
      epochs = n_epoch, 
      validation_data=(X_test, Y_test))

我收到此错误:

  

预计conv2d_171具有4个维度,但数组具有形状   (1240,240,240)

但是当我将输入大小从img_channels = 4更改为此时:

img_channels = 3
img_rows = 240
img_cols = 240

我遇到另一个错误:

  

预计input_8的形状为(240,240,3),但数组的形状为(240,240,4)

图像大小如下:

enter image description here

我应该调整图像大小吗?或翻转图像元素的顺序?

这是模型的代码:

    inputs = Input((img_rows, img_cols, img_channels))
    s = Lambda(lambda x: x / 255) (inputs)

    c1 = Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (s)
    c1 = Dropout(0.1) (c1)
    c1 = Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c1)
    p1 = MaxPooling2D((2, 2)) (c1)

    c2 = Conv2D(32, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (p1)
    c2 = Dropout(0.1) (c2)
    c2 = Conv2D(32, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c2)
    p2 = MaxPooling2D((2, 2)) (c2)

    c3 = Conv2D(64, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (p2)
    c3 = Dropout(0.2) (c3)
    c3 = Conv2D(64, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c3)
    p3 = MaxPooling2D((2, 2)) (c3)

    c4 = Conv2D(128, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (p3)
    c4 = Dropout(0.2) (c4)
    c4 = Conv2D(128, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c4)
    p4 = MaxPooling2D(pool_size=(2, 2)) (c4)

    c5 = Conv2D(256, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (p4)
    c5 = Dropout(0.3) (c5)
    c5 = Conv2D(256, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c5)

    u6 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same') (c5)
    u6 = concatenate([u6, c4])
    c6 = Conv2D(128, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (u6)
    c6 = Dropout(0.2) (c6)
    c6 = Conv2D(128, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c6)

    u7 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same') (c6)
    u7 = concatenate([u7, c3])
    c7 = Conv2D(64, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (u7)
    c7 = Dropout(0.2) (c7)
    c7 = Conv2D(64, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c7)

    u8 = Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same') (c7)
    u8 = concatenate([u8, c2])
    c8 = Conv2D(32, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (u8)
    c8 = Dropout(0.1) (c8)
    c8 = Conv2D(32, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c8)

    u9 = Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same') (c8)
    u9 = concatenate([u9, c1], axis=3)
    c9 = Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (u9)
    c9 = Dropout(0.1) (c9)
    c9 = Conv2D(16, (3, 3), activation='elu', kernel_initializer='he_normal', padding='same') (c9)

    outputs = Conv2D(1, (1, 1), activation='sigmoid') (c9)

1 个答案:

答案 0 :(得分:1)

我看到您正在使用Unet进行细分。问题出在您的标签上,每个标签都应该是(width, height, num_classes)之类的3D矩阵。因此,对于样本长度,它应该为(sample_length, width, height, num_classes)

根据您的情况,将其转换为(310, 240, 240, num_classes)。如果标签/目标图像是二进制的,则num_classes=1。否则,您可能需要对带注释的图像进行一些预处理,然后将它们一次性编码为num_classes

另外,请查看一下您是否多次构建计算图,因为conv2d_171似乎是一个很高的数字。我看不到您的体系结构中的太多层。