A PyTorch re-implementation of Neural Radiance Fields
NeRF: Representing Scenes as Neural Radiance Fields for View Synthesis
Ben Mildenhall*1, Pratul P. Srinivasan*1, Matthew Tancik*1, Jonathan T. Barron2, Ravi Ramamoorthi3, Ren Ng1
1UC Berkeley, 2Google Research, 3UC San Diego
*denotes equal contribution
A PyTorch re-implementation of Neural Radiance Fields.
What's the secret sauce behind this speedup?
Multiple aspects. Besides obvious enhancements such as data caching, effective memory management, etc. I drilled down through the entire NeRF codebase, and reduced data transfer b/w CPU and GPU, vectorized code where possible, and used efficient variants of pytorch ops (wrote some where unavailable). But for these changes, everything else is a faithful reproduction of the NeRF technique we all admire :)
The NeRF code release has an accompanying Colab notebook, that showcases training a feature-limited version of NeRF on a "tiny" scene. It's equivalent PyTorch notebook can be found at the following URL:
A neural radiance field is a simple fully connected network (weights are ~5MB) trained to reproduce input views of a single scene using a rendering loss. The network directly maps from spatial location and viewing direction (5D input) to color and opacity (4D output), acting as the "volume" so we can use volume rendering to differentiably render new views.
Optimizing a NeRF takes between a few hours and a day or two (depending on resolution) and only requires a single GPU. Rendering an image from an optimized NeRF takes somewhere between less than a second and ~30 seconds, again depending on resolution.
To train a "full" NeRF model (i.e., using 3D coordinates as well as ray directions, and the hierarchical sampling procedure), first setup dependencies.
In a new
pip install -r requirements.txt
Use the provided
environment.ymlfile to install the dependencies into an environment named
environment.ymlif you wish to change the name of the
conda env create conda activate nerf
Once everything is setup, to run experiments, first edit
config/lego.ymlto specify your own parameters.
The training script can be invoked by running
bash python train_nerf.py --config config/lego.yml
Optionally, if resuming training from a previous checkpoint, run
bash python train_nerf.py --config config/lego.yml --load-checkpoint path/to/checkpoint.ckpt
An optional, yet simple preprocessing step of caching rays from the dataset results in substantial compute time savings (reduced carbon footprint, yay!), especially when running multiple experiments. It's super-simple: run
bash python cache_dataset.py --datapath cache/nerf_synthetic/lego/ --halfres False --savedir cache/legocache/legofull --num-random-rays 8192 --num-variations 50
8192rays per image from the
legodataset. Each image is
800 x 800(since
halfresis set to
500such random samples (
8192rays each) are drawn per image. The script takes about 10 minutes to run, but the good thing is, this needs to be run only once per dataset.
NOTE: Do NOT forget to update thecachediroption (underdataset) in your config (.yml) file!
A Colab notebook for the full NeRF model (albeit on low-resolution data) can be accessed here.
Once you've trained your NeRF, it's time to use that to render the scene. Use the
eval_nerf.pyscript to do that. For the
lego-lowresexample, this would be
bash python eval_nerf.py --config pretrained/lego-lowres/config.yml --checkpoint pretrained/lego-lowres/checkpoint199999.ckpt --savedir cache/rendered/lego-lowres
You can create a
gifout of the saved images, for instance, by using Imagemagick.
bash convert cache/rendered/lego-lowres/*.png cache/rendered/lego-lowres.gif
This should give you a gif like this.
All said, this is not an official code release, and is instead a reproduction from the original code (released by the authors here).
The code is thoroughly tested (to the best of my abilities) to match the original implementation (and be much faster)! In particular, I have ensured that * Every individual module exactly (numerically) matches that of the TensorFlow implementation. This Colab notebook has all the tests, matching op for op (but is very scratchy to look at)! * Training works as expected (for Lego and LLFF scenes).
The organization of code WILL change around a lot, because I'm actively experimenting with this.
Pretrained models: Pretrained models for the following scenes are available in the
pretraineddirectory (all of them are currently lowres). I will continue adding models herein. ```
chair drums hotdog lego materials ship
Feel free to raise GitHub issues if you find anything concerning. Pull requests adding additional features are welcome too.
nerf-pytorchis available under the MIT License. For more details see: LICENSE and ACKNOWLEDGEMENTS.