mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-12-06 15:59:16 +08:00
added test set (using training data for now)
This commit is contained in:
1 parent
7e5a5bf524
commit
cf7034f0d1
10 files changed
+112
-57
No files matched your search
+52
-19
@@ -15,7 +15,8 @@ from mattport.nerf.dataset.image_dataset import ImageDataset, collate_batch
|
||||
from mattport.nerf.dataset.utils import get_dataset_inputs
|
||||
from mattport.nerf.optimizers import Optimizers
|
||||
from mattport.utils.decorators import check_main_thread
|
||||
from mattport.utils.writer import LocalWriter
|
||||
from mattport.utils.writer import LocalWriter, TensorboardWriter
|
||||
from mattport.nerf.metrics import get_psnr
|
||||
|
||||
logging.getLogger("PIL").setLevel(logging.WARNING)
|
||||
|
||||
@@ -38,7 +39,10 @@ class Trainer:
|
||||
self.start_step = 0
|
||||
# logging variables
|
||||
self.is_main_thread = local_rank % world_size == 0
|
||||
self.local_writer = LocalWriter(local_rank, world_size, save_dir=None)
|
||||
self.local_writer = LocalWriter(local_rank, world_size, save_dir=os.path.join(os.getcwd(), "writer"))
|
||||
self.tensorboard_writer = TensorboardWriter(
|
||||
local_rank, world_size, save_dir=os.path.join(os.getcwd(), "writer")
|
||||
)
|
||||
|
||||
def setup_dataset(self):
|
||||
"""_summary_"""
|
||||
@@ -118,40 +122,69 @@ class Trainer:
|
||||
)
|
||||
|
||||
def get_aggregated_loss(self, losses):
|
||||
aggregated_loss = 0.0
|
||||
loss_sum = 0.0
|
||||
loss_dict = {}
|
||||
for loss_name in losses.keys():
|
||||
# TODO(ethan): add loss weightings here from a config
|
||||
aggregated_loss += losses[loss_name]
|
||||
return aggregated_loss
|
||||
loss_sum += losses[loss_name]
|
||||
loss_dict[loss_name] = float(losses[loss_name])
|
||||
return loss_sum, loss_dict
|
||||
|
||||
def train(self) -> None:
|
||||
"""_summary_"""
|
||||
num_iterations = 1000
|
||||
num_iterations = self.config.max_num_iterations
|
||||
for step in range(self.start_step, self.start_step + num_iterations):
|
||||
batch = next(iter(self.train_dataloader))
|
||||
losses = self.train_iteration(step, batch)
|
||||
self.local_writer.write_text(f"{step}/{num_iterations}: losses", losses)
|
||||
if step % self.config.steps_per_save == 0:
|
||||
loss_dict = self.train_iteration(batch, step)
|
||||
if step != 0 and step % self.config.steps_per_log == 0:
|
||||
self.tensorboard_writer.write_scalar_dict(loss_dict, step, group="train")
|
||||
logging.info(f"{step}/{num_iterations} with losses {loss_dict}")
|
||||
# TODO: add the learning rates to tensorboard/logging
|
||||
if self.config.steps_per_save and step != 0 and step % self.config.steps_per_save == 0:
|
||||
self.save_checkpoint(self.config.model_dir, step)
|
||||
self.local_writer.write_text(f"ckpt at {step}", f"{losses}\n")
|
||||
logging.info(f"Saved ckpt at {step}")
|
||||
if step != 0 and step % self.config.steps_per_test == 0:
|
||||
self.test_image(image_idx=0, step=step)
|
||||
|
||||
def train_iteration(self, step: int, batch: dict):
|
||||
def train_iteration(self, batch: dict, step: int):
|
||||
"""Run one iteration with a batch of inputs."""
|
||||
# move batch to correct device
|
||||
ray_indices = batch.indices.to(f"cuda:{self.local_rank}")
|
||||
graph_outputs = self.graph(ray_indices)
|
||||
batch.pixels = batch.pixels.to(f"cuda:{self.local_rank}")
|
||||
# print(batch)
|
||||
losses = self.graph.get_losses(batch, graph_outputs) # or self.graph.module
|
||||
loss = self.get_aggregated_loss(losses)
|
||||
loss_sum, loss_dict = self.get_aggregated_loss(losses)
|
||||
self.optimizers.zero_grad_all()
|
||||
loss.backward()
|
||||
self.optimizers.scheduler_step_all()
|
||||
loss_sum.backward()
|
||||
self.optimizers.scheduler_step_all(step) # NOTE(ethan): I think the scheduler needs to know what step we are on
|
||||
self.optimizers.optimizer_step_all()
|
||||
return losses
|
||||
return loss_dict
|
||||
|
||||
def test(self, image_idx):
|
||||
def test_image(self, image_idx, step):
|
||||
"""Test a specific image."""
|
||||
logging.info("Running test iteration.")
|
||||
image = self.train_dataset[image_idx]["image"] # ground truth
|
||||
image_height, image_width, _ = image.shape
|
||||
pixel_coords = torch.meshgrid(torch.arange(image_height), torch.arange(image_width), indexing="ij")
|
||||
pixel_coords = torch.stack(pixel_coords, dim=-1).long()
|
||||
all_ray_indices = torch.cat([torch.ones_like(pixel_coords[..., :1]) * image_idx, pixel_coords], dim=-1).view(
|
||||
-1, 3
|
||||
)
|
||||
with torch.no_grad():
|
||||
ray_indices = None
|
||||
pass
|
||||
num_rays = all_ray_indices.shape[0]
|
||||
chunk_size = 1024
|
||||
rgb_coarse = []
|
||||
rgb_fine = []
|
||||
for i in range(0, num_rays, chunk_size):
|
||||
ray_indices = all_ray_indices[i : i + chunk_size].to(f"cuda:{self.local_rank}")
|
||||
graph_outputs = self.graph(ray_indices)
|
||||
rgb_coarse.append(graph_outputs["rgb_coarse"])
|
||||
rgb_fine.append(graph_outputs["rgb_fine"])
|
||||
rgb_coarse = torch.cat(rgb_coarse).view(image_height, image_width, 3).detach().cpu()
|
||||
rgb_fine = torch.cat(rgb_fine).view(image_height, image_width, 3).detach().cpu()
|
||||
|
||||
combined_image = torch.cat([image, rgb_coarse, rgb_fine], dim=1)
|
||||
self.tensorboard_writer.write_image("test/rgb_coarse_fine", combined_image, step)
|
||||
|
||||
fine_psnr = get_psnr(image, rgb_fine)
|
||||
self.tensorboard_writer.write_scalar("fine_psnr", fine_psnr, step, group="test")
|
||||
Reference in new issue
Block a user