Practical Labs/Lab: Convolution and Convolutional Neural Networks
Level 2 · computer-vision
Lab: Convolution and Convolutional Neural Networks
Build a minimal, verifiable implementation that demonstrates Convolution and Convolutional Neural Networks. Connect the code to the governing equation, inspect its output numerically, and explain one failure case.
Mathematical basis
For Convolution and Convolutional Neural Networks, compute and verify output size .
Instructions
- 01Read section 7 of the Convolution and Convolutional Neural Networks learning page and write its governing equation on paper.
- 02Implement the core operation in OpenCV / torchvision on CIFAR-10 or a synthetic 8×8 image. Do not call a library function that solves the whole task.
- 03Verify the implementation on a hand-computed three-element or 3×3 example before scaling up.
- 04Run the experiment with kernel size or padding and record top-1 accuracy or IoU.
- 05Inspect the output, identify one discrepancy with the theory, and write a one-paragraph explanation.
Starter code
import numpy as np
def conv2d_valid(x, w):
oh, ow = x.shape[0]-w.shape[0]+1, x.shape[1]-w.shape[1]+1
return np.array([[np.sum(x[i:i+w.shape[0], j:j+w.shape[1]]*w)
for j in range(ow)] for i in range(oh)])
x=np.ones((4,4)); w=np.array([[0.,1.],[2.,3.]])
y=conv2d_valid(x,w)
assert y.shape==(3,3) and np.allclose(y,6)
print(y)