mirror of
https://github.com/nerfstudio-project/nerfstudio.git
synced 2026-11-25 10:29:51 +08:00
refactoring configs; pulled network out of graph; created trainer heirarchy (#51)
* changed config graph structure * removed network udner graphs; added new heirarchy trainer * fixed callback in occupancy graph * lint * changed register callback to setter method
This commit is contained in:
1 parent
41073540c1
commit
91ef54963c
19 files changed
+163
-134
No files matched your search
@@ -78,7 +78,7 @@ python scripts/run_train.py data/dataset=blender_lego
|
|||||||
python scripts/run_train.py data/dataset=friends_TBBT-big_living_room
|
python scripts/run_train.py data/dataset=friends_TBBT-big_living_room
|
||||||
|
|
||||||
# Run with different datasets and config changes
|
# Run with different datasets and config changes
|
||||||
python scripts/run_train.py data/dataset=friends_TBBT-big_living_room graph.network.far_plane=14
|
python scripts/run_train.py data/dataset=friends_TBBT-big_living_room graph.far_plane=14
|
||||||
|
|
||||||
# [Experimental] Speed up the dataloading pipeline by caching DatasetInputs.
|
# [Experimental] Speed up the dataloading pipeline by caching DatasetInputs.
|
||||||
python scripts/run_data_preprocessor.py
|
python scripts/run_data_preprocessor.py
|
||||||
|
|||||||
+21
-20
@@ -16,15 +16,24 @@ logging:
|
|||||||
# writer logs losses and images per iteration
|
# writer logs losses and images per iteration
|
||||||
writer:
|
writer:
|
||||||
TensorboardWriter:
|
TensorboardWriter:
|
||||||
save_dir: "./"
|
log_dir: "./"
|
||||||
LocalWriter:
|
LocalWriter:
|
||||||
save_dir: "./"
|
log_dir: "./"
|
||||||
stats_to_track:
|
stats_to_track:
|
||||||
[ITER_LOAD_TIME, ITER_TRAIN_TIME, RAYS_PER_SEC, CURR_TEST_PSNR]
|
[ITER_LOAD_TIME, ITER_TRAIN_TIME, RAYS_PER_SEC, CURR_TEST_PSNR]
|
||||||
max_log_size: 10 # if 0, logs everything with no erasing
|
max_log_size: 10 # if 0, logs everything with no erasing
|
||||||
# profiler logs run times of functions and prints at end of training
|
# profiler logs run times of functions and prints at end of training
|
||||||
enable_profiler: True
|
enable_profiler: True
|
||||||
|
|
||||||
|
trainer:
|
||||||
|
model_dir: /tmp/pyrad_models/
|
||||||
|
steps_per_save: 100
|
||||||
|
steps_per_test: 1000
|
||||||
|
max_num_iterations: 1000000
|
||||||
|
resume_train:
|
||||||
|
load_dir: # pretrained model directory
|
||||||
|
load_step: # pretrained model's step number
|
||||||
|
|
||||||
experiment_name: blender_lego
|
experiment_name: blender_lego
|
||||||
method_name: vanilla_nerf
|
method_name: vanilla_nerf
|
||||||
|
|
||||||
@@ -68,24 +77,16 @@ data:
|
|||||||
# num_rays_per_chunk: 4096
|
# num_rays_per_chunk: 4096
|
||||||
|
|
||||||
graph:
|
graph:
|
||||||
model_dir: /tmp/pyrad_models/
|
_target_: pyrad.graphs.vanilla_nerf.NeRFGraph
|
||||||
steps_per_save: 100
|
collider_config:
|
||||||
steps_per_test: 1000
|
_target_: pyrad.graphs.modules.scene_colliders.NearFarCollider
|
||||||
max_num_iterations: 1000000
|
near_plane: 2.0
|
||||||
resume_train:
|
far_plane: 6.0
|
||||||
load_dir: # pretrained model directory
|
num_coarse_samples: 64
|
||||||
load_step: # pretrained model's step number
|
num_importance_samples: 128
|
||||||
network:
|
loss_coefficients:
|
||||||
_target_: pyrad.graphs.vanilla_nerf.NeRFGraph
|
rgb_loss_coarse: 1.0
|
||||||
collider_config:
|
rgb_loss_fine: 1.0
|
||||||
_target_: pyrad.graphs.modules.scene_colliders.NearFarCollider
|
|
||||||
near_plane: 2.0
|
|
||||||
far_plane: 6.0
|
|
||||||
num_coarse_samples: 64
|
|
||||||
num_importance_samples: 128
|
|
||||||
loss_coefficients:
|
|
||||||
rgb_loss_coarse: 1.0
|
|
||||||
rgb_loss_fine: 1.0
|
|
||||||
|
|
||||||
optimizers:
|
optimizers:
|
||||||
fields:
|
fields:
|
||||||
|
|||||||
@@ -10,13 +10,12 @@ data:
|
|||||||
num_rays_per_chunk: 16384 # for evaluation
|
num_rays_per_chunk: 16384 # for evaluation
|
||||||
|
|
||||||
graph:
|
graph:
|
||||||
network:
|
_target_: pyrad.graphs.instant_ngp.NGPGraph
|
||||||
_target_: pyrad.graphs.instant_ngp.NGPGraph
|
collider_config:
|
||||||
collider_config:
|
_target_: pyrad.graphs.modules.scene_colliders.AABBBoxCollider
|
||||||
_target_: pyrad.graphs.modules.scene_colliders.AABBBoxCollider
|
field_implementation: tcnn # torch, tcnn, ...
|
||||||
field_implementation: tcnn # torch, tcnn, ...
|
loss_coefficients: # loss coefficients
|
||||||
loss_coefficients: # loss coefficients
|
rgb_loss: 1.0
|
||||||
rgb_loss: 1.0
|
|
||||||
|
|
||||||
# optimizer options for network
|
# optimizer options for network
|
||||||
optimizers:
|
optimizers:
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ method_name: mipnerf
|
|||||||
# rgb = sigmoid(x) rgb = (1 + 2e) / (1 + exp(-x)) -e
|
# rgb = sigmoid(x) rgb = (1 + 2e) / (1 + exp(-x)) -e
|
||||||
|
|
||||||
graph:
|
graph:
|
||||||
network:
|
_target_: pyrad.graphs.mipnerf.MipNerfGraph
|
||||||
_target_: pyrad.graphs.mipnerf.MipNerfGraph
|
num_coarse_samples: 128
|
||||||
num_coarse_samples: 128
|
num_importance_samples: 128
|
||||||
num_importance_samples: 128
|
|
||||||
+10
-11
@@ -21,14 +21,13 @@ data:
|
|||||||
downscale_factor: 1
|
downscale_factor: 1
|
||||||
|
|
||||||
graph:
|
graph:
|
||||||
network:
|
_target_: pyrad.graphs.nerfw.NerfWGraph
|
||||||
_target_: pyrad.graphs.nerfw.NerfWGraph
|
collider_config:
|
||||||
collider_config:
|
_target_: pyrad.graphs.modules.scene_colliders.AABBBoxCollider
|
||||||
_target_: pyrad.graphs.modules.scene_colliders.AABBBoxCollider
|
num_coarse_samples: 64
|
||||||
num_coarse_samples: 64
|
num_importance_samples: 64
|
||||||
num_importance_samples: 64
|
loss_coefficients:
|
||||||
loss_coefficients:
|
rgb_loss_coarse: 1.0
|
||||||
rgb_loss_coarse: 1.0
|
rgb_loss_fine: 1.0
|
||||||
rgb_loss_fine: 1.0
|
uncertainty_loss: 1.0
|
||||||
uncertainty_loss: 1.0
|
density_loss: 0.01
|
||||||
density_loss: 0.01
|
|
||||||
@@ -21,13 +21,12 @@ data:
|
|||||||
downscale_factor: 1
|
downscale_factor: 1
|
||||||
|
|
||||||
graph:
|
graph:
|
||||||
network:
|
_target_: pyrad.graphs.semantic_nerf.SemanticNerfGraph
|
||||||
_target_: pyrad.graphs.semantic_nerf.SemanticNerfGraph
|
collider_config:
|
||||||
collider_config:
|
_target_: pyrad.graphs.modules.scene_colliders.AABBBoxCollider
|
||||||
_target_: pyrad.graphs.modules.scene_colliders.AABBBoxCollider
|
num_coarse_samples: 64
|
||||||
num_coarse_samples: 64
|
num_importance_samples: 64
|
||||||
num_importance_samples: 64
|
loss_coefficients:
|
||||||
loss_coefficients:
|
rgb_loss_coarse: 1.0
|
||||||
rgb_loss_coarse: 1.0
|
rgb_loss_fine: 1.0
|
||||||
rgb_loss_fine: 1.0
|
semantic_loss_fine: 0.05
|
||||||
semantic_loss_fine: 0.05
|
|
||||||
@@ -4,6 +4,9 @@ defaults:
|
|||||||
machine_config:
|
machine_config:
|
||||||
num_gpus: 0
|
num_gpus: 0
|
||||||
|
|
||||||
|
trainer:
|
||||||
|
max_num_iterations: 2
|
||||||
|
|
||||||
data:
|
data:
|
||||||
dataset_inputs_train:
|
dataset_inputs_train:
|
||||||
data_directory: tests/data/lego_test
|
data_directory: tests/data/lego_test
|
||||||
@@ -20,15 +23,13 @@ data:
|
|||||||
downscale_factor: 16
|
downscale_factor: 16
|
||||||
|
|
||||||
graph:
|
graph:
|
||||||
max_num_iterations: 2
|
num_coarse_samples: 4
|
||||||
network:
|
num_importance_samples: 4
|
||||||
num_coarse_samples: 4
|
|
||||||
num_importance_samples: 4
|
|
||||||
|
|
||||||
logging:
|
logging:
|
||||||
writer:
|
writer:
|
||||||
TensorboardWriter:
|
TensorboardWriter:
|
||||||
save_dir: "/tmp/"
|
log_dir: "/tmp/"
|
||||||
|
|
||||||
hydra:
|
hydra:
|
||||||
searchpath:
|
searchpath:
|
||||||
|
|||||||
@@ -11,9 +11,9 @@ logging:
|
|||||||
max_buffer_size: 20 # defines how many steps to average over (if reporting averages)
|
max_buffer_size: 20 # defines how many steps to average over (if reporting averages)
|
||||||
writer: # definition of all writers you want to use
|
writer: # definition of all writers you want to use
|
||||||
TensorboardWriter:
|
TensorboardWriter:
|
||||||
save_dir: "./" # must specify the output path for all writers
|
log_dir: "./" # must specify the output path for all writers
|
||||||
LocalWriter:
|
LocalWriter:
|
||||||
save_dir: "./"
|
log_dir: "./"
|
||||||
stats_to_track:
|
stats_to_track:
|
||||||
[ITER_LOAD_TIME, ITER_TRAIN_TIME, RAYS_PER_SEC, CURR_TEST_PSNR]
|
[ITER_LOAD_TIME, ITER_TRAIN_TIME, RAYS_PER_SEC, CURR_TEST_PSNR]
|
||||||
max_log_size: 10
|
max_log_size: 10
|
||||||
|
|||||||
@@ -115,20 +115,27 @@ To set up the graph config, create a new yaml under `configs/`.
|
|||||||
# configs/vanilla_nerf.yaml
|
# configs/vanilla_nerf.yaml
|
||||||
|
|
||||||
defaults:
|
defaults:
|
||||||
- default_setup # inherit the basic yaml heirarchy
|
- graph_default # inherit the basic yaml heirarchy
|
||||||
- _self_
|
- _self_
|
||||||
|
|
||||||
|
logging:
|
||||||
|
# <insert logging definitions here>
|
||||||
|
|
||||||
|
trainer:
|
||||||
|
# <override configurations for loading/training/saving model here>
|
||||||
|
|
||||||
experiment_name: blender_lego
|
experiment_name: blender_lego
|
||||||
method_name: vanilla_nerf
|
method_name: vanilla_nerf
|
||||||
|
|
||||||
graph:
|
|
||||||
network:
|
|
||||||
_target_: pyrad.graph.vanilla_nerf.NeRFGraph # set the target to the graph you defined
|
|
||||||
|
|
||||||
# <insert any additional graph related overrides here>
|
|
||||||
|
|
||||||
data:
|
data:
|
||||||
# <insert any additional dataset related overrides here>
|
# <insert any dataset related overrides here>
|
||||||
|
|
||||||
|
graph:
|
||||||
|
_target_: pyrad.graph.vanilla_nerf.NeRFGraph # set the target to the graph you defined
|
||||||
|
# <insert any graph related overrides here>
|
||||||
|
|
||||||
|
optimizers:
|
||||||
|
# <insert any optimizer related overrides here>
|
||||||
```
|
```
|
||||||
|
|
||||||
## Training and testing
|
## Training and testing
|
||||||
|
|||||||
+16
-17
@@ -17,7 +17,7 @@ Code to train model.
|
|||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
from typing import Callable, Dict, List
|
from typing import Dict
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
import torch.distributed as dist
|
import torch.distributed as dist
|
||||||
@@ -30,7 +30,6 @@ from pyrad.data.dataloader import EvalDataloader, setup_dataset_eval, setup_data
|
|||||||
from pyrad.graphs.base import setup_graph
|
from pyrad.graphs.base import setup_graph
|
||||||
from pyrad.optimizers.optimizers import setup_optimizers
|
from pyrad.optimizers.optimizers import setup_optimizers
|
||||||
from pyrad.utils import profiler, writer
|
from pyrad.utils import profiler, writer
|
||||||
from pyrad.utils.callbacks import update_occupancy
|
|
||||||
from pyrad.utils.decorators import check_main_thread
|
from pyrad.utils.decorators import check_main_thread
|
||||||
from pyrad.utils.writer import EventName, TimeWriter
|
from pyrad.utils.writer import EventName, TimeWriter
|
||||||
|
|
||||||
@@ -70,13 +69,15 @@ class Trainer:
|
|||||||
self.graph = setup_graph(self.config.graph, dataset_inputs_train, device=self.device)
|
self.graph = setup_graph(self.config.graph, dataset_inputs_train, device=self.device)
|
||||||
self.optimizers = setup_optimizers(self.config.optimizers, self.graph.get_param_groups())
|
self.optimizers = setup_optimizers(self.config.optimizers, self.graph.get_param_groups())
|
||||||
|
|
||||||
if self.config.graph.resume_train.load_dir:
|
if self.config.trainer.resume_train.load_dir:
|
||||||
self._load_checkpoint()
|
self._load_checkpoint()
|
||||||
|
|
||||||
if self.world_size > 1:
|
if self.world_size > 1:
|
||||||
self.graph = DDP(self.graph, device_ids=[self.local_rank])
|
self.graph = DDP(self.graph, device_ids=[self.local_rank])
|
||||||
dist.barrier(device_ids=[self.local_rank])
|
dist.barrier(device_ids=[self.local_rank])
|
||||||
|
|
||||||
|
self.graph.register_callbacks()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_aggregated_loss(cls, loss_dict: Dict[str, torch.tensor]):
|
def get_aggregated_loss(cls, loss_dict: Dict[str, torch.tensor]):
|
||||||
"""Returns the aggregated losses and the scalar for calling .backwards() on.
|
"""Returns the aggregated losses and the scalar for calling .backwards() on.
|
||||||
@@ -91,21 +92,21 @@ class Trainer:
|
|||||||
def train(self) -> None:
|
def train(self) -> None:
|
||||||
"""_summary_"""
|
"""_summary_"""
|
||||||
with TimeWriter(writer, EventName.TOTAL_TRAIN_TIME):
|
with TimeWriter(writer, EventName.TOTAL_TRAIN_TIME):
|
||||||
num_iterations = self.config.graph.max_num_iterations
|
num_iterations = self.config.trainer.max_num_iterations
|
||||||
iter_dataloader_train = iter(self.dataloader_train)
|
iter_dataloader_train = iter(self.dataloader_train)
|
||||||
for step in range(self.start_step, self.start_step + num_iterations):
|
for step in range(self.start_step, self.start_step + num_iterations):
|
||||||
with TimeWriter(writer, EventName.ITER_LOAD_TIME, step=step):
|
with TimeWriter(writer, EventName.ITER_LOAD_TIME, step=step):
|
||||||
ray_indices, batch = next(iter_dataloader_train)
|
ray_indices, batch = next(iter_dataloader_train)
|
||||||
|
|
||||||
with TimeWriter(writer, EventName.ITER_TRAIN_TIME, step=step) as t:
|
with TimeWriter(writer, EventName.ITER_TRAIN_TIME, step=step) as t:
|
||||||
loss_dict = self.train_iteration(ray_indices, batch, step, _callback=[update_occupancy])
|
loss_dict = self.train_iteration(ray_indices, batch, step)
|
||||||
writer.put_scalar(name=EventName.RAYS_PER_SEC, scalar=ray_indices.shape[0] / t.duration, step=step)
|
writer.put_scalar(name=EventName.RAYS_PER_SEC, scalar=ray_indices.shape[0] / t.duration, step=step)
|
||||||
|
|
||||||
if step != 0 and step % self.config.logging.steps_per_log == 0:
|
if step != 0 and step % self.config.logging.steps_per_log == 0:
|
||||||
writer.put_dict(name="Loss/train-loss_dict", scalar_dict=loss_dict, step=step)
|
writer.put_dict(name="Loss/train-loss_dict", scalar_dict=loss_dict, step=step)
|
||||||
if step != 0 and self.config.graph.steps_per_save and step % self.config.graph.steps_per_save == 0:
|
if step != 0 and self.config.trainer.steps_per_save and step % self.config.trainer.steps_per_save == 0:
|
||||||
self._save_checkpoint(self.config.graph.model_dir, step)
|
self._save_checkpoint(self.config.trainer.model_dir, step)
|
||||||
if step % self.config.graph.steps_per_test == 0:
|
if step % self.config.trainer.steps_per_test == 0:
|
||||||
self.eval_with_dataloader(self.dataloader_eval, step=step)
|
self.eval_with_dataloader(self.dataloader_eval, step=step)
|
||||||
self._write_out_storage(step)
|
self._write_out_storage(step)
|
||||||
|
|
||||||
@@ -119,9 +120,9 @@ class Trainer:
|
|||||||
"""
|
"""
|
||||||
if (
|
if (
|
||||||
step % self.config.logging.steps_per_log == 0
|
step % self.config.logging.steps_per_log == 0
|
||||||
or (self.config.graph.steps_per_save and step % self.config.graph.steps_per_save == 0)
|
or (self.config.trainer.steps_per_save and step % self.config.trainer.steps_per_save == 0)
|
||||||
or step % self.config.graph.steps_per_test == 0
|
or step % self.config.trainer.steps_per_test == 0
|
||||||
or step == self.config.graph.max_num_iterations
|
or step == self.config.trainer.max_num_iterations
|
||||||
):
|
):
|
||||||
writer.write_out_storage()
|
writer.write_out_storage()
|
||||||
|
|
||||||
@@ -158,9 +159,7 @@ class Trainer:
|
|||||||
)
|
)
|
||||||
|
|
||||||
@profiler.time_function
|
@profiler.time_function
|
||||||
def train_iteration(
|
def train_iteration(self, ray_indices: TensorType["num_rays", 3], batch: dict, step: int) -> Dict[str, float]:
|
||||||
self, ray_indices: TensorType["num_rays", 3], batch: dict, step: int, _callback: List[Callable] = None
|
|
||||||
) -> Dict[str, float]:
|
|
||||||
"""Run one iteration with a batch of inputs.
|
"""Run one iteration with a batch of inputs.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -177,9 +176,9 @@ class Trainer:
|
|||||||
loss.backward()
|
loss.backward()
|
||||||
self.optimizers.optimizer_step_all()
|
self.optimizers.optimizer_step_all()
|
||||||
self.optimizers.scheduler_step_all(step)
|
self.optimizers.scheduler_step_all(step)
|
||||||
if _callback:
|
if self.graph.callbacks:
|
||||||
for _func in _callback:
|
for _func in self.graph.callbacks:
|
||||||
_func(self.graph)
|
_func.after_step(step)
|
||||||
return loss_dict
|
return loss_dict
|
||||||
|
|
||||||
@profiler.time_function
|
@profiler.time_function
|
||||||
|
|||||||
@@ -38,9 +38,9 @@ class Field(nn.Module):
|
|||||||
frustums=Frustums(
|
frustums=Frustums(
|
||||||
origins=positions,
|
origins=positions,
|
||||||
directions=torch.ones_like(positions),
|
directions=torch.ones_like(positions),
|
||||||
frustum_starts=torch.zeros((*positions.shape[:-1], 1)),
|
frustum_starts=torch.zeros_like((positions[..., :1])),
|
||||||
frustum_ends=torch.zeros((*positions.shape[:-1], 1)),
|
frustum_ends=torch.zeros_like((positions[..., :1])),
|
||||||
pixel_area=torch.ones((*positions.shape[:-1], 1)),
|
pixel_area=torch.ones_like((positions[..., :1])),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
density, _ = self.get_density(point_samples)
|
density, _ = self.get_density(point_samples)
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ class OccupancyGrid(nn.Module):
|
|||||||
"""Module to keep track of the density and occupancy."""
|
"""Module to keep track of the density and occupancy."""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self, num_cascades: int = 1, resolution: int = 128, aabb=None, density_fn=None, update_every_num_iters: int = 16
|
self, num_cascades: int = 1, resolution: int = 128, aabb=None, update_every_num_iters: int = 16
|
||||||
) -> None:
|
) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
assert is_not_none(aabb), "The axis-aligned bounding box aabb is not defined!"
|
assert is_not_none(aabb), "The axis-aligned bounding box aabb is not defined!"
|
||||||
@@ -42,9 +42,7 @@ class OccupancyGrid(nn.Module):
|
|||||||
self.register_buffer("occupancy_grid", occupancy_grid)
|
self.register_buffer("occupancy_grid", occupancy_grid)
|
||||||
self.mean_density = 0.0
|
self.mean_density = 0.0
|
||||||
|
|
||||||
self.density_fn = density_fn
|
|
||||||
self.update_every_num_iters = update_every_num_iters
|
self.update_every_num_iters = update_every_num_iters
|
||||||
self.iteration_count = 0
|
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
"""Zero out the occupancy grid."""
|
"""Zero out the occupancy grid."""
|
||||||
@@ -67,7 +65,6 @@ class OccupancyGrid(nn.Module):
|
|||||||
"""
|
"""
|
||||||
# TODO(ethan): remove this pylint disable
|
# TODO(ethan): remove this pylint disable
|
||||||
# pylint: disable=invalid-name
|
# pylint: disable=invalid-name
|
||||||
|
|
||||||
device = self.occupancy_grid.device
|
device = self.occupancy_grid.device
|
||||||
|
|
||||||
assert self.num_cascades == 1, "Still need to implement code for multiple cascades."
|
assert self.num_cascades == 1, "Still need to implement code for multiple cascades."
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ def setup_graph(config: DictConfig, dataset_inputs: DatasetInputs, device: str)
|
|||||||
Args:
|
Args:
|
||||||
dataset_inputs (DatasetInputs): The inputs which will be used to define the camera parameters.
|
dataset_inputs (DatasetInputs): The inputs which will be used to define the camera parameters.
|
||||||
"""
|
"""
|
||||||
graph = instantiate_from_dict_config(config.network, **dataset_inputs.as_dict())
|
graph = instantiate_from_dict_config(config, **dataset_inputs.as_dict())
|
||||||
graph.to(device)
|
graph.to(device)
|
||||||
return graph
|
return graph
|
||||||
|
|
||||||
@@ -99,6 +99,11 @@ class Graph(AbstractGraph):
|
|||||||
self.populate_collider()
|
self.populate_collider()
|
||||||
self.populate_fields()
|
self.populate_fields()
|
||||||
self.populate_misc_modules() # populate the modules
|
self.populate_misc_modules() # populate the modules
|
||||||
|
self.callbacks = None
|
||||||
|
|
||||||
|
def register_callbacks(self): # pylint:disable=no-self-use
|
||||||
|
"""Option to register callback for training functions"""
|
||||||
|
self.callbacks = []
|
||||||
|
|
||||||
def populate_collider(self):
|
def populate_collider(self):
|
||||||
"""Set the scene bounds collider to use."""
|
"""Set the scene bounds collider to use."""
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ from pyrad.graphs.modules.ray_sampler import PDFSampler, UniformSampler
|
|||||||
from pyrad.utils import colors
|
from pyrad.utils import colors
|
||||||
from pyrad.cameras.rays import RayBundle
|
from pyrad.cameras.rays import RayBundle
|
||||||
from pyrad.utils import visualization, writer
|
from pyrad.utils import visualization, writer
|
||||||
|
from pyrad.utils.callbacks import Callback
|
||||||
|
|
||||||
|
|
||||||
class NGPGraph(Graph):
|
class NGPGraph(Graph):
|
||||||
@@ -45,6 +46,16 @@ class NGPGraph(Graph):
|
|||||||
self.field = None
|
self.field = None
|
||||||
super().__init__(intrinsics=intrinsics, camera_to_world=camera_to_world, **kwargs)
|
super().__init__(intrinsics=intrinsics, camera_to_world=camera_to_world, **kwargs)
|
||||||
|
|
||||||
|
def register_callbacks(self) -> None:
|
||||||
|
"""defining callbacks to run after every training iteration"""
|
||||||
|
self.callbacks = [
|
||||||
|
Callback(
|
||||||
|
self.occupancy_grid.update_every_num_iters,
|
||||||
|
self.occupancy_grid.update_occupancy_grid,
|
||||||
|
density_fn=self.field.density_fn,
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
def populate_fields(self):
|
def populate_fields(self):
|
||||||
"""Set the fields."""
|
"""Set the fields."""
|
||||||
# torch or tiny-cuda-nn version
|
# torch or tiny-cuda-nn version
|
||||||
|
|||||||
+26
-15
@@ -16,20 +16,31 @@
|
|||||||
Callback functions for training iterations
|
Callback functions for training iterations
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from pyrad.graphs.base import Graph
|
from typing import Callable
|
||||||
from pyrad.fields.occupancy_fields.occupancy_grid import OccupancyGrid
|
|
||||||
|
|
||||||
|
|
||||||
def update_occupancy(graph: Graph):
|
class Callback:
|
||||||
"""Update the occupancy grid depending on iteration count after every train step"""
|
"""Callback Object"""
|
||||||
if not hasattr(graph, "occupancy_pointers"):
|
|
||||||
occupancy_grids = []
|
def __init__(self, update_every_num_iters: int, func: Callable, *args, **kwargs):
|
||||||
for _, module in graph.__dict__["_modules"].items():
|
self.update_every_num_iters = update_every_num_iters
|
||||||
if isinstance(module, OccupancyGrid):
|
self.func = func
|
||||||
occupancy_grids.append(module)
|
self.args = args
|
||||||
graph.occupancy_pointers = occupancy_grids
|
self.kwargs = kwargs
|
||||||
for grid in graph.occupancy_pointers:
|
|
||||||
grid.iteration_count += 1
|
def before_step(self, step: int):
|
||||||
if grid.iteration_count % grid.update_every_num_iters == 0:
|
"""callback to run before training step
|
||||||
if grid.density_fn is not None:
|
|
||||||
grid.update_occupancy_grid(grid.density_fn)
|
Args:
|
||||||
|
step (int): current iteration step
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def after_step(self, step: int):
|
||||||
|
"""callback to run after training step
|
||||||
|
|
||||||
|
Args:
|
||||||
|
step (int): current iteration step
|
||||||
|
"""
|
||||||
|
if step % self.update_every_num_iters == 0:
|
||||||
|
self.func(*self.args, **self.kwargs)
|
||||||
+14
-14
@@ -141,13 +141,13 @@ def setup_event_writers(config: DictConfig) -> None:
|
|||||||
writer_class = getattr(sys.modules[__name__], writer_type)
|
writer_class = getattr(sys.modules[__name__], writer_type)
|
||||||
writer_config = logging_configs[writer_type]
|
writer_config = logging_configs[writer_type]
|
||||||
if writer_type == "LocalWriter":
|
if writer_type == "LocalWriter":
|
||||||
curr_writer = writer_class(writer_config.save_dir, writer_config.stats_to_track, writer_config.max_log_size)
|
curr_writer = writer_class(writer_config.log_dir, writer_config.stats_to_track, writer_config.max_log_size)
|
||||||
else:
|
else:
|
||||||
curr_writer = writer_class(writer_config.save_dir)
|
curr_writer = writer_class(writer_config.log_dir)
|
||||||
EVENT_WRITERS.append(curr_writer)
|
EVENT_WRITERS.append(curr_writer)
|
||||||
|
|
||||||
## configure all the global buffer basic information
|
## configure all the global buffer basic information
|
||||||
GLOBAL_BUFFER["max_iter"] = config.graph.max_num_iterations
|
GLOBAL_BUFFER["max_iter"] = config.trainer.max_num_iterations
|
||||||
GLOBAL_BUFFER["max_buffer_size"] = config.logging.max_buffer_size
|
GLOBAL_BUFFER["max_buffer_size"] = config.logging.max_buffer_size
|
||||||
GLOBAL_BUFFER["steps_per_log"] = config.logging.steps_per_log
|
GLOBAL_BUFFER["steps_per_log"] = config.logging.steps_per_log
|
||||||
GLOBAL_BUFFER["events"] = {}
|
GLOBAL_BUFFER["events"] = {}
|
||||||
@@ -156,8 +156,8 @@ def setup_event_writers(config: DictConfig) -> None:
|
|||||||
class Writer:
|
class Writer:
|
||||||
"""Writer class"""
|
"""Writer class"""
|
||||||
|
|
||||||
def __init__(self, save_dir: str):
|
def __init__(self, log_dir: str):
|
||||||
self.save_dir = save_dir
|
self.log_dir = log_dir
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def write_image(self, name: str, image: TensorType["H", "W", "C"], step: int) -> None:
|
def write_image(self, name: str, image: TensorType["H", "W", "C"], step: int) -> None:
|
||||||
@@ -225,9 +225,9 @@ class TimeWriter:
|
|||||||
class WandbWriter(Writer):
|
class WandbWriter(Writer):
|
||||||
"""WandDB Writer Class"""
|
"""WandDB Writer Class"""
|
||||||
|
|
||||||
def __init__(self, save_dir: str):
|
def __init__(self, log_dir: str):
|
||||||
super().__init__(save_dir)
|
super().__init__(log_dir)
|
||||||
wandb.init(dir=save_dir)
|
wandb.init(dir=log_dir)
|
||||||
|
|
||||||
def write_image(self, name: str, image: TensorType["H", "W", "C"], step: int) -> None:
|
def write_image(self, name: str, image: TensorType["H", "W", "C"], step: int) -> None:
|
||||||
"""_summary_
|
"""_summary_
|
||||||
@@ -253,9 +253,9 @@ class WandbWriter(Writer):
|
|||||||
class TensorboardWriter(Writer):
|
class TensorboardWriter(Writer):
|
||||||
"""Tensorboard Writer Class"""
|
"""Tensorboard Writer Class"""
|
||||||
|
|
||||||
def __init__(self, save_dir: str):
|
def __init__(self, log_dir: str):
|
||||||
super().__init__(save_dir)
|
super().__init__(log_dir)
|
||||||
self.tb_writer = SummaryWriter(log_dir=self.save_dir)
|
self.tb_writer = SummaryWriter(log_dir=self.log_dir)
|
||||||
|
|
||||||
def write_image(self, name: str, image: TensorType["H", "W", "C"], step: int) -> None:
|
def write_image(self, name: str, image: TensorType["H", "W", "C"], step: int) -> None:
|
||||||
"""_summary_
|
"""_summary_
|
||||||
@@ -310,13 +310,13 @@ def _format_time(seconds):
|
|||||||
class LocalWriter(Writer):
|
class LocalWriter(Writer):
|
||||||
"""Local Writer Class"""
|
"""Local Writer Class"""
|
||||||
|
|
||||||
def __init__(self, save_dir: str, stats_to_track: ListConfig, max_log_size: int = 0):
|
def __init__(self, log_dir: str, stats_to_track: ListConfig, max_log_size: int = 0):
|
||||||
"""
|
"""
|
||||||
Args:
|
Args:
|
||||||
stats_to_track (ListConfig): the names of stats that should be logged.
|
stats_to_track (ListConfig): the names of stats that should be logged.
|
||||||
max_log size (int): max number of lines that will be logged to teminal.
|
max_log size (int): max number of lines that will be logged to teminal.
|
||||||
"""
|
"""
|
||||||
super().__init__(save_dir)
|
super().__init__(log_dir)
|
||||||
self.stats_to_track = [EventName[name].value for name in stats_to_track]
|
self.stats_to_track = [EventName[name].value for name in stats_to_track]
|
||||||
self.max_log_size = max_log_size
|
self.max_log_size = max_log_size
|
||||||
self.keys = set()
|
self.keys = set()
|
||||||
@@ -325,7 +325,7 @@ class LocalWriter(Writer):
|
|||||||
def write_image(self, name: str, image: TensorType["H", "W", "C"], step: int) -> None:
|
def write_image(self, name: str, image: TensorType["H", "W", "C"], step: int) -> None:
|
||||||
if name in self.stats_to_track:
|
if name in self.stats_to_track:
|
||||||
image = to8b(image)
|
image = to8b(image)
|
||||||
image_path = os.path.join(self.save_dir, f"{name}.jpg")
|
image_path = os.path.join(self.log_dir, f"{name}.jpg")
|
||||||
imageio.imwrite(image_path, np.uint8(image.cpu().numpy() * 255.0))
|
imageio.imwrite(image_path, np.uint8(image.cpu().numpy() * 255.0))
|
||||||
|
|
||||||
def write_scalar(self, name: str, scalar: float, step: int) -> None:
|
def write_scalar(self, name: str, scalar: float, step: int) -> None:
|
||||||
|
|||||||
@@ -43,9 +43,9 @@ for dataset in ${DATASETS[@]}; do
|
|||||||
data.dataset_inputs_train.data_directory=data/blender/${dataset} \
|
data.dataset_inputs_train.data_directory=data/blender/${dataset} \
|
||||||
data.dataset_inputs_eval.data_directory=data/blender/${dataset} \
|
data.dataset_inputs_eval.data_directory=data/blender/${dataset} \
|
||||||
experiment_name=blender_${dataset}_${tag} \
|
experiment_name=blender_${dataset}_${tag} \
|
||||||
graph.model_dir=pyrad_models/ \
|
trainer.model_dir=pyrad_models/ \
|
||||||
graph.steps_per_save=25000 \
|
trainer.steps_per_save=25000 \
|
||||||
graph.max_num_iterations=2000000 \
|
trainer.max_num_iterations=2000000 \
|
||||||
logging.enable_profiler=False &
|
logging.enable_profiler=False &
|
||||||
echo "Launched ${METHOD} ${dataset} on gpu ${GPU_IDX[$idx]}, ${tag}"
|
echo "Launched ${METHOD} ${dataset} on gpu ${GPU_IDX[$idx]}, ${tag}"
|
||||||
done
|
done
|
||||||
@@ -30,14 +30,13 @@ def _load_best_ckpt(hydra_dir: str, config: DictConfig) -> str:
|
|||||||
Returns:
|
Returns:
|
||||||
str: path to the most recent checkpoint sorted by timestamps and step number
|
str: path to the most recent checkpoint sorted by timestamps and step number
|
||||||
"""
|
"""
|
||||||
model_dir = os.path.join(hydra_dir, config.graph.model_dir)
|
model_dir = os.path.join(hydra_dir, config.model_dir)
|
||||||
latest_ckpt = os.listdir(model_dir)
|
latest_ckpt = os.listdir(model_dir)
|
||||||
latest_ckpt.sort()
|
latest_ckpt.sort()
|
||||||
latest_ckpt = latest_ckpt[-1]
|
latest_ckpt = latest_ckpt[-1]
|
||||||
step = os.path.splitext(latest_ckpt)[0].split("-")[-1]
|
step = os.path.splitext(latest_ckpt)[0].split("-")[-1]
|
||||||
config.graph.resume_train.load_dir = model_dir
|
config.resume_train.load_dir = model_dir
|
||||||
config.graph.resume_train.load_step = int(step)
|
config.resume_train.load_step = int(step)
|
||||||
config.logging.writer.TensorboardWriter.save_dir = "/tmp/"
|
|
||||||
return os.path.join(model_dir, latest_ckpt)
|
return os.path.join(model_dir, latest_ckpt)
|
||||||
|
|
||||||
|
|
||||||
@@ -54,6 +53,8 @@ def _load_hydra_config(hydra_dir: str) -> DictConfig:
|
|||||||
hydra_dir = f"{hydra_dir}/{basename}"
|
hydra_dir = f"{hydra_dir}/{basename}"
|
||||||
initialize(config_path=os.path.join("../../", hydra_dir, ".hydra/"))
|
initialize(config_path=os.path.join("../../", hydra_dir, ".hydra/"))
|
||||||
config = compose("config.yaml")
|
config = compose("config.yaml")
|
||||||
|
## set the tensorboard to a /tmp/ output file for now
|
||||||
|
config.logging.writer.TensorboardWriter.log_dir = "/tmp/"
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
@@ -67,7 +68,7 @@ def main():
|
|||||||
# set up trainer, config, and checkpoint loading
|
# set up trainer, config, and checkpoint loading
|
||||||
hydra_dir = f"{hydra_base_dir}/blender_{dataset}_{benchmark_date}/{method}/"
|
hydra_dir = f"{hydra_base_dir}/blender_{dataset}_{benchmark_date}/{method}/"
|
||||||
config = _load_hydra_config(hydra_dir)
|
config = _load_hydra_config(hydra_dir)
|
||||||
ckpt = _load_best_ckpt(hydra_dir, config)
|
ckpt = _load_best_ckpt(hydra_dir, config.trainer)
|
||||||
|
|
||||||
# run evaluation
|
# run evaluation
|
||||||
avg_psnr, avg_rays_per_sec = run_inference(config)
|
avg_psnr, avg_rays_per_sec = run_inference(config)
|
||||||
|
|||||||
+4
-4
@@ -38,7 +38,7 @@ def _load_checkpoint(config: DictConfig, graph: Graph) -> None:
|
|||||||
load_path = os.path.join(config.load_dir, f"step-{config.load_step:09d}.ckpt")
|
load_path = os.path.join(config.load_dir, f"step-{config.load_step:09d}.ckpt")
|
||||||
assert os.path.exists(load_path), f"Checkpoint {load_path} does not exist"
|
assert os.path.exists(load_path), f"Checkpoint {load_path} does not exist"
|
||||||
loaded_state = torch.load(load_path, map_location="cpu")
|
loaded_state = torch.load(load_path, map_location="cpu")
|
||||||
graph.load_checkpoint(loaded_state)
|
graph.load_graph(loaded_state)
|
||||||
logging.info("done loading checkpoint from %s", load_path)
|
logging.info("done loading checkpoint from %s", load_path)
|
||||||
|
|
||||||
|
|
||||||
@@ -58,7 +58,7 @@ def run_inference(config: DictConfig, local_rank: int = 0, world_size: int = 1)
|
|||||||
graph = setup_graph(config.graph, dataset_inputs_train, device=device)
|
graph = setup_graph(config.graph, dataset_inputs_train, device=device)
|
||||||
|
|
||||||
# load checkpointed information
|
# load checkpointed information
|
||||||
_load_checkpoint(config.graph.resume_train, graph)
|
_load_checkpoint(config.trainer.resume_train, graph)
|
||||||
|
|
||||||
# calculate average psnr across test dataset
|
# calculate average psnr across test dataset
|
||||||
# TODO(ethan): trajector specification
|
# TODO(ethan): trajector specification
|
||||||
@@ -78,8 +78,8 @@ def run_inference(config: DictConfig, local_rank: int = 0, world_size: int = 1)
|
|||||||
@hydra.main(config_path="../configs", config_name="graph_default.yaml")
|
@hydra.main(config_path="../configs", config_name="graph_default.yaml")
|
||||||
def main(config: DictConfig):
|
def main(config: DictConfig):
|
||||||
"""Main function."""
|
"""Main function."""
|
||||||
assert config.graph.resume_train.load_dir, "Please specify checkpoint load path"
|
assert config.trainer.resume_train.load_dir, "Please specify checkpoint load path"
|
||||||
assert config.graph.resume_train.load_step, "Please specify checkpoint step to load"
|
assert config.trainer.resume_train.load_step, "Please specify checkpoint step to load"
|
||||||
|
|
||||||
avg_psnr, avg_rays_per_sec = run_inference(config)
|
avg_psnr, avg_rays_per_sec = run_inference(config)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user