test_resampling.py 1.52 KB
Newer Older
1
2
3
import pytest
import torch

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
4
from nerfacc import pack_info, ray_marching, ray_resampling
Ruilong Li's avatar
Ruilong Li committed
5
from nerfacc.cuda import ray_pdf_query
6
7
8
9
10
11
12
13
14
15
16

device = "cuda:0"
batch_size = 128


@pytest.mark.skipif(not torch.cuda.is_available, reason="No CUDA device")
def test_resampling():
    rays_o = torch.rand((batch_size, 3), device=device)
    rays_d = torch.randn((batch_size, 3), device=device)
    rays_d = rays_d / rays_d.norm(dim=-1, keepdim=True)

Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
17
    ray_indices, t_starts, t_ends = ray_marching(
18
19
20
21
22
23
        rays_o,
        rays_d,
        near_plane=0.1,
        far_plane=1.0,
        render_step_size=1e-3,
    )
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
24
    packed_info = pack_info(ray_indices, n_rays=batch_size)
25
26
27
28
29
30
31
    weights = torch.rand((t_starts.shape[0],), device=device)
    packed_info, t_starts, t_ends = ray_resampling(
        packed_info, t_starts, t_ends, weights, n_samples=32
    )
    assert t_starts.shape == t_ends.shape == (batch_size * 32, 1)


Ruilong Li's avatar
Ruilong Li committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def test_pdf_query():
    rays_o = torch.rand((1, 3), device=device)
    rays_d = torch.randn((1, 3), device=device)
    rays_d = rays_d / rays_d.norm(dim=-1, keepdim=True)

    packed_info, t_starts, t_ends = ray_marching(
        rays_o,
        rays_d,
        near_plane=0.1,
        far_plane=1.0,
        render_step_size=0.2,
    )
    weights = torch.rand((t_starts.shape[0],), device=device)
    weights_new = ray_pdf_query(
        packed_info,
        t_starts,
        t_ends,
        weights,
        packed_info,
        t_starts + 0.3,
        t_ends + 0.3,
    )

55
56
if __name__ == "__main__":
    test_resampling()
Ruilong Li's avatar
Ruilong Li committed
57
    test_pdf_query()