"examples/vscode:/vscode.git/clone" did not exist on "ba2ca4be6d20f9e661d396a8b3b76182b960cfc5"
README.md 7.93 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

Matthew Tancik's avatar
Matthew Tancik committed
13
[![Core Tests.](https://github.com/KAIR-BAIR/nerfacc/actions/workflows/code_checks.yml/badge.svg)](https://github.com/KAIR-BAIR/nerfacc/actions/workflows/code_checks.yml)
14
[![Documentation Status](https://readthedocs.com/projects/plenoptix-nerfacc/badge/?version=latest)](https://www.nerfacc.com/en/latest/?badge=latest)
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

Nail Ibrahimli's avatar
Nail Ibrahimli committed
19
NerfAcc is a PyTorch Nerf acceleration toolbox for both training and inference. It focuses on efficient volumetric rendering of radiance fields, which is universal and plug-and-play for most of the NeRFs.
20
21
22

Using NerfAcc, 

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
23
- The `vanilla NeRF` model with 8-layer MLPs can be trained to *better quality* (+~0.5 PNSR)
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
24
  in *1 hour* rather than *days* as in the paper.
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
25
26
- The `Instant-NGP NeRF` model can be trained to *equal quality* in *4.5 minutes*,
  comparing to the official pure-CUDA implementation.
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
27
- The `D-NeRF` model for *dynamic* objects can also be trained in *1 hour*
28
  rather than *2 days* as in the paper, and with *better quality* (+~2.5 PSNR).
29
30
- Both *bounded* and *unbounded* scenes are supported.

Nail Ibrahimli's avatar
Nail Ibrahimli committed
31
**And it is a pure Python interface with flexible APIs!**
Ruilong Li's avatar
readme  
Ruilong Li committed
32

33
34
## Installation

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

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

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

47
48
49
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
50
# e.g., torch 1.13.0 + cu117
51
52
53
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
54
55
56
57
58
59
| Windows & Linux | `cu102` | `cu113` | `cu116` | `cu117` |
|-----------------|---------|---------|---------|---------|
| torch 1.10.0    | ✅      | ✅      |         |         |
| torch 1.11.0    | ✅*     | ✅      |         |         |
| torch 1.12.0    | ✅*     | ✅      | ✅      |         |
| torch 1.13.0    |         |         | ✅      | ✅      |
60
61
62

\* Pytorch does not support Windows pre-built wheels for those combinations thus we do not support as well.

63
64
## Usage

Nail Ibrahimli's avatar
Nail Ibrahimli committed
65
The idea of NerfAcc is to perform efficient ray marching and volumetric rendering. 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 radiance field.
66
67
68
- `sigma_fn`: Compute density at each sample. It will be used by `nerfacc.ray_marching()` to skip the empty and occluded space during ray marching, which is where the major speedup comes from. 
- `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 network.

Nail Ibrahimli's avatar
Nail Ibrahimli committed
69
A simple example is like this:
70
71
72
73
74
75
76
77
78

``` 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)
79
optimizer = ...  # optimizer
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

def sigma_fn(
    t_starts: Tensor, t_ends:Tensor, ray_indices: Tensor
) -> Tensor:
    """ Query density values from a user-defined radiance field.
    :params t_starts: Start of the sample interval along the ray. (n_samples, 1).
    :params t_ends: End of the sample interval along the ray. (n_samples, 1).
    :params ray_indices: Ray indices that each sample belongs to. (n_samples,).
    :returns The post-activation density values. (n_samples, 1).
    """
    t_origins = rays_o[ray_indices]  # (n_samples, 3)
    t_dirs = rays_d[ray_indices]  # (n_samples, 3)
    positions = t_origins + t_dirs * (t_starts + t_ends) / 2.0
    sigmas = radiance_field.query_density(positions) 
    return sigmas  # (n_samples, 1)

def rgb_sigma_fn(
    t_starts: Tensor, t_ends: Tensor, ray_indices: Tensor
) -> Tuple[Tensor, Tensor]:
    """ Query rgb and density values from a user-defined radiance field.
    :params t_starts: Start of the sample interval along the ray. (n_samples, 1).
    :params t_ends: End of the sample interval along the ray. (n_samples, 1).
    :params ray_indices: Ray indices that each sample belongs to. (n_samples,).
    :returns The post-activation rgb and density values. 
        (n_samples, 3), (n_samples, 1).
    """
    t_origins = rays_o[ray_indices]  # (n_samples, 3)
    t_dirs = rays_d[ray_indices]  # (n_samples, 3)
    positions = t_origins + t_dirs * (t_starts + t_ends) / 2.0
    rgbs, sigmas = radiance_field(positions, condition=t_dirs)  
    return rgbs, sigmas  # (n_samples, 3), (n_samples, 1)

# Efficient Raymarching: Skip empty and occluded space, pack samples from all rays.
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
113
# ray_indices: (n_samples,). t_starts: (n_samples, 1). t_ends: (n_samples, 1).
114
with torch.no_grad():
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
115
    ray_indices, t_starts, t_ends = nerfacc.ray_marching(
116
117
118
        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, 
    )
119
120
121

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

126
# Optimize: Both the network and rays will receive gradients
127
128
129
130
131
132
optimizer.zero_grad()
loss = F.mse_loss(color, color_gt)
loss.backward()
optimizer.step()
```

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
133
## Examples: 
Ruilong Li's avatar
readme  
Ruilong Li committed
134

Nail Ibrahimli's avatar
Nail Ibrahimli committed
135
Before running those example scripts, please check the script about which dataset it is needed, and download the dataset first.
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
136

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
137
```bash
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
138
139
# clone the repo with submodules.
git clone --recursive git://github.com/KAIR-BAIR/nerfacc/
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
140
```
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
141

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
142
``` bash
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
143
# Instant-NGP NeRF in 4.5 minutes with reproduced performance!
144
# See results at here: https://www.nerfacc.com/en/latest/examples/ngp.html
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
145
python examples/train_ngp_nerf.py --train_split train --scene lego
Ruilong Li's avatar
readme  
Ruilong Li committed
146
147
```

Ruilong Li's avatar
Ruilong Li committed
148
``` bash
149
# Vanilla MLP NeRF in 1 hour with better performance!
150
# See results at here: https://www.nerfacc.com/en/latest/examples/vanilla.html
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
151
python examples/train_mlp_nerf.py --train_split train --scene lego
Ruilong Li's avatar
Ruilong Li committed
152
153
```

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
154
```bash
155
# D-NeRF for Dynamic objects in 1 hour with better performance!
156
# See results at here: https://www.nerfacc.com/en/latest/examples/dnerf.html
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
157
python examples/train_mlp_dnerf.py --train_split train --scene lego
158
159
```

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
160
```bash
161
# Instant-NGP on unbounded scenes in 20 minutes!
162
# See results at here: https://www.nerfacc.com/en/latest/examples/unbounded.html
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
163
python examples/train_ngp_nerf.py --train_split train --scene garden --auto_aabb --unbounded --cone_angle=0.004
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
164
```
165

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
166
167
168
169
Used by:
- [nerfstudio](https://github.com/nerfstudio-project/nerfstudio): A collaboration friendly studio for NeRFs.
- [instant-nsr-pl](https://github.com/bennyguo/instant-nsr-pl): NeuS in 10 minutes.

170

Ruilong Li's avatar
Ruilong Li committed
171
## Common Installation Issues
172
173


Ruilong Li's avatar
Ruilong Li committed
174
175
176
177
<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>
178

179
180
181
182
183
184
185
186
187
## Citation

```bibtex
@article{li2022nerfacc,
  title={NerfAcc: A General NeRF Accleration Toolbox.},
  author={Li, Ruilong and Tancik, Matthew and Kanazawa, Angjoo},
  journal={arXiv preprint arXiv:2210.04847},
  year={2022}
}
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
188
```