Implementations of three building blocks of modern differentiable rendering, built up from fitting a single image to training a neural radiance field. Each piece is written from scratch in PyTorch and is meant to be read as much as run.
- Neural fields — fitting an image with a SIREN and a ReLU-MLP, including autograd-based gradient and Laplacian supervision.
- Sphere tracing & volume rendering — two ways of turning an implicit 3D scene into an image.
- NeRF — a positional-encoded coordinate MLP trained by volume rendering on
a Blender-synthetic scene (e.g.
lego).
neural_fields/ SIREN, ReLU-MLP, autograd operators, single-image training loop
renderers/ camera rays, sphere tracing, and volume rendering of analytic scenes
nerf/ positional encoding, NeRF field, dataset loader, volume renderer, trainer
scripts/ entry points for each part
python -m venv venv && source venv/bin/activate
pip install -r requirements.txt # install the PyTorch build for your platformA neural field maps continuous coordinates to signal values. The SIREN uses sine activations (with the paper's initialisation) so it can represent high-frequency detail and have well-behaved derivatives; the ReLU-MLP is the baseline. The training loop also visualises the field's gradient and Laplacian, computed with autograd.
python -m scripts.fit_image --model SIREN --steps 1000
python -m scripts.fit_image --model MLP --steps 2000The SIREN reaches a sharp reconstruction in ~100 steps that the ReLU-MLP never reaches in 2000 — a direct consequence of spectral bias.
Two renderers for an implicit scene. Sphere tracing marches each ray by the signed-distance value to find a hard surface hit; volume rendering samples points along each ray and alpha-composites their density and colour — the same integral the NeRF uses, here on an analytic scene.
python -m scripts.render_demos # writes sphere_tracing.png and volume_rendering.pngA NeRF represents a scene as a function from a 3D point to a colour and a density. A point is Fourier-encoded, run through an MLP with a skip connection, and the resulting densities/colours along each ray are volume-rendered into a pixel. Training minimises the photometric error against posed images.
Download a scene from the NeRF-synthetic dataset and point --data at the scene
directory (the one containing transforms_train.json):
python -m scripts.train_nerf --data data/nerf_synthetic/lego --iters 20000Periodic renders and a final 360° orbit (spin.gif) are written to outputs/.
Training is fast on a GPU and runs (more slowly) on CPU; results on a small MLP
are blurred but clearly 3D.
- The volume renderer in
nerf/is the flat ray-batch counterpart of the one inrenderers/— same compositing integral, batched over randomly sampled rays and with stratified sampling for training. - The NeRF field is position-only (no view-dependent effects), which keeps it small enough to train quickly on a single scene.
- Sitzmann et al., Implicit Neural Representations with Periodic Activation Functions (SIREN), 2020.
- Mildenhall et al., NeRF: Representing Scenes as Neural Radiance Fields for View Synthesis, 2020.
