How to code a residual block using two layers of a basic CNN algorithm built with "tensorflow.keras"?

Viewed 6008

I have a basic CNN model's code built with tensorflow.keras library:

model = Sequential()

# First Layer
model.add(Conv2D(64, (3,3), input_shape = (IMG_SIZE,IMG_SIZE,1)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (3,3)))

# Second Layer
model.add(Conv2D(64, (3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (3,3)))

# Third Layer
model.add(Conv2D(64, (3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (3,3)))

# Fourth Layer
model.add(Conv2D(64, (3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (3,3)))

# Fifth Layer
model.add(Conv2D(64, (3,3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size = (3,3)))

model.add(Flatten())

# Sixth Layer
model.add(Dense(64))
model.add(Activation("relu"))

# Seventh Layer
model.add(Dense(1))
model.add(Activation('sigmoid'))

Now, I want to make a connection between the second and the fourth layer to achieve a residual block using tensorflow.keras library.

So, How should I modify the code to achieve such a residual block?

1 Answers

Residual Block from ResNet Architecture is the following :

Residual Block

You need to use the Keras functionnal API because Sequential models are too limited. Its implementation in Keras is :

from tensorflow.keras import layers

def resblock(x, kernelsize, filters):
    fx = layers.Conv2D(filters, kernelsize, activation='relu', padding='same')(x)
    fx = layers.BatchNormalization()(fx)
    fx = layers.Conv2D(filters, kernelsize, padding='same')(fx)
    out = layers.Add()([x,fx])
    out = layers.ReLU()(out)
    out = layers.BatchNormalization()(out)
    return out

BatchNormalisation()layer is not essential but may be a solid option to increase its accuracy. x needs also to have the same number of filters than the filters parameter.

Related