Skip to content

About

Blobs of colour trained by gradient descent into a photo, entirely in code. Then turned in 3D.

Resources

Stars

105 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

Gradient Descent for a Face

This isn't a photo. It's tens of thousands of blobs of colour that taught themselves to be one.

Left: the real photo. Right: blobs of colour training from noise into the same picture.

One photo goes in. The code starts from pure noise, and every step it paints the blobs, measures how wrong the picture is, and nudges every blob to be a little less wrong. No diffusion model and no image generator: the right-hand picture is drawn entirely by code. The method is the same training loop that AI image models use (gradient descent), pointed at a single photo.

The code was written by Claude (Fable 5.2 and Opus 5.5 in Claude Code).


Which one is the photo?

The real photo and the blob painting side by side

Left is the real photo. Right is the code, after 3,540 steps and 50,400 blobs.

Zoom in, and it's blobs

Close-up of the eye drawn as separate coloured discs   The eye forming out of noise as training runs

Left: the same eye with every blob shrunk and drawn as a hard disc, so you can see what it's made of. Right: that eye forming from noise over the whole run.

Real photo and code, full picture and a 7.5x zoom on the eye

How it works, in plain words

  1. A blob is a soft, see-through dab of colour. Each one is nine numbers: where it sits (2), how wide and tall it is (2), its angle (1), its colour (3) and how strong it is (1).
  2. Start random. 1,500 blobs with random positions, sizes and colours. It looks like confetti.
  3. Paint. The code draws all the blobs into a picture.
  4. Measure the mistake. It compares that picture with the photo and gets one number for how wrong it is: the loss.
  5. Nudge. The maths works out, for every number on every blob, which way makes the mistake smaller, and moves it a tiny step that way (the Adam optimiser).
  6. Repeat, thousands of times. Nobody tells it "this is an eye". The eye appears because it lowers the mistake.

Steps 0, 5, 15, 40, 100 and 300 of a short training run

Three tricks make it work:

  • Blurry first, sharp later. It learns a 160 px version of the photo, then 320 px, then close-up crops at 640 px and finally at the photo's full 1200 px.
  • More blobs where it's wrong. Every few steps it finds the spots with the biggest mistake and adds small new blobs there. The count grows from 1,500 toward 100,000.
  • Close-ups. Late in training it studies random zoomed-in patches, mostly of the face. That's where the lashes and stubble come from.

The whole face forming from noise

The 3D turn

The blob painting turned in 3D, with a blink and a small smile

The blob painting is pushed onto a depth surface and the camera turns. Honest note: the depth is not learned by the blobs. It comes from a separate, ready-made depth model (Depth Anything V2 Small) that guesses depth from the single photo. A full 360° spin isn't possible from one photo, because there is nothing behind the head to learn from. The blink and small smile are hand-made warps of the painting, not learned.

Run it yourself

Just watch it (no install)

The repo includes a recorded run, so you can play the film straight away. It only needs Python's built-in web server:

python3 -m http.server 5793

Then open http://localhost:5793/index.html and drag the bar at the bottom to scrub. graphic/loop-reel.html is the 9:16 explainer of the loop. Opening the files directly (file://) won't work, because the page has to fetch its data.

Train it yourself

Needs Python 3.10+. It picks the fastest device it finds: an NVIDIA GPU (CUDA), an Apple-silicon GPU (MPS), or the CPU. It was built and tested on an M3 Pro; the CUDA path has not been tested.

python3 -m venv .venv && .venv/bin/pip install -r requirements.txt

Train. This replaces the included recorded run in train_v2/ (about an hour on an M3 Pro for the full 7,000 steps; add --quick for a 60-step smoke test):

.venv/bin/python train_v2/train.py

A finished run writes train_v2/meta.json and the film picks it up. If you stop early, write the playback data for what exists so far (same file):

.venv/bin/python train_v2/live_meta.py

The depth surface for the 3D turn is already included (train/depth-tex.f32). To rebuild it, for example after changing the photo:

.venv/bin/python train/depth.py

To use your own photo, replace reference/sirio-portrait.webp and adjust the crop near the top of train_v2/train.py.

What's in here

Path What it is
train_v2/train.py The trainer: a hand-written 2D Gaussian splat renderer in PyTorch, Adam, densification and coarse-to-fine crops
train_v2/snapshots.bin, meta.json The recorded run the film replays (12 bytes per blob per snapshot)
train_v2/live_meta.py Writes the playback data for a run that stopped before the end
train/ An early 300-step test run, plus the depth files the 3D turn uses
train/depth.py Depth estimate and background mask for the 3D turn
index.html The film: one WebGL page that replays the real run, then the zoom proof and the 3D turn
graphic/ The 9:16 explainer of the loop, built from frames of the real run
BRIEF.md The original build brief

Limits

  • It learns one photo. It can copy a face perfectly, but it can't invent a new one.
  • The 3D is a depth guess from one photo, not real 3D.
  • The numbers on screen come from the recorded run. The included recorded run stops at step 3,960 of a planned 7,000 (58,800 blobs); the pictures above were captured at step 3,540.

Inspired by "Gradient Descent for Orchestra", a piece that shows music being learned by gradient descent.

About

Blobs of colour trained by gradient descent into a photo, entirely in code. Then turned in 3D.

Resources

Stars

105 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages