如何解决conv2d错误?

时间:2017-10-16 16:48:03

标签: python tensorflow convolution

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

这是张量流后端。

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

2 个答案:

答案 0 :(得分:1)

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

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)

答案 1 :(得分:0)

我没有使用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之后,以及分析了张量的尺寸后,我假设drop5输出了一个形状的张量(samples, new_rows, new_cols, nb_filter)[?,10,10,512] in your error message)。换句话说,您的drop5输出尺寸为10 x 10 x 512的图片,或等效地说出512 10 x 10张图片(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 10形状的过滤器对512 10 x 512张图片执行卷积。为了查看过滤器是否适合图像,我假设TensorFlow减去图像和过滤器尺寸。 [10, 10] - [10, 512] = [0, -502]表示过滤器比图像大得多,因此无法执行卷积,从而导致出现错误消息。

此问题的解决方案是更改nb_rownb_col维度。如果您希望过滤器大小超过10 x 10,则可以从drop5调整输出图像的大小。