"""2D convolution (as used in CNNs) and max pooling, written with plain NumPy loops. CNN libraries compute cross-correlation: the kernel is slid over the image without flipping it. That is what conv2d does, and it matches scipy.signal.correlate2d. Run as a script for a small demo: python conv.py """ from __future__ import annotations import numpy as np from numpy.typing import ArrayLike, NDArray def output_size(n: int, k: int, stride: int = 1, padding: int = 0) -> int: """Output length along one axis: floor((n + 2p - k) / s) + 1.""" return (n + 2 * padding - k) // stride + 1 def conv2d(image: ArrayLike, kernel: ArrayLike, stride: int = 1, padding: int = 0) -> NDArray[np.float64]: """Single-channel 2D cross-correlation with zero padding and a stride.""" x = np.asarray(image, dtype=float) w = np.asarray(kernel, dtype=float) if padding > 0: x = np.pad(x, padding, mode="constant") kh, kw = w.shape oh = (x.shape[0] - kh) // stride + 1 ow = (x.shape[1] - kw) // stride + 1 out = np.empty((oh, ow)) for i in range(oh): for j in range(ow): r, c = i * stride, j * stride out[i, j] = np.sum(x[r : r + kh, c : c + kw] * w) return out def conv_layer( x: ArrayLike, weights: ArrayLike, bias: ArrayLike, stride: int = 1, padding: int = 0 ) -> NDArray[np.float64]: """A convolution layer: input (C, H, W), weights (K, C, kh, kw), bias (K,) -> (K, H', W'). Each output channel k is the sum over input channels of conv2d(x[c], weights[k, c]), plus bias[k]. """ x = np.asarray(x, dtype=float) w = np.asarray(weights, dtype=float) b = np.asarray(bias, dtype=float) k_out, c_in = w.shape[:2] if x.shape[0] != c_in: raise ValueError(f"input has {x.shape[0]} channels, weights expect {c_in}") maps = [ sum(conv2d(x[c], w[k, c], stride, padding) for c in range(c_in)) + b[k] for k in range(k_out) ] return np.stack(maps) def relu(x: ArrayLike) -> NDArray[np.float64]: return np.maximum(np.asarray(x, dtype=float), 0.0) def max_pool2d(x: ArrayLike, size: int = 2, stride: int | None = None) -> NDArray[np.float64]: """Max pooling over size x size windows of a 2D array (no padding).""" a = np.asarray(x, dtype=float) s = size if stride is None else stride oh = (a.shape[0] - size) // s + 1 ow = (a.shape[1] - size) // s + 1 out = np.empty((oh, ow)) for i in range(oh): for j in range(ow): out[i, j] = a[i * s : i * s + size, j * s : j * s + size].max() return out def main() -> None: # A 6 x 6 image with a vertical edge between columns 2 and 3. image = np.zeros((6, 6)) image[:, 3:] = 1.0 sobel_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype=float) edges = conv2d(image, sobel_x) print("Sobel-x response (valid, 4 x 4):\n", edges) print("after ReLU and 2 x 2 max pool:\n", max_pool2d(relu(edges), 2)) print("output size for a 640-wide image, 3x3 kernel, stride 2, padding 1:", output_size(640, 3, 2, 1)) if __name__ == "__main__": main()