Get Started

Add padding to a CNN

Padding allows a convolutional layer to retain the resolution of the input into this layer. This is done by adding zeros around the edges of the input image, so that the convolution kernel can overlap with the pixels on the edge of the image.

This is a part of the course

“Image Modeling with Keras”

View Course

Exercise instructions

Add a Conv2D layer and choose a padding such that the output has the same size as the input.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Initialize the model
model = Sequential()

# Add the convolutional layer
model.add(____(10, kernel_size=3, activation='relu', 
                 input_shape=(img_rows, img_cols, 1), 
                 ____))

# Feed into output layer
model.add(Flatten())
model.add(Dense(3, activation='softmax'))
Edit and Run Code