Skip to content

mspitzna/NCAtorch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

108 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

NCA-torch Logo

A comprehensive PyTorch-based framework for Neural Cellular Automata research and applications

TMLR arXiv Project Website

๐ŸŒŸ Highlights

NCAtorch is an open-source, modular research framework that combines classical Cellular Automata concepts with learnable neural networks. This implementation provides a unified codebase for training, evaluating, and visualizing Neural Cellular Automata across diverse tasks.

Key features:

  • ๐ŸŽฏ Modular Architecture: Composable perception and update modules for flexible experimentation
  • ๐ŸŽจ Diverse Tasks: Image generation (emoji, handbags), texture synthesis, self-classifying NCAs, video prediction
  • ๐Ÿ–ผ๏ธ Latent Space NCAs: High-resolution generation (512x512) via pre-trained autoencoders
  • ๐ŸŽฎ Interactive Visualization: Real-time FastAPI-based web interface with painting tools
  • ๐Ÿ“Š Experiment Tracking: Integrated Weights & Biases logging
  • โš™๏ธ YAML Configuration: Pydantic-validated configuration system

๐Ÿ“‘ What's New

  • [13-05-2026] ๐ŸŒ€ Initial release of NCAtorch framework!

Community Works

If your work has improved NCAtorch and you would like more people to see it, please inform us!

๐Ÿš€ Quick Start

Installation

Prerequisites

  • Python 3.10 or higher
  • uv package manager
  • CUDA-capable GPU (recommended)

Setup

  1. Install uv (if not already installed):
curl -LsSf https://astral.sh/uv/install.sh | sh
  1. Clone the repository:
git clone https://github.com/mspitzna/NCAtorch.git
cd NCAtorch
  1. Install all dependencies (creates .venv automatically, pulls the correct PyTorch CUDA build):
uv sync --dev
  1. Activate environment:
source .venv/bin/activate

Training Your First Model

Train an NCA model using a configuration file:

ncatorch-train --config config/emoji_config.yaml

For latent space NCA (requires training an autoencoder first):

# Step 1 โ€” train the autoencoder (checkpoint saved to train_log/<run_folder>/ae_checkpoints/)
ncatorch-train-ae --config config/your_config.yaml

# Step 2 โ€” train the CA, pointing --folder to the AE training log
ncatorch-train --folder train_log/<run_folder>

Activate experiment tracking with Weights & Biases

  1. WandB console login (needs Weights & Biases account + API Key):
wandb login
  1. Edit config .yaml
WANDB: true
  1. Train an NCA model using a configuration file:
ncatorch-train --config config/emoji_config.yaml

๐Ÿ’ก Tip: Start with the emoji generation task for quick results and visual feedback!

๐ŸŽจ Interactive Visualization

Launch the web interface to interact with trained models:

ncatorch-ui

Then open your browser to http://localhost:8000

Options

# Force a specific device
ncatorch-ui --device cuda:0
ncatorch-ui --device cpu

# Custom host/port
ncatorch-ui --host 0.0.0.0 --port 8080

# Auto-reload on code changes (development)
ncatorch-ui --reload

๐Ÿ“น Demo Video

NCAtorch Interactive Demo

Click to watch the toolkit demo video

โš™๏ธ Configuration

Models and training are configured via YAML files. Each perception entry declares its own OUT_CHANNEL to set the number of filters emitted from that branch. Here's a basic example:

SEED: 42                            # Random seed (-1 for random)
DEVICE: "cuda"

LOGGING:
  WANDB: true
  PROJECT_NAME: "your_project"
  TRAIN_NAME: "your_run_name"
  LOG_INTERVAL: 500
  SAVE_INTERVAL: 25000
  INTERMEDIATE_LOGGING_STEPS: [5, 20, 35]  # Steps at which intermediate states are logged (must be < ITER_N_MIN)

MODEL:
  NAME: "MLP"                       # Update model architecture: MLP or ResNet
  HIDDEN_CHANNELS: [64, 128]        # Hidden layer sizes in the update model
  CHANNEL_N: 16                     # Number of CA state channels - visible and classification channels are included here
  LIVING_MASK: true                 # Zero out updates for cells below the alive threshold
  LIVING_MASK_INDEX: 3              # Alpha channel as living mask  
  CLAMP_OUTPUT: false               # Clamp state values to [-1, 1] after each step
  PERCEPTIONS:
    - MODE: "conv"                  # Neighbourhood operator: conv, attention, mh_attention, sobel
      KERNEL_SIZE: 3
      OUT_CHANNEL: 48               # Output channels from this perception branch
    - MODE: "attention"             # Multiple branches are concatenated before the update model
      OUT_CHANNEL: 32

