-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_evdnerf.py
1853 lines (1485 loc) · 88.5 KB
/
run_evdnerf.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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This script trains an EvDNeRF with various optional arguments, and periodically saves checkpoints and runs validations.
# For details, please refer to the paper EvDNeRF: Reconstructing Event Data with Dynamic Neural Radiance Fields.
# NOTE for consistency with previous NeRF codebases, we refer to images as "rgb" though we only consider intensity (1-channel) images.
import os, sys, glob
import imageio
import time
from numpy import empty
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm, trange
import json
from run_evdnerf_helpers import *
from load_blender import *
from metrics import *
# NOTE this suppresses tensorflow warnings and info
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
DEBUG = False
def set_seeds(seed):
np.random.seed(seed)
# random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def batchify(fn, chunk):
"""Constructs a version of 'fn' that applies to smaller batches.
"""
if chunk is None:
return fn
def ret(inputs_pos, inputs_time):
num_batches = inputs_pos.shape[0]
out_list = []
dx_list = []
for i in range(0, num_batches, chunk):
out, dx = fn(inputs_pos[i:i+chunk], [inputs_time[0][i:i+chunk], inputs_time[1][i:i+chunk]])
out_list += [out]
dx_list += [dx]
return torch.cat(out_list, 0), torch.cat(dx_list, 0)
return ret
def run_network(inputs, viewdirs, frame_time, fn, embed_fn, embeddirs_fn, embedtime_fn, netchunk=1024*64,
embd_time_discr=True):
"""Prepares inputs and applies network 'fn'.
inputs: N_rays x N_points_per_ray x 3
viewdirs: N_rays x 3
frame_time: N_rays x 1
"""
# assert len(torch.unique(frame_time)) == 1, "Only accepts all points from same time"
cur_time = torch.unique(frame_time)[0]
# embed position
inputs_flat = torch.reshape(inputs, [-1, inputs.shape[-1]])
embedded = embed_fn(inputs_flat)
# embed time
if embd_time_discr:
B, N, _ = inputs.shape
input_frame_time = frame_time[:, None].expand([B, N, 1])
input_frame_time_flat = torch.reshape(input_frame_time, [-1, 1])
embedded_time = embedtime_fn(input_frame_time_flat)
embedded_times = [embedded_time, embedded_time]
else:
assert NotImplementedError
# embed views
if viewdirs is not None:
input_dirs = viewdirs[:,None].expand(inputs.shape)
input_dirs_flat = torch.reshape(input_dirs, [-1, input_dirs.shape[-1]])
embedded_dirs = embeddirs_fn(input_dirs_flat)
embedded = torch.cat([embedded, embedded_dirs], -1)
outputs_flat, position_delta_flat = batchify(fn, netchunk)(embedded, embedded_times)
outputs = torch.reshape(outputs_flat, list(inputs.shape[:-1]) + [outputs_flat.shape[-1]])
position_delta = torch.reshape(position_delta_flat, list(inputs.shape[:-1]) + [position_delta_flat.shape[-1]])
return outputs, position_delta
def batchify_rays(rays_flat, chunk=1024*32, **kwargs):
"""Render rays in smaller minibatches to avoid OOM.
"""
all_ret = {}
for i in range(0, rays_flat.shape[0], chunk):
ret = render_rays(rays_flat[i:i+chunk], **kwargs)
for k in ret:
if k not in all_ret:
all_ret[k] = []
all_ret[k].append(ret[k])
all_ret = {k : torch.cat(all_ret[k], 0) for k in all_ret}
return all_ret
def render(H, W, focal, chunk=1024*32, rays=None, c2w=None, ndc=True,
near=0., far=1., frame_time=None,
use_viewdirs=False, c2w_staticcam=None,
**kwargs):
"""Render rays
Args:
H: int. Height of image in pixels.
W: int. Width of image in pixels.
focal: float. Focal length of pinhole camera.
chunk: int. Maximum number of rays to process simultaneously. Used to
control maximum memory usage. Does not affect final results.
rays: array of shape [2, batch_size, 3]. Ray origin and direction for
each example in batch.
c2w: array of shape [3, 4]. Camera-to-world transformation matrix.
ndc: bool. If True, represent ray origin, direction in NDC coordinates.
near: float or array of shape [batch_size]. Nearest distance for a ray.
far: float or array of shape [batch_size]. Farthest distance for a ray.
use_viewdirs: bool. If True, use viewing direction of a point in space in model.
c2w_staticcam: array of shape [3, 4]. If not None, use this transformation matrix for
camera while using other c2w argument for viewing directions.
Returns:
rgb_map: [batch_size, 3]. Predicted RGB values for rays.
disp_map: [batch_size]. Disparity map. Inverse of depth.
acc_map: [batch_size]. Accumulated opacity (alpha) along a ray.
extras: dict with everything returned by render_rays().
"""
if c2w is not None:
# special case to render full image
rays_o, rays_d = get_rays(H, W, focal, c2w)
else:
# use provided ray batch
rays_o, rays_d = rays
if use_viewdirs:
# provide ray directions as input
viewdirs = rays_d
if c2w_staticcam is not None:
# special case to visualize effect of viewdirs
rays_o, rays_d = get_rays(H, W, focal, c2w_staticcam)
viewdirs = viewdirs / torch.norm(viewdirs, dim=-1, keepdim=True)
viewdirs = torch.reshape(viewdirs, [-1,3]).float()
sh = rays_d.shape # [..., 3]
if ndc:
# for forward facing scenes
rays_o, rays_d = ndc_rays(H, W, focal, 1., rays_o, rays_d)
# Create ray batch
rays_o = torch.reshape(rays_o, [-1,3]).float()
rays_d = torch.reshape(rays_d, [-1,3]).float()
near, far = near * torch.ones_like(rays_d[...,:1]), far * torch.ones_like(rays_d[...,:1])
if len(frame_time.shape) <= 1:
frame_time = frame_time * torch.ones_like(rays_d[...,:1])
rays = torch.cat([rays_o, rays_d, near, far, frame_time], -1)
if use_viewdirs:
rays = torch.cat([rays, viewdirs], -1)
# Render and reshape
all_ret = batchify_rays(rays, chunk, **kwargs)
for k in all_ret:
k_sh = list(sh[:-1]) + list(all_ret[k].shape[1:])
all_ret[k] = torch.reshape(all_ret[k], k_sh)
k_extract = ['rgb_map', 'disp_map', 'acc_map']
ret_list = [all_ret[k] for k in k_extract]
ret_dict = {k : all_ret[k] for k in all_ret if k not in k_extract}
return ret_list + [ret_dict]
def render_path(render_poses, render_times, hwf, chunk, render_kwargs, gt_imgs=None, savedir=None, render_factor=0, save_also_gt=False, i_offset=0, scaling_factor=None, do_evim=False, render_scaler=torch.ones(1), starting_idx=0, pos_thresh=0.2, neg_thresh=0.2):
H, W, focal = hwf
if render_factor!=0:
# Render downsampled for speed
H = H//render_factor
W = W//render_factor
focal = focal/render_factor
if savedir is not None:
save_dir_estim = os.path.join(savedir, "estim")
save_dir_gt = os.path.join(savedir, "gt")
if not os.path.exists(save_dir_estim):
os.makedirs(save_dir_estim)
if save_also_gt and not os.path.exists(save_dir_gt):
os.makedirs(save_dir_gt)
if do_evim:
os.makedirs(os.path.join(save_dir_estim, 'rgb'))
os.makedirs(os.path.join(save_dir_estim, 'evim'))
if save_also_gt:
os.makedirs(os.path.join(save_dir_gt, 'rgb'))
os.makedirs(os.path.join(save_dir_gt, 'evim'))
if do_evim:
rgbs0 = []
disps0 = []
evims = []
for i in tqdm(range(starting_idx, len(render_poses)-1)):
rgb0, disp0, acc0, _ = render(H, W, focal, chunk=chunk, c2w=render_poses[i][:3,:4], frame_time=render_times[i], **render_kwargs)
rgb1, disp1, acc1, _ = render(H, W, focal, chunk=chunk, c2w=render_poses[i+1][:3,:4], frame_time=render_times[i+1], **render_kwargs)
pred_evim = compute_pred_ev(rgb1, rgb0, 1/torch.abs(render_scaler))
# scale rgbs as necessary
# if render_scaler is negative, scale rgbs to 1
if render_scaler < 0:
rgb0 /= rgb0.max()
rgb1 /= rgb1.max()
rgbs0.append(rgb0)
disps0.append(disp0)
evims.append(pred_evim)
if savedir is not None:
# save rgb0
rgb8_estim = to8b(rgbs0[-1].cpu().numpy())
filename = os.path.join(save_dir_estim, 'rgb', '{:06d}.png'.format(i+i_offset))
imageio.imwrite(filename, rgb8_estim)
# if last one, then save the rgb1 as well so we have a complete rgb image dataset
if i == len(render_poses)-2:
rgb8_estim = to8b(rgb1.cpu().numpy())
filename = os.path.join(save_dir_estim, 'rgb', '{:06d}.png'.format(i+i_offset+1))
imageio.imwrite(filename, rgb8_estim)
# save evim
pred_evim_vis = visualize_evim(pred_evim, darken_factor=1.0, pos_thresh=pos_thresh, neg_thresh=neg_thresh)
filename = os.path.join(save_dir_estim, 'evim', '{:06d}.png'.format(i+i_offset))
imageio.imwrite(filename, pred_evim_vis)
if save_also_gt:
rgb8_gt = to8b(gt_imgs['images'][i].cpu().numpy())
filename = os.path.join(save_dir_gt, 'rgb', '{:06d}.png'.format(i+i_offset))
imageio.imwrite(filename, rgb8_gt)
# if last one, then save the rgb1 as well so we have a complete rgb image dataset
if i == len(render_poses)-2:
rgb8_gt = to8b(gt_imgs['images'][i+1].cpu().numpy())
filename = os.path.join(save_dir_gt, 'rgb', '{:06d}.png'.format(i+i_offset+1))
imageio.imwrite(filename, rgb8_gt)
# also save test evim
gt_evim_vis = visualize_evim(gt_imgs['evims'][i], darken_factor=1.0, pos_thresh=pos_thresh, neg_thresh=neg_thresh)
filename = os.path.join(save_dir_gt, 'evim', '{:06d}.png'.format(i+i_offset))
imageio.imwrite(filename, gt_evim_vis)
rgbs0 = torch.stack(rgbs0, 0)
disps0 = torch.stack(disps0, 0)
evims = torch.stack(evims, 0)
return rgbs0, disps0, evims
else:
rgbs = []
disps = []
for i, (c2w, frame_time) in enumerate(zip(tqdm(render_poses), render_times)):
rgb, disp, acc, _ = render(H, W, focal, chunk=chunk, c2w=c2w[:3,:4], frame_time=frame_time, **render_kwargs)
rgbs.append(rgb.cpu().numpy())
disps.append(disp.cpu().numpy())
if savedir is not None:
rgb8_estim = to8b(rgbs[-1])
filename = os.path.join(save_dir_estim, '{:06d}.png'.format(i+i_offset))
imageio.imwrite(filename, rgb8_estim)
if save_also_gt:
rgb8_gt = to8b(gt_imgs[i])
filename = os.path.join(save_dir_gt, '{:06d}.png'.format(i+i_offset))
imageio.imwrite(filename, rgb8_gt)
rgbs = np.stack(rgbs, 0)
disps = np.stack(disps, 0)
return rgbs, disps
def create_nerf(args):
"""Instantiate NeRF's MLP model.
"""
embed_fn, input_ch = get_embedder(args.multires, 3, args.i_embed)
embedtime_fn, input_ch_time = get_embedder(args.multires, 1, args.i_embed)
input_ch_views = 0
embeddirs_fn = None
if args.use_viewdirs:
embeddirs_fn, input_ch_views = get_embedder(args.multires_views, 3, args.i_embed)
# NOTE output_ch not used when view_dirs=True (always)
output_ch = 5 if args.N_importance > 0 else 4
# NOTE adding output_color_ch argument to change it for rgb or gray
if args.dataset_type == 'blender':
output_color_ch = 3
elif args.dataset_type == 'gray' or 'events' in args.dataset_type:
output_color_ch = 1
else:
sys.exit(f'{args.dataset_type} dataset type not recognized for setting output_color_ch')
skips = [4]
model = NeRF.get_by_name(args.nerf_type, D=args.netdepth, W=args.netwidth,
input_ch=input_ch, output_ch=output_ch, skips=skips,
input_ch_views=input_ch_views, input_ch_time=input_ch_time,
use_viewdirs=args.use_viewdirs, embed_fn=embed_fn,
zero_canonical=not args.not_zero_canonical, output_color_ch=output_color_ch,
).to(device)
grad_vars = list(model.parameters())
model_fine = None
if args.use_two_models_for_fine:
model_fine = NeRF.get_by_name(args.nerf_type, D=args.netdepth_fine, W=args.netwidth_fine,
input_ch=input_ch, output_ch=output_ch, skips=skips,
input_ch_views=input_ch_views, input_ch_time=input_ch_time,
use_viewdirs=args.use_viewdirs, embed_fn=embed_fn,
zero_canonical=not args.not_zero_canonical).to(device)
grad_vars += list(model_fine.parameters())
network_query_fn = lambda inputs, viewdirs, ts, network_fn : run_network(inputs, viewdirs, ts, network_fn,
embed_fn=embed_fn,
embeddirs_fn=embeddirs_fn,
embedtime_fn=embedtime_fn,
netchunk=args.netchunk,
embd_time_discr=args.nerf_type!="temporal")
# create optimizer
optimizer = torch.optim.Adam(params=grad_vars, lr=args.lrate, betas=(0.9, 0.999))
start = 0
expname = args.expname
##########################
# Load checkpoints
if args.ft_file is not None and args.ft_file != 'None':
ckpts = [args.ft_file]
else:
if args.ft_path is not None and args.ft_path != 'None':
ckptdir = args.ft_path
else:
ckptdir = os.path.join(args.workspace, expname)
isExist = os.path.exists(ckptdir)
sorted_files = sorted(os.listdir(ckptdir)) if isExist else []
does_tar_exist = ['hit!' for f in sorted_files if 'tar' in f]
ckpts = [os.path.join(ckptdir, f) for f in sorted_files if 'tar' in f] if does_tar_exist else []
if not args.no_reload:
if len(ckpts) > 0:
ckpt_path = ckpts[-1] # choose the last weights file
mylogger(logfile, f'[CKPT] Reloading from {ckpt_path}')
ckpt = torch.load(ckpt_path)
start = ckpt['global_step']
optimizer.load_state_dict(ckpt['optimizer_state_dict'])
# Load model
model.load_state_dict(ckpt['network_fn_state_dict'])
if model_fine is not None:
model_fine.load_state_dict(ckpt['network_fine_state_dict'])
else:
mylogger(logfile, f'No ckpt found at directory {ckptdir}')
else:
mylogger(logfile, f'No reload!')
##########################
render_kwargs_train = {
'network_query_fn' : network_query_fn,
'perturb' : args.perturb,
'N_importance' : args.N_importance,
'network_fine': model_fine,
'N_samples' : args.N_samples,
'network_fn' : model,
'use_viewdirs' : args.use_viewdirs,
'white_bkgd' : args.white_bkgd,
'raw_noise_std' : args.raw_noise_std,
'use_two_models_for_fine' : args.use_two_models_for_fine,
}
# NDC only good for LLFF-style forward facing data
# if args.dataset_type != 'llff' or args.no_ndc:
if not args.three_view or args.no_ndc:
render_kwargs_train['ndc'] = False
render_kwargs_train['lindisp'] = args.lindisp
render_kwargs_test = {k : render_kwargs_train[k] for k in render_kwargs_train}
render_kwargs_test['perturb'] = False
render_kwargs_test['raw_noise_std'] = 0.
return render_kwargs_train, render_kwargs_test, start, grad_vars, optimizer
def raw2outputs(raw, z_vals, rays_d, raw_noise_std=0, white_bkgd=False, pytest=False):
"""Transforms model's predictions to semantically meaningful values.
Args:
raw: [num_rays, num_samples along ray, 4]. Prediction from model.
z_vals: [num_rays, num_samples along ray]. Integration time.
rays_d: [num_rays, 3]. Direction of each ray.
Returns:
rgb_map: [num_rays, 3]. Estimated RGB color of a ray.
disp_map: [num_rays]. Disparity map. Inverse of depth map.
acc_map: [num_rays]. Sum of weights along each ray.
weights: [num_rays, num_samples]. Weights assigned to each sampled color.
depth_map: [num_rays]. Estimated distance to object.
"""
raw2alpha = lambda raw, dists, act_fn=F.relu: 1.-torch.exp(-act_fn(raw)*dists)
dists = z_vals[...,1:] - z_vals[...,:-1]
dists = torch.cat([dists, torch.Tensor([1e10], device=device).expand(dists[...,:1].shape)], -1) # [N_rays, N_samples]
dists = dists * torch.norm(rays_d[...,None,:], dim=-1)
rgb = torch.sigmoid(raw[...,:1]) # [N_rays, N_samples, 1]
noise = 0.
if raw_noise_std > 0.:
noise = torch.randn(raw[...,3].shape) * raw_noise_std
# Overwrite randomly sampled data if pytest
if pytest:
np.random.seed(0)
noise = np.random.rand(*list(raw[...,3].shape)) * raw_noise_std
noise = torch.Tensor(noise, device=device)
alpha = raw2alpha(raw[...,-1] + noise, dists, torch.sigmoid) # [N_rays, N_samples]
# weights = alpha * tf.math.cumprod(1.-alpha + 1e-10, -1, exclusive=True)
weights = alpha * torch.cumprod(torch.cat([torch.ones((alpha.shape[0], 1)), 1.-alpha + 1e-10], -1), -1)[:, :-1]
rgb_map = torch.sum(weights[...,None] * rgb, -2) # [N_rays, 3]
depth_map = torch.sum(weights * z_vals, -1)
disp_map = 1./torch.max(1e-10 * torch.ones_like(depth_map), depth_map / torch.sum(weights, -1))
acc_map = torch.sum(weights, -1)
if white_bkgd:
rgb_map = rgb_map + (1.-acc_map[...,None])
# rgb_map = rgb_map + torch.cat([acc_map[..., None] * 0, acc_map[..., None] * 0, (1. - acc_map[..., None])], -1)
return rgb_map, disp_map, acc_map, weights, depth_map
def render_rays(ray_batch,
network_fn,
network_query_fn,
N_samples,
retraw=False,
lindisp=False,
perturb=0.,
N_importance=0,
network_fine=None,
white_bkgd=False,
raw_noise_std=0.,
verbose=False,
pytest=False,
z_vals=None,
use_two_models_for_fine=False):
"""Volumetric rendering.
Args:
ray_batch: array of shape [batch_size, ...]. All information necessary
for sampling along a ray, including: ray origin, ray direction, min
dist, max dist, and unit-magnitude viewing direction.
network_fn: function. Model for predicting RGB and density at each point
in space.
network_query_fn: function used for passing queries to network_fn.
N_samples: int. Number of different times to sample along each ray.
retraw: bool. If True, include model's raw, unprocessed predictions.
lindisp: bool. If True, sample linearly in inverse depth rather than in depth.
perturb: float, 0 or 1. If non-zero, each ray is sampled at stratified
random points in time.
N_importance: int. Number of additional times to sample along each ray.
These samples are only passed to network_fine.
network_fine: "fine" network with same spec as network_fn.
white_bkgd: bool. If True, assume a white background.
raw_noise_std: ...
verbose: bool. If True, print more debugging info.
Returns:
rgb_map: [num_rays, 3]. Estimated RGB color of a ray. Comes from fine model.
disp_map: [num_rays]. Disparity map. 1 / depth.
acc_map: [num_rays]. Accumulated opacity along each ray. Comes from fine model.
raw: [num_rays, num_samples, 4]. Raw predictions from model.
rgb0: See rgb_map. Output for coarse model.
disp0: See disp_map. Output for coarse model.
acc0: See acc_map. Output for coarse model.
z_std: [num_rays]. Standard deviation of distances along ray for each
sample.
"""
N_rays = ray_batch.shape[0]
rays_o, rays_d = ray_batch[:,0:3], ray_batch[:,3:6] # [N_rays, 3] each
viewdirs = ray_batch[:,-3:] if ray_batch.shape[-1] > 9 else None
bounds = torch.reshape(ray_batch[...,6:9], [-1,1,3])
near, far, frame_time = bounds[...,0], bounds[...,1], bounds[...,2] # [-1,1]
z_samples = None
rgb_map_0, disp_map_0, acc_map_0, position_delta_0 = None, None, None, None
if z_vals is None:
t_vals = torch.linspace(0., 1., steps=N_samples)
if not lindisp:
z_vals = near * (1.-t_vals) + far * (t_vals)
else:
z_vals = 1./(1./near * (1.-t_vals) + 1./far * (t_vals))
z_vals = z_vals.expand([N_rays, N_samples])
if perturb > 0.:
# get intervals between samples
mids = .5 * (z_vals[...,1:] + z_vals[...,:-1])
upper = torch.cat([mids, z_vals[...,-1:]], -1)
lower = torch.cat([z_vals[...,:1], mids], -1)
# stratified samples in those intervals
t_rand = torch.rand(z_vals.shape)
# Pytest, overwrite u with numpy's fixed random numbers
if pytest:
np.random.seed(0)
t_rand = np.random.rand(*list(z_vals.shape))
t_rand = torch.Tensor(t_rand, device=device)
z_vals = lower + (upper - lower) * t_rand
pts = rays_o[...,None,:] + rays_d[...,None,:] * z_vals[...,:,None] # [N_rays, N_samples, 3]
if N_importance <= 0:
raw, position_delta = network_query_fn(pts, viewdirs, frame_time, network_fn)
rgb_map, disp_map, acc_map, weights, depth_map = raw2outputs(raw, z_vals, rays_d, raw_noise_std, white_bkgd, pytest=pytest)
else:
if use_two_models_for_fine:
raw, position_delta_0 = network_query_fn(pts, viewdirs, frame_time, network_fn)
rgb_map_0, disp_map_0, acc_map_0, weights, _ = raw2outputs(raw, z_vals, rays_d, raw_noise_std, white_bkgd, pytest=pytest)
else:
with torch.no_grad():
raw, _ = network_query_fn(pts, viewdirs, frame_time, network_fn)
_, _, _, weights, _ = raw2outputs(raw, z_vals, rays_d, raw_noise_std, white_bkgd, pytest=pytest)
z_vals_mid = .5 * (z_vals[...,1:] + z_vals[...,:-1])
z_samples = sample_pdf(z_vals_mid, weights[...,1:-1], N_importance, det=(perturb==0.), pytest=pytest)
z_samples = z_samples.detach()
z_vals, _ = torch.sort(torch.cat([z_vals, z_samples], -1), -1)
pts = rays_o[...,None,:] + rays_d[...,None,:] * z_vals[...,:,None] # [N_rays, N_samples + N_importance, 3]
run_fn = network_fn if network_fine is None else network_fine
raw, position_delta = network_query_fn(pts, viewdirs, frame_time, run_fn)
rgb_map, disp_map, acc_map, weights, _ = raw2outputs(raw, z_vals, rays_d, raw_noise_std, white_bkgd, pytest=pytest)
ret = {'rgb_map' : rgb_map, 'disp_map' : disp_map, 'acc_map' : acc_map, 'z_vals' : z_vals, 'position_delta' : position_delta, 'weights' : weights}
if retraw:
ret['raw'] = raw
if N_importance > 0:
if rgb_map_0 is not None:
ret['rgb0'] = rgb_map_0
if disp_map_0 is not None:
ret['disp0'] = disp_map_0
if acc_map_0 is not None:
ret['acc0'] = acc_map_0
if position_delta_0 is not None:
ret['position_delta_0'] = position_delta_0
if z_samples is not None:
ret['z_std'] = torch.std(z_samples, dim=-1, unbiased=False) # [N_rays]
for k in ret:
if (torch.isnan(ret[k]).any() or torch.isinf(ret[k]).any()) and DEBUG:
mylogger(logfile, f"! [Numerical Error] {k} contains nan or inf.")
return ret
def form_eventframe_batched(evs, views, H, W, times0, times1=None, N_ev_rays=512, N=None, device='cpu', is_half_res=False, precrop_frac=1.0, pos_thresh=0.2, neg_thresh=0.2, center=None):
if center is None:
center = [H//2, W//2]
if precrop_frac > 0.0:
dH = int(H//2 * precrop_frac)
dW = int(W//2 * precrop_frac)
else:
dH = int(H//2)
dW = int(W//2)
x = torch.zeros(views.shape[0])
y = torch.zeros(views.shape[0])
times1_ = torch.zeros(views.shape[0])
target = torch.zeros(views.shape[0])
# for each target value, we need to choose a single event pixel or random pixel
# this would be more efficient if calculated each viewpoint's target values as a batch
# sorted_indices = np.argsort(views)
# views = views[sorted_indices]
# times0 = times0[sorted_indices]
# curr_view_i = -1
for i in range(views.shape[0]):
if i < N_ev_rays: # choose from nonzero events
# if views[i] > curr_view_i:
view_events = evs[views[i]]
# curr_view_i = views[i]
vet = view_events[view_events[:,0] >= times0[i]*1e9][:N]
if precrop_frac > 0.0:
precrop_mask = torch.bitwise_and( torch.bitwise_and( vet[:,2] >= (center[0] - dH) , vet[:,2] < (center[0] + dH) ) , torch.bitwise_and( vet[:,1] >= (center[1] - dW) , vet[:,1] < (center[1] + dW) ) )
vet = vet[precrop_mask]
# this is biased towards locations with lots of events
# rand_px = np.random.randint(0, vet.shape[0])
# rand_x = vet[rand_px, 1]
# rand_y = vet[rand_px, 2]
# so instead, choose randomly from the unique values
unique_xs = torch.unique(vet[:,1])
rand_x = unique_xs[np.random.randint(0, unique_xs.shape[0])]
unique_ys = torch.unique(vet[:,2])
rand_y = unique_ys[np.random.randint(0, unique_ys.shape[0])]
else: # choose randomly
rand_x = np.random.randint(max(center[0]-dW, 0), min(center[0]+dW, W))
rand_y = np.random.randint(max(center[1]-dH, 0), min(center[1]+dH, H))
x[i] = rand_x
y[i] = rand_y
rand_evs_idxs = torch.bitwise_and( vet[:,1]==rand_x , vet[:,2]==rand_y )
rand_negevs_idxs = torch.bitwise_and( rand_evs_idxs , vet[:,3]<0 )
rand_posevs_idxs = torch.bitwise_and( rand_evs_idxs , vet[:,3]>0 )
target_value = pos_thresh*rand_posevs_idxs.sum() - neg_thresh*rand_negevs_idxs.sum()
target[i] = target_value
times1_[i] = (vet[-1,0]+1) / 1e9
# if is_half_res:
# target /= 4.0
# un-sort the outputs to undo our original sorting
# x = x[np.argsort(sorted_indices)]
# y = y[np.argsort(sorted_indices)]
# target = target[np.argsort(sorted_indices)]
# times1_ = times1_[np.argsort(sorted_indices)]
return x.long(), y.long(), target.to(device), times1_.to(device)
def form_eventframe(view_events, H, W, times0, times1=None, N=None, device='cpu', is_half_res=False, pos_thresh=0.2, neg_thresh=0.2):
if times1 is not None:
# extract the time-sliced events
# it's likely that view_events is on cpu
valid_ev_idxs_timed = torch.bitwise_and(view_events[:,0] >= times0.cpu()*1e9, view_events[:,0] < times1[0].cpu()*1e9)
view_events_timed = view_events[valid_ev_idxs_timed]
elif N is not None:
view_events_timed = view_events[view_events[:,0] >= times0*1e9][:N]
times1 = (view_events_timed[-1,0]+1) / 1e9
else:
raise ValueError("form_eventframe() requires either times1 or N to be not None")
view_events_timed_pos = view_events_timed[view_events_timed[:,-1] > 0]
view_events_timed_neg = view_events_timed[view_events_timed[:,-1] < 0]
frame = pos_thresh*np.histogram2d(view_events_timed_pos[:,1].cpu().numpy(), view_events_timed_pos[:,2].cpu().numpy(), bins=(W, H), range=[[0, W], [0, H]])[0] - neg_thresh*np.histogram2d(view_events_timed_neg[:,1].cpu().numpy(), view_events_timed_neg[:,2].cpu().numpy(), bins=(W, H), range=[[0, W], [0, H]])[0]
# # if continuous eventstream was made half-res, then 4x events have coalesced into a single pixel.
# # make up for it by dividing the final sum per pixel by 4.
# if is_half_res:
# frame = (frame / 4.0) // pos_thresh * pos_thresh
return torch.Tensor(frame.T).to(device), torch.Tensor(times1).to(device)
def bin_evim(evim, target_maxabs_value, pos_thresh=0.2, neg_thresh=0.2):
binned_evim = evim * target_maxabs_value
pos_ids = evim > 0
neg_ids = evim < 0
binned_evim[pos_ids] = evim[pos_ids] // pos_thresh
binned_evim[neg_ids] = evim[neg_ids] // neg_thresh
return binned_evim
def visualize_evim(evim, pos_thresh=0.2, neg_thresh=0.2, darken_factor=0.7):
# darken_factor=1.0 means no extra darkening
frame = np.ones((*evim.shape, 3))
binned = bin_evim(evim, target_maxabs_value=1.0, pos_thresh=pos_thresh, neg_thresh=neg_thresh)
binned = binned.cpu()
# R or B pixel based on net of all events at pixel during timeframe
neg_ids = (binned<0).nonzero()
pos_ids = (binned>0).nonzero()
# note in below, binned[neg_ids] < 0 and binned[pos_ids] > 0
frame[neg_ids[:,0], neg_ids[:,1], 0] = darken_factor + binned[neg_ids[:,0], neg_ids[:,1]]/binned.abs().max()/(1/darken_factor)
frame[neg_ids[:,0], neg_ids[:,1], 1] = darken_factor + binned[neg_ids[:,0], neg_ids[:,1]]/binned.abs().max()/(1/darken_factor)
frame[pos_ids[:,0], pos_ids[:,1], 1] = darken_factor - binned[pos_ids[:,0], pos_ids[:,1]]/binned.abs().max()/(1/darken_factor)
frame[pos_ids[:,0], pos_ids[:,1], 2] = darken_factor - binned[pos_ids[:,0], pos_ids[:,1]]/binned.abs().max()/(1/darken_factor)
return (frame*255.0).astype(np.uint8)
def config_parser():
import configargparse
parser = configargparse.ArgumentParser()
parser.add_argument('--config', is_config_file=True,
help='config file path')
parser.add_argument("--expname", type=str,
help='experiment name')
parser.add_argument("--workspace", type=str, default='./logs/',
help='where to store ckpts and logs')
parser.add_argument("--suffix", type=str, default='',
help='(optional) suffix to append to workspace tag')
parser.add_argument("--root_path", type=str, default='',
help='path of main file')
parser.add_argument("--datadir", type=str, default='./data/llff/fern',
help='input data directory')
# training options
parser.add_argument("--nerf_type", type=str, default="original",
help='nerf network type')
parser.add_argument("--N_iter", type=int, default=500000,
help='num training iterations')
parser.add_argument("--netdepth", type=int, default=8,
help='layers in network')
parser.add_argument("--netwidth", type=int, default=256,
help='channels per layer')
parser.add_argument("--netdepth_fine", type=int, default=8,
help='layers in fine network')
parser.add_argument("--netwidth_fine", type=int, default=256,
help='channels per layer in fine network')
parser.add_argument("--N_rays", type=int, default=1024,
help='batch size (number of random rays per gradient step)')
parser.add_argument("--frac_bg_rays", type=float, default=0.1,
help='fraction of N_rays dedicated for random selection (bg)')
parser.add_argument("--do_half_precision", action='store_true',
help='(no longer implemented) do half precision training and inference')
parser.add_argument("--lrate", type=float, default=5e-4,
help='learning rate')
parser.add_argument("--lrate_decay", type=float, default=500e3,
help='exponential learning rate decay')
parser.add_argument("--chunk", type=int, default=1024*32,
help='number of rays processed in parallel, decrease if running out of memory')
parser.add_argument("--netchunk", type=int, default=1024*64,
help='number of pts sent through network in parallel, decrease if running out of memory')
parser.add_argument("--no_batching", action='store_true',
help='only take random rays from 1 image at a time')
parser.add_argument("--no_reload", action='store_true',
help='do not reload weights from saved ckpt')
parser.add_argument("--ft_path", type=str, default=None,
help='specific weights npy file to reload for coarse network')
parser.add_argument("--ft_file", type=str, default=None,
help='specific weights .tar file to reload for coarse network')
# rendering options
parser.add_argument("--N_samples", type=int, default=64,
help='number of coarse samples per ray')
parser.add_argument("--not_zero_canonical", action='store_true',
help='if set zero time is not the canonic space')
parser.add_argument("--N_importance", type=int, default=0,
help='number of additional fine samples per ray')
parser.add_argument("--perturb", type=float, default=1.,
help='set to 0. for no jitter, 1. for jitter')
parser.add_argument("--use_viewdirs", action='store_true',
help='use full 5D input instead of 3D')
parser.add_argument("--i_embed", type=int, default=0,
help='set 0 for default positional encoding, -1 for none')
parser.add_argument("--multires", type=int, default=10,
help='log2 of max freq for positional encoding (3D location)')
parser.add_argument("--multires_views", type=int, default=4,
help='log2 of max freq for positional encoding (2D direction)')
parser.add_argument("--raw_noise_std", type=float, default=0.,
help='std dev of noise added to regularize sigma_a output, 1e0 recommended')
parser.add_argument("--use_two_models_for_fine", action='store_true',
help='use two models for fine results')
# training options
parser.add_argument("--precrop_iters", type=int, default=0,
help='number of steps to train on central crops')
parser.add_argument("--precrop_iters_time", type=int, default=0,
help='number of steps to train on central time')
parser.add_argument("--precrop_frac", type=float,
default=.5, help='fraction of img taken for central crops')
parser.add_argument("--add_tv_loss", action='store_true',
help='evaluate tv loss')
parser.add_argument("--tv_loss_weight", type=float,
default=1.e-4, help='weight of tv loss')
# dataset options
parser.add_argument("--dataset_type", type=str, default='llff',
help='options: llff / blender / deepvoxels')
parser.add_argument("--testskip", type=int, default=4,
help='will load 1/N images from test/val sets, useful for large datasets like deepvoxels')
## deepvoxels flags
parser.add_argument("--shape", type=str, default='greek',
help='options : armchair / cube / greek / vase')
## blender flags
parser.add_argument("--white_bkgd", action='store_true',
help='set to render synthetic data on a white bkgd (always use for dvoxels)')
parser.add_argument("--half_res", action='store_true',
help='load data and make it half the resolution by interpolation')
## llff flags
parser.add_argument("--factor", type=int, default=8,
help='downsample factor for LLFF images')
parser.add_argument("--no_ndc", action='store_true',
help='do not use normalized device coordinates (set for non-forward facing scenes)')
parser.add_argument("--lindisp", action='store_true',
help='sampling linearly in disparity rather than depth')
parser.add_argument("--spherify", action='store_true',
help='set for spherical 360 scenes')
parser.add_argument("--llffhold", type=int, default=8,
help='will take every 1/N images as LLFF test set, paper uses 8')
# logging/saving options
parser.add_argument("--i_print", type=int, default=100,
help='frequency of console printout and metric logging')
parser.add_argument("--i_img", type=int, default=10000,
help='frequency of tensorboard image logging')
parser.add_argument("--i_weights", type=int, default=10000,
help='frequency of weight ckpt saving')
parser.add_argument("--i_testset", type=int, default=10000000,
help='frequency of testset saving')
parser.add_argument("--i_video", type=int, default=10000000,
help='frequency of render_poses video saving')
# my arguments
parser.add_argument("--random_seed", type=int, default=0,
help='set the random seed to use')
parser.add_argument("--keyframing", type=int, default=0,
help='set the highest keyframing split, to reduce by factors of 2')
parser.add_argument("--near", type=float, default=2.0,
help='ray distance bounds: near')
parser.add_argument("--far", type=float, default=6.0,
help='ray distance bounds: far')
parser.add_argument("--render_only", action='store_true',
help='do not optimize; reload weights and render out render_poses path')
parser.add_argument("--render_test", action='store_true',
help='render the test set instead of render_poses path')
parser.add_argument("--render_factor", type=int, default=0,
help='downsampling factor to speed up rendering, set 4 or 8 for fast preview')
parser.add_argument("--render_test_path", type=str, default='None',
help='directory containing transforms_test.json with requested test path')
parser.add_argument("--render_scaler", type=float, default=1.0,
help='Scaler to scale up image (exp) and evim (mult) renders')
parser.add_argument("--weight_init", type=str, default=None,
help='weight initialization of DNeRF')
parser.add_argument("--single_view", type=int, default=-1,
help="load only a single viewpoint of a multiview dataset")
parser.add_argument("--N_batched_evs", type=int, default=0,
help='(1 means randomize) train on N-batch of events instead of the datasets consecutive timesteps')
parser.add_argument("--randomized_t0", action='store_true',
help='randomize index0 instead of choosing from the [0, T-2]dataset timesteps')
parser.add_argument("--time_window", type=float, default=0.0,
help='(work in progress) time window duration of events to train on, if using randomized_t0 and not using N_batched_evs; if 0.0 then randomly chosen')
parser.add_argument("--lr_warmup_iters", type=int, default=0,
help='how many steps to do a linear warmup over')
parser.add_argument("--grad_clipping", type=float, default=np.inf,
help='what value to clip grad norms to (0.5 was used for real-world data)')
parser.add_argument("--batch_over_images", action='store_true',
help='batching over multiple training images instead of rays in single training images')
parser.add_argument("--gamma", type=float, default=-1.0,
help='gamma correction, meant for color-to-grayscale conversion')
parser.add_argument("--evim_darken_factor", type=float, default=1.0,
help='darkening factor to manually make the saved events image colored correctly or visually discernable')
parser.add_argument("--boi_dataselect", type=str, default='pcitt_uniform',
help='pcitt increases training horizon gradually, and sawtooth increases likelihood of seeing the new, later training samples')
parser.add_argument("--lr_decay_rate", type=float, default=0.1,
help='exponential learning rate decay factor (reached after lr_decay*1000 iters)')
# NOTE not using args.dnerf option -- instead, directly using old run_dnerf.py script
parser.add_argument("--dnerf", action='store_true',
help='whether to train rgb0 only, i.e. dnerf')
parser.add_argument("--num_views", type=int, default=18,
help='if dnerf, then need to manually specify number of training views in multiview dataset')
parser.add_argument("--num_timesteps", type=int, default=32,
help='if dnerf, then need to manually specify number of training timesteps in multiview dataset')
parser.add_argument("--is_e2vid", action='store_true',
help='Rendered t=t0 from model corresponds to ground truth t=t1 (used for E2Vid->DNeRF baseline)')
parser.add_argument("--render_train", action='store_true',
help='Render on training dataset rather than testing, usually to generate an image dataset on which to train a dnerf model')
parser.add_argument("--run_eval", type=int, default=0,
help='Eval on periodically saved weights over course of training; 0 means no eval, positive number is the skip factor when reading weights files (1 means eval on every saved weights file)')
parser.add_argument("--three_view", action='store_true',
help='whether to use only the first 3 views of training/val datasets, to emulate the 3-view real-data setup')
parser.add_argument("--render_validation", action='store_true',
help='Just run evaluation() function as would be done during training')
parser.add_argument("--ray_jitter", action='store_true',
help='Use ray jittering to improve validation viewpoint rendering')
parser.add_argument("--starting_idx", type=int, default=0,
help='Starting index for render function')
parser.add_argument("--dist_loss", type=float, default=0.0,
help='Use distortion regularization loss described in Mip-NeRF 360; dist_loss arg is the lambda regularizer on this term')
parser.add_argument("--ev_threshold_loss", type=float, default=0.0,
help='Use custom piecewise loss for event-based reconstruction that sets loss to 0 for the per-pixel correct number of generated events and MSE everywhere else')
parser.add_argument("--rgb_loss", type=float, default=0.0,
help='Use RGB loss (MSE, an actually single-channel) on both predicted images; rgb_loss arg is the weight on this term')
parser.add_argument("--pos_thresh", type=float, default=0.2,
help='Positive event threshold that is surpassed in the following inequality to generate a positive event: log(i1) - log(i0) > pos_thresh')
parser.add_argument("--neg_thresh", type=float, default=0.2,
help='Negative event threshold that is surpassed in the following inequality to generate a negative event: log(i1) - log(i0) < -neg_thresh')
parser.add_argument("--path_to_gt_ims", type=str, default='',
help='Path to ground truth images ONLY to serve for prediction image correction (affine transformation)')
parser.add_argument("--affineshift_predims", action='store_true',
help='Perform an affine transformation on predicted images to match white balance of ground truth images')
parser.add_argument("--target_scaledown", type=float, default=0.0,
help='(0.0 reverts to scaling by target.max()) On MSELoss between target and predicted evim, scale down the target by this factor.')
parser.add_argument("--num_views_training", type=int, default=0,
help='(0 means use all views) Number of interpolated views to use for training (usually of 18 views total), starting from view0.')
parser.add_argument("--render_test_path_testskip", type=int, default=1,
help='specifically for render_test_path, how many frames to skip between each rendered frame')
parser.add_argument("--render_test_path_start", type=int, default=0,
help='specifically for render_test_path, index to start rendering at')
return parser
def my_lr_scheduler(global_step, lrate, lrate_decay, lr_warmup_iters, decay_rate):
if global_step < lr_warmup_iters:
lr = (0.9*lrate)/lr_warmup_iters * global_step + 0.1*lrate
else:
decay_steps = lrate_decay
lr = lrate * (decay_rate ** ((global_step-lr_warmup_iters) / decay_steps))
return lr
def mylogger(logfile, msg):
print(msg)
logfile.write(msg+'\n')
def train():
#################################
### Experiment initialization ###
#################################
init_time = time.time()
args = config_parser().parse_args()
# set seeds
set_seeds(args.random_seed)
# Create log dir and file, and copy the args, config file
from datetime import datetime
expname = args.expname+datetime.now().strftime('_d%m_%d_t%H_%M')
if args.suffix != '':
expname += '_' + args.suffix
args.workspace = os.path.join(args.workspace, expname)
folder_name = args.workspace
counter = 1
while True:
if not os.path.exists(args.workspace):
break
args.workspace = f"{folder_name}_{counter}"
counter += 1
os.makedirs(args.workspace, exist_ok=False)
f = os.path.join(args.workspace, 'args.txt')
with open(f, 'w') as file:
for arg in sorted(vars(args)):
attr = getattr(args, arg)
file.write('{} = {}\n'.format(arg, attr))
if args.config is not None:
f = os.path.join(args.workspace, 'config.txt')
with open(f, 'w') as file:
file.write(open(args.config, 'r').read())
f = os.path.join(args.workspace, 'log.txt')
global logfile
logfile = open(f, 'w')
# Summary writers
writer = SummaryWriter(args.workspace)
# save transforms train and val
try:
from shutil import copyfile
copyfile(os.path.join(args.root_path, args.datadir, 'transforms_train.json'), os.path.join(args.workspace, 'transforms_train.json'))
copyfile(os.path.join(args.root_path, args.datadir, 'transforms_val.json'), os.path.join(args.workspace, 'transforms_val.json'))
if os.path.exists(os.path.join(args.root_path, args.datadir, 'transforms_test.json')):
copyfile(os.path.join(args.root_path, args.datadir, 'transforms_test.json'), os.path.join(args.workspace, 'transforms_test.json'))