mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-21 07:05:44 +08:00
Setting near and far with scene bounds collider. Also LR schedules. (#74)
* some initial cuda code as I learn. using git to save progress. * first cuda kernel for ray sampling that mostly works * cleaning up code. working on lr. structs for scene bounds aabb * added scene bounds collider code * updated setup * setup * setup * black * including some files * exporting env variable * new file * git workflows * github actions * github actions * github actions * removing env var
This commit is contained in:
1 parent
752df417ce
commit
676beebb14
45 files changed
+803
-8354
No files matched your search
@@ -0,0 +1,102 @@
|
||||
"""
|
||||
Test the CUDA bindings.
|
||||
"""
|
||||
import torch
|
||||
from mattport.cuda.sampler import Sampler
|
||||
import time
|
||||
import mattport_cuda
|
||||
import os
|
||||
|
||||
|
||||
def test_gradients():
|
||||
"""_summary_"""
|
||||
pass
|
||||
|
||||
|
||||
def test_outputs():
|
||||
"""_summary_"""
|
||||
pass
|
||||
|
||||
|
||||
def pytorch_sample_uniformly_along_ray_bundle(origins, directions, nears, fars, range_offsets):
|
||||
"""Sample along every ray with a constant step size."""
|
||||
time_steps = nears[:, None] + range_offsets[None, :] # (num_rays, max_num_samples)
|
||||
samples = origins[:, None, :] + directions[:, None, :] * time_steps[:, :, None]
|
||||
time_steps_mask = (time_steps <= fars[:, None]).float()
|
||||
return time_steps, samples, time_steps_mask
|
||||
|
||||
|
||||
def get_rgb_density(samples, time_steps_mask):
|
||||
num_rays, max_num_samples = time_steps_mask.shape
|
||||
total_max_num_samples = num_rays * max_num_samples
|
||||
|
||||
time_steps_mask_flat = time_steps_mask.view(total_max_num_samples) == 1.0
|
||||
samples_flat = samples.view(total_max_num_samples, 3)
|
||||
samples_flat_masked = samples_flat[time_steps_mask_flat]
|
||||
|
||||
# TODO: this goes through the forward pass of the MLP
|
||||
|
||||
rgb = torch.zeros((num_rays, max_num_samples, 3))
|
||||
density = torch.zeros((num_rays, max_num_samples))
|
||||
rgb.view(total_max_num_samples, 3)[time_steps_mask_flat] = 1.0
|
||||
density.view(total_max_num_samples)[time_steps_mask_flat] = 1.0
|
||||
return rgb, density
|
||||
|
||||
|
||||
def test_benchmark():
|
||||
"""The test cases assume that rays can be at most of length 1.0."""
|
||||
|
||||
device = torch.device("cuda")
|
||||
dtype = torch.float32
|
||||
|
||||
near_range = [0, 0.2]
|
||||
far_range = [0.8, 1.0]
|
||||
max_num_samples = 128 * 8
|
||||
offset = 1.0 / max_num_samples
|
||||
|
||||
print("offset is", offset)
|
||||
# import sys; sys.exit()
|
||||
|
||||
print("Running ray sampling tests.")
|
||||
# origins = torch.tensor()
|
||||
# we want these to be dtype=torch.float32 and device="cuda"
|
||||
num_rays = 1024 * 8
|
||||
print("num_rays:", num_rays)
|
||||
print("max_num_samples:", max_num_samples)
|
||||
origins = torch.zeros((num_rays, 3)).to(device)
|
||||
directions = torch.ones((num_rays, 3)).to(device)
|
||||
nears = torch.rand((num_rays)).to(device) * (near_range[1] - near_range[0]) + near_range[0]
|
||||
fars = torch.rand((num_rays)).to(device) * (far_range[1] - far_range[0]) + far_range[0]
|
||||
offsets = torch.ones(max_num_samples).to(device) * offset
|
||||
range_offsets = torch.arange(0, max_num_samples).to(device) * offset
|
||||
|
||||
start = time.time()
|
||||
time_steps_pt, samples_pt, time_steps_mask_pt = pytorch_sample_uniformly_along_ray_bundle(
|
||||
origins, directions, nears, fars, range_offsets
|
||||
)
|
||||
PYTORCH_SAMPLE_TIME = time.time() - start
|
||||
print("PYTORCH_SAMPLE_TIME:", PYTORCH_SAMPLE_TIME)
|
||||
|
||||
start = time.time()
|
||||
time_steps_cu, samples_cu, time_steps_mask_cu = mattport_cuda.sample_uniformly_along_ray_bundle(
|
||||
origins, directions, nears, fars, offsets, max_num_samples
|
||||
)
|
||||
CUDA_SAMPLE_TIME = time.time() - start
|
||||
print("CUDA_SAMPLE_TIME: ", CUDA_SAMPLE_TIME)
|
||||
|
||||
# assert torch.allclose(time_steps_pt, time_steps_cu)
|
||||
# assert torch.allclose(samples_pt, samples_cu)
|
||||
# assert torch.allclose(time_steps_mask_pt, time_steps_mask_cu)
|
||||
|
||||
# rgb_pt, density_pt = get_rgb_density(samples_pt, time_steps_mask_pt)
|
||||
# rgb_cu, density_cu = get_rgb_density(samples_cu, time_steps_mask_cu)
|
||||
|
||||
perc_samples_used = float(time_steps_mask_pt.sum() / (time_steps_mask_pt.shape[0] * time_steps_mask_pt.shape[1]))
|
||||
perc_samples_used_str = str(round(perc_samples_used * 100.0, 2))
|
||||
print(f"Using field network for %{perc_samples_used_str} of samples in this ray bundle.")
|
||||
|
||||
# TODO: rendering of rgb, density, and deltas with pytorch vs. cuda
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_benchmark()
|
||||
@@ -1,7 +1,7 @@
|
||||
# dataset options
|
||||
dataset:
|
||||
data_directory: data/lego
|
||||
dataset_type: blender
|
||||
dataset_format: blender
|
||||
downscale_factor: 1.0
|
||||
|
||||
# dataloader options
|
||||
@@ -15,7 +15,7 @@ dataloader:
|
||||
network:
|
||||
ray_generator:
|
||||
class_name: RayGenerator
|
||||
inputs: ['cxy'] # camera, x, and y indices
|
||||
inputs: ["cxy"] # camera, x, and y indices
|
||||
meta_data:
|
||||
in_dim: 0
|
||||
learning_rate: 0.0
|
||||
learning_rate: 0.0
|
||||
@@ -9,7 +9,7 @@ from tqdm import tqdm
|
||||
|
||||
from mattport.nerf.dataset.collate import CollateIterDataset, collate_batch_size_one
|
||||
from mattport.nerf.dataset.image_dataset import ImageDataset, collate_batch
|
||||
from mattport.nerf.dataset.utils import get_dataset_inputs_dict
|
||||
from mattport.nerf.dataset.utils import get_dataset_inputs
|
||||
from mattport.nerf.field_modules.ray_generator import RayGenerator
|
||||
from mattport.utils.io import get_absolute_path
|
||||
from mattport.viewer.plotly import visualize_dataset
|
||||
@@ -34,7 +34,7 @@ def test_dataloader(visualize=False):
|
||||
"""Testing for the dataloader from input dataset parameters to rays."""
|
||||
|
||||
data_directory = "tests/data/lego_test"
|
||||
dataset_type = "blender"
|
||||
dataset_format = "blender"
|
||||
downscale_factor = 1
|
||||
num_images_to_sample_from = 1
|
||||
num_times_to_repeat_images = 40
|
||||
@@ -42,9 +42,9 @@ def test_dataloader(visualize=False):
|
||||
num_workers = 0
|
||||
|
||||
data_directory = get_absolute_path(data_directory)
|
||||
dataset_inputs = get_dataset_inputs_dict(
|
||||
data_directory=data_directory, dataset_type=dataset_type, downscale_factor=downscale_factor
|
||||
)["train"]
|
||||
dataset_inputs = get_dataset_inputs(
|
||||
data_directory=data_directory, dataset_format=dataset_format, downscale_factor=downscale_factor, split="train"
|
||||
)
|
||||
image_dataset = ImageDataset(
|
||||
image_filenames=dataset_inputs.image_filenames, downscale_factor=dataset_inputs.downscale_factor
|
||||
)
|
||||
|
||||
@@ -5,6 +5,7 @@ import torch
|
||||
from mattport.nerf.sampler import PDFSampler, UniformSampler
|
||||
|
||||
from mattport.structures.rays import RayBundle
|
||||
from mattport.nerf.field_modules.scene_bounds_collider import NearFarCollider
|
||||
|
||||
|
||||
def test_uniform_sampler():
|
||||
@@ -12,11 +13,13 @@ def test_uniform_sampler():
|
||||
near_plane = 2
|
||||
far_plane = 4
|
||||
num_samples = 15
|
||||
sampler = UniformSampler(near_plane=near_plane, far_plane=far_plane, num_samples=num_samples)
|
||||
sampler = UniformSampler(num_samples=num_samples)
|
||||
|
||||
origins = torch.zeros((10, 3))
|
||||
directions = torch.ones_like(origins)
|
||||
ray_bundle = RayBundle(origins=origins, directions=directions)
|
||||
collider = NearFarCollider(near_plane, far_plane)
|
||||
ray_bundle = collider(ray_bundle)
|
||||
|
||||
ray_samples = sampler(ray_bundle)
|
||||
|
||||
@@ -34,8 +37,10 @@ def test_pdf_sampler():
|
||||
origins = torch.zeros((10, 3))
|
||||
directions = torch.ones_like(origins)
|
||||
ray_bundle = RayBundle(origins=origins, directions=directions)
|
||||
collider = NearFarCollider(near_plane, far_plane)
|
||||
ray_bundle = collider(ray_bundle)
|
||||
|
||||
uniform_sampler = UniformSampler(near_plane=near_plane, far_plane=far_plane, num_samples=num_samples)
|
||||
uniform_sampler = UniformSampler(num_samples=num_samples)
|
||||
coarse_ray_samples = uniform_sampler(ray_bundle)
|
||||
|
||||
weights = torch.ones((10, num_samples))
|
||||
|
||||
Reference in new issue
Block a user