Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ValueError: Inputs have incompatible shapes. Received shapes (20, 20, 16) and (22, 22, 16)

I try to build a version of ResNet with Xception I saw in an article for study purposes.

Here is the model so far (only the first block and skipping layer):

input= Input(shape=(48,48,1))
L1 = Conv2D(filters=8, kernel_size=(3,3), strides=(1,1), activation='relu')(input)
bn = BN()(L1)

L2 = Conv2D(filters=8, kernel_size=(3,3), strides=(1,1), activation='relu')(bn)
bn = BN()(L2)


# First Depthwise, BN = BatchNormalization, SC2D = SeparableConv2D
L3 = SC2D(filters=16, kernel_size=(1,1),activation='relu')(bn)
L3 = BN()(L3)

L3 = SC2D(filters=16, kernel_size=(3,3),activation='relu')(L3)
L3 = BN()(L3)

L3 = SC2D(filters=16, kernel_size=(1,1),activation='relu')(L3)
L3 = BN()(L3)

L3 = MaxPooling2D(pool_size=(3,3), strides=(2,2))(L3)

# skipping layer
skip = Conv2D(filters=16, kernel_size=(1,1), strides=(2,2), activation='relu')(bn)
skip = BN()(skip)
print('skip2:',skip.shape)

sum1 = Add()([L3,skip])

model = Model(inputs=input, outputs=sum1, name='test')

When I run I got:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ValueError: Inputs have incompatible shapes. Received shapes (20, 20, 16) and (22, 22, 16)

Here is an image of what I try to do:

First Depthwise block scheme

As you can see, I copy 1 by 1 scheme but got the error.

So my questions are: How to match the shapes, and why this does not work?

>Solution :

You probably forgot to set padding=same. The default value is valid. Here is a working example:

import tensorflow as tf

input= tf.keras.layers.Input(shape=(48,48,1))
L1 = tf.keras.layers.Conv2D(filters=8, kernel_size=(3,3), strides=(1,1), activation='relu', padding='same')(input)
bn = tf.keras.layers.BatchNormalization()(L1)

L2 = tf.keras.layers.Conv2D(filters=8, kernel_size=(3,3), strides=(1, 1), activation='relu', padding='same')(bn)
bn = tf.keras.layers.BatchNormalization()(L2)

L3 = tf.keras.layers.SeparableConv2D(filters=16, kernel_size=(1,1),activation='relu', padding='same')(bn)
L3 = tf.keras.layers.BatchNormalization()(L3)

L3 = tf.keras.layers.SeparableConv2D(filters=16, kernel_size=(3,3),activation='relu', padding='same')(L3)
L3 = tf.keras.layers.BatchNormalization()(L3)

L3 = tf.keras.layers.SeparableConv2D(filters=16, kernel_size=(1,1),activation='relu', padding='same')(L3)
L3 = tf.keras.layers.BatchNormalization()(L3)

L3 = tf.keras.layers.MaxPooling2D(pool_size=(3,3), strides=(2,2), padding='same')(L3)

# skipping layer
skip = tf.keras.layers.Conv2D(filters=16, kernel_size=(1,1), strides=(2,2), activation='relu', padding='same')(bn)
skip = tf.keras.layers.BatchNormalization()(skip)
sum1 = tf.keras.layers.Add()([L3,skip])

model = tf.keras.Model(inputs=input, outputs=sum1, name='test')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading