README.md 9.37 KB
Newer Older
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
1
2
3
4
5
6
7
8
9
10
11
<p>
  <!-- pypi-strip -->
  <picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://user-images.githubusercontent.com/3310961/199083722-881a2372-62c1-4255-8521-31a95a721851.png" />
  <source media="(prefers-color-scheme: light)" srcset="https://user-images.githubusercontent.com/3310961/199084143-0d63eb40-3f35-48d2-a9d5-78d1d60b7d66.png" />
  <!-- /pypi-strip -->
  <img alt="nerfacc logo" src="https://user-images.githubusercontent.com/3310961/199084143-0d63eb40-3f35-48d2-a9d5-78d1d60b7d66.png" width="350px" />
  <!-- pypi-strip -->
  </picture>
  <!-- /pypi-strip -->
</p>
Matthew Tancik's avatar
Matthew Tancik committed
12

Ruilong Li's avatar
Ruilong Li committed
13
14
[![Core Tests](https://github.com/nerfstudio-project/nerfacc/actions/workflows/code_checks.yml/badge.svg)](https://github.com/nerfstudio-project/nerfacc/actions/workflows/code_checks.yml)
[![Docs](https://github.com/nerfstudio-project/nerfacc/actions/workflows/doc.yml/badge.svg)](https://github.com/nerfstudio-project/nerfacc/actions/workflows/doc.yml)
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
15
[![Downloads](https://pepy.tech/badge/nerfacc)](https://pepy.tech/project/nerfacc)
Ruilong Li's avatar
readme  
Ruilong Li committed
16

17
https://www.nerfacc.com/
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
18

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
19
20
[News] 2023/04/04. If you were using `nerfacc <= 0.3.5` and would like to migrate to our latest version (`nerfacc >= 0.5.0`), Please check the [CHANGELOG](CHANGELOG.md) on how to migrate.

21
22
23
24
25
26
NerfAcc is a PyTorch Nerf acceleration toolbox for both training and inference. It focus on
efficient sampling in the volumetric rendering pipeline of radiance fields, which is 
universal and plug-and-play for most of the NeRFs.
With minimal modifications to the existing codebases, Nerfacc provides significant speedups 
in training various recent NeRF papers.
**And it is pure Python interface with flexible APIs!**
27

28
![Teaser](/docs/source/_static/images/teaser.jpg?raw=true)
Ruilong Li's avatar
readme  
Ruilong Li committed
29

30
31
## Installation

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
32
33
**Dependence**: Please install [Pytorch](https://pytorch.org/get-started/locally/) first.

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
34
The easist way is to install from PyPI. In this way it will build the CUDA code **on the first run** (JIT).
35
36
37
38
```
pip install nerfacc
```

Ruilong Li's avatar
Ruilong Li committed
39
Or install from source. In this way it will build the CUDA code during installation.
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
40
```
Ruilong Li's avatar
Ruilong Li committed
41
pip install git+https://github.com/nerfstudio-project/nerfacc.git
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
42
43
```

44
45
46
We also provide pre-built wheels covering major combinations of Pytorch + CUDA supported by [official Pytorch](https://pytorch.org/get-started/previous-versions/).

```
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
47
# e.g., torch 1.13.0 + cu117
48
49
50
pip install nerfacc -f https://nerfacc-bucket.s3.us-west-2.amazonaws.com/whl/torch-1.13.0_cu117.html
```

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
51
52
53
54
55
56
57
58
| Windows & Linux | `cu113` | `cu115` | `cu116` | `cu117` | `cu118` |
|-----------------|---------|---------|---------|---------|---------|
| torch 1.11.0    | ✅      | ✅      |         |         |         |
| torch 1.12.0    | ✅      |         | ✅      |         |         |
| torch 1.13.0    |         |         | ✅      | ✅      |         |
| torch 2.0.0     |         |         |         | ✅      | ✅      |

For previous version of nerfacc, please check [here](https://nerfacc-bucket.s3.us-west-2.amazonaws.com/whl/index.html) on the supported pre-built wheels.
59

60
61
## Usage

62
63
64
65
66
67
68
69
70
The idea of NerfAcc is to perform efficient volumetric sampling with a computationally cheap estimator to discover surfaces.
So NerfAcc can work with any user-defined radiance field. To plug the NerfAcc rendering pipeline into your code and enjoy 
the acceleration, you only need to define two functions with your radience field.

- `sigma_fn`: Compute density at each sample. It will be used by the estimator
  (e.g., `nerfacc.OccGridEstimator`, `nerfacc.PropNetEstimator`) to discover surfaces. 
- `rgb_sigma_fn`: Compute color and density at each sample. It will be used by 
  `nerfacc.rendering` to conduct differentiable volumetric rendering. This function 
  will receive gradients to update your radiance field.
71

72
An simple example is like this:
73
74
75
76
77
78
79
80
81

``` python
import torch
from torch import Tensor
import nerfacc 

radiance_field = ...  # network: a NeRF model
rays_o: Tensor = ...  # ray origins. (n_rays, 3)
rays_d: Tensor = ...  # ray normalized directions. (n_rays, 3)
82
83
84
optimizer = ...       # optimizer

estimator = nerfacc.OccGridEstimator(...)
85
86
87
88

def sigma_fn(
    t_starts: Tensor, t_ends:Tensor, ray_indices: Tensor
) -> Tensor:
89
    """ Define how to query density for the estimator."""
90
91
    t_origins = rays_o[ray_indices]  # (n_samples, 3)
    t_dirs = rays_d[ray_indices]  # (n_samples, 3)
92
    positions = t_origins + t_dirs * (t_starts + t_ends)[:, None] / 2.0
93
    sigmas = radiance_field.query_density(positions) 
94
    return sigmas  # (n_samples,)
95
96
97
98

def rgb_sigma_fn(
    t_starts: Tensor, t_ends: Tensor, ray_indices: Tensor
) -> Tuple[Tensor, Tensor]:
99
    """ Query rgb and density values from a user-defined radiance field. """
100
101
    t_origins = rays_o[ray_indices]  # (n_samples, 3)
    t_dirs = rays_d[ray_indices]  # (n_samples, 3)
102
    positions = t_origins + t_dirs * (t_starts + t_ends)[:, None] / 2.0
103
    rgbs, sigmas = radiance_field(positions, condition=t_dirs)  
104
    return rgbs, sigmas  # (n_samples, 3), (n_samples,)
105

106
107
108
109
110
# Efficient Raymarching:
# ray_indices: (n_samples,). t_starts: (n_samples,). t_ends: (n_samples,).
ray_indices, t_starts, t_ends = estimator.sampling(
    rays_o, rays_d, sigma_fn=sigma_fn, near_plane=0.2, far_plane=1.0, early_stop_eps=1e-4, alpha_thre=1e-2, 
)
111
112
113

# Differentiable Volumetric Rendering.
# colors: (n_rays, 3). opaicity: (n_rays, 1). depth: (n_rays, 1).
114
color, opacity, depth, extras = nerfacc.rendering(
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
115
116
    t_starts, t_ends, ray_indices, n_rays=rays_o.shape[0], rgb_sigma_fn=rgb_sigma_fn
)
117

118
# Optimize: Both the network and rays will receive gradients
119
120
121
122
123
124
optimizer.zero_grad()
loss = F.mse_loss(color, color_gt)
loss.backward()
optimizer.step()
```

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
125
## Examples: 
Ruilong Li's avatar
readme  
Ruilong Li committed
126

Jingchen Ye's avatar
Jingchen Ye committed
127
Before running those example scripts, please check the script about which dataset is needed, and download the dataset first. You could use `--data_root` to specify the path.
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
128

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
129
```bash
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
130
# clone the repo with submodules.
Ruilong Li's avatar
Ruilong Li committed
131
git clone --recursive git://github.com/nerfstudio-project/nerfacc/
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
132
```
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
133

134
135
### Static NeRFs

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
136
137
See full benchmarking here: https://www.nerfacc.com/en/stable/examples/static.html

138
Instant-NGP on NeRF-Synthetic dataset with better performance in 4.5 minutes.
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
139
``` bash
140
141
142
143
# Occupancy Grid Estimator
python examples/train_ngp_nerf_occ.py --scene lego --data_root data/nerf_synthetic
# Proposal Net Estimator
python examples/train_ngp_nerf_prop.py --scene lego --data_root data/nerf_synthetic
Ruilong Li's avatar
readme  
Ruilong Li committed
144
145
```

146
Instant-NGP on Mip-NeRF 360 dataset with better performance in 5 minutes.
Ruilong Li's avatar
Ruilong Li committed
147
``` bash
148
149
150
151
# Occupancy Grid Estimator
python examples/train_ngp_nerf_occ.py --scene garden --data_root data/360_v2
# Proposal Net Estimator
python examples/train_ngp_nerf_prop.py --scene garden --data_root data/360_v2
Ruilong Li's avatar
Ruilong Li committed
152
153
```

154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
Vanilla MLP NeRF on NeRF-Synthetic dataset in an hour.
``` bash
# Occupancy Grid Estimator
python examples/train_mlp_nerf.py --scene lego --data_root data/nerf_synthetic
```

TensoRF on Tanks&Temple and NeRF-Synthetic datasets (plugin in the official codebase).
``` bash
cd benchmarks/tensorf/
# (set up the environment for that repo)
bash script.sh nerfsyn-nerfacc-occgrid 0
bash script.sh tt-nerfacc-occgrid 0
```

### Dynamic NeRFs
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
169
170
171

See full benchmarking here: https://www.nerfacc.com/en/stable/examples/dynamic.html

172
173
174
175
176
177
178
T-NeRF on D-NeRF dataset in an hour.
``` bash
# Occupancy Grid Estimator
python examples/train_mlp_tnerf.py --scene lego --data_root data/dnerf
```

K-Planes on D-NeRF dataset (plugin in the official codebase).
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
179
```bash
180
181
182
cd benchmarks/kplanes/
# (set up the environment for that repo)
bash script.sh dnerf-nerfacc-occgrid 0
183
184
```

185
TiNeuVox on HyperNeRF and D-NeRF datasets (plugin in the official codebase).
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
186
```bash
187
188
189
190
191
cd benchmarks/tineuvox/
# (set up the environment for that repo)
bash script.sh dnerf-nerfacc-occgrid 0
bash script.sh hypernerf-nerfacc-occgrid 0
bash script.sh hypernerf-nerfacc-propnet 0
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
192
```
193

194
195
### Camera Optimization NeRFs

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
196
197
See full benchmarking here: https://www.nerfacc.com/en/stable/examples/camera.html

198
199
200
201
202
203
204
205
BARF on the NeRF-Synthetic dataset (plugin in the official codebase).
```bash
cd benchmarks/barf/
# (set up the environment for that repo)
bash script.sh nerfsyn-nerfacc-occgrid 0
```

### 3rd-Party Usages:
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
206
207

#### Awesome Codebases.
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
208
- [nerfstudio](https://github.com/nerfstudio-project/nerfstudio): A collaboration friendly studio for NeRFs.
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
209
210
- [sdfstudio](https://autonomousvision.github.io/sdfstudio/): A unified framework for surface reconstruction.
- [threestudio](https://github.com/threestudio-project/threestudio): A unified framework for 3D content creation.
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
211
- [instant-nsr-pl](https://github.com/bennyguo/instant-nsr-pl): NeuS in 10 minutes.
212
- [modelscope](https://github.com/modelscope/modelscope/blob/master/modelscope/models/cv/nerf_recon_acc/network/nerf.py): A collection of deep-learning algorithms.
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
213
214

#### Awesome Papers.
215
- [Representing Volumetric Videos as Dynamic MLP Maps, CVPR 2023](https://github.com/zju3dv/mlp_maps)
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
216
- [NeRSemble: Multi-view Radiance Field Reconstruction of Human Heads, ArXiv 2023](https://tobias-kirschstein.github.io/nersemble/)
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
217
- [HumanRF: High-Fidelity Neural Radiance Fields for Humans in Motion, ArXiv 2023](https://synthesiaresearch.github.io/humanrf/)
218

Ruilong Li's avatar
Ruilong Li committed
219
## Common Installation Issues
220

Ruilong Li's avatar
Ruilong Li committed
221
222
223
224
<details>
    <summary>ImportError: .../csrc.so: undefined symbol</summary>
    If you are installing a pre-built wheel, make sure the Pytorch and CUDA version matchs with the nerfacc version (nerfacc.__version__).
</details>
225

226
227
228
## Citation

```bibtex
229
230
@article{li2023nerfacc,
  title={NerfAcc: Efficient Sampling Accelerates NeRFs.},
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
231
  author={Li, Ruilong and Gao, Hang and Tancik, Matthew and Kanazawa, Angjoo},
Ruilong Li's avatar
Ruilong Li committed
232
  journal={arXiv preprint arXiv:2305.04966},
233
  year={2023}
234
}
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
235
```