mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-01 21:39:06 +08:00
Speedup eval and training with better data management (#492)
move to tensors to gpu earlier
This commit is contained in:
1 parent
96e1f2a698
commit
a43776d76d
5 files changed
+13
-12
No files matched your search
@@ -171,6 +171,9 @@ class Cameras:
|
|||||||
Rays for the given camera indices and coords.
|
Rays for the given camera indices and coords.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
if isinstance(camera_indices, torch.Tensor):
|
||||||
|
camera_indices = camera_indices.to(self.device)
|
||||||
|
|
||||||
if coords is None:
|
if coords is None:
|
||||||
coords = self.get_image_coords().to(self.device)
|
coords = self.get_image_coords().to(self.device)
|
||||||
|
|
||||||
@@ -237,7 +240,7 @@ class Cameras:
|
|||||||
if not isinstance(camera_indices, torch.Tensor):
|
if not isinstance(camera_indices, torch.Tensor):
|
||||||
ray_bundle_camera_indices = torch.Tensor([camera_indices]).broadcast_to(pixel_area.shape).to(self.device)
|
ray_bundle_camera_indices = torch.Tensor([camera_indices]).broadcast_to(pixel_area.shape).to(self.device)
|
||||||
else:
|
else:
|
||||||
ray_bundle_camera_indices = camera_indices
|
ray_bundle_camera_indices = camera_indices.view(pixel_area.shape)
|
||||||
|
|
||||||
return RayBundle(
|
return RayBundle(
|
||||||
origins=origins,
|
origins=origins,
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ class RaySamples(TensorDataclass):
|
|||||||
# mip-nerf version of transmittance calculation:
|
# mip-nerf version of transmittance calculation:
|
||||||
transmittance = torch.cumsum(delta_density[..., :-1, :], dim=-2)
|
transmittance = torch.cumsum(delta_density[..., :-1, :], dim=-2)
|
||||||
transmittance = torch.cat(
|
transmittance = torch.cat(
|
||||||
[torch.zeros((*transmittance.shape[:1], 1, 1)).to(densities.device), transmittance], dim=-2
|
[torch.zeros((*transmittance.shape[:1], 1, 1), device=densities.device), transmittance], dim=-2
|
||||||
)
|
)
|
||||||
transmittance = torch.exp(-transmittance) # [..., "num_samples"]
|
transmittance = torch.exp(-transmittance) # [..., "num_samples"]
|
||||||
|
|
||||||
|
|||||||
@@ -153,11 +153,9 @@ class TCNNCompoundField(Field):
|
|||||||
return density, base_mlp_out
|
return density, base_mlp_out
|
||||||
|
|
||||||
def get_outputs(self, ray_samples: RaySamples, density_embedding: Optional[TensorType] = None):
|
def get_outputs(self, ray_samples: RaySamples, density_embedding: Optional[TensorType] = None):
|
||||||
# TODO: add valid_mask masking!
|
|
||||||
# tcnn requires directions in the range [0,1]
|
|
||||||
if ray_samples.camera_indices is None:
|
if ray_samples.camera_indices is None:
|
||||||
raise AttributeError("Camera indices are not provided.")
|
raise AttributeError("Camera indices are not provided.")
|
||||||
camera_indices = ray_samples.camera_indices.squeeze().to(ray_samples.frustums.origins.device)
|
camera_indices = ray_samples.camera_indices.squeeze()
|
||||||
directions = get_normalized_directions(ray_samples.frustums.directions)
|
directions = get_normalized_directions(ray_samples.frustums.directions)
|
||||||
directions_flat = directions.view(-1, 3)
|
directions_flat = directions.view(-1, 3)
|
||||||
d = self.direction_encoding(directions_flat)
|
d = self.direction_encoding(directions_flat)
|
||||||
@@ -165,8 +163,8 @@ class TCNNCompoundField(Field):
|
|||||||
if self.training:
|
if self.training:
|
||||||
embedded_appearance = self.embedding_appearance(camera_indices)
|
embedded_appearance = self.embedding_appearance(camera_indices)
|
||||||
else:
|
else:
|
||||||
embedded_appearance = torch.zeros((*directions.shape[:-1], self.appearance_embedding_dim)).to(
|
embedded_appearance = torch.zeros(
|
||||||
directions.device
|
(*directions.shape[:-1], self.appearance_embedding_dim), device=directions.device
|
||||||
)
|
)
|
||||||
|
|
||||||
if density_embedding is None:
|
if density_embedding is None:
|
||||||
@@ -250,13 +248,14 @@ class TorchCompoundField(Field):
|
|||||||
|
|
||||||
if ray_samples.camera_indices is None:
|
if ray_samples.camera_indices is None:
|
||||||
raise AttributeError("Camera indices are not provided.")
|
raise AttributeError("Camera indices are not provided.")
|
||||||
camera_indices = ray_samples.camera_indices.squeeze().to(ray_samples.frustums.origins.device)
|
camera_indices = ray_samples.camera_indices.squeeze()
|
||||||
if self.training:
|
if self.training:
|
||||||
embedded_appearance = self.embedding_appearance(camera_indices)
|
embedded_appearance = self.embedding_appearance(camera_indices)
|
||||||
else:
|
else:
|
||||||
embedded_appearance = torch.zeros(
|
embedded_appearance = torch.zeros(
|
||||||
(*ray_samples.frustums.directions.shape[:-1], self.appearance_embedding_dim)
|
(*ray_samples.frustums.directions.shape[:-1], self.appearance_embedding_dim),
|
||||||
).to(ray_samples.frustums.directions.device)
|
device=ray_samples.frustums.directions.device,
|
||||||
|
)
|
||||||
|
|
||||||
outputs = {}
|
outputs = {}
|
||||||
for field_head in self.field_heads:
|
for field_head in self.field_heads:
|
||||||
|
|||||||
@@ -50,5 +50,4 @@ class RayGenerator(nn.Module):
|
|||||||
camera_indices=c,
|
camera_indices=c,
|
||||||
coords=coords,
|
coords=coords,
|
||||||
)
|
)
|
||||||
ray_bundle.camera_indices = c[..., None] # ["num_rays",1]
|
|
||||||
return ray_bundle
|
return ray_bundle
|
||||||
@@ -29,7 +29,7 @@ def components_from_spherical_harmonics(levels: int, directions: TensorType[...,
|
|||||||
directions: Spherical hamonic coefficients
|
directions: Spherical hamonic coefficients
|
||||||
"""
|
"""
|
||||||
num_components = levels**2
|
num_components = levels**2
|
||||||
components = torch.zeros((*directions.shape[:-1], num_components)).to(directions.device)
|
components = torch.zeros((*directions.shape[:-1], num_components), device=directions.device)
|
||||||
|
|
||||||
assert 1 <= levels <= 5, f"SH levels must be in [1,4], got {levels}"
|
assert 1 <= levels <= 5, f"SH levels must be in [1,4], got {levels}"
|
||||||
assert directions.shape[-1] == 3, f"Direction input should have three dimensions. Got {directions.shape[-1]}"
|
assert directions.shape[-1] == 3, f"Direction input should have three dimensions. Got {directions.shape[-1]}"
|
||||||
|
|||||||
Reference in new issue
Block a user