mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-03 22:38:48 +08:00
1 parent
7fc970f1c7
commit
1d14263127
10 files changed
+102
-153
No files matched your search
@@ -19,10 +19,18 @@ from __future__ import annotations
|
||||
|
||||
import typing
|
||||
from abc import abstractmethod
|
||||
from typing import Any, Dict, List, Optional
|
||||
from time import time
|
||||
from typing import Any, Dict, List, Optional, Union, cast
|
||||
|
||||
import torch
|
||||
import torch.distributed as dist
|
||||
from rich.progress import (
|
||||
BarColumn,
|
||||
MofNCompleteColumn,
|
||||
Progress,
|
||||
TextColumn,
|
||||
TimeElapsedColumn,
|
||||
)
|
||||
from torch import nn
|
||||
from torch.nn import Parameter
|
||||
from torch.nn.parallel import DistributedDataParallel as DDP
|
||||
@@ -34,13 +42,13 @@ from nerfactory.utils import profiler
|
||||
from nerfactory.utils.callbacks import TrainingCallback, TrainingCallbackAttributes
|
||||
|
||||
|
||||
def module_wrapper(module: nn.Module) -> nn.Module:
|
||||
def module_wrapper(ddp_or_model: Union[DDP, Model]) -> Model:
|
||||
"""
|
||||
If DDP, then return the .module. Otherwise, return the model.
|
||||
"""
|
||||
if isinstance(module, DDP):
|
||||
return module.module
|
||||
return module
|
||||
if isinstance(ddp_or_model, DDP):
|
||||
return cast(Model, ddp_or_model.module)
|
||||
return ddp_or_model
|
||||
|
||||
|
||||
class Pipeline(nn.Module):
|
||||
@@ -124,9 +132,6 @@ class Pipeline(nn.Module):
|
||||
Args:
|
||||
step: current iteration step to update sampler if using DDP (distributed)
|
||||
"""
|
||||
if self.world_size > 1 and step:
|
||||
assert self.datamanager.train_sampler is not None
|
||||
self.datamanager.train_sampler.set_epoch(step)
|
||||
ray_bundle, batch = self.datamanager.next_train(step)
|
||||
model_outputs = self.model(ray_bundle, batch)
|
||||
metrics_dict = self.model.get_metrics_dict(model_outputs, batch)
|
||||
@@ -144,9 +149,6 @@ class Pipeline(nn.Module):
|
||||
step: current iteration step
|
||||
"""
|
||||
self.eval()
|
||||
if self.world_size > 1:
|
||||
assert self.datamanager.eval_sampler is not None
|
||||
self.datamanager.eval_sampler.set_epoch(step)
|
||||
ray_bundle, batch = self.datamanager.next_eval(step)
|
||||
model_outputs = self.model(ray_bundle, batch)
|
||||
loss_dict = self.model.get_loss_dict(model_outputs, batch)
|
||||
@@ -177,14 +179,36 @@ class Pipeline(nn.Module):
|
||||
@abstractmethod
|
||||
@profiler.time_function
|
||||
def get_average_eval_image_metrics(self, step: Optional[int] = None):
|
||||
"""Iterate over all the images in the eval dataset and get the average."""
|
||||
"""Iterate over all the images in the eval dataset and get the average.
|
||||
|
||||
Returns:
|
||||
metrics_dict: dictionary of metrics
|
||||
"""
|
||||
self.eval()
|
||||
metrics_dict_list = []
|
||||
# TODO: add something like tqdm but so that it doesn't interfere with logging
|
||||
for camera_ray_bundle, batch in self.datamanager.fixed_indices_eval_dataloader:
|
||||
outputs = module_wrapper(self.model).get_outputs_for_camera_ray_bundle(camera_ray_bundle)
|
||||
metrics_dict, _ = module_wrapper(self.model).get_image_metrics_and_images(outputs, batch)
|
||||
metrics_dict_list.append(metrics_dict)
|
||||
num_images = len(self.datamanager.fixed_indices_eval_dataloader)
|
||||
with Progress(
|
||||
TextColumn("[progress.description]{task.description}"),
|
||||
BarColumn(),
|
||||
TimeElapsedColumn(),
|
||||
MofNCompleteColumn(),
|
||||
transient=True,
|
||||
) as progress:
|
||||
task = progress.add_task("[green]Evaluating all eval images...", total=num_images)
|
||||
for camera_ray_bundle, batch in self.datamanager.fixed_indices_eval_dataloader:
|
||||
# time this the following line
|
||||
inner_start = time()
|
||||
height, width = camera_ray_bundle.shape
|
||||
num_rays = height * width
|
||||
outputs = module_wrapper(self.model).get_outputs_for_camera_ray_bundle(camera_ray_bundle)
|
||||
metrics_dict, _ = module_wrapper(self.model).get_image_metrics_and_images(outputs, batch)
|
||||
assert "num_rays_per_sec" not in metrics_dict
|
||||
metrics_dict["num_rays_per_sec"] = num_rays / (time() - inner_start)
|
||||
fps_str = f"fps_at_{height}x{width}"
|
||||
assert fps_str not in metrics_dict
|
||||
metrics_dict[fps_str] = metrics_dict["num_rays_per_sec"] / (height * width)
|
||||
metrics_dict_list.append(metrics_dict)
|
||||
progress.advance(task)
|
||||
# average the metrics list
|
||||
metrics_dict = {}
|
||||
for key in metrics_dict_list[0].keys():
|
||||
@@ -194,10 +218,6 @@ class Pipeline(nn.Module):
|
||||
self.train()
|
||||
return metrics_dict
|
||||
|
||||
@abstractmethod
|
||||
def log_test_image_outputs(self) -> None:
|
||||
"""Log the test image outputs"""
|
||||
|
||||
def load_pipeline(self, loaded_state: Dict[str, Any]) -> None:
|
||||
"""Load the checkpoint from the given path
|
||||
|
||||
|
||||
Reference in new issue
Block a user