ml_artists · KoganStyleTransferField
Style Transfer Field (Gene Kogan — ml4a)
Inspired by Gene Kogan's Experiments with Style Transfer and the ml4a curriculum: neural style transfer iteratively updates an image so its Gram-matrix feature statistics match a style reference — the gradient of the style loss with respect to pixels defines a vector field. We replicate the kernel: build a synthetic multi-octave 'neural feature map' F(x,y) from phase-drifting sinusoids, take its ∇F, and spawn N agents that follow the gradient with momentum + jitter on a toroidal canvas. Agents deposit HSV-coloured trails; every K steps a 'neural style impulse' boosts the canvas with a soft tint derived from F itself, mimicking the stylization step. The output is a layered, slowly-rotating field of coloured streaks that flow along the underlying 'neural feature' topography.
Original: https://ml4a.github.io/ml4a/style_transfer/
▶ Try the live demo (JS port) ▶ Run reference.py via Pyodide 10 formulas have instant JS ports. The Pyodide path runs the actual Python in your browser — first load is ~6 MB, then instant.
Math essence
F(x,y) = Σ_k a_k · sin(k_x · R_θ·x + k_y · R_θ·y + φ_k·(1+0.3k)); ∇F computed by central differences and unit-normalised. Agent update: v ← momentum·v + speed·∇F(F(p)) + jitter; p ← p + v (mod dim). Deposit HSV-coloured trail at p. Every impulse_period steps: canvas += α·tint(F); canvas *= fade.
Parameters
| Name | Type | Default | Range | Description |
|---|---|---|---|---|
dim | int | 600 | 200–1500 | Square canvas side length (px). |
n_agents | int | 800 | 10–5000 | Number of trail-following agents. |
feature_scale | float | 6 | 0.5–30 | Spatial frequency of the dominant feature map (cycles per dim). |
n_octaves | int | 3 | 1–6 | Number of summed sinusoidal octaves in F. |
feature_drift | float | 0.04 | 0–0.5 | Per-step phase drift of F (radians). |
speed | float | 2.5 | 0–12 | Step length per agent per step. |
momentum | float | 0.85 | 0–0.999 | Velocity retention (0..1). |
trail_alpha | float | 0.05 | 0–0.5 | Canvas deposit opacity per agent per step. |
impulse_period | int | 30 | 0–500 | Steps between neural-style impulse texture boosts (0 disables). |
Reference
First 60 lines of reference.py. Full source:
library_external/ml_artists/KoganStyleTransferField/reference.py
"""KoganStyleTransferField — Gene Kogan (ml4a) "style transfer field" kernel.
Gene Kogan's *Experiments with Style Transfer* (and the broader ml4a
curriculum at ml4a.github.io) treats neural style transfer as a
*gradient-ascent* procedure: an output image is iteratively updated
so its feature activations at chosen convnet layers match those of a
style reference, while keeping activations at content layers close to
a content reference. The "field" we extract from this idea is the
gradient of the *Gram-matrix style loss* with respect to the input
image — a vector field over the canvas that, when followed, *increases
style-ness* of the image.
We translate the kernel to numpy:
1. Build a synthetic multi-octave "neural feature map" F(x, y)
using summed sinusoids with a couple of spatial frequencies and
a slow phase drift.
2. Compute its (x, y)-gradient as a smooth vector field
(∇F) using a Sobel filter.
3. Spawn N agents that follow the gradient (with momentum, jitter
and wrap-around), depositing a fading coloured trail on the
canvas as they go.
4. Optionally inject a "neural style impulse" — every K steps, add
a soft texture boost derived from F itself, mimicking the way
style transfer updates affect the rendered image.
References:
- https://ml4a.github.io/ml4a/style_transfer/
- https://genekogan.com/works/style-transfer/
- https://genekogan.com/
"""
from __future__ import annotations
from typing import ClassVar
import numpy as np
class KoganStyleTransferField:
"""Flow field driven by gradients of a synthetic neural feature map.
Parameters
----------
dim : int
Square canvas side length (px).
n_agents : int
Number of trail-following agents.
feature_scale : float
Spatial frequency of the dominant feature map (cycles per dim).
n_octaves : int
Number of summed sinusoidal octaves in F.
feature_drift : float
Per-step phase drift of F (radians per step, total).
speed : float
Step length per agent per step.
momentum : float
Velocity retention (0..1).
trail_alpha : float
Canvas deposit opacity per agent per step.
impulse_period : int
Number of steps between "neural style impulses" (texture boosts). Try running it locally
The reference implementation is self-contained Python with numpy. Install the package and run:
python library_external/ml_artists/KoganStyleTransferField/reference.py Need help? See Learn for the standard environment setup, or Playground for an in-browser sketchbook.
More from ml artists
DeepDream Field (Mike Tyka — AI: DeepDream / Inceptionism)
ml_artists · TykaDeepDreamField
Inspired by Mike Tyka's AI: DeepDream series (2015-) and his co-authorship of 'Inceptionism: Going Deeper into Neural Networks' with Mordvintsev and Olah.
GAN Watercolor (Helena Sarin — Neural Bricolage / #latentDoodles)
ml_artists · SarinWatercolor
Inspired by Helena Sarin's Neural Bricolage practice (#latentDoodles, GAN Weaver, The Book of veGAN): she trains GANs on her own watercolor paintings and sketches, then curates the output back into analog paintings.
Glitch (Mario Klingemann — glitch-print aesthetic)
ml_artists · KlingemannGlitch
Klingemann's practice amplifies the visible failure modes of digital pipelines — distorted faces mid-decode, colour-shifted artefacts, rectangular datamosh tears — into the subject matter itself.
Memories of Passersby (Mario Klingemann — Memories of Passersby I)
ml_artists · KlingemannMemorex
Inspired by Mario Klingemann's Memories of Passersby I (2018, Sotheby's) — a custom GAN trained on 17th–19th-century portraits that runs forever, never repeats an output, and 'observes itself' in a feedback loop.
Neural Biodiversity (Sofia Crespo — Neural Zoo / Artificial Natural History)
ml_artists · CrespoNeuralBiodiversity
Inspired by Sofia Crespo's Neural Zoo (2018-), Artificial Natural History (2019-), and Beneath the Neural Waves (2021): she uses 3D GANs, CNN-based texture synthesis and 3D style transfer to fabricate a 'natural history book that never was' — chimerical creatures whose anatomies recombine fragments of real species.