Da Ip
Da Ip
1
I understand that Plagiarism is a moral offence. I state that all codes/ observations /output screenshots in this
assignment are my own, and there is no cut/copy and pasted material from undisclosed sources and manipulated
to hide the identity, and any codes/observations/output screenshots if taken from books/ websites/colleagues work
are explicitly indicated with reference.
Convolution: A local operation useful in ML
● It is the multiplication and addition of elements of 2 matrices, one of which is big and other
is small. The element by element multiplication is done each time after step-by-step sliding
(horizontally and vertically).*2
● 3 matrices in the operation – input, filter/kernel, output
● Output is normally smaller than input.
● The values of the filter matrix (filter coefficients) will produce unique effects.
● Filter size is typically 3,5,7 etc
Exercise 1: Do convolution for each of the following input matrices and filter matrices. After all
exercises are done, try to make an observation about the effect of filter map.3
2
Note that matrix multiplication and convolution are different. To differentiate them the convolution
operation is indicated by the symbol (x).
3
Draw the 2x2 matrix on a transparent plastic sheet and place it on top of the input and do the process
for clear understanding.
(a)
1 1
1 1 1 1
1 1 1 1
1 1 1
1
1 1 1
1 1 1 1 1 1
(b)
1 1
1 1 1
1 1 1
1 1 1 1 1 1
1 1 1
1 1 1 1 1 1
(c)
1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1
1 1 1 1 1 1
(d)
1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1
1 1 1 1 1 1
(e)
7 7
7 7 7 2
7 7 7 7
7 7 7 9
7 7 7
7 7 7 7 7 7
(f)
23 9 5 10 11 30
7 22 4 8 25 24 10
9 4 23 28 4 29 12
1 4 28 24 7 28 14
4 30 4 3 23 29
29 28 27 25 30 25
Questions on Convolution.
A. What is the relationship between pattern of input and pattern of filter
B. What filters can approximately represent:
Ground glass Plane glass Dark glass Glass with sun film
Input Output
Filter
1 1
1 1 1 1 0 1 3
1 1 1 1 0 1 3 2
1 1 1 1 1 3 1 1
1 1 1 1 3 2 1 3
1 1 1 1 1 1 stride-1
1 1
1
1 1 1 1 1
1
1 1 1 1 1
1 1 1 1
1 1 1
Stride-2
1 1 1 1 1 1
1 1
1 1 1
1
1 1 1
1
1 1 1
1 1 1 1
1 1 1 1 1 1 Stride=1
1 1
1 1 1
1
1 1 1
1
1 1 1
1 1 1 1
1 1 1 1 1 1
Stride-2
Padding
You may have noticed that convolution operation mostly cannot be applied to pixels in the border
of the image. If the center of the filter is placed in a border pixel, a part of the filter will fall outside.
To address this situation there are 3 approaches:
1. Do not process the border pixels, leave them as such
2. Assume that the input image has one extra row/column which has zero value pixels.
3. Assume that the input image has one extra row/column which has the same pixels as in the
real borders.
Examples:
0 0 0 0 0 0 0 0
0 1 1 0 1 0 1 0 0 2
0 1 1 1 0 0 1 0 1 3 1
1
0 1 1 1 0 1 0 1 3 2 1
0 1 1 1 0 1 0 1 3 1 1 2
0 1 1 1 0 1 0 3 2 1 3 2
0 1 1 1 1 1 1 0 2 1 2 2 2 1
0 0 0 0 0 0 0 0
Zero padding
1 1 0 0 0 0 1 1
1 1 1 1 1 0 0 0 1 3
1
0 1 1 1 1 0 1 0 1 3 2
0 1 1 1 1 1 1 0 1 3 2 2
0 1 1 1 1 1 0 1 3 1 1 3
0 1 1 1 1 1 3 2 1 3 3
1 1 1 1 1 1 1 1 3 2 2 3 3 3
1 1 1 1 1 1 1 1
Same Padding
Exercise 3: Do the following convolution with specified padding method. (Input is 6x6 only but extra
column/row is provided to enable padding)
1 1
1 1 1 1
1 1 1 1
1 1 1
1
1 1 1
1 1 1 1 1 1
1 1
1 1 1
1
1 1 1
1
1 1 1
1 1 1 1
1 1 1 1 1 1
Dilation
Normally the filter exactly overlaps the convolved region. This need not always be the case. If a 2x2
filter sits over a 3x3 region with one row and column of the input data skipped, it is called dilation.
Input FIlter Output
1 1 0 1 2 1
1 1 1 1 1 2
0 1 1
1 1 1 1 1 2 2
1 1 1 0 1
1 0 1 1 0 2
0 1 1 1
Downsampling an Image:
In machine learning applications, often the input image need not be considered as such, for reasons
of adequacy as well as reasons of size. We can downsample an image or a convolved output in 3
ways:
Replace adjacent pixels with one, based on:
1. Maximum of the n values
2. Sum of the n values
3. Average of the n values
Exercise 4: Given below the feature map which are results of convolving images with filters. Do
specified pooling of action (all 2x2):
A. Max Pooling
B. Sum Pooling
C. Average Pooling
1 2 3 4 5 1
6 1 8 7 1 1 1
3 4 1 1 2 1 1
7 5 1 1 6 1
1
8 1 2 3 1 1
Max Pooling
1 1 1 1 1 1
1 2 3 4 5 1
6 1 8 7 1 1 1
3 4 1 1 2 1 1
7 5 1 1 6 1
1
8 1 2 3 1 1
Same Pooling
1 1 1 1 1 1
1 2 3 4 5 1
6 1 8 7 1 1 1
3 4 1 1 2 1 1
7 5 1 1 6 1
1
8 1 2 3 1 1 Average Pooling
1 1 1 1 1 1