Skip to content

← Image Pattern Lab step 1 of 1

Medium End-to-End

Image Pattern Lab

Your project

Can a model distinguish horizontal stripes from vertical stripes? Build a complete, CPU-friendly image pipeline on tiny grayscale images. The feature extractor uses fixed difference filters; only the linear head is trained. This is a stepping stone to a CNN, not a claim that you have trained a convolutional network.

1. Extract two features

Input images has shape (N, H, W), with H, W >= 2. For each image, compute the mean absolute difference between adjacent rows (horizontal contrast) and between adjacent columns (vertical contrast). Stack them in that order into features of shape (N, 2). These are valid two-pixel difference filters followed by absolute value and global average pooling; there is no padding.

2. Train the head

Start with two zero weights and a zero scalar bias. On every update compute z = features @ weights + bias, p = sigmoid(z), and error = p - labels. Use dw = features.T @ error / N and db = mean(error), then update both with lr. Record initial mean BCE and mean BCE after each update using max(z, 0) - labels*z + log1p(exp(-abs(z))).

Return contract

Return ordinary Python values: features (N by 2 nested list), weights (two floats), bias (float), probabilities (N floats from the final head), and losses (steps + 1 floats). Labels are float32 zeros or ones. Inputs are nonempty float32 tensors, steps is nonnegative and lr is nonnegative. Do not mutate inputs. A probability >= 0.5 predicts class 1 (vertical stripes).

Worked example

For [[0,0],[1,1]], the features are [1,0]. With label 0 and one update at lr 0.4, weights become [-0.2,0], bias becomes -0.2, and probability is sigmoid(-0.4) ≈ 0.401312. Initial loss is log(2).

Experiment after you pass

Export the script and change the public images. Try translation, brightness, and lower contrast. Which changes preserve these features? Try checkerboards: a two-feature representation cannot distinguish every image. The reported probabilities and loss describe training data, not held-out accuracy.