test_rendering.py 1.74 KB
Newer Older
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
1
2
3
4
import torch
import tqdm

from nerfacc import (
5
    unpack_to_ray_indices,
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    volumetric_marching,
    volumetric_rendering_accumulate,
    volumetric_rendering_steps,
    volumetric_rendering_weights,
)

device = "cuda:0"


def test_rendering():
    scene_aabb = torch.tensor([0, 0, 0, 1, 1, 1], device=device).float()
    scene_occ_binary = torch.ones((128 * 128 * 128), device=device).bool()
    rays_o = torch.rand((10000, 3), device=device)
    rays_d = torch.randn((10000, 3), device=device)
    rays_d = rays_d / rays_d.norm(dim=-1, keepdim=True)

    for step in tqdm.tqdm(range(1000)):
23
        (packed_info, frustum_starts, frustum_ends,) = volumetric_marching(
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
            rays_o,
            rays_d,
            aabb=scene_aabb,
            scene_resolution=[128, 128, 128],
            scene_occ_binary=scene_occ_binary,
        )

        sigmas = torch.rand_like(frustum_ends[:, :1], requires_grad=True) * 100

        (
            packed_info,
            frustum_starts,
            frustum_ends,
        ) = volumetric_rendering_steps(
            packed_info,
            sigmas,
            frustum_starts,
            frustum_ends,
        )
43
        ray_indices = unpack_to_ray_indices(packed_info)
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
44

45
46
47
        sigmas = torch.rand_like(frustum_ends[:, :1], requires_grad=True) * 100
        values = torch.rand_like(frustum_starts, requires_grad=True)
        weights = volumetric_rendering_weights(
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
            packed_info,
            sigmas,
            frustum_starts,
            frustum_ends,
        )

        accum_values = volumetric_rendering_accumulate(
            weights,
            ray_indices,
            values,
            n_rays=rays_o.shape[0],
        )

        accum_values.sum().backward()


if __name__ == "__main__":
    test_rendering()