What is Padding in Neural Networks


Introduction

Padding is one of the most used concepts in neural networks while working with convolutional neural networks. It is a most known concept to every neural network engineer and deep learning engineer to efficiently extract useful information from the given dataset in deep learning.

In this article, we will discuss padding, what it is, why we need padding in neural networks, what is the significance of the same, and how we can apply padding in neural networks with code examples. This article will help one to understand padding from scratch and apply it when necessary.

What is Padding?

Before directly jumping into the concept of padding, let us discuss the convolutional neural networks and their use cases. Convolutional neural networks are the type of intelligent neural network that works with image datasets. Here the network takes the images as input and identifies different patterns and behavior of the data. According to the recognitions from the input image dataset, the output is served.

Now the image size here is taken in pixels, where every different image dataset can have different sizes of images, and the neural networks should be optimized according to the input image size as well.

The padding is a type of extra layer that is added to the input images (in their borders). For example, let us suppose that we have an input image of size 10*10 pixels, then we can add one padding layer to the image, which will increase the size of the image to 11*11.

By adding an extra layer of pixels to the input image, we get several advantages that ultimately enhance the performance of the convolutional neural networks model.

Mostly, we add an extra layer with zeros, which is called zero padding. It will contain all zeros in the added extra layer in the outer part of the image.

Now let us discuss the significance of padding and what is the need to add padding lawyers to the images.

Why Do We Add Padding?

In neural networks, especially while working with the image dataset, padding offers several advantages.

Loss of Infiormation

While working with convolutional neural networks, we have an image on which the kernel or the filter is applied, which basically performs operations with the input images and returns an image that can be smaller in size.

For example, if we have an image size of 10*10 pixels and if a 3*3 filter is applied to the image, then the size of the output image will be 8*8 pixels, which is smaller than the actual one, and hence we are losing some of the information from the actual important image.

In this case, adding the extra layer of padding enhances the size of the input image; also, the outlet layers of the image will be the padded layers which will contain zero, and hence the information from the padding layers will be lost after applying a filter, not the core information from the actual parts of the image.

Outer Parts Information

In some cases, we are actually not interested in the middle part of the image. Instead, we are interested in the outer part of the image, which may consist of some information that is very useful for the training of the model. In such cases, the pixel reading or pixel observations from the outer part of the image become essential for the training of the model.

Now when the filter is applied to the actual image, it reduces the size of the actual image, and the information of the pixels from the outer part of the image may lose.

The padding will save us in this case, where an extra layer of pixels will ultimately make the outer part of the image the central part. Hence, the filter or kernel will focus on that, where the information from the outer parts will not be lost and will be considered important parameters for the training of the model.

Same input Size

As we discussed above that applying the kernel or the filter may reduce the size of an actual image, which may lead to the loss of some of the pieces of information. In some cases, we might need the exact size of the image, which is not achievable by applying the filter as well.

In such cases adding an extra layer of padding into the input layer will ultimately enhance the size of an actual image, which will lead to reducing the proper size of an input image after applying the filter to the same.

For example, if we have an image with a size of 10*10 pixels, and if we are applying the filter with the size 3*3 pixels, then the sizer of an output image will be 8*8 pixels. But adding one padding layer to the input layer will ultimately enhance the output image size, which will be 10*10 pixels.

Example

Now let us discuss the implementation of padding in neural networks.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D

# Create a Sequential model
model = Sequential()

# Add a convolutional layer with padding='valid' (no padding)
model.add(Conv2D(32, kernel_size=(3, 3), padding='valid', activation='relu', 
input_shape=(28, 28, 3)))

# Add another convolutional layer with padding='same'
model.add(Conv2D(64, kernel_size=(3, 3), padding='same', activation='relu'))

# Print the model summary
model.summary()

Output

Model: "sequential"
_________________________________________________________________
 Layer (type)            Output Shape           Param #   
=================================================================
 conv2d (Conv2D)          (None, 26, 26, 32)      896      
                                                 
 conv2d_1 (Conv2D)         (None, 26, 26, 64)      18496    
                                                 
=================================================================
Total params: 19,392
Trainable params: 19,392
Non-trainable params: 0
_________________________________________________________________

In the above, we can see that we have applied padding to the layers of the neural network while will add an extra layer of pixels to the image and will pass the same to the next layers.

Here padding = valid - which means that there will not be any pixels that will be added, and padding = same - which means the model will automatically try to make the input and output image of the same zie with the help of padding.

Conclusion

In this article, we discussed padding in neural networks, what they are, what the significance of the same is, and how we can add padding to the neural networks. This article will help one to understand the padding and apply the same whenever necessary.

Updated on: 17-Aug-2023

135 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements