v9.1.1
Menu

NeRF Positional Encoding

mediumPRONeural 3D RepresentationsNeural 3DCV: 3D Reconstruction

Apply positional encoding for Neural Radiance Fields (NeRF).

NeRF uses positional encoding to help networks learn high-frequency details. A scalar position pp is encoded as:

γ(p)=(sin⁡(20πp),cos⁡(20πp),sin⁡(21πp),cos⁡(21πp),...,sin⁡(2L−1πp),cos⁡(2L−1πp))\gamma(p) = (\sin(2^0 \pi p), \cos(2^0 \pi p), \sin(2^1 \pi p), \cos(2^1 \pi p), ..., \sin(2^{L-1} \pi p), \cos(2^{L-1} \pi p))

This maps a single coordinate to a 2L2L-dimensional vector. Higher frequencies capture fine details while lower frequencies capture smooth variations.

For 3D points, we apply this independently to x, y, z coordinates.

Example

Input
positional_encoding(0.5, 2)
Output
[1.0, 0.0, 0.0, -1.0]
Reasoning
  • Inputs: value =0.5= 0.5 is one scalar coordinate (say the xx of a 3D point) and L =2= 2 is the number of frequency bands, so the output has 2L=42L = 4 entries: one sine and one cosine per band.
  • Band 0 has angular frequency 20π=π2^0 \pi = \pi. The angle is π⋅0.5=π/2\pi \cdot 0.5 = \pi/2, giving sin⁡π2=1\sin \tfrac{\pi}{2} = 1 and cos⁡π2=0\cos \tfrac{\pi}{2} = 0.
  • Band 1 doubles the frequency to 21π=2π2^1 \pi = 2\pi. The angle is 2π⋅0.5=π2\pi \cdot 0.5 = \pi, giving sin⁡π=0\sin \pi = 0 and cos⁡π=−1\cos \pi = -1.
  • Output: the pairs in band order, sine before cosine: [1.0, 0.0, 0.0, -1.0]. In floating point the zeros are tiny values near 10−1610^{-16}, which show as 0.00.0 once rounded to 4 decimal places.
  • Notice that each band doubles the angle, so higher bands swing faster as value changes; that is what lets the network capture fine detail. With value =0= 0 every angle is 00, giving [0.0, 1.0, 0.0, 1.0].

Constraints

  • value: scalar position to encode
  • L: number of frequency bands
  • Return encoded vector of length 2L

Learn this in

More from CV: 3D Reconstruction

PRO

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test results

Sign in to run your code against the test cases.