Update docstrings

This commit is contained in:
Matt Tancik authored and Matthew Tancik committed 2022-06-10 09:03:43 -07:00
1 parent cc79708df7
commit 37a3692b45
11 files changed
+197 -130

No files matched your search

+48 -21
View File
@@ -24,6 +24,7 @@ import torch
import torch.distributed as dist
from omegaconf import DictConfig
from torch.nn.parallel import DistributedDataParallel as DDP
from torchtyping import TensorType
from pyrad.cameras.rays import RayBundle
from pyrad.data.dataloader import EvalDataloader, TrainDataloader
@@ -38,17 +39,16 @@ logging.getLogger("PIL").setLevel(logging.WARNING)
class Trainer:
"""Training class"""
"""Training class
Args:
config (DictConfig): _description_
local_rank (int, optional): _description_. Defaults to 0.
world_size (int, optional): _description_. Defaults to 1.
cpu (bool, optional): Whether or not to use the CPU.
"""
def __init__(self, config: DictConfig, local_rank: int = 0, world_size: int = 1):
"""_summary_
Args:
config (DictConfig): _description_
local_rank (int, optional): _description_. Defaults to 0.
world_size (int, optional): _description_. Defaults to 1.
cpu (bool, optional): Whether or not to use the CPU.
"""
self.config = config
self.local_rank = local_rank
self.world_size = world_size
@@ -133,7 +133,7 @@ class Trainer:
"""Load the checkpoint from the given path
Args:
load_path (str): path from which to load the model
load_config (DictConfig): Configuration for loading a model.
Returns:
int: step iteration associated with the loaded checkpoint
@@ -154,8 +154,6 @@ class Trainer:
Args:
output_dir (str): directory to save the checkpoint
step (int): number of steps in training for given checkpoint
model (Graph): Graph model to be saved
optimizers (Optimizers): Optimizers to be saved
"""
if not os.path.exists(output_dir):
os.makedirs(output_dir)
@@ -181,7 +179,7 @@ class Trainer:
return loss_sum
def train(self) -> None:
"""_summary_"""
"""Main training loop."""
train_start = time()
num_iterations = self.config.graph.max_num_iterations
iter_dataloader_train = iter(self.dataloader_train)
@@ -227,8 +225,12 @@ class Trainer:
)
self._write_out_storage(num_iterations)
def _write_out_storage(self, step):
"""Perform writes only during appropriate time steps"""
def _write_out_storage(self, step: int) -> None:
"""Perform writes only during appropriate time steps
Args:
step (int): Current training step.
"""
if (
step % self.config.logging.steps_per_log == 0
or (self.config.graph.steps_per_save and step % self.config.graph.steps_per_save == 0)
@@ -238,8 +240,19 @@ class Trainer:
writer.write_out_storage()
@profiler.time_function
def train_iteration(self, ray_indices, batch: dict, step: int, _callback: List[Callable] = None):
"""Run one iteration with a batch of inputs."""
def train_iteration(
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.
Args:
ray_indices (TensorType["num_rays", 3]): Contains camera, row, and col indicies for target rays.
batch (dict): Batch of training data.
step (int): Current training step.
Returns:
Dict[str, float]: Dictionary of model losses.
"""
_, loss_dict = self.graph.forward(ray_indices, batch=batch)
loss = loss_dict["aggregated_loss"]
self.optimizers.zero_grad_all()
@@ -252,8 +265,17 @@ class Trainer:
return loss_dict
@profiler.time_function
def test_image(self, camera_ray_bundle: RayBundle, batch: dict, step: int = None):
"""Test a specific image."""
def test_image(self, camera_ray_bundle: RayBundle, batch: dict, step: int = None) -> float:
"""Test a specific image.
Args:
camera_ray_bundle (RayBundle): Bundle of test rays.
batch (dict): Batch of data.
step (int): Current training step.
Returns:
float: PSNR
"""
self.graph.eval()
image_idx = int(camera_ray_bundle.camera_indices[0, 0])
outputs = self.graph.get_outputs_for_camera_ray_bundle(camera_ray_bundle)
@@ -261,7 +283,12 @@ class Trainer:
self.graph.train()
return psnr
def eval_with_dataloader(self, dataloader: EvalDataloader, step: int = None):
"""Run evaluation with a given dataloader."""
def eval_with_dataloader(self, dataloader: EvalDataloader, step: int = None) -> None:
"""Run evaluation with a given dataloader.
Args:
dataloader (EvalDataLoader): Evaluation dataloader.
step (int): Current training iteration.
"""
for camera_ray_bundle, batch in dataloader:
self.test_image(camera_ray_bundle, batch, step=step)