0

我有以下问题,我想从我的 Keras 模型中的一个层的输出中删除一个“垃圾箱”。

没有删除垃圾箱的代码如下所示并且有效:

def create_detector_network():
    input = Input(shape=(128, 128, 512))
    x = Conv2D(128, kernel_size=3, strides=1, name='detect_1', padding='same')(input)
    x = BatchNormalization()(x)
    x = Conv2D(65, kernel_size=1, strides=1, name='detect_2')(x)
    x = BatchNormalization()(x)
    x = Activation('softmax')(x)
    x = keras.layers.UpSampling2D(size=(8, 8), data_format=None, interpolation='nearest')(x)
    x = Conv2D(1, kernel_size=1, strides=1, name='reduce_dim')(x)
    return Model(input, x)

但是,如果我将删除添加到网络:

def create_detector_network():
    input = Input(shape=(128, 128, 512))
    x = Conv2D(128, kernel_size=3, strides=1, name='detect_1', padding='same')(input)
    x = BatchNormalization()(x)
    x = Conv2D(65, kernel_size=1, strides=1, name='detect_2')(x)
    x = BatchNormalization()(x)
    x = Activation('softmax')(x)
    x = Lambda(lambda x: x[:, :, :-1], output_shape= (128, 128, 64))(x)  #x[:, :, :-1] <------
    x = keras.layers.UpSampling2D(size=(8, 8), data_format=None, interpolation='nearest')(x)
    x = Conv2D(1, kernel_size=1, strides=1, name='reduce_dim')(x)
    return Model(input, x)

我得到以下 model.summary() 输出,其中 lambda 层之后的维度再次增加到 65:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_38 (InputLayer)        (None, 128, 128, 512)     0         
_________________________________________________________________
detect_1 (Conv2D)            (None, 128, 128, 128)     589952    
_________________________________________________________________
batch_normalization_37 (Batc (None, 128, 128, 128)     512       
_________________________________________________________________
detect_2 (Conv2D)            (None, 128, 128, 65)      8385      
_________________________________________________________________
batch_normalization_38 (Batc (None, 128, 128, 65)      260       
_________________________________________________________________
activation_10 (Activation)   (None, 128, 128, 65)      0         
_________________________________________________________________
lambda_6 (Lambda)            (None, 128, 128, 64)      0         
_________________________________________________________________
up_sampling2d_18 (UpSampling (None, 1024, 1016, 65)    0         
_________________________________________________________________
reduce_dim (Conv2D)          (None, 1024, 1016, 1)     66        
=================================================================

任何人都可以解释为什么会发生这种情况以及如何解决它?

4

1 回答 1

1

在我的机器上正常工作(TF 2.2)。我修改了 lambda 以处理批量维度

def create_detector_network():
    inp = Input(shape=(128, 128, 512))
     x = Conv2D(128, kernel_size=3, strides=1, name='detect_1', padding='same')(inp)
     x = BatchNormalization()(x)
     x = Conv2D(65, kernel_size=1, strides=1, name='detect_2')(x)
     x = BatchNormalization()(x)
     x = Activation('softmax')(x)
     x = Lambda(lambda x: x[:,:,:,:-1])(x) 
     x = UpSampling2D(size=(8, 8), data_format=None, interpolation='nearest')(x)
     x = Conv2D(1, kernel_size=1, strides=1, name='reduce_dim')(x)

     return Model(inp, x)

这是总结

_________________________________________________________________
Layer (type)                 Output Shape              Param   
=================================================================
input_33 (InputLayer)        [(None, 128, 128, 512)]   0         
_________________________________________________________________
detect_1 (Conv2D)            (None, 128, 128, 128)     589952    
_________________________________________________________________
batch_normalization_14 (Batc (None, 128, 128, 128)     512       
_________________________________________________________________
detect_2 (Conv2D)            (None, 128, 128, 65)      8385      
_________________________________________________________________
batch_normalization_15 (Batc (None, 128, 128, 65)      260       
_________________________________________________________________
activation_7 (Activation)    (None, 128, 128, 65)      0         
_________________________________________________________________
lambda_7 (Lambda)            (None, 128, 128, 64)      0         
_________________________________________________________________
up_sampling2d_7 (UpSampling2 (None, 1024, 1024, 64)    0         
_________________________________________________________________
reduce_dim (Conv2D)          (None, 1024, 1024, 1)     65        
=================================================================
于 2020-05-01T15:51:56.817 回答