可变输入大小图像上的批量标准化

时间:2018-09-18 22:41:43

标签: python tensorflow machine-learning keras keras-layer

我正在尝试使用可变大小的输入形状(图像)来开发模型。请查看下面的代码以获取详细信息,

def create_bbox_network():
    image_input = Input(shape=(3, None, None))
    conv1  = Conv2D(16, (3, 3), input_shape=(3, None, None), activation='relu', strides=(1, 1), padding='same')(image_input)
    batch_norm1 = BatchNormalization()(conv1)

    conv2 = Conv2D(16, (3, 3), activation='relu', padding='same')(batch_norm1)
    batch_norm2 = BatchNormalization()(conv2)

    conv3 = Conv2D(16, (3, 3), activation='relu', padding='same')(batch_norm2)
    batch_norm3 = BatchNormalization()(conv3)

    conv4 = Conv2D(16, (3, 3), activation='relu', padding='same')(batch_norm3)
    max_pooling_c4 = MaxPooling2D(pool_size=(2, 2))(conv4)
    batch_norm4 = BatchNormalization()(max_pooling_c4)

    conv5 = Conv2D(16, (3, 3), activation='relu', padding='same')(batch_norm4)
    max_pooling_c5 = MaxPooling2D(pool_size=(2, 2))(conv5)
    batch_norm5 = BatchNormalization()(max_pooling_c5)

    dense_layer = GlobalMaxPooling2D()(batch_norm5)
    fc_1 = Dense(64, activation='relu')(dense_layer)
    fc_2 = Dense(64, activation='relu')(fc_1)
    out = Dense(4, activation='sigmoid')(fc_2)

    model = Model(inputs=image_input, outputs=out)
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

运行上面的代码给我一个错误,

ValueError: Axis -1 of input tensor should have a defined dimension 
but the layer received an input with shape (None, 16, None, None)
Note: For detail where code is failing see line 4. batch_norm1 layer.

我在这里想念什么?我了解使用可变输入形状时无法使用Flatten()层。但BatchNormalization()函数的情况与此相同。如果是这样,为什么我们不能在输入大小可变的情况下使用BatchNormalization?可以以有意义的方式在此使用的此功能的替代方法是什么?

根据我的理解,批量归一化是通过计算输出层所有张量的均值和sd,然后仅对输出进行归一化来计算的。我了解使用flatten会遇到大小可变权重的问题,因此无法使用。但是我看不出为什么不能在该层使用BatchNormalization的任何原因。

0 个答案:

没有答案