mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-07 00:09:14 +08:00
Tancik/remove valid mask (#655)
* Install updates * Remove valid mask logic
This commit is contained in:
1 parent
4f5cc28b8d
commit
6162f9d81f
8 files changed
+13
-85
No files matched your search
@@ -28,6 +28,11 @@
|
|||||||
</picture>
|
</picture>
|
||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
<!-- Use this for pypi package (and disable above). Hacky workaround -->
|
||||||
|
<!-- <p align="center">
|
||||||
|
<img alt="nerfstudio" src="https://docs.nerf.studio/en/latest/_images/logo.png" width="400">
|
||||||
|
</p> -->
|
||||||
|
|
||||||
<p align="center"> A collaboration friendly studio for NeRFs </p>
|
<p align="center"> A collaboration friendly studio for NeRFs </p>
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
@@ -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.
|
We reccomend using conda to manage dependencies. Make sure to install [Conda](https://docs.conda.io/en/latest/miniconda.html) before preceding.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
conda create --name nerfstudio -y python=3.8.13;
|
conda create --name nerfstudio -y python=3.8;
|
||||||
conda activate nerfstudio
|
conda activate nerfstudio
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -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.
|
We recommend using conda to manage dependencies. Make sure to install [Conda](https://docs.conda.io/en/latest/miniconda.html) before preceding.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
conda create --name nerfstudio -y python=3.8.13
|
conda create --name nerfstudio -y python=3.8
|
||||||
conda activate nerfstudio
|
conda activate nerfstudio
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
|
|
||||||
|
|||||||
@@ -89,8 +89,6 @@ class RaySamples(TensorDataclass):
|
|||||||
"""Frustums along ray."""
|
"""Frustums along ray."""
|
||||||
camera_indices: Optional[TensorType["bs":..., 1]] = None
|
camera_indices: Optional[TensorType["bs":..., 1]] = None
|
||||||
"""Camera index."""
|
"""Camera index."""
|
||||||
valid_mask: Optional[TensorType["bs":..., 1]] = None
|
|
||||||
"""Rays that are valid."""
|
|
||||||
deltas: Optional[TensorType["bs":..., 1]] = None
|
deltas: Optional[TensorType["bs":..., 1]] = None
|
||||||
""""width" of each sample."""
|
""""width" of each sample."""
|
||||||
spacing_starts: Optional[TensorType["bs":..., "num_samples", 1]] = None
|
spacing_starts: Optional[TensorType["bs":..., "num_samples", 1]] = None
|
||||||
@@ -125,24 +123,6 @@ class RaySamples(TensorDataclass):
|
|||||||
|
|
||||||
return weights
|
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
|
@dataclass
|
||||||
class RayBundle(TensorDataclass):
|
class RayBundle(TensorDataclass):
|
||||||
@@ -161,8 +141,6 @@ class RayBundle(TensorDataclass):
|
|||||||
"""Distance along ray to start sampling"""
|
"""Distance along ray to start sampling"""
|
||||||
fars: Optional[TensorType[..., 1]] = None
|
fars: Optional[TensorType[..., 1]] = None
|
||||||
"""Rays Distance along ray to stop sampling"""
|
"""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
|
metadata: Optional[Dict[str, TensorType["num_rays", "latent_dims"]]] = None
|
||||||
"""Additional metadata or data needed for interpolation, will mimic shape of rays"""
|
"""Additional metadata or data needed for interpolation, will mimic shape of rays"""
|
||||||
|
|
||||||
@@ -223,8 +201,6 @@ class RayBundle(TensorDataclass):
|
|||||||
"""
|
"""
|
||||||
device = self.origins.device
|
device = self.origins.device
|
||||||
|
|
||||||
valid_mask = torch.ones((bin_starts.shape), dtype=torch.bool, device=device)
|
|
||||||
|
|
||||||
dists = bin_ends - bin_starts # [..., num_samples, 1]
|
dists = bin_ends - bin_starts # [..., num_samples, 1]
|
||||||
deltas = dists * torch.norm(self.directions[:, :], dim=-1)[..., None, None]
|
deltas = dists * torch.norm(self.directions[:, :], dim=-1)[..., None, None]
|
||||||
|
|
||||||
@@ -246,7 +222,6 @@ class RayBundle(TensorDataclass):
|
|||||||
ray_samples = RaySamples(
|
ray_samples = RaySamples(
|
||||||
frustums=frustums,
|
frustums=frustums,
|
||||||
camera_indices=camera_indices, # [..., 1, 1]
|
camera_indices=camera_indices, # [..., 1, 1]
|
||||||
valid_mask=valid_mask, # [..., num_samples, 1]
|
|
||||||
deltas=deltas, # [..., num_samples, 1]
|
deltas=deltas, # [..., num_samples, 1]
|
||||||
spacing_starts=spacing_starts, # [..., num_samples, 1]
|
spacing_starts=spacing_starts, # [..., num_samples, 1]
|
||||||
spacing_ends=spacing_ends, # [..., num_samples, 1]
|
spacing_ends=spacing_ends, # [..., num_samples, 1]
|
||||||
|
|||||||
@@ -74,34 +74,8 @@ class Field(nn.Module):
|
|||||||
Args:
|
Args:
|
||||||
ray_samples: Samples to evaluate field on.
|
ray_samples: Samples to evaluate field on.
|
||||||
"""
|
"""
|
||||||
valid_mask = ray_samples.valid_mask
|
density, density_embedding = self.get_density(ray_samples)
|
||||||
|
field_outputs = self.get_outputs(ray_samples, density_embedding=density_embedding)
|
||||||
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)
|
|
||||||
|
|
||||||
field_outputs[FieldHeadNames.DENSITY] = density # type: ignore
|
field_outputs[FieldHeadNames.DENSITY] = density # type: ignore
|
||||||
return field_outputs
|
return field_outputs
|
||||||
@@ -84,12 +84,7 @@ class AABBBoxCollider(SceneBoxCollider):
|
|||||||
# clamp to near plane
|
# clamp to near plane
|
||||||
nears = torch.clamp(nears, min=self.near_plane)
|
nears = torch.clamp(nears, min=self.near_plane)
|
||||||
|
|
||||||
# fars < 0: means the ray is behind the camera
|
return nears, fars
|
||||||
# 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
|
|
||||||
|
|
||||||
def forward(self, ray_bundle: RayBundle) -> RayBundle:
|
def forward(self, ray_bundle: RayBundle) -> RayBundle:
|
||||||
"""Intersects the rays with the scene box and updates the near and far values.
|
"""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
|
ray_bundle: specified ray bundle to operate on
|
||||||
"""
|
"""
|
||||||
aabb = self.scene_box.aabb
|
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.nears = nears[..., None]
|
||||||
ray_bundle.fars = fars[..., None]
|
ray_bundle.fars = fars[..., None]
|
||||||
ray_bundle.valid_mask = valid_mask[..., None]
|
|
||||||
return ray_bundle
|
return ray_bundle
|
||||||
|
|
||||||
|
|
||||||
@@ -123,5 +117,4 @@ class NearFarCollider(SceneBoxCollider):
|
|||||||
ones = torch.ones_like(ray_bundle.origins[..., 0:1])
|
ones = torch.ones_like(ray_bundle.origins[..., 0:1])
|
||||||
ray_bundle.nears = ones * self.near_plane
|
ray_bundle.nears = ones * self.near_plane
|
||||||
ray_bundle.fars = ones * self.far_plane
|
ray_bundle.fars = ones * self.far_plane
|
||||||
ray_bundle.valid_mask = ones.bool()
|
|
||||||
return ray_bundle
|
return ray_bundle
|
||||||
@@ -132,20 +132,9 @@ class Model(nn.Module):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
if self.collider is not None:
|
if self.collider is not None:
|
||||||
intersected_ray_bundle = self.collider(ray_bundle) # pylint: disable=not-callable
|
ray_bundle = self.collider(ray_bundle)
|
||||||
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
|
|
||||||
|
|
||||||
outputs = self.get_outputs(intersected_ray_bundle)
|
return self.get_outputs(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
|
|
||||||
|
|
||||||
def get_metrics_dict(self, outputs, batch) -> Dict[str, torch.Tensor]:
|
def get_metrics_dict(self, outputs, batch) -> Dict[str, torch.Tensor]:
|
||||||
"""Compute and returns metrics.
|
"""Compute and returns metrics.
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ from nerfstudio.data.datamanagers import (
|
|||||||
from nerfstudio.engine.callbacks import TrainingCallback, TrainingCallbackAttributes
|
from nerfstudio.engine.callbacks import TrainingCallback, TrainingCallbackAttributes
|
||||||
from nerfstudio.models.base_model import Model, ModelConfig
|
from nerfstudio.models.base_model import Model, ModelConfig
|
||||||
from nerfstudio.utils import profiler
|
from nerfstudio.utils import profiler
|
||||||
from nerfstudio.utils.misc import get_masked_dict
|
|
||||||
|
|
||||||
|
|
||||||
def module_wrapper(ddp_or_model: Union[DDP, Model]) -> Model:
|
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)
|
ray_bundle, batch = self.datamanager.next_train(step)
|
||||||
model_outputs = self.model(ray_bundle)
|
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)
|
metrics_dict = self.model.get_metrics_dict(model_outputs, batch)
|
||||||
if "camera_opt" in self.datamanager.get_param_groups():
|
if "camera_opt" in self.datamanager.get_param_groups():
|
||||||
# Report the camera optimization metrics
|
# Report the camera optimization metrics
|
||||||
@@ -283,9 +279,6 @@ class VanillaPipeline(Pipeline):
|
|||||||
self.eval()
|
self.eval()
|
||||||
ray_bundle, batch = self.datamanager.next_eval(step)
|
ray_bundle, batch = self.datamanager.next_eval(step)
|
||||||
model_outputs = self.model(ray_bundle)
|
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)
|
metrics_dict = self.model.get_metrics_dict(model_outputs, batch)
|
||||||
loss_dict = self.model.get_loss_dict(model_outputs, batch, metrics_dict)
|
loss_dict = self.model.get_loss_dict(model_outputs, batch, metrics_dict)
|
||||||
self.train()
|
self.train()
|
||||||
|
|||||||
@@ -70,7 +70,6 @@ def test_depth_renderer():
|
|||||||
ray_samples = RaySamples(
|
ray_samples = RaySamples(
|
||||||
frustums=frustums,
|
frustums=frustums,
|
||||||
camera_indices=torch.ones((num_samples, 1)),
|
camera_indices=torch.ones((num_samples, 1)),
|
||||||
valid_mask=torch.ones((num_samples, 1)),
|
|
||||||
deltas=torch.ones((num_samples, 1)),
|
deltas=torch.ones((num_samples, 1)),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user