TRAINING:
  BATCH_SIZE: 18
  STEPS: 50000
  LOSS_FN: "mse"                    # Reconstruction loss: mse, l1, lpips, vggstyle
  LEARNING_RATE: 0.0005
  LR_SCHEDULE_MODE: "cosine"        # LR schedule: step, cosine, constant
  WARMUP_STEPS: 2000                # Linear LR warm-up duration
  ITER_N_MIN: 20                    # Minimum CA rollout steps per batch
  ITER_N_MAX: 26                    # Maximum CA rollout steps per batch (sampled uniformly)

DATASET:
  NAME: "emoji"                     # Dataset: for example emoji, e2h, mnist, cifar10
  TARGET_SIZE: 64                   # Spatial resolution of the target image
  TARGET_PADDING: 16                # Zero-padding added around the target (depends on dataset used)
  EMOJIS:
    - "๐Ÿ˜ญ"
    - "๐Ÿ”ฅ"

PATTERN_POOL:
  ENABLED: true                     # Use a persistent sample pool across steps
  POOL_SIZE: 256
  POOL_START_RATIO: 0.5             # Fraction of each batch drawn from the pool

๐Ÿ’ก See the config/ directory for complete per-task examples.

๐ŸŽฏ Supported Tasks

๐Ÿ–ผ๏ธ Image Generation

  • Emoji Generation: Generate emoji from Unicode characters
  • Edge-to-Handbag (E2H): Conditional generation from edge maps

๐ŸŽจ Texture Synthesis

  • Organizing Textures: DTD texture synthesis with style loss

๐Ÿ”ข Classification

  • MNIST: Self-classifying digit recognition
  • CIFAR-10: Multi-class image classification

๐ŸŽฌ Video Prediction

  • Moving MNIST: Temporal dynamics and video prediction

๐Ÿ–ผ๏ธ High-Resolution Generation

  • Latent Space NCAs: 512x512 generation via pre-trained autoencoders

๐Ÿ“‚ Project Structure

nca-torch/
โ”œโ”€โ”€ nca/                      # Core library
โ”‚   โ”œโ”€โ”€ core/
โ”‚   โ”‚   โ”œโ”€โ”€ models/          # NCA models, autoencoders, critics
โ”‚   โ”‚   โ””โ”€โ”€ losses/          # Loss functions
โ”‚   โ”œโ”€โ”€ data/
โ”‚   โ”‚   โ””โ”€โ”€ datasets/        # Dataset implementations
โ”‚   โ”œโ”€โ”€ training/
โ”‚   โ”‚   โ””โ”€โ”€ trainers/        # Training logic
โ”‚   โ””โ”€โ”€ utils/               # Utilities and visualization
โ”œโ”€โ”€ app/                      # FastAPI web application
โ”‚   โ”œโ”€โ”€ fastapi_backend.py   # Server entry point
โ”‚   โ”œโ”€โ”€ templates/           # HTML templates
โ”‚   โ””โ”€โ”€ scripts/             # Frontend JavaScript
โ”œโ”€โ”€ train_scripts/            # Training entry points
โ”œโ”€โ”€ config/                   # YAML configuration files
โ””โ”€โ”€ datasets/                 # Dataset storage
โ””โ”€โ”€ train_log/                # Training logs

๐Ÿ“š Documentation

Guide Description
Custom Perception Add a new neighborhood operator
Custom Update Module Add a new update architecture
Custom Dataset Add a new dataset and wire it into the training pipeline
Custom Trainer Add a new training loop by implementing two methods and registering one entry
Custom Logging Observer Add a diagnostic that hooks into the CA rollout and logs itself

๐Ÿ“ Citation

When using this code or the NCAtorch framework in your project, consider citing our works as follows:

@article{ncatorch,
  title={A New Kind of Network? Review and Reference Implementation of Neural Cellular Automata},
  author={Martin Spitznagel and Janis Keuper},
  journal={Transactions on Machine Learning Research (TMLR)},
  year={2026}
}

About

Neural Cellular Automata in PyTorch (NCA)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors