Skip to content

Bharath2/NeRF.cpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NeRF.cpp

C++ / LibTorch implementation of NeRF. Given RGB images of a static scene with known camera intrinsics and per-image extrinsics, fits a SIREN-encoded radiance field and renders RGB and depth from arbitrary novel viewpoints.

License: BSD-3-Clause C++20 LibTorch CUDA

RGB orbit Depth orbit
RGB Depth

Pipeline

  1. Ray generation. For each pixel the camera-space direction ((x − W/2)/f, −(y − H/2)/f, −1) is rotated into world space by the extrinsics; the camera origin is the translation column.
  2. Sampling. Stratified jittered sampling between near and far bounds: the interval is split into bins and one sample is drawn uniformly inside each bin.
  3. SIREN encoding. Each input coordinate is passed through its own SirenLayer, replacing NeRF's Fourier positional encoding. Encoded features for the three axes are flattened per ray sample.
  4. MLP. Sine-activated hidden layers feed a softplus σ head and a sigmoid RGB head; the RGB head additionally consumes the view-direction features.
  5. Volume render. Discretised quadrature of σ along the ray gives per-sample α and transmittance; the resulting weights composite RGB and depth, alpha-composited onto a configurable background.
  6. Loss. Photometric MSE or pseudo-Huber on the rendered image (selectable via LossType in src/main.cpp) plus a regulariser of the form ∑ exp(−σᵢ²/c) on the std σᵢ of the first-layer SIREN encoder weights, pushing them away from zero to keep the encoded frequency set diverse.

Training uses Adam. Network width/depth, sample count, batch size, learning rate, and iteration count live as constants in src/main.cpp and the SirenNeRF constructor.

Results

Trained and evaluated on the NeRF synthetic dataset (held-out test split).

Scene PSNR ↑ RMSE ↓ SSIM ↑
lego
ship

Build

Requires a C++20 compiler, CMake ≥ 3.20, LibTorch (with CUDA if GPU training is wanted), and nlohmann_json.

mkdir build && cd build
cmake ..
make

Run

./NeRF.cpp <data_path> <output_path>

load_dataset reads <data_path>/transforms.json — a shared camera_angle_x (FoV) and a transform_matrix (4×4 camera-to-world) per frame — plus the referenced image files. Scenes from the NeRF synthetic dataset (e.g. lego, ship) work directly.

References

License

BSD 3-Clause.

About

NeRF implementation in C++ for 3D reconstruction and Novel View Synthesis using LibTorch.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages