Types of padding in convolution layer

15 Jan, 2019
Let’s discuss padding and its types in convolution layers. In convolution layer we have kernels and to make the final filter more informative we use padding in image matrix or any kind of input array. We have three types of padding that are as follows.
Padding Full :Let’s assume a kernel as a sliding window. We have to come with the solution of padding zeros on the input array. This is a very famous implementation and will be easier to show how it works with a simple example, consider x as a filter and h as an input array.x[i] = [6, 2]h[i] = [1, 2, 5, 4]Using the zero padding, we can calculate the convolution.You have to invert the filter x, otherwise the operation would be cross-correlation. First step, (now with zero padding):= 2 * 0 + 6 * 1 = 6Second step:= 2 * 1 + 6 * 2 = 14Third step:= 2 * 2 + 6 * 5 = 34Fourth step:= 2 * 5 + 6 * 4 = 34Fifth step:= 2 * 4 + 6 * 0 = 8The result of the convolution for this case, listing all the steps above, would be: Y = [6 14 34 34 8]# importing numpyimport numpy as np  x = [6, 2]h = [1, 2, 5, 4]  y = np.convolve(x, h, "full")print(y)  Output:[ 6 14 34 34  8]
Padding same :In this type of padding, we only append zero to the left of the array and to the top of the 2D input matrix.# importing numpyimport numpy as np  x = [6, 2]h = [1, 2, 5, 4]  y = np.convolve(x, h, "same")print(y)Output:[ 6 14 34 34]
Padding valid :In this type of padding, we got the reduced output matrix as the size of the output array is reduced. We only applied the kernel when we had a compatible position on the h array, in some cases you want a dimensionality reduction.# importing numpyimport numpy as np  x = [6, 2]h = [1, 2, 5, 4]  y = np.convolve(x, h, "valid")print(y)Output:[14 34 34]

Padding Full :Let’s assume a kernel as a sliding window. We have to come with the solution of padding zeros on the input array. This is a very famous implementation and will be easier to show how it works with a simple example, consider x as a filter and h as an input array.x[i] = [6, 2]h[i] = [1, 2, 5, 4]Using the zero padding, we can calculate the convolution.You have to invert the filter x, otherwise the operation would be cross-correlation. First step, (now with zero padding):= 2 * 0 + 6 * 1 = 6Second step:= 2 * 1 + 6 * 2 = 14Third step:= 2 * 2 + 6 * 5 = 34Fourth step:= 2 * 5 + 6 * 4 = 34Fifth step:= 2 * 4 + 6 * 0 = 8The result of the convolution for this case, listing all the steps above, would be: Y = [6 14 34 34 8]# importing numpyimport numpy as np  x = [6, 2]h = [1, 2, 5, 4]  y = np.convolve(x, h, "full")print(y)  Output:[ 6 14 34 34  8]

Let’s assume a kernel as a sliding window. We have to come with the solution of padding zeros on the input array. This is a very famous implementation and will be easier to show how it works with a simple example, consider x as a filter and h as an input array.
x[i] = [6, 2]h[i] = [1, 2, 5, 4]
Using the zero padding, we can calculate the convolution.
You have to invert the filter x, otherwise the operation would be cross-correlation. First step, (now with zero padding):
= 2 * 0 + 6 * 1 = 6
Second step:
= 2 * 1 + 6 * 2 = 14
Third step:
= 2 * 2 + 6 * 5 = 34
Fourth step:
= 2 * 5 + 6 * 4 = 34
Fifth step:
= 2 * 4 + 6 * 0 = 8
The result of the convolution for this case, listing all the steps above, would be: Y = [6 14 34 34 8]
# importing numpyimport numpy as np  x = [6, 2]h = [1, 2, 5, 4]  y = np.convolve(x, h, "full")print(y)  
[ 6 14 34 34  8]

Padding same :In this type of padding, we only append zero to the left of the array and to the top of the 2D input matrix.# importing numpyimport numpy as np  x = [6, 2]h = [1, 2, 5, 4]  y = np.convolve(x, h, "same")print(y)Output:[ 6 14 34 34]

In this type of padding, we only append zero to the left of the array and to the top of the 2D input matrix.
# importing numpyimport numpy as np  x = [6, 2]h = [1, 2, 5, 4]  y = np.convolve(x, h, "same")print(y)
[ 6 14 34 34]

Padding valid :In this type of padding, we got the reduced output matrix as the size of the output array is reduced. We only applied the kernel when we had a compatible position on the h array, in some cases you want a dimensionality reduction.# importing numpyimport numpy as np  x = [6, 2]h = [1, 2, 5, 4]  y = np.convolve(x, h, "valid")print(y)Output:[14 34 34]

In this type of padding, we got the reduced output matrix as the size of the output array is reduced. We only applied the kernel when we had a compatible position on the h array, in some cases you want a dimensionality reduction.
# importing numpyimport numpy as np  x = [6, 2]h = [1, 2, 5, 4]  y = np.convolve(x, h, "valid")print(y)
[14 34 34]

Python-numpy
Technical Scripter 2018
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Iterate over a list in Python
Python OOPs Concepts