mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-04 14:58:55 +08:00
CPU usage improvement and minor changes (#108)
* cpu fixes, masking logic but commented out, nerfw and semanticnerf in progress * pylint
This commit is contained in:
1 parent
5e9d064db6
commit
3226a1c989
18 files changed
+118
-67
No files matched your search
@@ -107,7 +107,7 @@ class Graph(AbstractGraph):
|
||||
# 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)
|
||||
masked_batch = get_masked_dict(batch, valid_mask) # NOTE(ethan): this is really slow if on CPU!
|
||||
outputs = self.get_outputs(masked_intersected_ray_bundle)
|
||||
loss_dict = self.get_loss_dict(outputs=outputs, batch=masked_batch)
|
||||
return outputs, loss_dict
|
||||
@@ -128,6 +128,9 @@ class Graph(AbstractGraph):
|
||||
@torch.no_grad()
|
||||
def get_outputs_for_camera(self, intrinsics, camera_to_world, chunk_size=1024, training_camera_index=0):
|
||||
"""Takes in camera parameters and computes the output of the graph."""
|
||||
# NOTE(ethan): this function has a spike in CPU usage
|
||||
|
||||
device = self.get_device()
|
||||
assert len(intrinsics.shape) == 1
|
||||
num_intrinsics_params = len(intrinsics)
|
||||
camera_class = get_camera_model(num_intrinsics_params)
|
||||
@@ -135,9 +138,9 @@ class Graph(AbstractGraph):
|
||||
camera_ray_bundle = camera.generate_camera_rays()
|
||||
# TODO(ethan): decide how to properly handle the image indices for validation images
|
||||
camera_ray_bundle.set_camera_indices(camera_index=training_camera_index)
|
||||
camera_ray_bundle.move_to_device(device)
|
||||
image_height, image_width = camera_ray_bundle.origins.shape[:2]
|
||||
|
||||
device = self.get_device()
|
||||
num_rays = len(camera_ray_bundle)
|
||||
|
||||
outputs = {}
|
||||
@@ -146,7 +149,6 @@ class Graph(AbstractGraph):
|
||||
start_idx = i
|
||||
end_idx = i + chunk_size
|
||||
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.collider(original_ray_bundle)
|
||||
outputs = self.get_outputs(intersected_ray_bundle)
|
||||
for output_name, output in outputs.items():
|
||||
@@ -156,5 +158,5 @@ class Graph(AbstractGraph):
|
||||
return outputs
|
||||
|
||||
@abstractmethod
|
||||
def log_test_image_outputs(self, image_idx, step, image, outputs):
|
||||
def log_test_image_outputs(self, image_idx, step, image, mask, outputs):
|
||||
"""Writes the test image outputs."""
|
||||
@@ -118,7 +118,7 @@ class NGPGraph(Graph):
|
||||
loss_dict["aggregated_loss"] = self.get_aggregated_loss_from_loss_dict(loss_dict)
|
||||
return loss_dict
|
||||
|
||||
def log_test_image_outputs(self, image_idx, step, image, outputs):
|
||||
def log_test_image_outputs(self, image_idx, step, image, mask, outputs):
|
||||
rgb = outputs["rgb"]
|
||||
acc = visualization.apply_colormap(outputs["accumulation"])
|
||||
depth = visualization.apply_depth_colormap(
|
||||
|
||||
@@ -22,28 +22,27 @@ from mattport.structures.rays import RayBundle, RaySamples
|
||||
from mattport.utils import visualization, writer
|
||||
|
||||
|
||||
class NerfWField(NeRFField):
|
||||
class NerfWField(Field):
|
||||
"""The NeRF-W field which has appearance and transient conditioning."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
num_layers=8,
|
||||
layer_width=256,
|
||||
skip_connections: Tuple = (4,),
|
||||
num_images: int = 0,
|
||||
appearance_embedding_dim: int = 48,
|
||||
transient_embedding_dim: int = 16,
|
||||
position_encoding: Encoding = Identity(in_dim=3),
|
||||
direction_encoding: Encoding = Identity(in_dim=3),
|
||||
base_mlp_num_layers: int = 8,
|
||||
base_mlp_layer_width: int = 256,
|
||||
head_mlp_num_layers: int = 2,
|
||||
head_mlp_layer_width: int = 128,
|
||||
skip_connections: Tuple[int] = (4,),
|
||||
field_heads: Tuple[FieldHead] = (RGBFieldHead(),),
|
||||
) -> None:
|
||||
assert num_images > 0
|
||||
super().__init__()
|
||||
self.num_images = num_images
|
||||
self.appearance_embedding_dim = appearance_embedding_dim
|
||||
self.transient_embedding_dim = transient_embedding_dim
|
||||
super().__init__(num_layers, layer_width, skip_connections)
|
||||
self.appearance_embedding_dim = 48
|
||||
self.transient_embedding_dim = 16
|
||||
|
||||
def build_mlp_base(self):
|
||||
self.embedding_appearance = Embedding(self.num_images, self.appearance_embedding_dim)
|
||||
self.embedding_transient = Embedding(self.num_images, self.transient_embedding_dim)
|
||||
super().build_mlp_base()
|
||||
self.mlp_transient = MLP(
|
||||
in_dim=self.mlp_base.get_out_dim() + self.embedding_transient.get_out_dim(),
|
||||
out_dim=self.layer_width // 2,
|
||||
@@ -52,8 +51,7 @@ class NerfWField(NeRFField):
|
||||
activation=nn.ReLU(),
|
||||
)
|
||||
|
||||
def build_mlp_rgb(self):
|
||||
self.mlp_rgb = MLP(
|
||||
self.mlp_head = MLP(
|
||||
in_dim=self.mlp_base.get_out_dim()
|
||||
+ self.encoding_dir.get_out_dim()
|
||||
+ self.embedding_appearance.get_out_dim(),
|
||||
@@ -193,7 +191,7 @@ class NerfWGraph(NeRFGraph):
|
||||
loss_dict["aggregated_loss"] = self.get_aggregated_loss_from_loss_dict(loss_dict)
|
||||
return loss_dict
|
||||
|
||||
def log_test_image_outputs(self, image_idx, step, image, outputs):
|
||||
def log_test_image_outputs(self, image_idx, step, image, mask, outputs):
|
||||
rgb_coarse = outputs["rgb_coarse"]
|
||||
rgb_fine = outputs["rgb_fine"]
|
||||
rgb_fine_static = outputs["rgb_fine_static"]
|
||||
|
||||
@@ -149,8 +149,8 @@ class SemanticNerfGraph(NeRFGraph):
|
||||
loss_dict["aggregated_loss"] = self.get_aggregated_loss_from_loss_dict(loss_dict)
|
||||
return loss_dict
|
||||
|
||||
def log_test_image_outputs(self, image_idx, step, image, outputs):
|
||||
super().log_test_image_outputs(image_idx, step, image, outputs)
|
||||
def log_test_image_outputs(self, image_idx, step, image, mask, outputs):
|
||||
super().log_test_image_outputs(image_idx, step, image, mask, outputs)
|
||||
semantic_labels = torch.argmax(torch.nn.functional.softmax(outputs["semantic_logits"], dim=-1), dim=-1) / len(
|
||||
self.stuff_classes
|
||||
)
|
||||
|
||||
@@ -129,7 +129,7 @@ class NeRFGraph(Graph):
|
||||
loss_dict["aggregated_loss"] = self.get_aggregated_loss_from_loss_dict(loss_dict)
|
||||
return loss_dict
|
||||
|
||||
def log_test_image_outputs(self, image_idx, step, image, outputs):
|
||||
def log_test_image_outputs(self, image_idx, step, image, mask, outputs):
|
||||
rgb_coarse = outputs["rgb_coarse"]
|
||||
rgb_fine = outputs["rgb_fine"]
|
||||
acc_coarse = visualization.apply_colormap(outputs["accumulation_coarse"])
|
||||
@@ -150,10 +150,12 @@ class NeRFGraph(Graph):
|
||||
combined_rgb = torch.cat([image, rgb_coarse, rgb_fine], dim=1)
|
||||
combined_acc = torch.cat([acc_coarse, acc_fine], dim=1)
|
||||
combined_depth = torch.cat([depth_coarse, depth_fine], dim=1)
|
||||
mask = visualization.apply_depth_colormap(mask[..., None])
|
||||
|
||||
writer.put_image(name=f"image_idx_{image_idx}", image=combined_rgb, step=step, group="img")
|
||||
writer.put_image(name=f"image_idx_{image_idx}", image=combined_acc, step=step, group="accumulation")
|
||||
writer.put_image(name=f"image_idx_{image_idx}", image=combined_depth, step=step, group="depth")
|
||||
writer.put_image(name=f"image_idx_{image_idx}", image=mask, step=step, group="mask")
|
||||
|
||||
# Switch images from [H, W, C] to [1, C, H, W] for metrics computations
|
||||
image = torch.moveaxis(image, -1, 0)[None, ...]
|
||||
|
||||
Reference in new issue
Block a user