I have the following problem, I want to remove a "Dustbin" from the output of one of the layers in my Keras model.
The Code without the dustbin removal looks like this and works:
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)
However, if I add the removal to the network:
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)
I get the following model.summary() output, where the dimension after the lambda layer increases to 65 again:
_________________________________________________________________
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
=================================================================
Can anybody explain why this is happening and how to solve it?
on my machine works correctly (TF 2.2). I modify the lambda to take care also of batch dimension
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)
this is the summary
_________________________________________________________________
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
=================================================================