移除Keras CNN内的垃圾箱

问题描述 投票:0回答:1

我有以下问题,我想从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        
=================================================================

谁能解释一下为什么会发生这种情况以及如何解决?

python keras plaidml
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        
=================================================================
© www.soinside.com 2019 - 2024. All rights reserved.