mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-04 14:58:55 +08:00
Generalize get average image metrics (#3188)
generalize get average image metrics Co-authored-by: Le Hoanh <leh19@csl-leh19.cs.aalto.fi> Co-authored-by: J.Y <132313008+jb-ye@users.noreply.github.com>
This commit is contained in:
1 file changed
+31
-9
@@ -345,12 +345,19 @@ class VanillaPipeline(Pipeline):
|
|||||||
return metrics_dict, images_dict
|
return metrics_dict, images_dict
|
||||||
|
|
||||||
@profiler.time_function
|
@profiler.time_function
|
||||||
def get_average_eval_image_metrics(
|
def get_average_image_metrics(
|
||||||
self, step: Optional[int] = None, output_path: Optional[Path] = None, get_std: bool = False
|
self,
|
||||||
|
data_loader,
|
||||||
|
image_prefix: str,
|
||||||
|
step: Optional[int] = None,
|
||||||
|
output_path: Optional[Path] = None,
|
||||||
|
get_std: bool = False,
|
||||||
):
|
):
|
||||||
"""Iterate over all the images in the eval dataset and get the average.
|
"""Iterate over all the images in the dataset and get the average.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
data_loader: the data loader to iterate over
|
||||||
|
image_prefix: prefix to use for the saved image filenames
|
||||||
step: current training step
|
step: current training step
|
||||||
output_path: optional path to save rendered images to
|
output_path: optional path to save rendered images to
|
||||||
get_std: Set True if you want to return std with the mean metric.
|
get_std: Set True if you want to return std with the mean metric.
|
||||||
@@ -360,8 +367,7 @@ class VanillaPipeline(Pipeline):
|
|||||||
"""
|
"""
|
||||||
self.eval()
|
self.eval()
|
||||||
metrics_dict_list = []
|
metrics_dict_list = []
|
||||||
assert isinstance(self.datamanager, (VanillaDataManager, ParallelDataManager, FullImageDatamanager))
|
num_images = len(data_loader)
|
||||||
num_images = len(self.datamanager.fixed_indices_eval_dataloader)
|
|
||||||
if output_path is not None:
|
if output_path is not None:
|
||||||
output_path.mkdir(exist_ok=True, parents=True)
|
output_path.mkdir(exist_ok=True, parents=True)
|
||||||
with Progress(
|
with Progress(
|
||||||
@@ -371,9 +377,9 @@ class VanillaPipeline(Pipeline):
|
|||||||
MofNCompleteColumn(),
|
MofNCompleteColumn(),
|
||||||
transient=True,
|
transient=True,
|
||||||
) as progress:
|
) as progress:
|
||||||
task = progress.add_task("[green]Evaluating all eval images...", total=num_images)
|
task = progress.add_task("[green]Evaluating all images...", total=num_images)
|
||||||
idx = 0
|
idx = 0
|
||||||
for camera, batch in self.datamanager.fixed_indices_eval_dataloader:
|
for camera, batch in data_loader:
|
||||||
# time this the following line
|
# time this the following line
|
||||||
inner_start = time()
|
inner_start = time()
|
||||||
outputs = self.model.get_outputs_for_camera(camera=camera)
|
outputs = self.model.get_outputs_for_camera(camera=camera)
|
||||||
@@ -383,7 +389,9 @@ class VanillaPipeline(Pipeline):
|
|||||||
if output_path is not None:
|
if output_path is not None:
|
||||||
for key in image_dict.keys():
|
for key in image_dict.keys():
|
||||||
image = image_dict[key] # [H, W, C] order
|
image = image_dict[key] # [H, W, C] order
|
||||||
vutils.save_image(image.permute(2, 0, 1).cpu(), output_path / f"eval_{key}_{idx:04d}.png")
|
vutils.save_image(
|
||||||
|
image.permute(2, 0, 1).cpu(), output_path / f"{image_prefix}_{key}_{idx:04d}.png"
|
||||||
|
)
|
||||||
|
|
||||||
assert "num_rays_per_sec" not in metrics_dict
|
assert "num_rays_per_sec" not in metrics_dict
|
||||||
metrics_dict["num_rays_per_sec"] = (num_rays / (time() - inner_start)).item()
|
metrics_dict["num_rays_per_sec"] = (num_rays / (time() - inner_start)).item()
|
||||||
@@ -393,7 +401,7 @@ class VanillaPipeline(Pipeline):
|
|||||||
metrics_dict_list.append(metrics_dict)
|
metrics_dict_list.append(metrics_dict)
|
||||||
progress.advance(task)
|
progress.advance(task)
|
||||||
idx = idx + 1
|
idx = idx + 1
|
||||||
# average the metrics list
|
|
||||||
metrics_dict = {}
|
metrics_dict = {}
|
||||||
for key in metrics_dict_list[0].keys():
|
for key in metrics_dict_list[0].keys():
|
||||||
if get_std:
|
if get_std:
|
||||||
@@ -406,9 +414,23 @@ class VanillaPipeline(Pipeline):
|
|||||||
metrics_dict[key] = float(
|
metrics_dict[key] = float(
|
||||||
torch.mean(torch.tensor([metrics_dict[key] for metrics_dict in metrics_dict_list]))
|
torch.mean(torch.tensor([metrics_dict[key] for metrics_dict in metrics_dict_list]))
|
||||||
)
|
)
|
||||||
|
|
||||||
self.train()
|
self.train()
|
||||||
return metrics_dict
|
return metrics_dict
|
||||||
|
|
||||||
|
@profiler.time_function
|
||||||
|
def get_average_eval_image_metrics(
|
||||||
|
self, step: Optional[int] = None, output_path: Optional[Path] = None, get_std: bool = False
|
||||||
|
):
|
||||||
|
"""Get the average metrics for evaluation images."""
|
||||||
|
|
||||||
|
"""Get the average metrics for evaluation images."""
|
||||||
|
assert isinstance(self.datamanager, (VanillaDataManager, ParallelDataManager, FullImageDatamanager))
|
||||||
|
image_prefix = "eval"
|
||||||
|
return self.get_average_image_metrics(
|
||||||
|
self.datamanager.fixed_indices_eval_dataloader, image_prefix, step, output_path, get_std
|
||||||
|
)
|
||||||
|
|
||||||
def load_pipeline(self, loaded_state: Dict[str, Any], step: int) -> None:
|
def load_pipeline(self, loaded_state: Dict[str, Any], step: int) -> None:
|
||||||
"""Load the checkpoint from the given path
|
"""Load the checkpoint from the given path
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user