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

This exercise is part of the course

Image Modeling with Keras

AdvancedSkill Level
4.4+
18 reviews

Learn to conduct image analysis using Keras with Python by constructing, training, and evaluating convolutional neural networks.

Convolutions are the fundamental building blocks of convolutional neural networks. In this chapter, you will be introducted to convolutions and learn how they operate on image data. You will also see how you incorporate convolutions into Keras neural networks.

Exercise 1: ConvolutionsExercise 2: One dimensional convolutionsExercise 3: Image convolutionsExercise 4: Defining image convolution kernelsExercise 5: Implementing image convolutions in KerasExercise 6: Convolutional network for image classificationExercise 7: Training a CNN to classify clothing typesExercise 8: Evaluating a CNN with test dataExercise 9: Tweaking your convolutionsExercise 10: Add padding to a CNN
Exercise 11: Add strides to a convolutional networkExercise 12: Calculate the size of convolutional layer output

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free