如何解决 conv2d 错误?

how to solve the conv2d error?

我是 python 和 tensorflow 的初学者。 我在尺寸问题上有错误。 有没有人可以解决这个问题? 我的代码如下,错误来自“aux = Convolution2D”行。 错误消息是“ValueError:负维度大小是由输入形状 [?,10,10,512], [10,512,512,1] 的 'conv2d_15/convolution' (op: 'Conv2D') 的 10 减去 512 引起的。

这是tensorflow后端。

def _conv_bn_relu(nb_filter, nb_row, nb_col, subsample=(1, 1)):
        def f(input):
            conv = Convolution2D(nb_filter=nb_filter, nb_row=nb_row, nb_col=nb_col,
                                 subsample=subsample, init="he_normal",
                                 border_mode="same")(input)
            norm = BatchNormalization()(conv)
            return ELU()(norm)
        return f

def get_unet():
    inputs = Input((img_rows, img_cols, 1), name='main_input')
    conv1 = _conv_bn_relu(32, 7, 7)(inputs)
    conv1 = _conv_bn_relu(32, 3, 3)(conv1)
    pool1 = _conv_bn_relu(32, 2, 2, subsample=(2, 2))(conv1)
    drop1 = Dropout(0.5)(pool1)

    conv2 = _conv_bn_relu(64, 3, 3)(drop1)
    conv2 = _conv_bn_relu(64, 3, 3)(conv2)
    pool2 = _conv_bn_relu(64, 2, 2, subsample=(2, 2))(conv2)
    drop2 = Dropout(0.5)(pool2)

    conv3 = _conv_bn_relu(128, 3, 3)(drop2)
    conv3 = _conv_bn_relu(128, 3, 3)(conv3)
    pool3 = _conv_bn_relu(128, 2, 2, subsample=(2, 2))(conv3)
    drop3 = Dropout(0.5)(pool3)

    conv4 = _conv_bn_relu(256, 3, 3)(drop3)
    conv4 = _conv_bn_relu(256, 3, 3)(conv4)
    pool4 = _conv_bn_relu(256, 2, 2, subsample=(2, 2))(conv4)
    drop4 = Dropout(0.5)(pool4)

    conv5 = _conv_bn_relu(512, 3, 3)(drop4)
    conv5 = _conv_bn_relu(512, 3, 3)(conv5)
    drop5 = Dropout(0.5)(conv5)
    print(drop5.shape)

    # Using conv to mimic fully connected layer.
    aux = Convolution2D(nb_filter=1, nb_row=drop5._keras_shape[2], nb_col=drop5._keras_shape[3],
                        subsample=(1, 1), init="he_normal", activation='sigmoid')(drop5)
    aux = Flatten(name='aux_output')(aux)

    # up6 = concatenate([Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(drop5), conv4], axis=3)
    up6 = merge([UpSampling2D()(drop5), conv4], mode='concat', concat_axis=1)
    conv6 = _conv_bn_relu(256, 3, 3)(up6)
    conv6 = _conv_bn_relu(256, 3, 3)(conv6)
    drop6 = Dropout(0.5)(conv6)

    # up7 = concatenate([Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(drop6), conv3], axis=3)
    up7 = merge([UpSampling2D()(drop6), conv3], mode='concat', concat_axis=1)
    conv7 = _conv_bn_relu(128, 3, 3)(up7)
    conv7 = _conv_bn_relu(128, 3, 3)(conv7)
    drop7 = Dropout(0.5)(conv7)

    # up8 = concatenate([Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(drop7), conv2], axis=3)
    up8 = merge([UpSampling2D()(drop7), conv2], mode='concat', concat_axis=1)
    conv8 = _conv_bn_relu(64, 3, 3)(up8)
    conv8 = _conv_bn_relu(64, 3, 3)(conv8)
    drop8 = Dropout(0.5)(conv8)

    # up9 = concatenate([Conv2DTranspose(32, (2, 2), strides=(2, 2), padding='same')(drop8), conv1], axis=3)
    up9 = merge([UpSampling2D()(drop8), conv1], mode='concat', concat_axis=1)
    conv9 = _conv_bn_relu(32, 3, 3)(up9)
    conv9 = _conv_bn_relu(32, 3, 3)(conv9)
    drop9 = Dropout(0.5)(conv9)

    conv10 = Convolution2D(1, 1, 1, activation='sigmoid', init="he_normal", name='main_output')(drop9)

    # model = Model(inputs=[inputs], outputs=[conv10])
    model = Model(inputs=[inputs], outputs=[conv10, aux])

    # model.compile(optimizer=Adam(lr=1e-5), loss={'main_output': dice_loss},
    #               metrics={'main_output': dice},
    #               loss_weights={'main_output': 1})
    model.compile(optimizer=Adam(lr=1e-5), loss={'main_output': dice_loss, 'aux_output': 'binary_crossentropy'},
                  metrics={'main_output': dice, 'aux_output': 'acc'},
                  loss_weights={'main_output': 1, 'aux_output': 0.5})

    return model

我认为你应该更改这一行:

aux = Convolution2D(nb_filter=1, nb_row=drop5._keras_shape[2], nb_col=drop5._keras_shape[3],
                    subsample=(1, 1), init="he_normal", activation='sigmoid')(drop5)

至:

aux = Convolution2D(nb_filter=1, nb_row=drop5._keras_shape[1], nb_col=drop5._keras_shape[2],
                    subsample=(1, 1), init="he_normal", activation='sigmoid')(drop5)

我不使用 Keras,但我认为您代码中的问题在于您放入的过滤器大小

aux = Convolution2D(nb_filter=1, nb_row=drop5._keras_shape[2], nb_col=drop5._keras_shape[3], subsample=(1, 1), init="he_normal", activation='sigmoid')(drop5)

很难推断出张量的维度,但在通读 Keras documentation of Convolution2D, as well as having analysed the dimensions of your tensors, I assume drop5 outputs a tensor of shape (samples, new_rows, new_cols, nb_filter) ([?,10,10,512] in your error message). In other words, yourdrop5 outputs an image with dimensions 10 x 10 x 512, or equivalently speaking 512 10 x 10 images (this is a great read if you want to learn more about CNNs) 之后。

当您现在设置 nb_row=drop5._keras_shape[2]nb_col=drop5._keras_shape[3] 时,您将过滤器的尺寸设置为 nb_row=10nb_col=512。这意味着您将尝试使用 10 x 512 形过滤器对 512 10 x 10 图像执行卷积。为了查看过滤器是否适合图像,我假设 TensorFlow 减去图像和过滤器维度。 [10, 10] - [10, 512] = [0, -502] 显示过滤器比图像大得多,因此无法执行卷积,因此出现您的错误消息。

解决此问题的方法是更改​​ nb_rownb_col 维度。如果您想要比 10 x 10 更大的过滤器大小,您可以调整 drop5.

的输出图像大小