CS180 — Project 4: Neural Radiance Fields

In this project, we build an end-to-end nerf pipeline that is capable of reconstructing a 3D view of an object from a series of 2D pictures.


Part 0: Calibration, Scanning, and Dataset Packaging

Before training a NeRF, we need accurate camera poses for each input image. To obtain these, we use ArUco marker-based calibration. ArUco tags are fiducial markers with known geometry that can be reliably detected in images. By placing a printed ArUco board in the scene and detecting the markers across multiple frames, we can solve the Perspective-n-Point (PnP) problem to recover the camera's intrinsic parameters (focal length, principal point, distortion coefficients) and extrinsic parameters (rotation and translation relative to the board).

The PnP problem estimates the pose of a calibrated camera given a set of 3D-to-2D point correspondences. OpenCV's cv2.aruco module handles marker detection, and cv2.calibrateCamera jointly optimizes intrinsics and per-frame extrinsics. From the resulting rotation vectors and translation vectors, we construct a camera-to-world (c2w) matrix for each frame, which is the representation NeRF expects.

Calibrated camera poses visualization 1 Calibrated camera poses visualization 2
Visualizing the calibrated camera poses with Viser confirms consistent poses before NeRF training.

Part 1: Neural Field for a 2D Image

Before tackling the full 3D NeRF, we first build a simpler 2D neural field that learns to map pixel coordinates (u, v) to RGB color values. This serves as a warm-up to understand the key components: positional encoding, network architecture, and training loop.

A naive MLP that takes raw (u, v) coordinates as input struggles to capture high-frequency details in the image. This is because neural networks have a spectral bias toward learning low-frequency functions. To overcome this, we use positional encoding, which maps low-dimensional input coordinates into a higher-dimensional space using sinusoidal functions at multiple frequencies. This allows the network to represent fine details and sharp edges.

The network architecture is a simple MLP with sinusoidal positional encoding on the input coordinates. We use a learning rate of 1e-2 with the Adam optimizer and train for 2000 iterations.

2D NeRF architecture diagram
Architecture diagram for the 2D neural field.

Cat Image Training Progression

Cat iteration 1 Cat iteration 15 Cat iteration 50 Cat iteration 200 Cat iteration 2000
Training progression on the cat image. Taken at iterations 1, 15, 50, 200 and 2000.

Emir Image Training Progression

Emir iteration 1 Emir iteration 200 Emir iteration 400 Emir iteration 1000 Emir iteration 2000
Training progression on the Emir image. Taken at iterations 1, 200, 400, 1000 and 2000.

PSNR Curve

PSNR curve for cat image
PSNR curve over training iterations for the cat image. Higher PSNR indicates better reconstruction quality.

Hyperparameter Sweep

We conduct a hyperparameter sweep over different positional encoding frequencies and network widths to understand how these choices affect the quality of the learned 2D neural field.

Hyperparameter sweep grid
Hyperparameter sweep showing the effect of different positional encoding frequencies and network sizes on reconstruction quality.

Part 2: Multi-view Neural Radiance Field on Lego

Now we extend from 2D to 3D. A Neural Radiance Field (NeRF) represents a scene as a continuous volumetric function that maps a 3D position (x, y, z) and viewing direction (theta, phi) to a color (r, g, b) and volume density (sigma). By querying this function along camera rays and applying volume rendering, we can synthesize novel views of the scene.

The camera-to-world (c2w) matrix is a 4x4 transformation that converts points from camera coordinates to world coordinates. Given pixel coordinates and camera intrinsics, we can compute ray origins and directions in world space. For each pixel, we cast a ray and sample points along it.

We implement a dataloader that randomly samples rays from the training images. For each ray, we sample points along it using stratified sampling, adding uniform noise to the sample positions to prevent aliasing. The sampled 3D coordinates are passed through positional encoding and fed into the MLP.

Volume rendering is the process of accumulating color and density along each ray to produce a final pixel color. For each sample point along a ray, the network predicts a color and density. The final pixel color is computed as a weighted sum of the sample colors, where the weights depend on the accumulated transmittance (how much light has been absorbed so far) and the density at each point.

Network Architecture

3D NeRF architecture diagram
Architecture diagram for the multi-view NeRF. The network takes positional-encoded 3D coordinates and viewing directions as input, and outputs RGB color and volume density.

Ray Visualization

Ray visualization
Visualization of sampled rays from multiple camera viewpoints, showing how rays are cast through the scene.

Lego Training Progression

Lego iteration 1 Lego iteration 50 Lego iteration 400 Lego iteration 1000 Lego iteration 2000
Training progression on the Lego dataset. Taken at iterations 1, 50, 400, 1000 and 2000.

Lego PSNR Curve

Lego PSNR curve
PSNR curve over training iterations for the Lego dataset.

Lego 3D Render

Lego 3D render rotating around z-axis
Novel view synthesis of the Lego scene, rendered by rotating the camera around the object.

Custom Dataset: Stadium

We also train NeRF on our own captured dataset of a stadium scene, using the camera poses obtained from the ArUco calibration in Part 0.

Stadium Training Progression

Stadium iteration 1 Stadium iteration 2 Stadium iteration 3 Stadium iteration 4 Stadium iteration 5
Training progression on the custom stadium dataset.

Stadium PSNR Curve

Stadium PSNR curve
PSNR curve over training iterations for the stadium dataset.

Stadium 3D Render

Stadium 3D render orbit
Novel view synthesis of the stadium scene, rendered by orbiting the camera around the object.