Project 47: Convolutional Kernel Explorer

A self-contained deep-dive from the canonical Math from Foundations to Machine Learning curriculum.

  • Difficulty: Level 2: Intermediate
  • Time: 8-16 hours
  • Language: Python with NumPy and an image library
  • Prerequisites: Projects 21, 25, and 45
  • Source: NN P07

What You Will Build

Create an image laboratory that implements discrete 2D convolution directly from nested loops before optimizing it. Users can load grayscale or RGB images, choose or edit kernels, and inspect padded input, each sliding window, elementwise products, accumulated output, and final feature map. Include identity, box blur, Gaussian blur, sharpen, emboss, horizontal/vertical Sobel, and Laplacian filters. Support padding, stride, multiple channels, normalization/display policies, and comparison against a trusted image-library implementation.

Real World Outcome

An interactive CLI or small GUI applies selected kernels and displays original image, kernel matrix, intermediate response, and normalized output side by side. A probe mode clicking one output pixel shows the exact source patch and arithmetic that produced it; benchmark and correctness reports compare your result with a reference convolution.

Core Question

How can one small matrix detect or transform a visual pattern everywhere in an image?

Concepts You Must Understand First

  • Discrete convolution versus cross-correlation and the meaning of kernel flipping.
  • Sliding windows, receptive fields, padding, stride, and output-size formulas.
  • Linear image filters and separability; Gaussian and Sobel kernels.
  • RGB channel aggregation and multi-output feature maps.
  • Pixel range, clipping, normalization, signed edge responses, and dtype overflow.

Build Milestones

  1. Implement valid single-channel convolution and verify every output on tiny hand-computed arrays.
  2. Add same/valid padding, stride, output-shape validation, and probeable window arithmetic.
  3. Add the preset kernel gallery and explain the visual role of each filter.
  4. Extend to color images and multiple kernels, displaying every feature map.
  5. Compare correctness and runtime with a trusted library; optionally add a vectorized path.

Hints in Layers

  1. Begin with a 4×4 array and 2×2 kernel where every result can be calculated on paper.
  2. Compute expected output dimensions before allocating; reject non-integral stride placements explicitly.
  3. Preserve floating-point signed responses until display time—edge filters naturally produce negatives.

Common Pitfalls and Debugging

  • Features appear mirrored or signs reverse: your code and reference disagree on convolution versus correlation. State the convention and flip the kernel only when required.
  • Borders shift or output size is wrong: padding is asymmetric or the size formula is incorrect. Print padded shape and first/last window coordinates.
  • Edges become black or wrap around: unsigned integer arithmetic overflowed. Convert to float before multiplying and choose an explicit visualization transform.

Definition of Done

  • Tiny-array tests verify values, padding, stride, and output dimensions.
  • At least eight named kernels produce explainable image effects.
  • Probe mode reveals the exact arithmetic for any selected output location.
  • Grayscale and RGB paths have documented channel semantics.
  • Results match a trusted reference within a justified tolerance.
  • Signed/filter outputs are displayed without silently corrupting values.

Previous: Project 46 · Complete learning path · Next: Project 48