Jupyter Notebook#
CMSE381 - Lec 31 - CNNs#
# Everyone's favorite standard imports
import numpy as np
import pandas as pd
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import time
Playing with CNNs#
Our next job is to get some more intuition for the Convolutional Neural Net architecture.
Load in the toy “image” on the DataSets page to try out some tools.
M = np.loadtxt('../../DataSets/DL-ToyImage.csv')
plt.matshow(M)
plt.colorbar()
plt.show()
Convolution Layer#
First, we’re going to try out convlution on this image. Here’s a convolution filter matrix for us to test this onn.
F = np.zeros((5,5))
F[:,:2] = 1
F[:, 3:] = 1
plt.spy(F)
print(F)
plt.show()
✅ Do this: Extract a portion of the matrix \(M\) from the top left of the same size as the filter matrix \(F\) and take the dot product of the two. This is the entry that would go in entry \([0, 0]\) of the convolved image.
# Your code here
✅ Do this: Do the same thing, but shifted right by one. This is the entry that would go in entry \([0, 1]\) of the convolved image.
# Your code here
✅ Do this: Now, do this for every entry in the matrix. Update this NewM
matrix so that each entry is the convolution of the input image.
NewM = np.zeros((30-F.shape[0],30-F.shape[0]))
for i in range(NewM.shape[0]):
for j in range(NewM.shape[1]):
NewM[i,j] = np.NaN #<----------- you need to fix this
plt.matshow(NewM)
plt.colorbar()
plt.show()
✅ Do this: Here’s a different filter. What happens when you convolve the image with this one instead?
F = np.zeros((5,5))
F[:2,:] = 1
print(F)
plt.spy(F)
plt.show()
# Your code here
Pooling Layer#
After our convolution layer, we often build a pooling layer.
✅ Do this: Fix up the following code to create a pooled image from your convolved image.
poolM = np.zeros( (NewM.shape[0]//2, NewM.shape[1]//2) )
for i in range(NewM.shape[0]//2):
for j in range(NewM.shape[0]//2):
poolM[i,j] = np.NaN #<------------Fix this
plt.matshow(poolM)
plt.colorbar()
plt.show()
Playing with a pre-trained classifier#
As with the regular neural nets, trying to train our own CNN is beyond the scope of the class. But we can play with some toys to see how the trained versions work.
Open the following in another browser tab to answer the questions below: https://poloclub.github.io/cnn-explainer/
✅ Q:
Read the section below on what each layer of the network does.
What is the 2nd top prediction output for the bell pepper picture?
Clicking on the “+” button lets you upload a picture. What does your face get classified as?
Congratulations, we’re done!#
Written by Dr. Liz Munch, Michigan State University
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.