From 6162f9d81ffd39b55600a59daf42aabd1a617658 Mon Sep 17 00:00:00 2001
From: Matthew Tancik
A collaboration friendly studio for NeRFs
@@ -71,7 +76,7 @@ For more complex changes (e.g. running with your own data/ setting up a new NeRF We reccomend using conda to manage dependencies. Make sure to install [Conda](https://docs.conda.io/en/latest/miniconda.html) before preceding. ```bash -conda create --name nerfstudio -y python=3.8.13; +conda create --name nerfstudio -y python=3.8; conda activate nerfstudio python -m pip install --upgrade pip ``` diff --git a/docs/quickstart/installation.md b/docs/quickstart/installation.md index 7db10083d..262edc05a 100644 --- a/docs/quickstart/installation.md +++ b/docs/quickstart/installation.md @@ -5,7 +5,7 @@ We recommend using conda to manage dependencies. Make sure to install [Conda](https://docs.conda.io/en/latest/miniconda.html) before preceding. ```bash -conda create --name nerfstudio -y python=3.8.13 +conda create --name nerfstudio -y python=3.8 conda activate nerfstudio python -m pip install --upgrade pip diff --git a/nerfstudio/cameras/rays.py b/nerfstudio/cameras/rays.py index 306abc020..021314b1d 100644 --- a/nerfstudio/cameras/rays.py +++ b/nerfstudio/cameras/rays.py @@ -89,8 +89,6 @@ class RaySamples(TensorDataclass): """Frustums along ray.""" camera_indices: Optional[TensorType["bs":..., 1]] = None """Camera index.""" - valid_mask: Optional[TensorType["bs":..., 1]] = None - """Rays that are valid.""" deltas: Optional[TensorType["bs":..., 1]] = None """"width" of each sample.""" spacing_starts: Optional[TensorType["bs":..., "num_samples", 1]] = None @@ -125,24 +123,6 @@ class RaySamples(TensorDataclass): return weights - def set_valid_mask(self, valid_mask: TensorType[..., "num_samples"]) -> None: - """Sets valid mask - - Args: - valid_mask: the mask to use - """ - self.valid_mask = valid_mask - - def apply_masks(self) -> "RaySamples": - """Use valid_mask to mask samples. - - Returns: - New set of masked samples. - """ - if self.valid_mask is not None: - return self[self.valid_mask[..., 0]] - return self - @dataclass class RayBundle(TensorDataclass): @@ -161,8 +141,6 @@ class RayBundle(TensorDataclass): """Distance along ray to start sampling""" fars: Optional[TensorType[..., 1]] = None """Rays Distance along ray to stop sampling""" - valid_mask: Optional[TensorType[..., 1, bool]] = None - """Rays that are valid""" metadata: Optional[Dict[str, TensorType["num_rays", "latent_dims"]]] = None """Additional metadata or data needed for interpolation, will mimic shape of rays""" @@ -223,8 +201,6 @@ class RayBundle(TensorDataclass): """ device = self.origins.device - valid_mask = torch.ones((bin_starts.shape), dtype=torch.bool, device=device) - dists = bin_ends - bin_starts # [..., num_samples, 1] deltas = dists * torch.norm(self.directions[:, :], dim=-1)[..., None, None] @@ -246,7 +222,6 @@ class RayBundle(TensorDataclass): ray_samples = RaySamples( frustums=frustums, camera_indices=camera_indices, # [..., 1, 1] - valid_mask=valid_mask, # [..., num_samples, 1] deltas=deltas, # [..., num_samples, 1] spacing_starts=spacing_starts, # [..., num_samples, 1] spacing_ends=spacing_ends, # [..., num_samples, 1] diff --git a/nerfstudio/fields/base_field.py b/nerfstudio/fields/base_field.py index 77ead7346..7e6821999 100644 --- a/nerfstudio/fields/base_field.py +++ b/nerfstudio/fields/base_field.py @@ -74,34 +74,8 @@ class Field(nn.Module): Args: ray_samples: Samples to evaluate field on. """ - valid_mask = ray_samples.valid_mask - - if valid_mask is not None: - # Hacky handling of empty masks. Tests on a single ray but doesn't use results - if not valid_mask.any(): - # TODO: Can we just throw an exception here? If not we need to update this to include some fake metadata - ray_samples = RaySamples(frustums=Frustums.get_mock_frustum().to(valid_mask.device)) - else: - ray_samples = ray_samples.apply_masks() - density_masked, density_embedding_masked = self.get_density(ray_samples) - field_outputs_masked = self.get_outputs(ray_samples, density_embedding=density_embedding_masked) - - field_outputs = {} - for k, value in field_outputs_masked.items(): - zeros = torch.zeros( - *valid_mask.shape[:-1], value.shape[-1], dtype=value.dtype, device=valid_mask.device - ) - if valid_mask.any(): - zeros[valid_mask[..., 0]] = value - else: - zeros[0, :] = value - field_outputs[k] = zeros - density = torch.zeros(valid_mask.shape, dtype=density_masked.dtype, device=valid_mask.device) - if valid_mask.any(): - density[valid_mask[..., 0]] = density_masked - else: - density, density_embedding = self.get_density(ray_samples) - field_outputs = self.get_outputs(ray_samples, density_embedding=density_embedding) + density, density_embedding = self.get_density(ray_samples) + field_outputs = self.get_outputs(ray_samples, density_embedding=density_embedding) field_outputs[FieldHeadNames.DENSITY] = density # type: ignore return field_outputs diff --git a/nerfstudio/model_components/scene_colliders.py b/nerfstudio/model_components/scene_colliders.py index b7ec3f153..46980e6e5 100644 --- a/nerfstudio/model_components/scene_colliders.py +++ b/nerfstudio/model_components/scene_colliders.py @@ -84,12 +84,7 @@ class AABBBoxCollider(SceneBoxCollider): # clamp to near plane nears = torch.clamp(nears, min=self.near_plane) - # fars < 0: means the ray is behind the camera - # nears > fars: means no intersection - valid_mask = ((fars > nears).float() * (fars > 0).float()).bool() - nears[~valid_mask] = 0.0 - fars[~valid_mask] = 0.0 - return nears, fars, valid_mask + return nears, fars def forward(self, ray_bundle: RayBundle) -> RayBundle: """Intersects the rays with the scene box and updates the near and far values. @@ -99,10 +94,9 @@ class AABBBoxCollider(SceneBoxCollider): ray_bundle: specified ray bundle to operate on """ aabb = self.scene_box.aabb - nears, fars, valid_mask = self._intersect_with_aabb(ray_bundle.origins, ray_bundle.directions, aabb) + nears, fars = self._intersect_with_aabb(ray_bundle.origins, ray_bundle.directions, aabb) ray_bundle.nears = nears[..., None] ray_bundle.fars = fars[..., None] - ray_bundle.valid_mask = valid_mask[..., None] return ray_bundle @@ -123,5 +117,4 @@ class NearFarCollider(SceneBoxCollider): ones = torch.ones_like(ray_bundle.origins[..., 0:1]) ray_bundle.nears = ones * self.near_plane ray_bundle.fars = ones * self.far_plane - ray_bundle.valid_mask = ones.bool() return ray_bundle diff --git a/nerfstudio/models/base_model.py b/nerfstudio/models/base_model.py index 21b844882..ef36f53c8 100644 --- a/nerfstudio/models/base_model.py +++ b/nerfstudio/models/base_model.py @@ -132,20 +132,9 @@ class Model(nn.Module): """ if self.collider is not None: - intersected_ray_bundle = self.collider(ray_bundle) # pylint: disable=not-callable - valid_mask = intersected_ray_bundle.valid_mask[..., 0] - intersected_ray_bundle = intersected_ray_bundle[valid_mask] - else: - # NOTE(ruilongli): we don't need collider for ngp - intersected_ray_bundle = ray_bundle - valid_mask = None + ray_bundle = self.collider(ray_bundle) - outputs = self.get_outputs(intersected_ray_bundle) - # TODO: update this once outputs is no longer a dictionary and is instead typed - if valid_mask is not None: - assert "valid_mask" not in outputs, "valid_mask should not be in outputs" - outputs["valid_mask"] = valid_mask - return outputs + return self.get_outputs(ray_bundle) def get_metrics_dict(self, outputs, batch) -> Dict[str, torch.Tensor]: """Compute and returns metrics. diff --git a/nerfstudio/pipelines/base_pipeline.py b/nerfstudio/pipelines/base_pipeline.py index 29f8f2915..f8cced990 100644 --- a/nerfstudio/pipelines/base_pipeline.py +++ b/nerfstudio/pipelines/base_pipeline.py @@ -45,7 +45,6 @@ from nerfstudio.data.datamanagers import ( from nerfstudio.engine.callbacks import TrainingCallback, TrainingCallbackAttributes from nerfstudio.models.base_model import Model, ModelConfig from nerfstudio.utils import profiler -from nerfstudio.utils.misc import get_masked_dict def module_wrapper(ddp_or_model: Union[DDP, Model]) -> Model: @@ -249,9 +248,6 @@ class VanillaPipeline(Pipeline): """ ray_bundle, batch = self.datamanager.next_train(step) model_outputs = self.model(ray_bundle) - if "valid_mask" in model_outputs: - valid_mask = model_outputs["valid_mask"] - batch = get_masked_dict(batch, valid_mask) metrics_dict = self.model.get_metrics_dict(model_outputs, batch) if "camera_opt" in self.datamanager.get_param_groups(): # Report the camera optimization metrics @@ -283,9 +279,6 @@ class VanillaPipeline(Pipeline): self.eval() ray_bundle, batch = self.datamanager.next_eval(step) model_outputs = self.model(ray_bundle) - if "valid_mask" in model_outputs: - valid_mask = model_outputs["valid_mask"] - batch = get_masked_dict(batch, valid_mask) metrics_dict = self.model.get_metrics_dict(model_outputs, batch) loss_dict = self.model.get_loss_dict(model_outputs, batch, metrics_dict) self.train() diff --git a/tests/model_components/test_renderers.py b/tests/model_components/test_renderers.py index 053b4936d..26417e4e4 100644 --- a/tests/model_components/test_renderers.py +++ b/tests/model_components/test_renderers.py @@ -70,7 +70,6 @@ def test_depth_renderer(): ray_samples = RaySamples( frustums=frustums, camera_indices=torch.ones((num_samples, 1)), - valid_mask=torch.ones((num_samples, 1)), deltas=torch.ones((num_samples, 1)), )