Brief
A convolution slides a small filter across an image and takes a dot product at every position. The same weights are reused everywhere, so a 3 × 3 filter on a 3-channel image has only 27 weights plus a bias, whatever the image size. Stacks of these filters are what let a network pick out edges, then textures, then ears and noses. A network built from them is a convolutional neural network (CNN).
You write the forward and backward passes with plain loops first and check them against
torch.nn.functional.conv2d. Deep-learning convolution doesn't flip the filter, so you are really computing a
cross-correlation. With input height , kernel size , padding and stride , the output height is
.
Loops are correct but slow. The fix is a classic kernel trick called im2col. Copy every input patch into one column of a big matrix, reshape the filters into rows, and the whole convolution becomes one matrix multiplication that NumPy hands to a fast BLAS library. You trade memory for speed. To judge the result, count the work: a convolution does floating-point operations for images, filters and input channels.
Then you use your engine. The Oxford-IIIT Pet dataset has 7,349 photos of 37 cat and dog breeds, about 200 per
breed, from Oxford's Visual Geometry Group. Load it with torchvision.datasets.OxfordIIITPet. Every breed is either
a cat or a dog, so you can turn the 37 labels into 2 and start with the easier task.
DATA · Oxford-IIIT Pet
Objectives
Read-only. Log in to update progress.
VICTORY CONDITIONS
- Forward and backward outputs match torch.nn.functional.conv2d within 1e-5, and im2col runs at least 20× faster than your loops on the same input, recorded with `python tools/mlarena.py record BL-14 "<max error>; <speedup>x"`.
- On the official test split, the classifier beats always guessing dog. About two thirds of the photos are dogs.
- The README has a table of time and GFLOP/s for loops, im2col and PyTorch.
Side quests
Terms in plain words
- Convolution
- Sliding a small grid of weights over an image and taking a dot product at each position. Deep-learning libraries skip the kernel flip, so strictly it's cross-correlation.
- Channel
- One layer of numbers per pixel position. A colour photo has 3 channels, and each filter in a conv layer produces one output channel.
- Stride and padding
- Stride is how far the filter moves between positions. Padding adds a border of zeros so the filter can sit on edge pixels.
- im2col
- Copying every input patch into a column of one big matrix, so the whole convolution becomes a single matrix multiplication.
- GFLOP/s
- Billions of floating-point operations per second. It tells you how close your code gets to what the hardware can do.