Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- README.md +20 -5
- app.py +135 -0
- index.html +27 -17
- nca_mountain.npz +3 -0
- nca_numpy.py +90 -0
README.md
CHANGED
|
@@ -1,10 +1,25 @@
|
|
| 1 |
---
|
| 2 |
-
title: Growing
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
|
|
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Growing NCA
|
| 3 |
+
emoji: 🏔️
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: static
|
| 7 |
pinned: false
|
| 8 |
+
license: mit
|
| 9 |
+
short_description: Neural cellular automaton that grows and regrows a mountain
|
| 10 |
---
|
| 11 |
|
| 12 |
+
# Growing a mountain with a Neural Cellular Automaton
|
| 13 |
+
|
| 14 |
+
Every pixel runs the same small neural network (8,320 weights) and sees only its 3×3 neighborhood. Starting from a
|
| 15 |
+
single live cell, the cells grow the 🏔️ emoji, hold it stable, and regrow it after damage. A PyTorch
|
| 16 |
+
reimplementation of [Growing Neural Cellular Automata](https://distill.pub/2020/growing-ca/) (Mordvintsev et al.,
|
| 17 |
+
Distill 2020), trained on an Apple M1 Pro.
|
| 18 |
+
|
| 19 |
+
The app runs entirely in your browser ([stlite](https://github.com/whitphx/stlite)). Training is in PyTorch; the
|
| 20 |
+
weights are exported to `nca_mountain.npz` and the Space steps the automaton with a numpy port of the model,
|
| 21 |
+
since Pyodide has no PyTorch. A parity test checks that the two implementations agree.
|
| 22 |
+
|
| 23 |
+
Code: [github.com/itstheryguy/nca-experiment-1](https://github.com/itstheryguy/nca-experiment-1).
|
| 24 |
+
|
| 25 |
+
Emoji: [Noto Emoji](https://github.com/googlefonts/noto-emoji) by Google, Apache License 2.0.
|
app.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Growing NCA: the Hugging Face Space.
|
| 2 |
+
|
| 3 |
+
Runs in the visitor's browser with stlite (Streamlit on WebAssembly), so the Space needs no server. Pyodide has
|
| 4 |
+
no PyTorch, so the CA runs on a numpy port of the model (nca_numpy.py) with weights exported from training.
|
| 5 |
+
The same file also runs as a normal Streamlit app: `streamlit run space/app.py`.
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import base64
|
| 9 |
+
import struct
|
| 10 |
+
import time
|
| 11 |
+
import zlib
|
| 12 |
+
from pathlib import Path
|
| 13 |
+
|
| 14 |
+
import numpy as np
|
| 15 |
+
import streamlit as st
|
| 16 |
+
|
| 17 |
+
import nca_numpy as ca
|
| 18 |
+
|
| 19 |
+
HERE = Path(__file__).parent
|
| 20 |
+
CHANNELS, GRID = 16, 64
|
| 21 |
+
FRAME_SECONDS = 0.1 # fragment timer while playing
|
| 22 |
+
REPO = "https://github.com/itstheryguy/nca-experiment-1"
|
| 23 |
+
PAPER = "https://distill.pub/2020/growing-ca/"
|
| 24 |
+
VIEWS = ["Color", "Hidden channel", "All 16 channels"]
|
| 25 |
+
|
| 26 |
+
st.set_page_config(page_title="Growing NCA", page_icon="🏔️", layout="centered")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
@st.cache_resource
|
| 30 |
+
def weights():
|
| 31 |
+
return ca.load_weights(HERE / "nca_mountain.npz")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def reset():
|
| 35 |
+
st.session_state.x = ca.make_seed(CHANNELS, GRID)
|
| 36 |
+
st.session_state.steps = 0
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
if "x" not in st.session_state:
|
| 40 |
+
st.session_state.rng = np.random.default_rng()
|
| 41 |
+
reset()
|
| 42 |
+
st.session_state.playing = True
|
| 43 |
+
|
| 44 |
+
wts = weights()
|
| 45 |
+
rng = st.session_state.rng
|
| 46 |
+
|
| 47 |
+
st.title("🏔️ Growing a mountain from one cell")
|
| 48 |
+
st.markdown(
|
| 49 |
+
"Every pixel runs the same tiny neural network (about 8,000 weights), and sees only its 8 neighbors. "
|
| 50 |
+
"From a single live cell they grow the mountain, hold it steady, and regrow it when you damage it. "
|
| 51 |
+
f"A PyTorch reimplementation of [Growing Neural Cellular Automata]({PAPER}), running in your browser."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Controls. Any widget change reruns the whole script, which also re-registers the fragment timer below.
|
| 55 |
+
c1, c2, c3, c4 = st.columns(4)
|
| 56 |
+
c1.button("Reset", on_click=reset, width="stretch", icon=":material/restart_alt:")
|
| 57 |
+
st.session_state.playing = c2.toggle("Play", value=st.session_state.playing)
|
| 58 |
+
if c3.button("Damage", width="stretch", icon=":material/bolt:", help="Erase a random circle"):
|
| 59 |
+
cy, cx = rng.integers(GRID // 4, 3 * GRID // 4, size=2)
|
| 60 |
+
st.session_state.x = ca.erase(st.session_state.x, cy, cx, radius=rng.integers(6, 14))
|
| 61 |
+
if c4.button("Cut in half", width="stretch", icon=":material/content_cut:"):
|
| 62 |
+
st.session_state.x = st.session_state.x.copy()
|
| 63 |
+
st.session_state.x[:, :, GRID // 2:] = 0
|
| 64 |
+
|
| 65 |
+
v1, v2 = st.columns([2, 1])
|
| 66 |
+
view = v1.selectbox("View", VIEWS)
|
| 67 |
+
channel = 4
|
| 68 |
+
if view == "Hidden channel":
|
| 69 |
+
channel = v2.slider("Channel", 4, 15, 4, help="Channels 4-15 are the hidden state the cells use to coordinate.")
|
| 70 |
+
speed = st.slider("Steps per frame", 1, 8, 2)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def png_data_uri(img):
|
| 74 |
+
"""Encode an (H, W) or (H, W, 3) uint8 array as a PNG data URI, without Pillow.
|
| 75 |
+
|
| 76 |
+
The frame goes inline in an <img>, not through st.image: st.image stores each frame as a media file that the
|
| 77 |
+
page fetches separately, and at animation speed the file is replaced before the fetch arrives (404).
|
| 78 |
+
"""
|
| 79 |
+
h, w = img.shape[:2]
|
| 80 |
+
rows = np.concatenate([np.zeros((h, 1), np.uint8), img.reshape(h, -1)], axis=1) # filter byte 0 per row
|
| 81 |
+
|
| 82 |
+
def chunk(tag, data):
|
| 83 |
+
return struct.pack(">I", len(data)) + tag + data + struct.pack(">I", zlib.crc32(tag + data))
|
| 84 |
+
|
| 85 |
+
ihdr = struct.pack(">IIBBBBB", w, h, 8, 2 if img.ndim == 3 else 0, 0, 0, 0)
|
| 86 |
+
png = b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", ihdr) + chunk(b"IDAT", zlib.compress(rows.tobytes(), 1)) + chunk(b"IEND", b"")
|
| 87 |
+
return "data:image/png;base64," + base64.b64encode(png).decode()
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def render(x):
|
| 91 |
+
if view == "Color":
|
| 92 |
+
return ca.to_rgb(x)
|
| 93 |
+
if view == "Hidden channel":
|
| 94 |
+
return ca.to_heatmap(x, channel)
|
| 95 |
+
tiles = [np.pad(ca.to_heatmap(x, c), 1, constant_values=255) for c in range(CHANNELS)] # RGBA, then hidden
|
| 96 |
+
return np.vstack([np.hstack(tiles[r * 4:(r + 1) * 4]) for r in range(4)])
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
@st.fragment(run_every=FRAME_SECONDS if st.session_state.playing else None)
|
| 100 |
+
def board():
|
| 101 |
+
if st.session_state.playing:
|
| 102 |
+
t = time.perf_counter()
|
| 103 |
+
x = st.session_state.x
|
| 104 |
+
for _ in range(speed):
|
| 105 |
+
x = ca.step(x, wts, rng)
|
| 106 |
+
st.session_state.x = x
|
| 107 |
+
st.session_state.steps += speed
|
| 108 |
+
st.session_state.ms = (time.perf_counter() - t) * 1000 / speed
|
| 109 |
+
# Upscaled by the browser with pixelated sampling, so cells stay crisp and the payload stays ~10 KB.
|
| 110 |
+
st.html(f'<img src="{png_data_uri(render(st.session_state.x))}" alt="NCA grid, {view.lower()}" '
|
| 111 |
+
'style="display:block;margin:auto;width:100%;max-width:512px;image-rendering:pixelated">')
|
| 112 |
+
ms = st.session_state.get("ms")
|
| 113 |
+
st.caption(f"Step {st.session_state.steps}" + (f" · {ms:.0f} ms per step" if ms else ""))
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
board()
|
| 117 |
+
|
| 118 |
+
with st.expander("How it works"):
|
| 119 |
+
st.markdown(f"""
|
| 120 |
+
The grid is 64×64 cells, each holding 16 numbers: color and opacity (RGBA) plus 12 hidden "chemical" channels.
|
| 121 |
+
Each step, every cell:
|
| 122 |
+
|
| 123 |
+
1. **Perceives** its 3×3 neighborhood with fixed filters: its own state, and horizontal and vertical gradients
|
| 124 |
+
(Sobel), giving 48 numbers.
|
| 125 |
+
2. **Updates** by passing those through a small network shared by all cells: 48 → 128 (ReLU) → 16, added to its
|
| 126 |
+
state. Only a random half of cells update each step, so the cells can't rely on a global clock.
|
| 127 |
+
3. **Lives or dies**: a cell with no neighbor above 0.1 opacity is cleared to empty.
|
| 128 |
+
|
| 129 |
+
Training starts from the seed, runs 64–96 steps, and compares the RGBA channels to the emoji. A pool of past
|
| 130 |
+
states is fed back in, and some are damaged, so the rule learns to keep the mountain stable and regrow missing
|
| 131 |
+
parts, not only grow it once.
|
| 132 |
+
"""
|
| 133 |
+
+ (f"\nCode: [{REPO.removeprefix('https://')}]({REPO})." if REPO else ""))
|
| 134 |
+
|
| 135 |
+
st.caption("Emoji: Noto Emoji 🏔️ by Google, Apache License 2.0.")
|
index.html
CHANGED
|
@@ -1,19 +1,29 @@
|
|
| 1 |
<!doctype html>
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
</html>
|
|
|
|
| 1 |
<!doctype html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
| 6 |
+
<title>Growing NCA</title>
|
| 7 |
+
<!-- stlite runs Streamlit in the browser (Pyodide). Pinned so an upstream release can't change the app. -->
|
| 8 |
+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@stlite/browser@1.12.0/build/stlite.css" />
|
| 9 |
+
</head>
|
| 10 |
+
<body>
|
| 11 |
+
<div id="root"></div>
|
| 12 |
+
<script type="module">
|
| 13 |
+
import { mount } from "https://cdn.jsdelivr.net/npm/@stlite/browser@1.12.0/build/stlite.js";
|
| 14 |
+
mount(
|
| 15 |
+
{
|
| 16 |
+
// Streamlit and numpy come with stlite. The CA runs on numpy; Pyodide has no PyTorch.
|
| 17 |
+
requirements: [],
|
| 18 |
+
entrypoint: "app.py",
|
| 19 |
+
files: {
|
| 20 |
+
"app.py": { url: "./app.py" },
|
| 21 |
+
"nca_numpy.py": { url: "./nca_numpy.py" },
|
| 22 |
+
"nca_mountain.npz": { url: "./nca_mountain.npz" },
|
| 23 |
+
},
|
| 24 |
+
},
|
| 25 |
+
document.getElementById("root"),
|
| 26 |
+
);
|
| 27 |
+
</script>
|
| 28 |
+
</body>
|
| 29 |
</html>
|
nca_mountain.npz
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1cc45e4c0e6007754a81f1244b5deb59f247544454d0d0ed6eea9611ef43cddb
|
| 3 |
+
size 34264
|
nca_numpy.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Numpy port of the NCA update rule in nca.py, so the Space can run in the browser (Pyodide has no torch).
|
| 2 |
+
|
| 3 |
+
The state is one grid, shaped (C, H, W) float32. Keep this in sync with `CAModel.forward` in nca.py.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
ALIVE_THRESHOLD = 0.1
|
| 9 |
+
SOBEL_X = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]], dtype=np.float32) / 8.0
|
| 10 |
+
SOBEL_Y = SOBEL_X.T.copy()
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def load_weights(path_or_file):
|
| 14 |
+
z = np.load(path_or_file)
|
| 15 |
+
return {k: z[k] for k in ("w1", "b1", "w2")} | {"fire_rate": float(z["fire_rate"])}
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def make_seed(channels, grid):
|
| 19 |
+
x = np.zeros((channels, grid, grid), dtype=np.float32)
|
| 20 |
+
x[3:, grid // 2, grid // 2] = 1.0
|
| 21 |
+
return x
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _filter3x3(x, k):
|
| 25 |
+
"""Depthwise 3x3 cross-correlation with zero padding, like F.conv2d(padding=1, groups=C)."""
|
| 26 |
+
_, h, w = x.shape
|
| 27 |
+
p = np.pad(x, ((0, 0), (1, 1), (1, 1)))
|
| 28 |
+
out = np.zeros_like(x)
|
| 29 |
+
for i in range(3):
|
| 30 |
+
for j in range(3):
|
| 31 |
+
if k[i, j]:
|
| 32 |
+
out += k[i, j] * p[:, i:i + h, j:j + w]
|
| 33 |
+
return out
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def _max3x3(a):
|
| 37 |
+
"""3x3 neighborhood max of a 2D array, zero padded."""
|
| 38 |
+
h, w = a.shape
|
| 39 |
+
p = np.pad(a, 1)
|
| 40 |
+
return np.max([p[i:i + h, j:j + w] for i in range(3) for j in range(3)], axis=0)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def _alive(x):
|
| 44 |
+
"""3x3 max of alpha > threshold. Zero padding is fine: zero never exceeds the threshold."""
|
| 45 |
+
return _max3x3(x[3]) > ALIVE_THRESHOLD
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def step(x, wts, rng, fire_rate=None):
|
| 49 |
+
"""One CA step. Returns a new (C, H, W) array.
|
| 50 |
+
|
| 51 |
+
Same result as CAModel.forward, but the MLP only runs on cells that fire and lie within one cell of a live
|
| 52 |
+
cell. Cells farther out end up zeroed by the life mask, and can't change any kept cell's post-update life
|
| 53 |
+
test, so their update never matters. Under WebAssembly (no fast BLAS) this is about 4x faster.
|
| 54 |
+
"""
|
| 55 |
+
fire_rate = wts["fire_rate"] if fire_rate is None else fire_rate
|
| 56 |
+
c, h, w = x.shape
|
| 57 |
+
pre_life = _alive(x)
|
| 58 |
+
fire = rng.random((h, w)) <= fire_rate
|
| 59 |
+
idx = np.flatnonzero(fire & _max3x3(pre_life))
|
| 60 |
+
# (3C, H, W), interleaved per channel [c0, c0_dx, c0_dy, c1, ...] to match the torch depthwise conv
|
| 61 |
+
y = np.stack([x, _filter3x3(x, SOBEL_X), _filter3x3(x, SOBEL_Y)], axis=1).reshape(3 * c, h * w)
|
| 62 |
+
hid = np.maximum(wts["w1"] @ y[:, idx] + wts["b1"][:, None], 0)
|
| 63 |
+
x = x.reshape(c, h * w).copy()
|
| 64 |
+
x[:, idx] += wts["w2"] @ hid
|
| 65 |
+
x = x.reshape(c, h, w)
|
| 66 |
+
life = pre_life & _alive(x)
|
| 67 |
+
return (x * life).astype(np.float32)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def erase(x, cy, cx, radius):
|
| 71 |
+
"""Zero a circle of cells (for click-to-damage). Coordinates in pixels."""
|
| 72 |
+
_, h, w = x.shape
|
| 73 |
+
yy, xx = np.ogrid[:h, :w]
|
| 74 |
+
x = x.copy()
|
| 75 |
+
x[:, (yy - cy) ** 2 + (xx - cx) ** 2 <= radius ** 2] = 0
|
| 76 |
+
return x
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
def to_rgb(x):
|
| 80 |
+
"""RGB on a white background, (H, W, 3) uint8."""
|
| 81 |
+
a = np.clip(x[3], 0, 1)
|
| 82 |
+
rgb = np.clip(1.0 - a[None] + x[:3], 0, 1)
|
| 83 |
+
return (rgb.transpose(1, 2, 0) * 255).astype(np.uint8)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def to_heatmap(x, channel):
|
| 87 |
+
"""One channel scaled to grayscale, (H, W) uint8. Hidden channels are unbounded, so scale by the max |value|."""
|
| 88 |
+
v = x[channel]
|
| 89 |
+
m = float(np.abs(v).max()) or 1.0
|
| 90 |
+
return ((v / m * 0.5 + 0.5) * 255).astype(np.uint8)
|