mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-25 18:39:55 +08:00
Reduce GPU memory usage in CacheDataloader (#1730)
* Reduce GPU memory usage in CacheDataloader * Move also masks and seg.masks, depths to cpu * Fix linter issues
This commit is contained in:
1 parent
d4b04376ab
commit
3b0f7581d8
10 files changed
+29
-11
No files matched your search
@@ -107,7 +107,7 @@ class DepthNerfactoModel(NerfactoModel):
|
||||
) -> Tuple[Dict[str, float], Dict[str, torch.Tensor]]:
|
||||
"""Appends ground truth depth to the depth image."""
|
||||
metrics, images = super().get_image_metrics_and_images(outputs, batch)
|
||||
ground_truth_depth = batch["depth_image"]
|
||||
ground_truth_depth = batch["depth_image"].to(self.device)
|
||||
if not self.config.is_euclidean_depth:
|
||||
ground_truth_depth = ground_truth_depth * outputs["directions_norm"]
|
||||
|
||||
|
||||
@@ -248,11 +248,12 @@ class NerfplayerNerfactoModel(NerfactoModel):
|
||||
outputs["rendered_pred_normal_loss"]
|
||||
)
|
||||
if "depth_image" in batch.keys() and self.config.depth_weight > 0:
|
||||
mask = (batch["depth_image"] != 0).view([-1])
|
||||
depth_image = batch["depth_image"].to(self.device)
|
||||
mask = (depth_image != 0).view([-1])
|
||||
loss_dict["depth_loss"] = 0
|
||||
|
||||
def compute_depth_loss(x):
|
||||
return self.config.depth_weight * (x - batch["depth_image"][mask]).pow(2).mean()
|
||||
return self.config.depth_weight * (x - depth_image[mask]).pow(2).mean()
|
||||
|
||||
loss_dict["depth_loss"] = compute_depth_loss(outputs["depth"][mask])
|
||||
for i in range(self.config.num_proposal_iterations):
|
||||
|
||||
@@ -226,9 +226,10 @@ class NerfplayerNGPModel(NGPModel):
|
||||
rgb_loss = self.rgb_loss(image, outputs["rgb"])
|
||||
loss_dict = {"rgb_loss": rgb_loss}
|
||||
if "depth_image" in batch.keys() and self.config.depth_weight > 0 and self.training:
|
||||
mask = batch["depth_image"] != 0
|
||||
depth_image = batch["depth_image"].to(self.device)
|
||||
mask = depth_image != 0
|
||||
# First we calculate the depth value, just like most of the papers.
|
||||
loss_dict["depth_loss"] = (outputs["depth"][mask] - batch["depth_image"][mask]).abs().mean()
|
||||
loss_dict["depth_loss"] = (outputs["depth"][mask] - depth_image[mask]).abs().mean()
|
||||
# But this is not enough -- it will lead to fog like reconstructions, even with depth supervision.
|
||||
# Because this loss only cares about the mean value.
|
||||
# (Feel free to try it and see the fog-like effects on DyCheck by commenting out the following loss.)
|
||||
@@ -244,7 +245,7 @@ class NerfplayerNGPModel(NGPModel):
|
||||
# "clear" loss (as it is directly applied to the network outputs, rather than post-processed values).
|
||||
# But such a loss also has drawbacks: it tends to overfit wrong (or noise) presented in the depth map.
|
||||
# Some structures are floating in the air with this loss...
|
||||
gt_depth_packed = batch["depth_image"][outputs["ray_indices"]]
|
||||
gt_depth_packed = depth_image[outputs["ray_indices"]]
|
||||
steps = (outputs["ray_samples"].frustums.starts + outputs["ray_samples"].frustums.ends) / 2
|
||||
# empty area should not be too close to the given depth, so lets add a margin to the gt depth
|
||||
margin = (self.scene_box.aabb.max() - self.scene_box.aabb.min()) / 128
|
||||
|
||||
@@ -254,7 +254,7 @@ class SemanticNerfWModel(Model):
|
||||
|
||||
# semantic loss
|
||||
loss_dict["semantics_loss"] = self.config.semantic_loss_weight * self.cross_entropy_loss(
|
||||
outputs["semantics"], batch["semantics"][..., 0].long()
|
||||
outputs["semantics"], batch["semantics"][..., 0].long().to(self.device)
|
||||
)
|
||||
return loss_dict
|
||||
|
||||
@@ -301,6 +301,6 @@ class SemanticNerfWModel(Model):
|
||||
images_dict["semantics_colormap"] = self.colormap.to(self.device)[semantic_labels]
|
||||
|
||||
# valid mask
|
||||
images_dict["mask"] = batch["mask"].repeat(1, 1, 3)
|
||||
images_dict["mask"] = batch["mask"].repeat(1, 1, 3).to(self.device)
|
||||
|
||||
return metrics_dict, images_dict
|
||||
Reference in new issue
Block a user