Examples¶
Four short scripts in the
examples/ folder of
the repository. Each one solves exact transports between two discrete
measures, weighted point clouds, and draws a figure. They need NumPy,
Matplotlib and Pillow; the sample pictures come from scikit-image, the
generative network from PyTorch. The times were measured on a laptop with 8
cores.
Morphing two words¶
morph.py
fills the letters of "deblais" and of "remblais" with 100 000 grains of sand
each, all of the same mass, and solves the assignment between the two sets of
grains: which grain of the first word goes to which grain of the second, so
that the sum of the squared distances is minimal. Each grain then moves in a
straight line to its partner, at constant speed:
This is the displacement interpolation of McCann, the shortest path between the two words in the Wasserstein space. The color of a grain is its height in the first word.
The assignment of \(10^5\) grains, among \(10^{10}\) possible pairs, takes 12 seconds. Letters are a hard case for the auction that deblais uses on assignment problems: between two letters there is no grain, and the prices can climb for a long time before a grain crosses the gap. deblais detects these price wars and hands the problem back to the simplex (see Concepts).
python examples/morph.py # the figure
python examples/morph.py --n 300000 --video morph.mp4
Color transfer¶
color_transfer.py
gives a photograph the colors of another one. The colors of a picture are a
cloud of points in the RGB cube, one per pixel, grouped by cells of the cube:
a point per occupied cell, at the mean color of its pixels and weighted by
their number. The transport between the colors of the photograph (11 045
cells) and those of the palette (8 935 cells) is solved exactly; each cell then
moves to the barycenter of the colors it is sent to (plan.map()), and its
pixels move with it. The recolored photograph has the color distribution of
the palette, obtained by moving every color as little as possible.
Three seconds, transport included.
python examples/color_transfer.py # rocket and coffee, from scikit-image
python examples/color_transfer.py photo.jpg palette.jpg --out recolored.png
Stippling¶
stippling.py
draws a picture with 20 000 dots of ink. The picture is a discrete measure:
one point per pixel (261 479 of them), of mass its darkness. The dots all
have the same mass. An iteration solves the transport between the dots and
the pixels, then moves every dot to the barycenter of the ink it receives.
After a few iterations each dot carries the same amount of ink: the dots are
dense where the picture is dark, sparse where it is light, without clumps and
without a visible grid.
Twelve transports of 20 000 dots against 261 479 pixels take 52 seconds.
python examples/stippling.py # scikit-image's "camera"
python examples/stippling.py photo.jpg --n 30000
A generative network¶
generative.py
trains a small PyTorch network to turn Gaussian noise into eight blobs on a
circle. The loss of a step is the exact squared Wasserstein-2
distance between a batch of 4 096 generated points and a batch of 4 096 data
points, computed by deblais.wasserstein2. Its gradient with respect to the
generated points is given by the envelope theorem,
with \(\sigma\) the optimal assignment of the batch: every generated point is pulled toward the data point it is matched with, and the gradient flows back into the weights of the network. No entropic regularization, so no blur.
The network first spreads the noise on a ring, then gathers the ring into eight blobs. A network is a continuous map: from the Gaussian noise, a connected cloud, it reaches eight separate blobs only by stretching thin filaments between them, which stay visible after training. The 6 000 steps, 6 000 exact transports between 4 096 points, take 35 minutes on the laptop: the blobs are a hard case for the solver (see Performance). The loss does not go to zero: two batches of 4 096 points drawn from the same distribution are at a distance of their own, the sampling noise of \(W_2^2\). A structure thinner than that noise, a tight spiral for instance, is not seen by the gradient.
python examples/generative.py
python examples/generative.py --data word --word OT --batch 8192 --steps 5000
Paris to London¶
The largest example has its own page: ten million grains of sand moved from the streets of Paris to the streets of London, with an interactive viewer.



