mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-03 22:38:48 +08:00
updating metrics logging (#460)
* integrated changes * update * stratified * black * bump version * upgrade version * improved wandb logging * fix split for eval * comments
This commit is contained in:
1 parent
4c1e443a41
commit
e22eab8c86
22 files changed
+300
-219
No files matched your search
@@ -21,6 +21,7 @@ import typing
|
||||
from abc import abstractmethod
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
import torch
|
||||
import torch.distributed as dist
|
||||
from torch import nn
|
||||
from torch.nn import Parameter
|
||||
@@ -34,6 +35,15 @@ from nerfactory.utils.callbacks import TrainingCallback, TrainingCallbackAttribu
|
||||
from nerfactory.utils.writer import EventName, TimeWriter
|
||||
|
||||
|
||||
def module_wrapper(module: nn.Module) -> nn.Module:
|
||||
"""
|
||||
If DDP, then return the .module. Otherwise, return the model.
|
||||
"""
|
||||
if isinstance(module, DDP):
|
||||
return module.module
|
||||
return module
|
||||
|
||||
|
||||
class Pipeline(nn.Module):
|
||||
"""The intent of this class is to provide a higher level interface for the Model
|
||||
that will be easy to use for our Trainer class.
|
||||
@@ -103,7 +113,7 @@ class Pipeline(nn.Module):
|
||||
@property
|
||||
def device(self):
|
||||
"""Returns the device that the model is on."""
|
||||
return self.model.module.device if self.world_size > 1 else self.model.device
|
||||
return module_wrapper(self.model).device
|
||||
|
||||
@abstractmethod
|
||||
@profiler.time_function
|
||||
@@ -116,8 +126,8 @@ class Pipeline(nn.Module):
|
||||
step: current iteration step to update sampler if using DDP (distributed)
|
||||
"""
|
||||
if self.world_size > 1:
|
||||
self.datamanager.sampler.set_epoch(step)
|
||||
ray_bundle, batch = self.datamanager.next_train()
|
||||
self.datamanager.train_image_dataloader.sampler.set_epoch(step)
|
||||
ray_bundle, batch = self.datamanager.next_train(step)
|
||||
model_outputs, loss_dict, metrics_dict = self.model(ray_bundle, batch)
|
||||
return model_outputs, loss_dict, metrics_dict
|
||||
|
||||
@@ -131,25 +141,54 @@ class Pipeline(nn.Module):
|
||||
step: current iteration step
|
||||
"""
|
||||
self.eval()
|
||||
# NOTE(ethan): next_eval() is not being used right now
|
||||
assert self.datamanager.eval_dataloader is not None
|
||||
for camera_ray_bundle, batch in self.datamanager.eval_dataloader:
|
||||
with TimeWriter(None, None, write=False) as test_t:
|
||||
assert camera_ray_bundle.camera_indices is not None
|
||||
image_idx = int(camera_ray_bundle.camera_indices[0, 0])
|
||||
if self.world_size > 1:
|
||||
outputs = self.model.module.get_outputs_for_camera_ray_bundle(camera_ray_bundle)
|
||||
metrics_dict = self.model.module.log_test_image_outputs(image_idx, step, batch, outputs)
|
||||
else:
|
||||
outputs = self.model.get_outputs_for_camera_ray_bundle(camera_ray_bundle)
|
||||
metrics_dict = self.model.log_test_image_outputs(image_idx, step, batch, outputs)
|
||||
if step is not None:
|
||||
writer.put_time(
|
||||
name=EventName.TEST_RAYS_PER_SEC,
|
||||
duration=len(camera_ray_bundle) / test_t.duration,
|
||||
step=step,
|
||||
avg_over_steps=True,
|
||||
)
|
||||
if self.world_size > 1:
|
||||
self.datamanager.eval_image_dataloader.sampler.set_epoch(step)
|
||||
ray_bundle, batch = self.datamanager.next_eval(step)
|
||||
model_outputs, loss_dict, metrics_dict = self.model(ray_bundle, batch)
|
||||
self.train()
|
||||
return model_outputs, loss_dict, metrics_dict
|
||||
|
||||
@abstractmethod
|
||||
@profiler.time_function
|
||||
def get_eval_image_metrics_and_images(self, step: Optional[int] = None):
|
||||
"""This function gets your evaluation loss dict. It needs to get the data
|
||||
from the DataManager and feed it to the model's forward function
|
||||
|
||||
Args:
|
||||
step: current iteration step
|
||||
"""
|
||||
self.eval()
|
||||
image_idx, camera_ray_bundle, batch = self.datamanager.next_eval_image(step)
|
||||
with TimeWriter(None, None, write=False) as test_t:
|
||||
outputs = module_wrapper(self.model).get_outputs_for_camera_ray_bundle(camera_ray_bundle)
|
||||
metrics_dict, images_dict = module_wrapper(self.model).get_image_metrics_and_images(outputs, batch)
|
||||
writer.put_time(
|
||||
name=EventName.TEST_RAYS_PER_SEC,
|
||||
duration=len(camera_ray_bundle) / test_t.duration,
|
||||
step=step,
|
||||
avg_over_steps=True,
|
||||
)
|
||||
metrics_dict["image_idx"] = image_idx
|
||||
self.train()
|
||||
return metrics_dict, images_dict
|
||||
|
||||
@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."""
|
||||
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)
|
||||
# average the metrics list
|
||||
metrics_dict = {}
|
||||
for key in metrics_dict_list[0].keys():
|
||||
metrics_dict[key] = float(
|
||||
torch.mean(torch.tensor([metrics_dict[key] for metrics_dict in metrics_dict_list]))
|
||||
)
|
||||
self.train()
|
||||
return metrics_dict
|
||||
|
||||
@@ -171,10 +210,7 @@ class Pipeline(nn.Module):
|
||||
) -> List[TrainingCallback]:
|
||||
"""Returns the training callbacks from both the Dataloader and the Model."""
|
||||
datamanager_callbacks = self.datamanager.get_training_callbacks(training_callback_attributes)
|
||||
if self.world_size > 1:
|
||||
model_callbacks = self.model.module.get_training_callbacks(training_callback_attributes)
|
||||
else:
|
||||
model_callbacks = self.model.get_training_callbacks(training_callback_attributes)
|
||||
model_callbacks = module_wrapper(self.model).get_training_callbacks(training_callback_attributes)
|
||||
callbacks = datamanager_callbacks + model_callbacks
|
||||
return callbacks
|
||||
|
||||
@@ -185,9 +221,6 @@ class Pipeline(nn.Module):
|
||||
A list of dictionaries containing the pipeline's param groups.
|
||||
"""
|
||||
datamanager_params = self.datamanager.get_param_groups()
|
||||
if self.world_size > 1:
|
||||
model_params = self.model.module.get_param_groups()
|
||||
else:
|
||||
model_params = self.model.get_param_groups()
|
||||
model_params = module_wrapper(self.model).get_param_groups()
|
||||
# TODO(ethan): assert that key names don't overlap
|
||||
return {**datamanager_params, **model_params}
|
||||
Reference in new issue
Block a user