mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-04 06:49:13 +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
+39
-11
@@ -12,9 +12,12 @@ from torch import nn
|
||||
from torch.nn import Parameter
|
||||
from torchtyping import TensorType
|
||||
|
||||
from mattport.nerf.dataset.structs import SceneBounds
|
||||
from mattport.nerf.field_modules.ray_generator import RayGenerator
|
||||
from mattport.nerf.field_modules.scene_bounds_collider import AABBBoxCollider
|
||||
from mattport.structures.cameras import get_camera_model
|
||||
from mattport.structures.rays import RayBundle
|
||||
from mattport.utils.misc import get_masked_dict
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -41,13 +44,24 @@ class Node:
|
||||
class Graph(nn.Module):
|
||||
"""_summary_"""
|
||||
|
||||
def __init__(self, intrinsics=None, camera_to_world=None, loss_coefficients: DictConfig = None, **kwargs) -> None:
|
||||
def __init__(
|
||||
self,
|
||||
intrinsics=None,
|
||||
camera_to_world=None,
|
||||
loss_coefficients: DictConfig = None,
|
||||
scene_bounds: SceneBounds = None,
|
||||
**kwargs,
|
||||
) -> None:
|
||||
super().__init__()
|
||||
self.intrinsics = intrinsics
|
||||
self.camera_to_world = camera_to_world
|
||||
self.scene_bounds = scene_bounds
|
||||
self.loss_coefficients = loss_coefficients
|
||||
# assert self.scene_bounds is not None
|
||||
self.kwargs = kwargs
|
||||
self.scene_bounds_collider = None
|
||||
self.ray_generator = RayGenerator(self.intrinsics, self.camera_to_world)
|
||||
self.populate_collider()
|
||||
self.populate_modules() # populate the modules
|
||||
|
||||
# to keep track of which device the nn.Module is on
|
||||
@@ -57,6 +71,10 @@ class Graph(nn.Module):
|
||||
"""Returns the device that the torch parameters are on."""
|
||||
return self.device_indicator_param.device
|
||||
|
||||
def populate_collider(self):
|
||||
"""Set the scene bounds collider to use."""
|
||||
self.scene_bounds_collider = AABBBoxCollider(self.scene_bounds)
|
||||
|
||||
@abstractmethod
|
||||
def populate_modules(self):
|
||||
"""Initializes the modules that are part of the network."""
|
||||
@@ -89,13 +107,22 @@ class Graph(nn.Module):
|
||||
|
||||
def forward(self, ray_indices: TensorType["num_rays", 3], batch: Union[str, Dict[str, torch.tensor]] = None):
|
||||
"""Forward function that needs to be overridden."""
|
||||
# get the rays:
|
||||
ray_bundle = self.ray_generator.forward(ray_indices) # RayBundle
|
||||
outputs = self.get_outputs(ray_bundle)
|
||||
if not isinstance(batch, type(None)):
|
||||
loss_dict = self.get_loss_dict(outputs=outputs, batch=batch)
|
||||
return outputs, loss_dict
|
||||
return outputs
|
||||
# get the rays
|
||||
original_ray_bundle = self.ray_generator.forward(ray_indices) # RayBundle
|
||||
intersected_ray_bundle = self.scene_bounds_collider(original_ray_bundle)
|
||||
|
||||
if isinstance(batch, type(None)):
|
||||
# during inference, keep all rays
|
||||
outputs = self.get_outputs(intersected_ray_bundle)
|
||||
return outputs
|
||||
|
||||
# during training, keep only the rays that intersect the scene. discard the rest
|
||||
valid_mask = intersected_ray_bundle.valid_mask
|
||||
masked_intersected_ray_bundle = intersected_ray_bundle.get_masked_ray_bundle(valid_mask)
|
||||
masked_batch = get_masked_dict(batch, valid_mask)
|
||||
outputs = self.get_outputs(masked_intersected_ray_bundle)
|
||||
loss_dict = self.get_loss_dict(outputs=outputs, batch=masked_batch)
|
||||
return outputs, loss_dict
|
||||
|
||||
@abstractmethod
|
||||
def get_loss_dict(self, outputs, batch) -> Dict[str, torch.tensor]:
|
||||
@@ -130,9 +157,10 @@ class Graph(nn.Module):
|
||||
for i in range(0, num_rays, chunk_size):
|
||||
start_idx = i
|
||||
end_idx = i + chunk_size
|
||||
ray_bundle = camera_ray_bundle.get_row_major_sliced_ray_bundle(start_idx, end_idx)
|
||||
ray_bundle.move_to_device(device)
|
||||
outputs = self.get_outputs(ray_bundle)
|
||||
original_ray_bundle = camera_ray_bundle.get_row_major_sliced_ray_bundle(start_idx, end_idx)
|
||||
original_ray_bundle.move_to_device(device)
|
||||
intersected_ray_bundle = self.scene_bounds_collider(original_ray_bundle)
|
||||
outputs = self.get_outputs(intersected_ray_bundle)
|
||||
for output_name, output in outputs.items():
|
||||
outputs_lists[output_name].append(output)
|
||||
for output_name, outputs_list in outputs_lists.items():
|
||||
|
||||
@@ -20,6 +20,7 @@ from mattport.nerf.renderers import AccumulationRenderer, DepthRenderer, RGBRend
|
||||
from mattport.nerf.sampler import PDFSampler, UniformSampler
|
||||
from mattport.structures import colors
|
||||
from mattport.structures.rays import RayBundle, RaySamples
|
||||
from mattport.nerf.field_modules.scene_bounds_collider import NearFarCollider
|
||||
from mattport.utils import stats_tracker, visualization, writer
|
||||
|
||||
|
||||
@@ -116,12 +117,13 @@ class NeRFGraph(Graph):
|
||||
self.field_coarse = NeRFField()
|
||||
self.field_fine = NeRFField()
|
||||
|
||||
def populate_collider(self):
|
||||
self.scene_bounds_collider = NearFarCollider(self.near_plane, self.far_plane)
|
||||
|
||||
def populate_modules(self):
|
||||
|
||||
# samplers
|
||||
self.sampler_uniform = UniformSampler(
|
||||
near_plane=self.near_plane, far_plane=self.far_plane, num_samples=self.num_coarse_samples
|
||||
)
|
||||
self.sampler_uniform = UniformSampler(num_samples=self.num_coarse_samples)
|
||||
self.sampler_pdf = PDFSampler(num_samples=self.num_importance_samples)
|
||||
|
||||
# field
|
||||
@@ -147,11 +149,13 @@ class NeRFGraph(Graph):
|
||||
Dict[str, List[Parameter]]: Mapping of different parameter groups
|
||||
"""
|
||||
param_groups = {}
|
||||
# param_groups["cameras"] = list(self.ray_generator.parameters())
|
||||
param_groups["fields"] = list(self.field_coarse.parameters()) + list(self.field_fine.parameters())
|
||||
return param_groups
|
||||
|
||||
def get_outputs(self, ray_bundle: RayBundle):
|
||||
|
||||
# TODO: set the new rays...
|
||||
|
||||
# coarse network:
|
||||
uniform_ray_samples = self.sampler_uniform(ray_bundle) # RaySamples
|
||||
|
||||
|
||||
Reference in new issue
Block a user