DRAW: a tiny network learns to draw a face

Starter code by @athrvam__. Python, numpy and Pillow only.

Download draw.py

Run it

  1. Install the two packages: pip install numpy pillow
  2. Save the file as draw.py
  3. Run python draw.py to draw the smiley
  4. Or try your own small picture: python draw.py my_picture.png

It trains for 3,000 steps and saves learning_steps.png, showing the picture at steps 0, 10, 100, 400, 1200 and 3000.

Catch: this network fits one picture. It memorizes it. It does not learn what a face is.

The code

"""
DRAW: watch a tiny neural network learn to draw one picture.

The network only ever sees pixel positions (x, y). For each position it guesses
a brightness. Each training step: guess, measure the error, adjust the weights.

Needs: numpy, pillow.   Run:  python draw.py                (draws a smiley)
                              python draw.py my_picture.png (any small picture)

It fits ONE image. It memorizes that picture. It does not learn what a face is.
"""
import sys
import numpy as np
from PIL import Image, ImageDraw

SIZE = 64          # picture is resized to SIZE x SIZE, grayscale
STEPS = 3000
HIDDEN = 128
LR = 3e-4
SEED = 0


def smiley():
    im = Image.new("L", (SIZE, SIZE), 0)
    d = ImageDraw.Draw(im)
    d.ellipse([4, 4, 59, 59], outline=255, width=3)
    d.ellipse([20, 20, 28, 28], fill=255)
    d.ellipse([36, 20, 44, 28], fill=255)
    d.arc([16, 28, 48, 50], 20, 160, fill=255, width=3)
    return im


def load(path):
    if path is None:
        return smiley()
    return Image.open(path).convert("L").resize((SIZE, SIZE))


def train(img, steps=STEPS, keep=None):
    """Returns (snapshots, errors). snapshots: {step: SIZExSIZE array in 0..1}."""
    target = (np.array(img, dtype=float) / 255).ravel()
    ys, xs = np.mgrid[0:SIZE, 0:SIZE]
    coords = np.c_[xs.ravel() / SIZE, ys.ravel() / SIZE]
    rng = np.random.default_rng(SEED)
    B = rng.normal(0, 3, (2, 64))                 # random sine/cosine features of (x, y)
    p = 2 * np.pi * coords @ B
    X = np.c_[np.sin(p), np.cos(p)]
    def init(i, o):
        return rng.normal(0, np.sqrt(2 / i), (i, o)), np.zeros(o)
    P = [*init(128, HIDDEN), *init(HIDDEN, HIDDEN), *init(HIDDEN, 1)]
    m = [np.zeros_like(a) for a in P]
    v = [np.zeros_like(a) for a in P]
    n = len(target)
    snaps, errs = {}, []
    for t in range(steps + 1):
        W1, b1, W2, b2, W3, b3 = P
        a1 = np.maximum(0, X @ W1 + b1)
        a2 = np.maximum(0, a1 @ W2 + b2)
        out = (a2 @ W3 + b3)[:, 0]                 # 1. GUESS
        err = float(np.mean((out - target) ** 2))   # 2. MEASURE ERROR
        errs.append(err)
        if keep is None or t in keep:
            snaps[t] = out.reshape(SIZE, SIZE).copy()
        do = (2 * (out - target) / n)[:, None]      # 3. ADJUST (backpropagation)
        g3 = a2.T @ do; gb3 = do.sum(0)
        d2 = (do @ W3.T) * (a2 > 0); g2 = a1.T @ d2; gb2 = d2.sum(0)
        d1 = (d2 @ W2.T) * (a1 > 0); g1 = X.T @ d1; gb1 = d1.sum(0)
        for i, (a, g) in enumerate(zip(P, [g1, gb1, g2, gb2, g3, gb3])):
            m[i] = .9 * m[i] + .1 * g
            v[i] = .999 * v[i] + .001 * g * g
            a -= LR * (m[i] / (1 - .9 ** (t + 1))) / (np.sqrt(v[i] / (1 - .999 ** (t + 1))) + 1e-8)
    return snaps, errs


if __name__ == "__main__":
    img = load(sys.argv[1] if len(sys.argv) > 1 else None)
    marks = [0, 10, 100, 400, 1200, STEPS]
    snaps, errs = train(img, keep=set(marks))
    strip = Image.new("L", (SIZE * len(marks), SIZE))
    for i, k in enumerate(marks):
        strip.paste(Image.fromarray((np.clip(snaps[k], 0, 1) * 255).astype(np.uint8)), (i * SIZE, 0))
        print(f"step {k:5d}  error {errs[k]:.4f}")
    strip.resize((strip.width * 4, strip.height * 4), Image.NEAREST).save("learning_steps.png")
    print("saved learning_steps.png (left to right: steps", marks, ")")