Paul's Python CoursePPC

Project 6 of 14, for Aaesha

Image Classifier

A convolutional network's forward pass by hand in NumPy, seeing the way the visual cortex starts to: edges on the Clocktower, pooling, and four trained filters that classify handwritten digits from thirty-six numbers each.

About 2 hours, in 12 steps.

Sign up free to open itLog in

Act 7 needs a free account, so your work is saved as you go. Act 1 is open to everyone.

The brief

  • AaeshaThe digit network reads a picture as sixty-four numbers in a row and has no idea which pixel's next to which.
  • AaeshaEyes don't work like that, because cells in the visual cortex each watch one small patch and fire for an edge.
  • NeilSo it's little grids slid across the picture, then. I trained four at home in PyTorch and copied out the numbers.

In seeing.py, by hand with NumPy, no SciPy or PyTorch. Neil's trained numbers are in filters.py.

  1. convolve(image, kernel): slide the kernel over the picture. At each place it fits entirely, multiply it by the patch underneath and add the lot up. For an image of height H and width W and a kernel of height kh and width kw, the answer is (H - kh + 1, W - kw + 1).
  2. max_pool(feature, size=2): the biggest number in each size by size block. Rows and columns that don't make a whole block are dropped.
  3. conv_layer(image, kernels, biases): for each kernel, convolve, add that kernel's bias and apply relu. The maps come back stacked, shape (number of kernels, H', W').
  4. forward(image, params): the maps from params["kernels"] and params["kernel_biases"], each pooled, flattened filter by filter into one row, then row @ params["W"] + params["b"] and softmax: ten chances.
  5. Run as a program, it reads the 297 held-back digits from the Handwritten Digit Recogniser through Neil's filters.
What it prints
A digit (8, 8) becomes maps (4, 6, 6), pooled to (4, 3, 3): 36 numbers, then 10 chances
Read 289 of 297 digits it had never seen

What you learn

What's in it

  1. A window slid across a picture
  2. A window on the Clocktower
  3. A kernel that finds edges
  4. Edges
  5. Keeping the strongest
  6. The biggest in each block
  7. A network that sees
  8. How Paul would do it
  9. Fix Riya's version
  10. A network that sees
  11. Ship it