text
stringlengths
1
93.6k
dataiter = iter(dataloader)
batch = next(dataiter)
batch = accelerate.utils.send_to_device(batch, accelerator.device)
if reset_stats and accelerator.is_main_process:
stats_buffer = []
train_start_time = time.time()
reset_stats = False
# use lr_fn to control learning rate
learning_rate = lr_fn(step)
for param_group in optimizer.param_groups:
# param_group['lr'] = learning_rate
if 'name' in param_group and param_group['name'].startswith('bil_grids'):
# Scale the learning rate of bilateral grids.
param_group['lr'] = learning_rate * config.bilgrid_lr_scale
else:
param_group['lr'] = learning_rate
# fraction of training period
train_frac = np.clip((step - 1) / (config.max_steps - 1), 0, 1)
# Indicates whether we need to compute output normal or depth maps in 2D.
compute_extras = (config.compute_disp_metrics or config.compute_normal_metrics)
optimizer.zero_grad()
with accelerator.autocast():
renderings, ray_history = model(
True,
batch,
train_frac=train_frac,
compute_extras=compute_extras,
zero_glo=False)
losses = {}
# supervised by data
data_loss, stats = train_utils.compute_data_loss(batch, renderings, config)
losses['data'] = data_loss
# interlevel loss in MipNeRF360
if config.interlevel_loss_mult > 0 and not module.single_mlp:
losses['interlevel'] = train_utils.interlevel_loss(ray_history, config)
# interlevel loss in ZipNeRF360
if config.anti_interlevel_loss_mult > 0 and not module.single_mlp:
losses['anti_interlevel'] = train_utils.anti_interlevel_loss(ray_history, config)
# distortion loss
if config.distortion_loss_mult > 0:
losses['distortion'] = train_utils.distortion_loss(ray_history, config)
# opacity loss
if config.opacity_loss_mult > 0:
losses['opacity'] = train_utils.opacity_loss(renderings, config)
# orientation loss in RefNeRF
if (config.orientation_coarse_loss_mult > 0 or
config.orientation_loss_mult > 0):
losses['orientation'] = train_utils.orientation_loss(batch, module, ray_history,
config)
# hash grid l2 weight decay
if config.hash_decay_mults > 0:
losses['hash_decay'] = train_utils.hash_decay_loss(ray_history, config)
# normal supervision loss in RefNeRF
if (config.predicted_normal_coarse_loss_mult > 0 or
config.predicted_normal_loss_mult > 0):
losses['predicted_normals'] = train_utils.predicted_normal_loss(
module, ray_history, config)
# regularizations of bilateral grids
if hasattr(module, 'bil_grids') and module.bil_grids is not None:
if config.bilgrid_tv_loss_mult > 0:
losses['tv_bilgrids'] = train_utils.bilateral_grid_tv_loss(module, config)
loss = sum(losses.values())
stats['loss'] = loss.item()
stats['losses'] = tree_map(lambda x: x.item(), losses)
# accelerator automatically handle the scale
accelerator.backward(loss)
# clip gradient by max/norm/nan
train_utils.clip_gradients(model, accelerator, config)
optimizer.step()
stats['psnrs'] = image.mse_to_psnr(stats['mses'])
stats['psnr'] = stats['psnrs'][-1]
# Log training summaries. This is put behind a host_id check because in
# multi-host evaluation, all hosts need to run inference even though we
# only use host 0 to record results.
if accelerator.is_main_process:
stats_buffer.append(stats)
if step == init_step + 1 or step % config.print_every == 0:
elapsed_time = time.time() - train_start_time
steps_per_sec = config.print_every / elapsed_time
rays_per_sec = config.batch_size * steps_per_sec
# A robust approximation of total training time, in case of pre-emption.
total_time += int(round(TIME_PRECISION * elapsed_time))
total_steps += config.print_every