Maps to: UC Berkeley CS294-158 Deep Unsupervised Learning (Spring 2024), Lecture 13b · Thu 4/18/2024 · Neural Radiance Fields (guest lecture: Ben Mildenhall). Basis: The slide link on the course page is broken (it returns an error page), so this lecture is written from the papers. These are the work of the guest speaker and his collaborators and of the line of research the talk is known to cover:
- Core method: NeRF (Mildenhall et al., ECCV 2020) and Fourier features (Tancik et al., NeurIPS 2020).
- Extensions: mip-NeRF (Barron et al., ICCV 2021), Instant-NGP (Müller et al., 2022), 3D Gaussian splatting (Kerbl et al., 2023).
- Generative 3-D: GRAF (Schwarz et al., 2020), Dream Fields (Jain et al., 2022) and DreamFusion (Poole et al., 2022).
The code builds an analytic 3-D scene whose true images are known from every viewpoint, so every rendering error is exact. It then:
- renders the scene by volume rendering;
- measures spectral bias and positional encoding;
- trains tiny NeRFs from 4–32 views;
- makes a 3-D field from 2-D information alone by score distillation with an exact denoiser.
Code:
code/lecture25/(4 scripts +nerf.py; PyTorch on CPU; script 03 trains 7 NeRFs on the first run, everything else takes minutes) Reading time: 2.5 hours including running the code
0. How to read this lecture
0.1 Learning goals
After this lecture you should be able to:
- Volume rendering. Derive the volume rendering integral and NeRF’s quadrature (alpha compositing). Explain transmittance, weights, expected depth, and stratified and hierarchical sampling.
- Positional encoding. Explain spectral bias and why coordinate networks need a positional encoding or Fourier features. Choose the encoding’s bandwidth.
- NeRF. Describe NeRF’s architecture and training, what view dependence buys, and why few views are hard. Place mip-NeRF, Instant-NGP and Gaussian splatting.
- Generative 3-D. Explain how a generative model of 3-D can come from 2-D: GRAF’s adversarial NeRF, Dream Fields’ CLIP loss, and DreamFusion’s score distillation. Explain the Janus problem and why view-dependent conditioning fixes it.
0.2 Prerequisites and scope
| Assumed | Introduced here | Elsewhere |
|---|---|---|
| Lecture 11–12 (diffusion, denoisers, exact denoisers, score distillation through a 2-D renderer in Lecture 12) | volume rendering, positional encoding, NeRF | — |
| Lecture 15 / 22 (CLIP) | GRAF, Dream Fields, DreamFusion, the Janus problem | |
| Basic projective geometry (pinhole camera) |
0.3 Map
§1 novel-view synthesis: why a neural field
§2 volume rendering: the integral, its quadrature, sampling ← code 01
§3 spectral bias and positional encoding ← code 02
§4 NeRF: architecture, training, views, and what came next ← code 03
§5 generative 3-D: GRAF, Dream Fields, DreamFusion and score distillation ← code 04
§6 summary §7 pitfalls §8 exercises §9 further reading §10 the end of the course
0.4 Notation
| symbol | meaning |
|---|---|
| a ray from camera center in unit direction | |
| volume density | |
| emitted color | |
| transmittance | |
| compositing weights | |
| PSNR | in dB, for images in |
1. Novel-view synthesis: why a neural field?
Given photos of a scene from known cameras, render it from a new camera. This problem goes back decades, through several representations:
- light fields (densely sampled rays);
- image-based rendering (warp nearby photos);
- multi-plane images;
- voxel grids (memory grows as resolution³).
NeRF’s idea (Mildenhall et al., 2020): represent the scene as a continuous function , an MLP. Render it with differentiable volume rendering, and fit the MLP so that its renderings reproduce the photos. Three properties matter:
- Compact. A few MB of weights instead of a dense voxel grid.
- Continuous. Query at any resolution, without a grid.
- Differentiable end to end. The only supervision needed is the photos and their camera poses (estimated by structure-from-motion, e.g. COLMAP).
In this course’s language, NeRF is not a generative model: it over-fits one scene. It enters a course on unsupervised learning because it is the representation that 3-D generative models build on (§5).
2. Volume rendering
2.1 The integral
Model the scene as a cloud of particles with density : the probability of a ray being stopped in is . The probability that the ray travels from the camera to without being stopped is the transmittance:
This follows from . The expected color of the ray is a probability-weighted average of the colors where the ray stops, plus the background if it never stops.
2.2 The quadrature: alpha compositing
Split the ray at depths with gaps , and assume and are constant in each interval. Within interval the ray is stopped with probability Integrating exactly under that assumption gives
This is front-to-back alpha compositing of semi-transparent slabs, the “over” operator of computer graphics. It is differentiable in every and . The same weights give an expected depth and an opacity for free.
One ray, measured (script 01, Part 1). The center pixel of a view hits the red sphere. Density rises from 0 to ~15 between depths 2.97 and 3.43. The weights peak at 0.228 near depth 3.13, where the ray enters the solid, and vanish behind it ( falls to 0.002): occlusion falls out of the formula. The pixel’s opacity is 0.998, its expected depth 3.166. Compositing front-to-back and back-to-front gives the same color, to .
2.3 How many samples? (Part 2)
Here is how the quadrature converges, measured as PSNR against a 1,024-sample reference (4 views, 32 × 32):
| samples per ray | uniform midpoints | stratified (one draw) | hierarchical () |
|---|---|---|---|
| 8 | 19.7 | 18.2 | 19.1 |
| 16 | 27.6 | 24.1 | 32.7 |
| 32 | 38.0 | 34.5 | 41.3 |
| 64 | 47.9 | 44.2 | 48.1 |
| 128 | 54.2 | 51.3 | 54.3 |
| 256 | 60.7 | 58.2 | 60.2 |
- Midpoints of equal bins converge fastest for a fixed scene but always query the same depths.
- Stratified sampling (one random depth per bin, NeRF’s choice for training) is slightly worse for one image. But over training the network is queried at every depth, so it learns a continuous field instead of the field’s values at fixed depths.
- Hierarchical sampling spends a second batch of samples by inverse-transform sampling the first pass’s weights: where the ray actually hits something. It is the most accurate at a small budget (16–32 samples). NeRF trains a “coarse” and a “fine” network this way.
3. Spectral bias and positional encoding
An MLP fed raw coordinates learns smooth functions. Deep networks fit low frequencies first and high frequencies only slowly (spectral bias, Rahaman et al. 2019). Fine geometry and texture are exactly the high frequencies.
The fix: NeRF maps each coordinate through with for positions and for directions.
Why it works (Tancik et al., 2020). In the neural-tangent-kernel limit, an MLP on behaves like kernel regression with a stationary kernel whose bandwidth is set by the encoding’s frequencies.
- Random Fourier features with make that bandwidth one tunable knob.
- Too small : blurry.
- Too large : the kernel is so narrow that the network interpolates noise between training points.
Measured (script 02). Part 1 fits a 1-D target with . The table gives the residual amplitude per frequency (1 = nothing learned):
| input | steps | freq 1 | freq 4 | freq 16 | freq 48 |
|---|---|---|---|---|---|
| raw | 100 | 0.05 | 0.81 | 0.99 | 1.00 |
| raw | 300 | 0.01 | 0.01 | 0.92 | 0.99 |
| raw | 3000 | 0.01 | 0.03 | 0.69 | 0.93 |
| NeRF encoding, 8 frequencies | 100 | 0.00 | 0.00 | 0.00 | 0.01 |
| Fourier features, | 100 | 0.00 | 0.00 | 0.00 | 0.00 |
On raw , frequency 1 is fitted within 100 steps and frequency 4 within 300. Frequencies 16 and 48 are still 69% and 93% unfitted after 3,000 steps: spectral bias, measured. With either encoding, every component is fitted within 100 steps.
Part 2 regresses a 64 × 64 rendering from , training on 25% of the pixels (every other row and column):
| input encoding | input dim | train PSNR | held-out PSNR |
|---|---|---|---|
| raw | 2 | 35.9 | 32.4 |
| NeRF encoding, 4 frequencies | 18 | 59.6 | 33.6 |
| NeRF encoding, 8 frequencies | 34 | 63.6 | 14.5 |
| Fourier features, | 256 | 63.5 | 37.0 |
| Fourier features, | 256 | 74.4 | 31.6 |
| Fourier features, | 256 | 63.1 | 9.6 |
| Fourier features, | 256 | 50.2 | 9.3 |
The rendering is simple (flat colored solids), so raw coordinates already reach 32.4 dB on held-out pixels, with blurred edges. The best encoding is Fourier features with (37.0 dB).
The informative failures are at the top of the frequency range. Encodings with frequencies the training grid cannot determine fit the training pixels almost perfectly but alias in between: 8 NeRF frequencies give 14.5 dB, and give 9.6 and 9.3. With every other pixel given, a frequency above 32 cycles per image is invisible in the data, and the network fills it with anything.
The encoding’s bandwidth must match both the signal and the sampling. NeRF’s 10 frequencies suit dozens of dense, overlapping views (and mip-NeRF’s integrated encoding damps the frequencies a pixel cannot resolve).
4. NeRF
4.1 The model and its training
NeRF’s MLP has eight 256-wide layers, with a skip connection.
- The density depends on position only.
- The color depends on position and viewing direction: a feature vector is concatenated with . This lets a surface look different from different angles (specular highlights) while keeping the geometry view-independent.
Training. Each step samples a batch of rays from all training images and renders them with the coarse and fine passes. It minimizes . There is no 3-D supervision of any kind.
Cost. The original NeRF needed a day of GPU time per scene.
4.2 Measured (script 03)
A tiny NeRF (4 × 128 MLP, 6 frequencies, 48 stratified samples per ray, 1,500 steps of 1,024 rays). Images are 32 × 32, the cameras sit on a ring at two elevations, and there are 7 held-out views:
| training views | PSNR, training views | PSNR, 7 novel views | depth error, novel views |
|---|---|---|---|
| 4 | 47.3 | 8.4 | 1.329 |
| 8 | 42.3 | 36.6 | 0.017 |
| 16 | 42.2 | 39.2 | 0.014 |
| 32 | 41.8 | 41.1 | 0.014 |
Four views are not enough. The NeRF reproduces its 4 training views almost perfectly (47.3 dB) and is useless anywhere else (8.4 dB, depth off by 1.3 units). With nothing constraining the space between 90°-apart cameras, the optimization finds some density that explains each photo separately: a per-scene fit with no prior has no reason to find the scene.
From 8 views on, novel views are almost as good as training views (36.6 → 41.1 dB), and the rendered depth is right to 0.014 units. The gap between training and novel PSNR shrinks as views are added, the signature of over-fitting disappearing. This is why few-view NeRF needs priors (pixelNeRF, RegNeRF, diffusion priors).
Ablations with 16 views:
| model | PSNR, training views | PSNR, novel views | depth error |
|---|---|---|---|
| positional encoding (6 frequencies), 48 samples | 42.2 | 39.2 | 0.014 |
| no positional encoding (raw ) | 34.6 | 34.1 | 0.109 |
| + view-dependent color (direction encoded) | 42.1 | 39.4 | 0.015 |
| hierarchical sampling, 24 coarse + 24 fine | 41.9 | 38.7 | 0.015 |
- Without positional encoding the NeRF is blurry: 34.1 dB, and depth errors 8× larger. Spectral bias again, now in 3-D.
- View-dependent color changes nothing here (39.4 vs 39.2 dB): our scene’s colors do not depend on the viewing direction, so the extra input has nothing to explain. In real scenes it captures specular highlights.
- Hierarchical sampling with the same total budget (24 + 24) does not beat 48 stratified samples on this compact scene (38.7 dB). Its advantage, which script 01 showed for the quadrature, matters when the scene occupies a small part of a long ray, as in real captures with distant backgrounds.
4.3 What came next (context)
- mip-NeRF (Barron et al., 2021) renders cones instead of rays. It encodes a whole Gaussian region of space with an integrated positional encoding (high frequencies are damped where the cone is wide), which removes aliasing across scales. Zip-NeRF combines this with grids.
- Instant-NGP (Müller et al., 2022) replaces most of the MLP with a multiresolution hash table of learned features: a positional encoding that is learned per scene. Training drops from a day to seconds.
- 3D Gaussian splatting (Kerbl et al., 2023) drops the neural field. It optimizes millions of explicit 3-D Gaussians with colors and opacities, and rasterizes them by alpha compositing, the same formula as §2.2, sorted by depth. It renders in real time.
- Few-view and generalizable NeRFs (pixelNeRF, RegNeRF and others) add priors learned across scenes. That is where NeRF meets this course’s generative models.
5. Generative 3-D
A NeRF fits one scene from many posed photos. A generative 3-D model should produce new scenes, ideally learned from 2-D images only, since 3-D data are scarce. Three routes:
GRAF / π-GAN / EG3D (adversarial). The generator is a NeRF conditioned on a latent code (shape and appearance). Its renderings from random cameras go to a 2-D GAN discriminator that compares them with real photos (Lectures 9–10). Multi-view consistency is built in, because every image is a rendering of one 3-D field. EG3D makes this efficient with a tri-plane representation.
Dream Fields (CLIP). Optimize one NeRF so that its renderings from random cameras have a high CLIP score for a caption, plus a transparency penalty. Lecture 22 showed that pixel-level CLIP guidance makes adversarial patterns. The NeRF’s 3-D structure and the augmentations are what keep Dream Fields’ renderings reasonable.
DreamFusion (score distillation). Use a pretrained text-to-image diffusion model as the critic. For a rendering :
- noise it: ;
- let the diffusion model predict the noise, ;
- update the NeRF along
The diffusion model’s Jacobian is dropped, which is why it is cheap. Up to that approximation, this is the gradient of a weighted sum over noise levels of : it pushes every rendering toward images the prior finds likely (Lecture 12 used the same gradient on a 2-D superellipse renderer).
The Janus problem. A text-to-image prior does not know which view it is looking at. Most training images of “a dog” show its face, so SDS pushes every view toward a face, producing multi-faced objects. DreamFusion’s fix is view-dependent prompting: append “front view” / “side view” / “back view” to the prompt according to the camera’s azimuth.
Measured (script 04). The prior is the exact optimal denoiser of a finite set of images, the 24 true views of the scene at 16 × 16 (Lecture 11’s exact denoisers: is a softmax-weighted average of the set’s images). A small NeRF is trained from scratch, three ways (3 seeds):
| how the field is trained | PSNR vs the true view at that azimuth | PSNR vs the closest of the 24 images |
|---|---|---|
| reconstruction (posed images, photometric loss) | 33.4 ± 0.3 | 33.4 |
| SDS, view-conditioned prior (images within ±15° of the camera) | 27.6 ± 1.2 | 27.6 |
| SDS, view-agnostic prior (all 24 images) | 15.7 ± 0.1 | 18.3 |
Which image does the render at each azimuth resemble most (seed 0)?
| render azimuth | 0 | 30 | 60 | 90 | 120 | 150 | 180 | 210 | 240 | 270 | 300 | 330 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| reconstruction | 0 | 30 | 60 | 90 | 120 | 150 | 180 | 210 | 240 | 270 | 300 | 330 |
| SDS, view-conditioned | 0 | 30 | 60 | 90 | 120 | 150 | 180 | 210 | 240 | 270 | 300 | 330 |
| SDS, view-agnostic | 195 | 195 | 255 | 195 | 255 | 255 | 255 | 195 | 195 | 255 | 195 | 195 |
- Score distillation creates a 3-D field from a purely 2-D prior. No camera was ever paired with a target image. With the view-conditioned prior, every render resembles the true view of its own azimuth (27.6 dB, 6 dB short of reconstruction from posed images).
- The view-agnostic prior gives the Janus problem in an extreme form. The field became rotationally symmetric: a dark band of mixed colors on the floor disk that looks the same from every camera (figure
04_sds.png). By PSNR its renders are closest to the views from 195° and 255° wherever the camera is, but they are poor copies even of those (18.3 dB). Every camera is pulled toward some image of the set, and the only 3-D field that can satisfy “look like a typical image from every side” is one that looks alike from every side. With a text-to-image prior, whose typical image of an animal shows its face, the same pull produces the familiar multi-faced objects.
The SDS gradient is mode-seeking (Exercise 5): it pulls each render toward whichever prior image is nearest, not toward the one that belongs to the camera. Conditioning the prior on the view, DreamFusion’s “front / side / back view” prompts, is what makes the 3-D object consistent.
6. Summary
- Volume rendering turns a density and color field into pixels by alpha compositing along rays: . It is differentiable, handles occlusion automatically, and gives depth and opacity for free. The quadrature converged to 41 dB at 32 samples with hierarchical sampling.
- Spectral bias makes MLPs on raw coordinates blurry. On raw input, frequency 16 was still 69% unfitted after 3,000 steps; with an encoding, everything was fitted in 100. Positional encodings and Fourier features fix this, but their bandwidth must match the signal and the sampling: 9–15 dB held-out with too many frequencies, 37 dB at the right scale.
- NeRF fits one scene from posed photos. Ours reached 39–41 dB on novel views from 16–32 views, and failed completely from 4 (8.4 dB) despite a near-perfect fit of those 4. Without positional encoding: 34 dB. Successors made it anti-aliased (mip-NeRF), fast (Instant-NGP) and real-time (Gaussian splatting).
- Generative 3-D comes from 2-D models: GRAF (a GAN over renderings), Dream Fields (CLIP), DreamFusion (score distillation). SDS with an exact view-conditioned prior built a consistent 3-D field from no paired data (27.6 dB). A view-agnostic prior produced a field that looks alike from every side (15.7 dB): the Janus problem, cured by conditioning the prior on the view.
7. Common pitfalls
- Too few samples per ray, or a fixed grid of depths. Both alias. Stratify during training, and use hierarchical sampling to spend samples where the weights are.
- Raw coordinates into an MLP. The result is blurry (spectral bias). Encode them, but match the encoding’s bandwidth to the data: too many frequencies alias between training samples.
- Few or badly posed views. NeRF is a per-scene optimization with no prior. With few views it fits the training photos and hallucinates the rest; with wrong camera poses nothing is consistent.
- Expecting a NeRF to be a generative model. It reproduces one scene; generation needs a latent code with a learned distribution (GRAF) or an external prior (SDS).
- Score distillation with a view-agnostic prior. Every view is pushed toward the prior’s most typical image, producing Janus objects. Condition the prior on the camera.
- Confusing a soft density with hard surfaces. A density field accumulates opacity before the surface (our scene’s solids look slightly “fat”). Surface-based variants (NeuS, VolSDF) control this.
8. Exercises
Exercise 1 (transmittance — derivation). From “the probability of being stopped in is ”, derive . Show that .
Answer
Transmittance. , so and with .
The integral. , so . The weights of a pixel sum to its opacity, and the remaining is the background’s share.
Exercise 2 (the quadrature is exact for piecewise-constant fields — derivation). Show that if and are constant on each interval , the integral equals exactly.
Answer
On interval , . Then and . Summing over intervals gives the formula. The only approximation is treating the field as piecewise constant, hence the convergence in §2.3.
Exercise 3 (front-to-back = back-to-front — derivation). Show that applied from down to 1 (starting from the background) gives the same color as the front-to-back sum.
Answer
Unroll the recursion: These are exactly and . Script 01 matches the two orders to .
Exercise 4 (Fourier features as a kernel — derivation). For , show that . Conclude that the kernel is stationary and that its width shrinks as grows when .
Answer
Stationarity. with and . The result depends only on : the kernel is stationary.
Width. In expectation over , : a Gaussian kernel of width . A large means each training point influences only a tiny neighbourhood, so between training points the prediction is unconstrained (script 02’s aliasing).
Exercise 5 (SDS — derivation). Show that if the prior were a single image (the denoiser returns ), the SDS update with moves the rendering along in expectation.
Answer
, so Gradient descent therefore moves toward with step .
With a finite set, is replaced by a posterior-weighted average of the set’s images. At low noise that average collapses to the image nearest to : SDS is mode-seeking. With a view-agnostic set, the nearest image need not be the view of the current camera, hence the Janus result.
Exercise 6 (coding). In script 04, widen the view-conditioned window from ±15° to ±45°. Predict the effect on “PSNR vs the true view”, then run it.
Answer
(Run it.) A wider window lets each camera be pulled toward images taken up to 45° away, so the views blur together and start to disagree. Expect consistency to degrade between the ±15° result and the view-agnostic one.
9. Further reading
- B. Mildenhall, P. Srinivasan, M. Tancik, J. Barron, R. Ramamoorthi, R. Ng. NeRF: Representing Scenes as Neural Radiance Fields for View Synthesis, ECCV 2020.
- M. Tancik et al. Fourier Features Let Networks Learn High Frequency Functions in Low Dimensional Domains, NeurIPS 2020; N. Rahaman et al. On the Spectral Bias of Neural Networks, ICML 2019.
- J. Barron et al. Mip-NeRF (ICCV 2021), Mip-NeRF 360 (CVPR 2022), Zip-NeRF (ICCV 2023).
- T. Müller et al. Instant Neural Graphics Primitives with a Multiresolution Hash Encoding, SIGGRAPH 2022.
- B. Kerbl et al. 3D Gaussian Splatting for Real-Time Radiance Field Rendering, SIGGRAPH 2023.
- K. Schwarz et al. GRAF (NeurIPS 2020); E. Chan et al. π-GAN (CVPR 2021), EG3D (CVPR 2022).
- A. Jain et al. Zero-Shot Text-Guided Object Generation with Dream Fields, CVPR 2022.
- B. Poole, A. Jain, J. Barron, B. Mildenhall. DreamFusion: Text-to-3D using 2D Diffusion, ICLR 2023.
- J. Kajiya, B. Von Herzen. Ray Tracing Volume Densities, SIGGRAPH 1984; N. Max. Optical Models for Direct Volume Rendering, 1995 (the rendering equation used here).
- P. Wang et al. NeuS (2021); L. Yariv et al. VolSDF (2021) — densities derived from signed distance functions.
10. The end of the course
This was the last lecture. The course went from 1-D histograms (Lecture 2) to:
- autoregressive models, flows, VAEs, GANs and diffusion (Lectures 3–12), each measured against the exact distribution of ShapeWorld;
- self-supervised representations (Lectures 13–15);
- large language and video models (Lectures 16–17);
- semi-supervised learning and unsupervised alignment (18–19);
- compression, where likelihood becomes a file size (20–21);
- multimodal models and the systems that train them (22–23);
- two frontiers (24–25).
One thread ran through all of it: a model of the data is a measurable object, whether as bits per example, as samples that are valid or not, or as features that make the world’s factors explicit. Where the truth was known, every claim could be checked, and many of the checks surprised us. The “plan vs reality” section of every README records them.