-
Notifications
You must be signed in to change notification settings - Fork 6
/
opt.py
671 lines (573 loc) · 30.8 KB
/
opt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
# Copyright 2021 Alex Yu
# First, install svox2
# Then, python opt.py <path_to>/nerf_synthetic/<scene> -t ckpt/<some_name>
# or use launching script: sh launch.sh <EXP_NAME> <GPU> <DATA_DIR>
import torch
import torch.cuda
import torch.optim
import torch.nn.functional as F
import svox2
import json
import imageio
import os
from os import path
import shutil
import gc
import numpy as np
import math
import argparse
import cv2
from util.dataset import datasets
from util.util import Timing, get_expon_lr_func, generate_dirs_equirect, viridis_cmap
from util import config_util
from warnings import warn
from datetime import datetime
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm
from typing import NamedTuple, Optional, Union
device = "cuda" if torch.cuda.is_available() else "cpu"
parser = argparse.ArgumentParser()
config_util.define_common_args(parser)
group = parser.add_argument_group("general")
group.add_argument('--train_dir', '-t', type=str, default='ckpt',
help='checkpoint and logging directory')
group.add_argument('--reso',
type=str,
default=
"[[256, 256, 256], [512, 512, 512]]",
help='List of grid resolution (will be evaled as json);'
'resamples to the next one every upsamp_every iters, then ' +
'stays at the last one; ' +
'should be a list where each item is a list of 3 ints or an int')
group.add_argument('--upsamp_every', type=int, default=
3 * 12800,
help='upsample the grid every x iters')
group.add_argument('--init_iters', type=int, default=
0,
help='do not upsample for first x iters')
group.add_argument('--upsample_density_add', type=float, default=
0.0,
help='add the remaining density by this amount when upsampling')
group.add_argument('--basis_type',
choices=['sh', '3d_texture', 'mlp'],
default='sh',
help='Basis function type')
group.add_argument('--basis_reso', type=int, default=32,
help='basis grid resolution (only for learned texture)')
group.add_argument('--sh_dim', type=int, default=9, help='SH/learned basis dimensions (at most 10)')
group.add_argument('--mlp_posenc_size', type=int, default=4, help='Positional encoding size if using MLP basis; 0 to disable')
group.add_argument('--mlp_width', type=int, default=32, help='MLP width if using MLP basis')
group.add_argument('--background_nlayers', type=int, default=0,#32,
help='Number of background layers (0=disable BG model)')
group.add_argument('--background_reso', type=int, default=512, help='Background resolution')
group = parser.add_argument_group("optimization")
group.add_argument('--n_iters', type=int, default=10 * 12800, help='total number of iters to optimize for')
group.add_argument('--batch_size', type=int, default=
5000,
#100000,
# 2000,
help='batch size')
# TODO: make the lr higher near the end
group.add_argument('--sigma_optim', choices=['sgd', 'rmsprop'], default='rmsprop', help="Density optimizer")
group.add_argument('--lr_sigma', type=float, default=3e1, help='SGD/rmsprop lr for sigma')
group.add_argument('--lr_sigma_final', type=float, default=5e-2)
group.add_argument('--lr_sigma_decay_steps', type=int, default=250000)
group.add_argument('--lr_sigma_delay_steps', type=int, default=15000,
help="Reverse cosine steps (0 means disable)")
group.add_argument('--lr_sigma_delay_mult', type=float, default=1e-2)#1e-4)#1e-4)
group.add_argument('--sh_optim', choices=['sgd', 'rmsprop'], default='rmsprop', help="SH optimizer")
group.add_argument('--lr_sh', type=float, default=
1e-2,
help='SGD/rmsprop lr for SH')
group.add_argument('--lr_sh_final', type=float,
default=
5e-6
)
group.add_argument('--lr_sh_decay_steps', type=int, default=250000)
group.add_argument('--lr_sh_delay_steps', type=int, default=0, help="Reverse cosine steps (0 means disable)")
group.add_argument('--lr_sh_delay_mult', type=float, default=1e-2)
group.add_argument('--lr_fg_begin_step', type=int, default=0, help="Foreground begins training at given step number")
# BG LRs
group.add_argument('--bg_optim', choices=['sgd', 'rmsprop'], default='rmsprop', help="Background optimizer")
group.add_argument('--lr_sigma_bg', type=float, default=3e0,
help='SGD/rmsprop lr for background')
group.add_argument('--lr_sigma_bg_final', type=float, default=3e-3,
help='SGD/rmsprop lr for background')
group.add_argument('--lr_sigma_bg_decay_steps', type=int, default=250000)
group.add_argument('--lr_sigma_bg_delay_steps', type=int, default=0, help="Reverse cosine steps (0 means disable)")
group.add_argument('--lr_sigma_bg_delay_mult', type=float, default=1e-2)
group.add_argument('--lr_color_bg', type=float, default=1e-1,
help='SGD/rmsprop lr for background')
group.add_argument('--lr_color_bg_final', type=float, default=5e-6,#1e-4,
help='SGD/rmsprop lr for background')
group.add_argument('--lr_color_bg_decay_steps', type=int, default=250000)
group.add_argument('--lr_color_bg_delay_steps', type=int, default=0, help="Reverse cosine steps (0 means disable)")
group.add_argument('--lr_color_bg_delay_mult', type=float, default=1e-2)
# END BG LRs
group.add_argument('--basis_optim', choices=['sgd', 'rmsprop'], default='rmsprop', help="Learned basis optimizer")
group.add_argument('--lr_basis', type=float, default=#2e6,
1e-6,
help='SGD/rmsprop lr for SH')
group.add_argument('--lr_basis_final', type=float,
default=
1e-6
)
group.add_argument('--lr_basis_decay_steps', type=int, default=250000)
group.add_argument('--lr_basis_delay_steps', type=int, default=0,#15000,
help="Reverse cosine steps (0 means disable)")
group.add_argument('--lr_basis_begin_step', type=int, default=0)#4 * 12800)
group.add_argument('--lr_basis_delay_mult', type=float, default=1e-2)
group.add_argument('--rms_beta', type=float, default=0.95, help="RMSProp exponential averaging factor")
group.add_argument('--print_every', type=int, default=20, help='print every')
group.add_argument('--save_every', type=int, default=5,
help='save every x epochs')
group.add_argument('--eval_every', type=int, default=1,
help='evaluate every x epochs')
group.add_argument('--init_sigma', type=float,
default=0.1,
help='initialization sigma')
group.add_argument('--init_sigma_bg', type=float,
default=0.1,
help='initialization sigma (for BG)')
# Extra logging
group.add_argument('--log_mse_image', action='store_true', default=False)
group.add_argument('--log_depth_map', action='store_true', default=False)
group.add_argument('--log_depth_map_use_thresh', type=float, default=None,
help="If specified, uses the Dex-neRF version of depth with given thresh; else returns expected term")
group = parser.add_argument_group("misc experiments")
group.add_argument('--thresh_type',
choices=["weight", "sigma"],
default="weight",
help='Upsample threshold type')
group.add_argument('--weight_thresh', type=float,
default=0.0005 * 512,
# default=0.025 * 512,
help='Upsample weight threshold; will be divided by resulting z-resolution')
group.add_argument('--density_thresh', type=float,
default=5.0,
help='Upsample sigma threshold')
group.add_argument('--background_density_thresh', type=float,
default=1.0+1e-9,
help='Background sigma threshold for sparsification')
group.add_argument('--max_grid_elements', type=int,
default=44_000_000,
help='Max items to store after upsampling '
'(the number here is given for 22GB memory)')
group.add_argument('--tune_mode', action='store_true', default=False,
help='hypertuning mode (do not save, for speed)')
group.add_argument('--tune_nosave', action='store_true', default=False,
help='do not save any checkpoint even at the end')
group = parser.add_argument_group("losses")
# Foreground TV
group.add_argument('--lambda_tv', type=float, default=1e-5)
group.add_argument('--tv_sparsity', type=float, default=0.01)
group.add_argument('--tv_logalpha', action='store_true', default=False,
help='Use log(1-exp(-delta * sigma)) as in neural volumes')
group.add_argument('--lambda_tv_sh', type=float, default=1e-3)
group.add_argument('--tv_sh_sparsity', type=float, default=0.01)
group.add_argument('--lambda_tv_lumisphere', type=float, default=0.0)#1e-2)#1e-3)
group.add_argument('--tv_lumisphere_sparsity', type=float, default=0.01)
group.add_argument('--tv_lumisphere_dir_factor', type=float, default=0.0)
group.add_argument('--tv_decay', type=float, default=1.0)
group.add_argument('--lambda_l2_sh', type=float, default=0.0)#1e-4)
group.add_argument('--tv_early_only', type=int, default=1, help="Turn off TV regularization after the first split/prune")
group.add_argument('--tv_contiguous', type=int, default=1,
help="Apply TV only on contiguous link chunks, which is faster")
# End Foreground TV
group.add_argument('--lambda_sparsity', type=float, default=
0.0,
help="Weight for sparsity loss as in SNeRG/PlenOctrees " +
"(but applied on the ray)")
group.add_argument('--lambda_beta', type=float, default=
0.0,
help="Weight for beta distribution sparsity loss as in neural volumes")
# Background TV
group.add_argument('--lambda_tv_background_sigma', type=float, default=1e-2)
group.add_argument('--lambda_tv_background_color', type=float, default=1e-2)
group.add_argument('--tv_background_sparsity', type=float, default=0.01)
# End Background TV
# Basis TV
group.add_argument('--lambda_tv_basis', type=float, default=0.0,
help='Learned basis total variation loss')
# End Basis TV
group.add_argument('--weight_decay_sigma', type=float, default=1.0)
group.add_argument('--weight_decay_sh', type=float, default=1.0)
group.add_argument('--lr_decay', action='store_true', default=True)
group.add_argument('--n_train', type=int, default=None, help='Number of training images. Defaults to use all avaiable.')
group.add_argument('--nosphereinit', action='store_true', default=False,
help='do not start with sphere bounds (please do not use for 360)')
group.add_argument('--offset', type=int, default=250)
args = parser.parse_args()
config_util.maybe_merge_config_file(args)
assert args.lr_sigma_final <= args.lr_sigma, "lr_sigma must be >= lr_sigma_final"
assert args.lr_sh_final <= args.lr_sh, "lr_sh must be >= lr_sh_final"
assert args.lr_basis_final <= args.lr_basis, "lr_basis must be >= lr_basis_final"
os.makedirs(args.train_dir, exist_ok=True)
summary_writer = SummaryWriter(args.train_dir)
reso_list = json.loads(args.reso)
reso_id = 0
with open(path.join(args.train_dir, 'args.json'), 'w') as f:
json.dump(args.__dict__, f, indent=2)
# Changed name to prevent errors
shutil.copyfile(__file__, path.join(args.train_dir, 'opt_frozen.py'))
torch.manual_seed(20200823)
np.random.seed(20200823)
factor = 1
def deploy_dset(dset):
dset.c2w = torch.from_numpy(dset.c2w)
dset.gt = torch.from_numpy(dset.gt).float()
if not dset.is_train_split:
dset.render_c2w = torch.from_numpy(dset.render_c2w)
else:
dset.gen_rays()
return dset
dset = datasets[args.dataset_type](
args.data_dir,
split="train",
device=device,
factor=factor,
n_images=args.n_train,
offset=args.offset,
**config_util.build_data_options(args))
if args.background_nlayers > 0 and not dset.should_use_background:
warn('Using a background model for dataset type ' + str(type(dset)) + ' which typically does not use background')
dset_test = datasets[args.dataset_type](
args.data_dir, split="test", **config_util.build_data_options(args))
deploy_dset(dset)
deploy_dset(dset_test)
global_start_time = datetime.now()
grid = svox2.SparseGrid(reso=reso_list[reso_id],
center=dset.scene_center,
radius=dset.scene_radius,
use_sphere_bound=dset.use_sphere_bound and not args.nosphereinit,
basis_dim=args.sh_dim,
use_z_order=True,
device=device,
basis_reso=args.basis_reso,
basis_type=svox2.__dict__['BASIS_TYPE_' + args.basis_type.upper()],
mlp_posenc_size=args.mlp_posenc_size,
mlp_width=args.mlp_width,
background_nlayers=args.background_nlayers,
background_reso=args.background_reso)
# DC -> gray; mind the SH scaling!
grid.sh_data.data[:] = 0.0
grid.density_data.data[:] = 0.0 if args.lr_fg_begin_step > 0 else args.init_sigma
if grid.use_background:
grid.background_data.data[..., -1] = args.init_sigma_bg
# grid.background_data.data[..., :-1] = 0.5 / svox2.utils.SH_C0
# grid.sh_data.data[:, 0] = 4.0
# osh = grid.density_data.data.shape
# den = grid.density_data.data.view(grid.links.shape)
# # den[:] = 0.00
# # den[:, :256, :] = 1e9
# # den[:, :, 0] = 1e9
# grid.density_data.data = den.view(osh)
optim_basis_mlp = None
if grid.basis_type == svox2.BASIS_TYPE_3D_TEXTURE:
grid.reinit_learned_bases(init_type='sh')
# grid.reinit_learned_bases(init_type='fourier')
# grid.reinit_learned_bases(init_type='sg', upper_hemi=True)
# grid.basis_data.data.normal_(mean=0.28209479177387814, std=0.001)
elif grid.basis_type == svox2.BASIS_TYPE_MLP:
# MLP!
optim_basis_mlp = torch.optim.Adam(
grid.basis_mlp.parameters(),
lr=args.lr_basis
)
grid.requires_grad_(True)
config_util.setup_render_opts(grid.opt, args)
print('Render options', grid.opt)
gstep_id_base = 0
resample_cameras = [
svox2.Camera(c2w.to(device=device),
dset.intrins.get('fx', i),
dset.intrins.get('fy', i),
dset.intrins.get('cx', i),
dset.intrins.get('cy', i),
width=dset.get_image_size(i)[1],
height=dset.get_image_size(i)[0],
ndc_coeffs=dset.ndc_coeffs) for i, c2w in enumerate(dset.c2w)
]
ckpt_path = path.join(args.train_dir, 'ckpt.npz')
lr_sigma_func = get_expon_lr_func(args.lr_sigma, args.lr_sigma_final, args.lr_sigma_delay_steps,
args.lr_sigma_delay_mult, args.lr_sigma_decay_steps)
lr_sh_func = get_expon_lr_func(args.lr_sh, args.lr_sh_final, args.lr_sh_delay_steps,
args.lr_sh_delay_mult, args.lr_sh_decay_steps)
lr_basis_func = get_expon_lr_func(args.lr_basis, args.lr_basis_final, args.lr_basis_delay_steps,
args.lr_basis_delay_mult, args.lr_basis_decay_steps)
lr_sigma_bg_func = get_expon_lr_func(args.lr_sigma_bg, args.lr_sigma_bg_final, args.lr_sigma_bg_delay_steps,
args.lr_sigma_bg_delay_mult, args.lr_sigma_bg_decay_steps)
lr_color_bg_func = get_expon_lr_func(args.lr_color_bg, args.lr_color_bg_final, args.lr_color_bg_delay_steps,
args.lr_color_bg_delay_mult, args.lr_color_bg_decay_steps)
lr_sigma_factor = 1.0
lr_sh_factor = 1.0
lr_basis_factor = 1.0
last_upsamp_step = args.init_iters
if args.enable_random:
warn("Randomness is enabled for training (normal for LLFF & scenes with background)")
epoch_id = -1
while True:
dset.shuffle_rays()
epoch_id += 1
epoch_size = dset.rays.origins.size(0)
batches_per_epoch = (epoch_size-1)//args.batch_size+1
# Test
def eval_step():
# Put in a function to avoid memory leak
print('Eval step')
with torch.no_grad():
stats_test = {'psnr' : 0.0, 'mse' : 0.0}
# Standard set
N_IMGS_TO_EVAL = min(20 if epoch_id > 0 else 5, dset_test.n_images)
N_IMGS_TO_SAVE = N_IMGS_TO_EVAL # if not args.tune_mode else 1
img_eval_interval = dset_test.n_images // N_IMGS_TO_EVAL
img_save_interval = (N_IMGS_TO_EVAL // N_IMGS_TO_SAVE)
img_ids = range(0, dset_test.n_images, img_eval_interval)
# Special 'very hard' specular + fuzz set
# img_ids = [2, 5, 7, 9, 21,
# 44, 45, 47, 49, 56,
# 80, 88, 99, 115, 120,
# 154]
# img_save_interval = 1
n_images_gen = 0
for i, img_id in tqdm(enumerate(img_ids), total=len(img_ids)):
c2w = dset_test.c2w[img_id].to(device=device)
cam = svox2.Camera(c2w,
dset_test.intrins.get('fx', img_id),
dset_test.intrins.get('fy', img_id),
dset_test.intrins.get('cx', img_id),
dset_test.intrins.get('cy', img_id),
width=dset_test.get_image_size(img_id)[1],
height=dset_test.get_image_size(img_id)[0],
ndc_coeffs=dset_test.ndc_coeffs)
rgb_pred_test = grid.volume_render_image(cam, use_kernel=True)
rgb_gt_test = dset_test.gt[img_id].to(device=device)
all_mses = ((rgb_gt_test - rgb_pred_test) ** 2).cpu()
if i % img_save_interval == 0:
img_pred = rgb_pred_test.cpu()
img_pred.clamp_max_(1.0)
summary_writer.add_image(f'test/image_{img_id:04d}',
img_pred, global_step=gstep_id_base, dataformats='HWC')
if args.log_mse_image:
mse_img = all_mses / all_mses.max()
summary_writer.add_image(f'test/mse_map_{img_id:04d}',
mse_img, global_step=gstep_id_base, dataformats='HWC')
if args.log_depth_map:
depth_img = grid.volume_render_depth_image(cam,
args.log_depth_map_use_thresh if
args.log_depth_map_use_thresh else None
)
depth_img = viridis_cmap(depth_img.cpu())
summary_writer.add_image(f'test/depth_map_{img_id:04d}',
depth_img,
global_step=gstep_id_base, dataformats='HWC')
rgb_pred_test = rgb_gt_test = None
mse_num : float = all_mses.mean().item()
psnr = -10.0 * math.log10(mse_num)
if math.isnan(psnr):
print('NAN PSNR', i, img_id, mse_num)
assert False
stats_test['mse'] += mse_num
stats_test['psnr'] += psnr
n_images_gen += 1
if grid.basis_type == svox2.BASIS_TYPE_3D_TEXTURE or \
grid.basis_type == svox2.BASIS_TYPE_MLP:
# Add spherical map visualization
EQ_RESO = 256
eq_dirs = generate_dirs_equirect(EQ_RESO * 2, EQ_RESO)
eq_dirs = torch.from_numpy(eq_dirs).to(device=device).view(-1, 3)
if grid.basis_type == svox2.BASIS_TYPE_MLP:
sphfuncs = grid._eval_basis_mlp(eq_dirs)
else:
sphfuncs = grid._eval_learned_bases(eq_dirs)
sphfuncs = sphfuncs.view(EQ_RESO, EQ_RESO*2, -1).permute([2, 0, 1]).cpu().numpy()
stats = [(sphfunc.min(), sphfunc.mean(), sphfunc.max())
for sphfunc in sphfuncs]
sphfuncs_cmapped = [viridis_cmap(sphfunc) for sphfunc in sphfuncs]
for im, (minv, meanv, maxv) in zip(sphfuncs_cmapped, stats):
cv2.putText(im, f"{minv=:.4f} {meanv=:.4f} {maxv=:.4f}", (10, 20),
0, 0.5, [255, 0, 0])
sphfuncs_cmapped = np.concatenate(sphfuncs_cmapped, axis=0)
summary_writer.add_image(f'test/spheric',
sphfuncs_cmapped, global_step=gstep_id_base, dataformats='HWC')
# END add spherical map visualization
stats_test['mse'] /= n_images_gen
stats_test['psnr'] /= n_images_gen
for stat_name in stats_test:
summary_writer.add_scalar('test/' + stat_name,
stats_test[stat_name], global_step=gstep_id_base)
summary_writer.add_scalar('epoch_id', float(epoch_id), global_step=gstep_id_base)
print('eval stats:', stats_test)
if epoch_id % max(factor, args.eval_every) == 0: #and (epoch_id > 0 or not args.tune_mode):
# NOTE: we do an eval sanity check, if not in tune_mode
eval_step()
gc.collect()
def train_step():
print('Train step')
pbar = tqdm(enumerate(range(0, epoch_size, args.batch_size)), total=batches_per_epoch)
stats = {"mse" : 0.0, "psnr" : 0.0, "invsqr_mse" : 0.0}
for iter_id, batch_begin in pbar:
gstep_id = iter_id + gstep_id_base
if args.lr_fg_begin_step > 0 and gstep_id == args.lr_fg_begin_step:
grid.density_data.data[:] = args.init_sigma
lr_sigma = lr_sigma_func(gstep_id) * lr_sigma_factor
lr_sh = lr_sh_func(gstep_id) * lr_sh_factor
lr_basis = lr_basis_func(gstep_id - args.lr_basis_begin_step) * lr_basis_factor
lr_sigma_bg = lr_sigma_bg_func(gstep_id - args.lr_basis_begin_step) * lr_basis_factor
lr_color_bg = lr_color_bg_func(gstep_id - args.lr_basis_begin_step) * lr_basis_factor
if not args.lr_decay:
lr_sigma = args.lr_sigma * lr_sigma_factor
lr_sh = args.lr_sh * lr_sh_factor
lr_basis = args.lr_basis * lr_basis_factor
batch_end = min(batch_begin + args.batch_size, epoch_size)
batch_origins = dset.rays.origins[batch_begin: batch_end]
batch_dirs = dset.rays.dirs[batch_begin: batch_end]
rgb_gt = dset.rays.gt[batch_begin: batch_end]
rays = svox2.Rays(batch_origins, batch_dirs)
# with Timing("volrend_fused"):
rgb_pred = grid.volume_render_fused(rays, rgb_gt,
beta_loss=args.lambda_beta,
sparsity_loss=args.lambda_sparsity,
randomize=args.enable_random)
# with Timing("loss_comp"):
mse = F.mse_loss(rgb_gt, rgb_pred)
# Stats
mse_num : float = mse.detach().item()
psnr = -10.0 * math.log10(mse_num)
stats['mse'] += mse_num
stats['psnr'] += psnr
stats['invsqr_mse'] += 1.0 / mse_num ** 2
if (iter_id + 1) % args.print_every == 0:
# Print averaged stats
pbar.set_description(f'epoch {epoch_id} psnr={psnr:.2f}')
for stat_name in stats:
stat_val = stats[stat_name] / args.print_every
summary_writer.add_scalar(stat_name, stat_val, global_step=gstep_id)
stats[stat_name] = 0.0
# if args.lambda_tv > 0.0:
# with torch.no_grad():
# tv = grid.tv(logalpha=args.tv_logalpha, ndc_coeffs=dset.ndc_coeffs)
# summary_writer.add_scalar("loss_tv", tv, global_step=gstep_id)
# if args.lambda_tv_sh > 0.0:
# with torch.no_grad():
# tv_sh = grid.tv_color()
# summary_writer.add_scalar("loss_tv_sh", tv_sh, global_step=gstep_id)
# with torch.no_grad():
# tv_basis = grid.tv_basis() # summary_writer.add_scalar("loss_tv_basis", tv_basis, global_step=gstep_id)
summary_writer.add_scalar("lr_sh", lr_sh, global_step=gstep_id)
summary_writer.add_scalar("lr_sigma", lr_sigma, global_step=gstep_id)
if grid.basis_type == svox2.BASIS_TYPE_3D_TEXTURE:
summary_writer.add_scalar("lr_basis", lr_basis, global_step=gstep_id)
if grid.use_background:
summary_writer.add_scalar("lr_sigma_bg", lr_sigma_bg, global_step=gstep_id)
summary_writer.add_scalar("lr_color_bg", lr_color_bg, global_step=gstep_id)
if args.weight_decay_sh < 1.0:
grid.sh_data.data *= args.weight_decay_sigma
if args.weight_decay_sigma < 1.0:
grid.density_data.data *= args.weight_decay_sh
# # For outputting the % sparsity of the gradient
# indexer = grid.sparse_sh_grad_indexer
# if indexer is not None:
# if indexer.dtype == torch.bool:
# nz = torch.count_nonzero(indexer)
# else:
# nz = indexer.size()
# with open(os.path.join(args.train_dir, 'grad_sparsity.txt'), 'a') as sparsity_file:
# sparsity_file.write(f"{gstep_id} {nz}\n")
# Apply TV/Sparsity regularizers
if args.lambda_tv > 0.0:
# with Timing("tv_inpl"):
grid.inplace_tv_grad(grid.density_data.grad,
scaling=args.lambda_tv,
sparse_frac=args.tv_sparsity,
logalpha=args.tv_logalpha,
ndc_coeffs=dset.ndc_coeffs,
contiguous=args.tv_contiguous)
if args.lambda_tv_sh > 0.0:
# with Timing("tv_color_inpl"):
grid.inplace_tv_color_grad(grid.sh_data.grad,
scaling=args.lambda_tv_sh,
sparse_frac=args.tv_sh_sparsity,
ndc_coeffs=dset.ndc_coeffs,
contiguous=args.tv_contiguous)
if args.lambda_tv_lumisphere > 0.0:
grid.inplace_tv_lumisphere_grad(grid.sh_data.grad,
scaling=args.lambda_tv_lumisphere,
dir_factor=args.tv_lumisphere_dir_factor,
sparse_frac=args.tv_lumisphere_sparsity,
ndc_coeffs=dset.ndc_coeffs)
if args.lambda_l2_sh > 0.0:
grid.inplace_l2_color_grad(grid.sh_data.grad,
scaling=args.lambda_l2_sh)
if grid.use_background and (args.lambda_tv_background_sigma > 0.0 or args.lambda_tv_background_color > 0.0):
grid.inplace_tv_background_grad(grid.background_data.grad,
scaling=args.lambda_tv_background_color,
scaling_density=args.lambda_tv_background_sigma,
sparse_frac=args.tv_background_sparsity,
contiguous=args.tv_contiguous)
if args.lambda_tv_basis > 0.0:
tv_basis = grid.tv_basis()
loss_tv_basis = tv_basis * args.lambda_tv_basis
loss_tv_basis.backward()
# print('nz density', torch.count_nonzero(grid.sparse_grad_indexer).item(),
# ' sh', torch.count_nonzero(grid.sparse_sh_grad_indexer).item())
# Manual SGD/rmsprop step
if gstep_id >= args.lr_fg_begin_step:
grid.optim_density_step(lr_sigma, beta=args.rms_beta, optim=args.sigma_optim)
grid.optim_sh_step(lr_sh, beta=args.rms_beta, optim=args.sh_optim)
if grid.use_background:
grid.optim_background_step(lr_sigma_bg, lr_color_bg, beta=args.rms_beta, optim=args.bg_optim)
if gstep_id >= args.lr_basis_begin_step:
if grid.basis_type == svox2.BASIS_TYPE_3D_TEXTURE:
grid.optim_basis_step(lr_basis, beta=args.rms_beta, optim=args.basis_optim)
elif grid.basis_type == svox2.BASIS_TYPE_MLP:
optim_basis_mlp.step()
optim_basis_mlp.zero_grad()
train_step()
gc.collect()
gstep_id_base += batches_per_epoch
# ckpt_path = path.join(args.train_dir, f'ckpt_{epoch_id:05d}.npz')
# Overwrite prev checkpoints since they are very huge
if args.save_every > 0 and (epoch_id + 1) % max(
factor, args.save_every) == 0 and not args.tune_mode:
print('Saving', ckpt_path)
grid.save(ckpt_path)
if (gstep_id_base - last_upsamp_step) >= args.upsamp_every:
last_upsamp_step = gstep_id_base
if reso_id < len(reso_list) - 1:
print('* Upsampling from', reso_list[reso_id], 'to', reso_list[reso_id + 1])
if args.tv_early_only > 0:
print('turning off TV regularization')
args.lambda_tv = 0.0
args.lambda_tv_sh = 0.0
elif args.tv_decay != 1.0:
args.lambda_tv *= args.tv_decay
args.lambda_tv_sh *= args.tv_decay
reso_id += 1
use_sparsify = True
z_reso = reso_list[reso_id] if isinstance(reso_list[reso_id], int) else reso_list[reso_id][2]
grid.resample(reso=reso_list[reso_id],
sigma_thresh=args.density_thresh,
weight_thresh=args.weight_thresh / z_reso if use_sparsify else 0.0,
dilate=2, #use_sparsify,
cameras=resample_cameras if args.thresh_type == 'weight' else None,
max_elements=args.max_grid_elements)
if grid.use_background and reso_id <= 1:
grid.sparsify_background(args.background_density_thresh)
if args.upsample_density_add:
grid.density_data.data[:] += args.upsample_density_add
if factor > 1 and reso_id < len(reso_list) - 1:
print('* Using higher resolution images due to large grid; new factor', factor)
factor //= 2
dset.gen_rays(factor=factor)
dset.shuffle_rays()
if gstep_id_base >= args.n_iters:
print('* Final eval and save')
eval_step()
global_stop_time = datetime.now()
secs = (global_stop_time - global_start_time).total_seconds()
timings_file = open(os.path.join(args.train_dir, 'time_mins.txt'), 'a')
timings_file.write(f"{secs / 60}\n")
if not args.tune_nosave:
grid.save(ckpt_path)
break