test_loss.py 865 Bytes
Newer Older
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
1
2
3
import pytest
import torch

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

device = "cuda:0"
batch_size = 32
eps = 1e-6


@pytest.mark.skipif(not torch.cuda.is_available, reason="No CUDA device")
def test_distortion():
    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
18
    ray_indices, t_starts, t_ends = ray_marching(
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
19
20
21
22
23
24
        rays_o,
        rays_d,
        near_plane=0.1,
        far_plane=1.0,
        render_step_size=1e-3,
    )
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
25
    packed_info = pack_info(ray_indices, n_rays=batch_size)
Ruilong Li(李瑞龙)'s avatar
Ruilong Li(李瑞龙) committed
26
27
28
29
30
31
32
    weights = torch.rand((t_starts.shape[0],), device=device)
    loss = distortion(packed_info, weights, t_starts, t_ends)
    assert loss.shape == (batch_size,)


if __name__ == "__main__":
    test_distortion()