Implement convolution

Description


Unformatted Attachment Preview

Don't use plagiarized sources. Get Your Custom Assignment on
Implement convolution
From as Little as $13/Page

INFO371 Lab: Neural Networks
February 25, 2024
Introduction
This task asks you to implement the convolutional filter. We do it slightly simpler and use only a single
color layer, as convolution filters on color images do not look very nice.
Convolutions and convolutions are explained in Lecture Notes, Section 9.2.1.
1
Basic Convolution
1. Load an image and display a single color layer of it. You can do it as:
import matplotlib.pyplot as plt
from matplotlib.image import imread
X = imread(“img/snoqualmie-trestle.jpg”)[:,:,1]
_ = plt.imshow(X, cmap=”Greys_r”)
_ = plt.show()
0
100
200
300
400
500
600
700
800
0
200
400
600
Here I display the green layer of an image of snoqualmie-trestle. The image should contain a few
edges as we are going to do edge detection below.
I recommend to take a small image, as large ones may be slow.
1
2. Create a convolutional fiter that detects horizontal edges. You may use either 2 × 2 filter, or a larger
filter. It does not have quadratic, e.g. 3 × 4 is perfectly fine.
3. How big (in pixels) is your filter? Based on this–how much padding would you need?
Note: below you do not need to add any padding, but it is important to understand what it means,
and in terms of how large will be your final image.
4. Create a new empty matrix of almost the same size as X–just smaller by padding.
You can create it as
import numpy as np
Y = np.empty((99,98))
or in many other ways. But ensure the size is correct!
5. Move the convolutional filter over all the image. Each time perform the convolution, and put the
result in the new matrix (the empty one you just created). This can be easily done by a double
loop.
6. Explain, why are some horizontal edges bright while others are dark.
2
Strides and pooling (not graded)
This is a challenging task. If you still have time and interest, play with strides and pooling.
1. Now lets use strides = 2 for convolution. What will be the size of your final image?
2. Repeat the convolution step from above.
Note that it is trickier to write the results into the new matrix now.
3. Display the new image. How does it differ from the image in Question 1?
4. Take your convoluted image and implement max pooling with window size 5 and strides 5! Display
the result.
5. Explain, what features of the resulting image are picked up by the white areas.
2

Purchase answer to see full
attachment