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:
ValueError: Inputs have incompatible shapes. Received shapes (20, 20, 16) and (22, 22, 16)
Here is an image of what I try to do:
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?
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')