Learn Python

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Introduction

Setup

ANN

Working process ANN

Propagation

Bias parameter

Activation function

Loss function

Overfitting and Underfitting

Optimization function

Chain rule

Minima

Gradient problem

Weight initialization

Dropout

ANN Regression Exercise

ANN Classification Exercise

Hyper parameter tuning

CNN

CNN basics

Convolution

Padding

Pooling

Data argumentation

Flattening

Create Custom Dataset

Binary Classification Exercise

Multiclass Classification Exercise

Transfer learning

Transfer model Basic template

RNN

How RNN works

LSTM

Bidirectional RNN

Sequence to sequence

Attention model

Transformer model

Bag of words

Tokenization & Stop words

Stemming & Lemmatization

TF-IDF

N-Gram

Word embedding

Normalization

Pos tagging

Parser

semantic analysis

Regular expression

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Everything about cnn padding?

We know that when we apply convolution we lose some information. This means if we apply convolution on a 6x6 matrix with a 3x3 filter then we get a 4x4 matrix image as a result. It means that we are losing some information.

The formula of matrix size after convolution:
size=n-f+1
Here,
n=Main image matrix size
f=filter matrix size

So if our input matrix size is 6x6 and if we want our output image matrix also 6x6. This means the real size of the main image then we use padding.

Let's think, if our main image matrix is 6x6 then we are getting 4x4 matrix as result.
new image = n-f+1=6-3+1=4
Here we use 3x3 filter.
Now let's find n value for 6x6 matrix if we want to get as output image matrix.
n-f+1=6
=>n=6+f-1
=>n=6+3-1
=>8

Here 6 is our output image matrix size. So what we did, we find n means our main image matrix size for getting 6x6 output and we get the result is 8. This means, if we can increase our main image matrix size to 8x8 and then if we apply convolution then we will get 6x6 means the main image size matrix image and no information will be loss. Here filter size is 3x3.

To increase our image matrix size we use padding. It will not change the image. We will just add a layer at 4 sides of our main image and this is called padding. In padding, we add a new layer of four sides of our main image.

We can use any number of layers. Here we will use p=1 means one layer padding. One layer padding means it will add one layer on each side of our main image. If we use p=4 then it will add 4 layers on each side of our main image. After applying padding(p=1) our image size will increase. It means now our image size is 8x8. Now if we apply convolution then we will get a 6x6 matrix. It means no information loss.

Why here I'm using p=1?
Look we have to make 6x6 matrix to 8x8 matrix. While doing padding you have to put the original image in the center and then have to do padding around that original image matrix. Now if we add 1 layer around the main image 6x6 matrix then from each side of the matrix we will get 8 cells or 8 row and columns. It means we will get 8x8 matrix.

Now there is a question, how we can fill our padding layer cells with value?
There are two ways:
1.Zero padding:
Its means that we will fill all the cells with zero.

Our main image layer


0 0 0 1 1 1
0 0 0 0 1 1
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 1 1 1

After padding:


0 0 0 0 0 0 0 0
0 0 0 0 0 1 1 0
0 0 0 0 0 1 1 0
0 0 0 0 1 1 1 0
0 0 0 0 1 1 1 0
0 0 0 0 1 1 1 0
0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0

2.Nearest value:
Here we try to find the nearest value of that cell and then fill it with that same value.

Our main image layer:


0 0 0 1 1 1
0 0 0 0 1 1
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 1 1 1
0 0 0 1 1 1

After padding:


0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1
0 0 0 0 1 1 1 1

Among these two techniques, zero padding is mostly used.

Now after using padding the formula of finding the output image size is:
size=n+2p-f+1
Here,
n=Main image matrix size
f=filter matrix size
p= how many layers we add at one side.

So the facility of doing padding is that we are not changing our main image but can get more and more near to all information from our image.

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.