mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-16 12:53:44 +08:00
Initial ray generation and dataset code (#12)
* ray generator and cameras structure progress * test renames * added pass * conflicts * fixing some black errors * reformated files * merged master and .gitignore * fixing linting * removing old folder * removed return False * pylint fixes * update * updates * update * update * update * update * update * update * update * update * update * update
This commit is contained in:
1 parent
1995afda57
commit
0b7dfcdc5d
21 files changed
+652
-139
No files matched your search
@@ -0,0 +1,9 @@
|
||||
import pytest
|
||||
|
||||
|
||||
def test_positional_encoding():
|
||||
assert True
|
||||
|
||||
|
||||
def test_lower_case():
|
||||
assert "MATTPORT".lower() == "mattport"
|
||||
@@ -0,0 +1,56 @@
|
||||
"""
|
||||
Test ray generation.
|
||||
"""
|
||||
import torch
|
||||
|
||||
from mattport.nerf.dataset.blender_dataset import load_blender_data
|
||||
from mattport.nerf.modules.ray_generator import RayGenerator
|
||||
from mattport.structures.rays import RayBundle
|
||||
from mattport.utils.io import get_absolute_path
|
||||
|
||||
|
||||
def test_ray_generator():
|
||||
"""Test the ray generation works given camera, row, and col indices as input."""
|
||||
|
||||
# load data
|
||||
datadir = get_absolute_path("data/lego")
|
||||
image_filenames, poses, focal_length, image_height, image_width = load_blender_data(
|
||||
datadir, half_res=False, testskip=1
|
||||
)
|
||||
cx = image_width / 2.0
|
||||
cy = image_height / 2.0
|
||||
camera_to_world = torch.from_numpy(poses[:, :3]) # camera to world transform
|
||||
num_cameras = len(image_filenames)
|
||||
num_intrinsics_params = 3
|
||||
intrinsics = torch.ones((num_cameras, num_intrinsics_params), dtype=torch.float32)
|
||||
intrinsics *= torch.tensor([focal_length, cx, cy])
|
||||
|
||||
# Initialize the ray generator with the cameras.
|
||||
ray_generator = RayGenerator(intrinsics, camera_to_world)
|
||||
|
||||
# Random ray indices.
|
||||
print("Random ray indexing.")
|
||||
num_rays = 1024
|
||||
ray_indices = torch.floor(torch.rand((num_rays, 3)) * torch.tensor([num_cameras, image_height, image_width])).long()
|
||||
ray_bundle = ray_generator(ray_indices)
|
||||
assert isinstance(ray_bundle, RayBundle)
|
||||
|
||||
# TODO(ethan): visualize the RayBundle object
|
||||
# TODO(ethan): add other test cases? e.g., origin and camera_indices checks
|
||||
|
||||
# check that the origins are correct
|
||||
c = ray_indices[:, 0]
|
||||
assert torch.allclose(ray_bundle.origins, camera_to_world[c, :3, 3])
|
||||
|
||||
# Ray indices for one camera.
|
||||
coords = torch.meshgrid(torch.arange(image_height), torch.arange(image_width), indexing="ij")
|
||||
coords = torch.stack(coords, dim=-1)
|
||||
coords = coords.view(-1, 2)
|
||||
num_rays = coords.shape[0]
|
||||
camera_index = 0
|
||||
ray_indices = torch.cat([torch.ones_like(coords[:, :1]) * camera_index, coords], dim=-1).long()
|
||||
ray_bundle = ray_generator(ray_indices)
|
||||
|
||||
print(ray_bundle.camera_indices)
|
||||
|
||||
# assert False
|
||||
@@ -0,0 +1,12 @@
|
||||
import torch
|
||||
|
||||
from mattport.structures.cameras import PinholeCamera
|
||||
from mattport.structures.rays import RayBundle, CameraRayBundle
|
||||
|
||||
|
||||
def test_pinhole_camera():
|
||||
pinhole_camera = PinholeCamera(cx=400.0, cy=400.0, fx=10.0, fy=10.0)
|
||||
camera_ray_bundle = pinhole_camera.generate_camera_rays()
|
||||
assert isinstance(camera_ray_bundle, CameraRayBundle)
|
||||
assert torch.allclose(camera_ray_bundle.origins[0], torch.tensor([0.0, 0.0, 0.0]))
|
||||
# assert False
|
||||
Reference in new issue
Block a user