Problems with Un-Pooling

Good morning everyone,

I’m currently working on implementing de QuickNAT architecture in Python. This model has a decoder path with un-pooling operations. I’m trying to replicate this using UpSampling2D from TensorFlow Keras, but I obtain this message: “A Concatenate layer requires inputs with matching shapes except for the concatenation axis. Received: input_shape=[(None, 54, 34, 64), (None, 55, 35, 64)]” while trying to make the deep conections (concatenate).

My code for the dense blocks is:
conv1 = BatchNormalization()(inputs)
conv1 = Activation(‘relu’)(conv1)
conv1 = Conv2D(64, kernel_size=(5, 5), padding=‘same’)(conv1)

conv2 = BatchNormalization()(conv1)
conv2 = Activation('relu')(conv2)
conv2 = Conv2D(64, kernel_size=(5, 5), padding='same')(conv2)

conv3 = BatchNormalization()(conv2)
conv3 = Activation('relu')(conv3)
conv3 = Conv2D(64, kernel_size=(1, 1), padding='same')(conv3)

pool1 = MaxPooling2D(pool_size=(2, 2))(conv3)

and, for the decoder part, the code is four times this:
#Decoder
up1 = UpSampling2D(size=(2, 2))(conv13)
merge1 = concatenate([up1, conv12], axis=3)

#DenseBlock 1
conv14 = BatchNormalization()(merge1)
conv14 = Activation('relu')(conv14)
conv14 = Conv2D(64, kernel_size=(5, 5), padding='same')(conv14)

conv15 = BatchNormalization()(conv14)
conv15 = Activation('relu')(conv15)
conv15 = Conv2D(64, kernel_size=(5, 5), padding='same')(conv15)

conv16 = BatchNormalization()(conv15)
conv16 = Activation('relu')(conv16)
conv16 = Conv2D(64, kernel_size=(1, 1), padding='same')(conv16)

pool5 = MaxPooling2D(pool_size=(2, 2))(conv16)

The error comes from concatenate, of course. However, I don’t know how to proceed. I apologize for any grammatical mistakes and thank you in advance.

Hi @Sara_Ortega_Espina, The tf.keras.layers.Concatenate takes input a list of tensors, all of the same shape except for the concatenation axis, in you case as your are using axis=3 the input tensors should have shape (None, 54, 34, x) and (None, 54, 34, y) or (None, 55 ,35, x) and (None,55,35, y).

x = np.arange(117504).reshape(1, 54, 34, 64)
y = np.arange(120960).reshape(1, 54, 35, 64)
tf.keras.layers.Concatenate(axis=3)([x, y])

#error: ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concatenation axis. Received: input_shape=[(1, 54, 34, 64), (1, 54, 35, 64)]

The above code gives error because the axis 2 shape of first tensor did not match with the axis 2 shape of the second tensor.

x = np.arange(117504).reshape(1, 54, 34, 64)
y = np.arange(120960).reshape(1, 54, 35, 64)
tf.keras.layers.Concatenate(axis=2)([x, y])

The above code works fine because while concatenating we have used axis 2 so the axis 2 of tensors can have a different shape but the remaining axis shape should match. Thank You.