mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-01 21:39:06 +08:00
Updating metrics dict code and logic for all the graphs (#198)
* illustrating logic of metrics dict with vanilla nerf * updated all graph impls * clean up. * cleanup. * format. Co-authored-by: Alexander Kristoffersen <akristoffersen@berkeley.edu>
This commit is contained in:
1 parent
0777883fb6
commit
934bd4bd72
8 files changed
+43
-23
No files matched your search
@@ -186,12 +186,9 @@ class Graph(AbstractGraph):
|
|||||||
masked_batch = get_masked_dict(batch, valid_mask) # NOTE(ethan): this is really slow if on CPU!
|
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)
|
outputs = self.get_outputs(masked_intersected_ray_bundle)
|
||||||
metrics_dict = self.get_metrics_dict(outputs=outputs, batch=masked_batch)
|
metrics_dict = self.get_metrics_dict(outputs=outputs, batch=masked_batch)
|
||||||
loss_dict = self.get_loss_dict(outputs=outputs, batch=masked_batch)
|
loss_dict = self.get_loss_dict(
|
||||||
|
outputs=outputs, batch=masked_batch, metrics_dict=metrics_dict, loss_coefficients=self.loss_coefficients
|
||||||
# scaling losses by coefficients.
|
)
|
||||||
for loss_name in loss_dict.keys():
|
|
||||||
if loss_name in self.loss_coefficients:
|
|
||||||
loss_dict[loss_name] *= self.loss_coefficients[loss_name]
|
|
||||||
return outputs, loss_dict, metrics_dict
|
return outputs, loss_dict, metrics_dict
|
||||||
|
|
||||||
def forward(self, ray_indices: TensorType["num_rays", 3], batch: Union[str, Dict[str, torch.tensor]] = None):
|
def forward(self, ray_indices: TensorType["num_rays", 3], batch: Union[str, Dict[str, torch.tensor]] = None):
|
||||||
@@ -199,16 +196,16 @@ class Graph(AbstractGraph):
|
|||||||
ray_bundle = self.ray_generator.forward(ray_indices)
|
ray_bundle = self.ray_generator.forward(ray_indices)
|
||||||
return self.forward_after_ray_generator(ray_bundle, batch=batch)
|
return self.forward_after_ray_generator(ray_bundle, batch=batch)
|
||||||
|
|
||||||
@abstractmethod
|
|
||||||
def get_loss_dict(self, outputs, batch) -> Dict[str, torch.tensor]:
|
|
||||||
"""Computes and returns the losses dict."""
|
|
||||||
|
|
||||||
def get_metrics_dict(self, outputs, batch) -> Dict[str, torch.tensor]:
|
def get_metrics_dict(self, outputs, batch) -> Dict[str, torch.tensor]:
|
||||||
"""Compute and obtain metrics and coefficients."""
|
"""Compute and returns metrics."""
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
# pylint: disable=no-self-use
|
# pylint: disable=no-self-use
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients) -> Dict[str, torch.tensor]:
|
||||||
|
"""Computes and returns the losses dict."""
|
||||||
|
|
||||||
@torch.no_grad()
|
@torch.no_grad()
|
||||||
def get_outputs_for_camera_ray_bundle(self, camera_ray_bundle: RayBundle):
|
def get_outputs_for_camera_ray_bundle(self, camera_ray_bundle: RayBundle):
|
||||||
"""Takes in camera parameters and computes the output of the graph."""
|
"""Takes in camera parameters and computes the output of the graph."""
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ from nerfactory.fields.modules.field_heads import FieldHeadNames
|
|||||||
from nerfactory.graphs.base import Graph
|
from nerfactory.graphs.base import Graph
|
||||||
from nerfactory.graphs.modules.ray_sampler import NGPSpacedSampler
|
from nerfactory.graphs.modules.ray_sampler import NGPSpacedSampler
|
||||||
from nerfactory.optimizers.loss import MSELoss
|
from nerfactory.optimizers.loss import MSELoss
|
||||||
from nerfactory.utils import colors, visualization, writer
|
from nerfactory.utils import colors, misc, visualization, writer
|
||||||
from nerfactory.utils.callbacks import Callback
|
from nerfactory.utils.callbacks import Callback
|
||||||
|
|
||||||
|
|
||||||
@@ -106,7 +106,7 @@ class NGPGraph(Graph):
|
|||||||
}
|
}
|
||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
def get_loss_dict(self, outputs, batch):
|
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients):
|
||||||
device = self.get_device()
|
device = self.get_device()
|
||||||
image = batch["image"].to(device)
|
image = batch["image"].to(device)
|
||||||
if "mask" in outputs:
|
if "mask" in outputs:
|
||||||
@@ -115,6 +115,7 @@ class NGPGraph(Graph):
|
|||||||
else:
|
else:
|
||||||
rgb_loss = self.rgb_loss(image, outputs["rgb"])
|
rgb_loss = self.rgb_loss(image, outputs["rgb"])
|
||||||
loss_dict = {"rgb_loss": rgb_loss}
|
loss_dict = {"rgb_loss": rgb_loss}
|
||||||
|
loss_dict = misc.scale_dict(loss_dict, loss_coefficients)
|
||||||
return loss_dict
|
return loss_dict
|
||||||
|
|
||||||
def process_outputs_as_images(self, outputs): # pylint:disable=no-self-use
|
def process_outputs_as_images(self, outputs): # pylint:disable=no-self-use
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ from nerfactory.renderers.renderers import (
|
|||||||
DepthRenderer,
|
DepthRenderer,
|
||||||
RGBRenderer,
|
RGBRenderer,
|
||||||
)
|
)
|
||||||
from nerfactory.utils import colors, visualization, writer
|
from nerfactory.utils import colors, misc, visualization, writer
|
||||||
|
|
||||||
|
|
||||||
class MipNerfGraph(Graph):
|
class MipNerfGraph(Graph):
|
||||||
@@ -134,11 +134,12 @@ class MipNerfGraph(Graph):
|
|||||||
}
|
}
|
||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
def get_loss_dict(self, outputs, batch):
|
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients):
|
||||||
image = batch["image"]
|
image = batch["image"]
|
||||||
rgb_loss_coarse = self.rgb_loss(image, outputs["rgb_coarse"])
|
rgb_loss_coarse = self.rgb_loss(image, outputs["rgb_coarse"])
|
||||||
rgb_loss_fine = self.rgb_loss(image, outputs["rgb_fine"])
|
rgb_loss_fine = self.rgb_loss(image, outputs["rgb_fine"])
|
||||||
loss_dict = {"rgb_loss_coarse": rgb_loss_coarse, "rgb_loss_fine": rgb_loss_fine}
|
loss_dict = {"rgb_loss_coarse": rgb_loss_coarse, "rgb_loss_fine": rgb_loss_fine}
|
||||||
|
loss_dict = misc.scale_dict(loss_dict, loss_coefficients)
|
||||||
return loss_dict
|
return loss_dict
|
||||||
|
|
||||||
def log_test_image_outputs(self, image_idx, step, batch, outputs):
|
def log_test_image_outputs(self, image_idx, step, batch, outputs):
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ from nerfactory.renderers.renderers import (
|
|||||||
DepthRenderer,
|
DepthRenderer,
|
||||||
RGBRenderer,
|
RGBRenderer,
|
||||||
)
|
)
|
||||||
from nerfactory.utils import colors, visualization, writer
|
from nerfactory.utils import colors, misc, visualization, writer
|
||||||
|
|
||||||
|
|
||||||
class MipNerf360Graph(Graph):
|
class MipNerf360Graph(Graph):
|
||||||
@@ -143,7 +143,7 @@ class MipNerf360Graph(Graph):
|
|||||||
}
|
}
|
||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
def get_loss_dict(self, outputs, batch):
|
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients) -> Dict[str, torch.tensor]:
|
||||||
image = batch["image"]
|
image = batch["image"]
|
||||||
rgb_loss_coarse = self.rgb_loss(image, outputs["rgb_coarse"])
|
rgb_loss_coarse = self.rgb_loss(image, outputs["rgb_coarse"])
|
||||||
rgb_loss_fine = self.rgb_loss(image, outputs["rgb_fine"])
|
rgb_loss_fine = self.rgb_loss(image, outputs["rgb_fine"])
|
||||||
@@ -153,6 +153,7 @@ class MipNerf360Graph(Graph):
|
|||||||
"ray_loss_coarse": torch.mean(outputs["ray_loss_coarse"]),
|
"ray_loss_coarse": torch.mean(outputs["ray_loss_coarse"]),
|
||||||
"ray_loss_fine": torch.mean(outputs["ray_loss_fine"]),
|
"ray_loss_fine": torch.mean(outputs["ray_loss_fine"]),
|
||||||
}
|
}
|
||||||
|
loss_dict = misc.scale_dict(loss_dict, loss_coefficients)
|
||||||
return loss_dict
|
return loss_dict
|
||||||
|
|
||||||
def log_test_image_outputs(self, image_idx, step, batch, outputs):
|
def log_test_image_outputs(self, image_idx, step, batch, outputs):
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ from nerfactory.renderers.renderers import (
|
|||||||
RGBRenderer,
|
RGBRenderer,
|
||||||
UncertaintyRenderer,
|
UncertaintyRenderer,
|
||||||
)
|
)
|
||||||
from nerfactory.utils import colors, visualization, writer
|
from nerfactory.utils import colors, misc, visualization, writer
|
||||||
|
|
||||||
|
|
||||||
class NerfWGraph(Graph):
|
class NerfWGraph(Graph):
|
||||||
@@ -180,7 +180,7 @@ class NerfWGraph(Graph):
|
|||||||
}
|
}
|
||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
def get_loss_dict(self, outputs, batch):
|
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients):
|
||||||
device = outputs["rgb_coarse"].device
|
device = outputs["rgb_coarse"].device
|
||||||
image = batch["image"].to(device)
|
image = batch["image"].to(device)
|
||||||
rgb_coarse = outputs["rgb_coarse"]
|
rgb_coarse = outputs["rgb_coarse"]
|
||||||
@@ -198,6 +198,7 @@ class NerfWGraph(Graph):
|
|||||||
"uncertainty_loss": uncertainty_loss,
|
"uncertainty_loss": uncertainty_loss,
|
||||||
"density_loss": density_loss,
|
"density_loss": density_loss,
|
||||||
}
|
}
|
||||||
|
loss_dict = misc.scale_dict(loss_dict, loss_coefficients)
|
||||||
return loss_dict
|
return loss_dict
|
||||||
|
|
||||||
def log_test_image_outputs(self, image_idx, step, batch, outputs):
|
def log_test_image_outputs(self, image_idx, step, batch, outputs):
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ from nerfactory.fields.modules.mlp import MLP
|
|||||||
from nerfactory.fields.nerf_field import NeRFField
|
from nerfactory.fields.nerf_field import NeRFField
|
||||||
from nerfactory.graphs.vanilla_nerf import NeRFGraph
|
from nerfactory.graphs.vanilla_nerf import NeRFGraph
|
||||||
from nerfactory.renderers.renderers import SemanticRenderer
|
from nerfactory.renderers.renderers import SemanticRenderer
|
||||||
from nerfactory.utils import writer
|
from nerfactory.utils import misc, writer
|
||||||
|
|
||||||
|
|
||||||
class SemanticNerfField(Field):
|
class SemanticNerfField(Field):
|
||||||
@@ -167,7 +167,7 @@ class SemanticNerfGraph(NeRFGraph):
|
|||||||
}
|
}
|
||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
def get_loss_dict(self, outputs, batch):
|
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients):
|
||||||
image = batch["image"]
|
image = batch["image"]
|
||||||
rgb_loss_coarse = self.rgb_loss(image, outputs["rgb_coarse"])
|
rgb_loss_coarse = self.rgb_loss(image, outputs["rgb_coarse"])
|
||||||
rgb_loss_fine = self.rgb_loss(image, outputs["rgb_fine"])
|
rgb_loss_fine = self.rgb_loss(image, outputs["rgb_fine"])
|
||||||
@@ -179,6 +179,7 @@ class SemanticNerfGraph(NeRFGraph):
|
|||||||
"rgb_loss_fine": rgb_loss_fine,
|
"rgb_loss_fine": rgb_loss_fine,
|
||||||
"semantic_loss_fine": semantic_loss_fine,
|
"semantic_loss_fine": semantic_loss_fine,
|
||||||
}
|
}
|
||||||
|
loss_dict = misc.scale_dict(loss_dict, loss_coefficients)
|
||||||
return loss_dict
|
return loss_dict
|
||||||
|
|
||||||
def log_test_image_outputs(self, image_idx, step, batch, outputs):
|
def log_test_image_outputs(self, image_idx, step, batch, outputs):
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ from nerfactory.renderers.renderers import (
|
|||||||
DepthRenderer,
|
DepthRenderer,
|
||||||
RGBRenderer,
|
RGBRenderer,
|
||||||
)
|
)
|
||||||
from nerfactory.utils import colors, visualization, writer
|
from nerfactory.utils import colors, misc, visualization, writer
|
||||||
from nerfactory.utils.callbacks import Callback
|
from nerfactory.utils.callbacks import Callback
|
||||||
|
|
||||||
|
|
||||||
@@ -167,7 +167,8 @@ class NeRFGraph(Graph):
|
|||||||
}
|
}
|
||||||
return outputs
|
return outputs
|
||||||
|
|
||||||
def get_loss_dict(self, outputs, batch) -> Dict[str, torch.tensor]:
|
def get_loss_dict(self, outputs, batch, metrics_dict, loss_coefficients) -> Dict[str, torch.tensor]:
|
||||||
|
# Scaling metrics by coefficients to create the losses.
|
||||||
device = outputs["rgb_coarse"].device
|
device = outputs["rgb_coarse"].device
|
||||||
image = batch["image"].to(device)
|
image = batch["image"].to(device)
|
||||||
|
|
||||||
@@ -175,6 +176,7 @@ class NeRFGraph(Graph):
|
|||||||
rgb_loss_fine = self.rgb_loss(image, outputs["rgb_fine"])
|
rgb_loss_fine = self.rgb_loss(image, outputs["rgb_fine"])
|
||||||
|
|
||||||
loss_dict = {"rgb_loss_coarse": rgb_loss_coarse, "rgb_loss_fine": rgb_loss_fine}
|
loss_dict = {"rgb_loss_coarse": rgb_loss_coarse, "rgb_loss_fine": rgb_loss_fine}
|
||||||
|
loss_dict = misc.scale_dict(loss_dict, loss_coefficients)
|
||||||
return loss_dict
|
return loss_dict
|
||||||
|
|
||||||
def log_test_image_outputs(self, image_idx, step, batch, outputs):
|
def log_test_image_outputs(self, image_idx, step, batch, outputs):
|
||||||
|
|||||||
@@ -126,3 +126,19 @@ def human_format(num):
|
|||||||
k = 1000.0
|
k = 1000.0
|
||||||
magnitude = int(floor(log(num, k)))
|
magnitude = int(floor(log(num, k)))
|
||||||
return f"{(num / k**magnitude):.2f} {units[magnitude]}"
|
return f"{(num / k**magnitude):.2f} {units[magnitude]}"
|
||||||
|
|
||||||
|
|
||||||
|
def scale_dict(dictionary: Dict[Any, Any], coefficients: DictConfig) -> Dict[Any, Any]:
|
||||||
|
"""Scale a dictionary in-place given a coefficients dictionary.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
dictionary: input dict to be scaled.
|
||||||
|
coefficients: scalar dict config for holding coefficients.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Input dict scaled by coefficients.
|
||||||
|
"""
|
||||||
|
for key in dictionary:
|
||||||
|
if key in coefficients:
|
||||||
|
dictionary[key] *= coefficients[key]
|
||||||
|
return dictionary
|
||||||
Reference in new issue
Block a user