清除Keras CNN内的垃圾箱



我有以下问题,我想从Keras模型中的一个层的输出中删除一个"Dustbin"。

没有垃圾箱清除的代码看起来是这样的,并且有效:

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        
=================================================================

有人能解释为什么会发生这种情况以及如何解决吗?

在我的机器上工作正常(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        
=================================================================

最新更新