-
Notifications
You must be signed in to change notification settings - Fork 1
/
render_utils.py
1773 lines (1398 loc) · 72.3 KB
/
render_utils.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
import os, sys
import numpy as np
import imageio
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
from run_nerf_helpers import *
from bokeh_utils import MPIBokehRenderer_final,MPIBokehRenderer_blending_final
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
DEBUG = False
# INFERENCE = True
def splat_rgb_img(ret, ratio, R_w2t, t_w2t, j, H, W, focal, fwd_flow):
import softsplat
raw_rgba_s = torch.cat([ret['raw_rgb'], ret['raw_alpha'].unsqueeze(-1)], dim=-1)
raw_rgba = raw_rgba_s[:, :, j, :].permute(2, 0, 1).unsqueeze(0).contiguous().cuda()
pts_ref = ret['pts_ref'][:, :, j, :3]
pts_ref_e_G = NDC2Euclidean(pts_ref, H, W, focal)
if fwd_flow:
pts_post = pts_ref + ret['raw_sf_ref2post'][:, :, j, :]
else:
pts_post = pts_ref + ret['raw_sf_ref2prev'][:, :, j, :]
pts_post_e_G = NDC2Euclidean(pts_post, H, W, focal)
pts_mid_e_G = (pts_post_e_G - pts_ref_e_G) * ratio + pts_ref_e_G
pts_mid_e_local = se3_transform_points(pts_mid_e_G,
R_w2t.unsqueeze(0).unsqueeze(0),
t_w2t.unsqueeze(0).unsqueeze(0))
pts_2d_mid = perspective_projection(pts_mid_e_local, H, W, focal)
xx, yy = torch.meshgrid(torch.linspace(0, W-1, W), torch.linspace(0, H-1, H))
xx = xx.t()
yy = yy.t()
pts_2d_original = torch.stack([xx, yy], -1)
flow_2d = pts_2d_mid - pts_2d_original
flow_2d = flow_2d.permute(2, 0, 1).unsqueeze(0).contiguous().cuda()
splat_raw_rgba_dy = softsplat.FunctionSoftsplat(tenInput=raw_rgba,
tenFlow=flow_2d,
tenMetric=None,
strType='average')
# splatting for static nerf
pts_rig_e_local = se3_transform_points(pts_ref_e_G,
R_w2t.unsqueeze(0).unsqueeze(0),
t_w2t.unsqueeze(0).unsqueeze(0))
pts_2d_rig = perspective_projection(pts_rig_e_local, H, W, focal)
flow_2d_rig = pts_2d_rig - pts_2d_original
flow_2d_rig = flow_2d_rig.permute(2, 0, 1).unsqueeze(0).contiguous().cuda()
raw_rgba_rig = torch.cat([ret['raw_rgb_rigid'], ret['raw_alpha_rigid'].unsqueeze(-1)], dim=-1)
raw_rgba_rig = raw_rgba_rig[:, :, j, :].permute(2, 0, 1).unsqueeze(0).contiguous().cuda()
splat_raw_rgba_rig = softsplat.FunctionSoftsplat(tenInput=raw_rgba_rig,
tenFlow=flow_2d_rig,
tenMetric=None,
strType='average')
splat_alpha_dy = splat_raw_rgba_dy[0, 3:4, :, :]
splat_rgb_dy = splat_raw_rgba_dy[0, 0:3, :, :]
splat_alpha_rig = splat_raw_rgba_rig[0, 3:4, :, :]
splat_rgb_rig = splat_raw_rgba_rig[0, 0:3, :, :]
return splat_alpha_dy, splat_rgb_dy, splat_alpha_rig, splat_rgb_rig
from poseInterpolator import *
def render_slowmo_bt(disps, render_poses, bt_poses,
hwf, chunk, render_kwargs,
gt_imgs=None, savedir=None,
render_factor=0, target_idx=10):
# import scipy.io
H, W, focal = hwf
if render_factor!=0:
# Render downsampled for speed
H = H//render_factor
W = W//render_factor
focal = focal/render_factor
t = time.time()
count = 0
save_img_dir = os.path.join(savedir, 'images')
# save_depth_dir = os.path.join(savedir, 'depths')
os.makedirs(save_img_dir, exist_ok=True)
# os.makedirs(save_depth_dir, exist_ok=True)
for i, cur_time in enumerate(np.linspace(target_idx - 10., target_idx + 10., 200 + 1).tolist()):
flow_time = int(np.floor(cur_time))
ratio = cur_time - np.floor(cur_time)
print('cur_time ', i, cur_time, ratio)
t = time.time()
int_rot, int_trans = linear_pose_interp(render_poses[flow_time, :3, 3],
render_poses[flow_time, :3, :3],
render_poses[flow_time + 1, :3, 3],
render_poses[flow_time + 1, :3, :3],
ratio)
int_poses = np.concatenate((int_rot, int_trans[:, np.newaxis]), 1)
int_poses = np.concatenate([int_poses[:3, :4], np.array([0.0, 0.0, 0.0, 1.0])[np.newaxis, :]], axis=0)
int_poses = np.dot(int_poses, bt_poses[i])
render_pose = torch.Tensor(int_poses).to(device)
R_w2t = render_pose[:3, :3].transpose(0, 1)
t_w2t = -torch.matmul(R_w2t, render_pose[:3, 3:4])
num_img = gt_imgs.shape[0]
img_idx_embed_1 = (np.floor(cur_time))/float(num_img) * 2. - 1.0
img_idx_embed_2 = (np.floor(cur_time) + 1)/float(num_img) * 2. - 1.0
print('img_idx_embed_1 ', cur_time, img_idx_embed_1)
ret1 = render_sm(img_idx_embed_1, 0, False,
num_img,
H, W, focal,
chunk=1024*16,
c2w=render_pose,
**render_kwargs)
ret2 = render_sm(img_idx_embed_2, 0, False,
num_img,
H, W, focal,
chunk=1024*16,
c2w=render_pose,
**render_kwargs)
T_i = torch.ones((1, H, W))
final_rgb = torch.zeros((3, H, W))
num_sample = ret1['raw_rgb'].shape[2]
# final_depth = torch.zeros((1, H, W))
z_vals = ret1['z_vals']
for j in range(0, num_sample):
splat_alpha_dy_1, splat_rgb_dy_1, \
splat_alpha_rig_1, splat_rgb_rig_1 = splat_rgb_img(ret1, ratio, R_w2t, t_w2t,
j, H, W, focal, True)
splat_alpha_dy_2, splat_rgb_dy_2, \
splat_alpha_rig_2, splat_rgb_rig_2 = splat_rgb_img(ret2, 1. - ratio, R_w2t, t_w2t,
j, H, W, focal, False)
final_rgb += T_i * (splat_alpha_dy_1 * splat_rgb_dy_1 + \
splat_alpha_rig_1 * splat_rgb_rig_1 ) * (1.0 - ratio)
final_rgb += T_i * (splat_alpha_dy_2 * splat_rgb_dy_2 + \
splat_alpha_rig_2 * splat_rgb_rig_2 ) * ratio
# splat_alpha = splat_alpha1 * (1. - ratio) + splat_alpha2 * ratio
# final_rgb += T_i * (splat_alpha1 * (1. - ratio) * splat_rgb1 + splat_alpha2 * ratio * splat_rgb2)
alpha_1_final = (1.0 - (1. - splat_alpha_dy_1) * (1. - splat_alpha_rig_1) ) * (1. - ratio)
alpha_2_fianl = (1.0 - (1. - splat_alpha_dy_2) * (1. - splat_alpha_rig_2) ) * ratio
alpha_final = alpha_1_final + alpha_2_fianl
# final_depth += T_i * (alpha_final) * z_vals[..., j]
T_i = T_i * (1.0 - alpha_final + 1e-10)
filename = os.path.join(savedir, 'slow-mo_%03d.jpg'%(i))
rgb8 = to8b(final_rgb.permute(1, 2, 0).cpu().numpy())
# final_depth = torch.clamp(final_depth/percentile(final_depth, 98), 0., 1.)
# depth8 = to8b(final_depth.permute(1, 2, 0).repeat(1, 1, 3).cpu().numpy())
start_y = (rgb8.shape[1] - 512) // 2
rgb8 = rgb8[:, start_y:start_y+ 512, :]
# depth8 = depth8[:, start_y:start_y+ 512, :]
filename = os.path.join(save_img_dir, '{:03d}.jpg'.format(i))
imageio.imwrite(filename, rgb8)
# filename = os.path.join(save_depth_dir, '{:03d}.jpg'.format(i))
# imageio.imwrite(filename, depth8)
def render_lockcam_slowmo(ref_c2w, num_img,
hwf, chunk, render_kwargs,
gt_imgs=None, savedir=None,
render_factor=0,
target_idx=5):
H, W, focal = hwf
if render_factor!=0:
# Render downsampled for speed
H = H//render_factor
W = W//render_factor
focal = focal/render_factor
H, W, focal = hwf
if render_factor!=0:
# Render downsampled for speed
H = H//render_factor
W = W//render_factor
focal = focal/render_factor
t = time.time()
count = 0
for i, cur_time in enumerate(np.linspace(target_idx - 8., target_idx + 8., 160 + 1).tolist()):
ratio = cur_time - np.floor(cur_time)
render_pose = ref_c2w[:3,:4] #render_poses[i % num_frame_per_cycle][:3,:4]
R_w2t = render_pose[:3, :3].transpose(0, 1)
t_w2t = -torch.matmul(R_w2t, render_pose[:3, 3:4])
num_img = gt_imgs.shape[0]
img_idx_embed_1 = (np.floor(cur_time))/float(num_img) * 2. - 1.0
img_idx_embed_2 = (np.floor(cur_time) + 1)/float(num_img) * 2. - 1.0
print('render lock camera time ', i, cur_time, ratio, time.time() - t)
t = time.time()
ret1 = render_sm(img_idx_embed_1, 0, False,
num_img,
H, W, focal,
chunk=1024*16,
c2w=render_pose,
**render_kwargs)
ret2 = render_sm(img_idx_embed_2, 0, False,
num_img,
H, W, focal,
chunk=1024*16,
c2w=render_pose,
**render_kwargs)
T_i = torch.ones((1, H, W))
final_rgb = torch.zeros((3, H, W))
num_sample = ret1['raw_rgb'].shape[2]
for j in range(0, num_sample):
splat_alpha_dy_1, splat_rgb_dy_1, \
splat_alpha_rig_1, splat_rgb_rig_1 = splat_rgb_img(ret1, ratio, R_w2t,
t_w2t, j, H, W,
focal, True)
splat_alpha_dy_2, splat_rgb_dy_2, \
splat_alpha_rig_2, splat_rgb_rig_2 = splat_rgb_img(ret2, 1. - ratio, R_w2t,
t_w2t, j, H, W,
focal, False)
final_rgb += T_i * (splat_alpha_dy_1 * splat_rgb_dy_1 + \
splat_alpha_rig_1 * splat_rgb_rig_1 ) * (1.0 - ratio)
final_rgb += T_i * (splat_alpha_dy_2 * splat_rgb_dy_2 + \
splat_alpha_rig_2 * splat_rgb_rig_2 ) * ratio
alpha_1_final = (1.0 - (1. - splat_alpha_dy_1) * (1. - splat_alpha_rig_1) ) * (1. - ratio)
alpha_2_fianl = (1.0 - (1. - splat_alpha_dy_2) * (1. - splat_alpha_rig_2) ) * ratio
alpha_final = alpha_1_final + alpha_2_fianl
T_i = T_i * (1.0 - alpha_final + 1e-10)
filename = os.path.join(savedir, '%03d.jpg'%(i))
rgb8 = to8b(final_rgb.permute(1, 2, 0).cpu().numpy())
start_y = (rgb8.shape[1] - 512) // 2
rgb8 = rgb8[:, start_y:start_y+ 512, :]
imageio.imwrite(filename, rgb8)
def render_sm(img_idx, chain_bwd, chain_5frames,
num_img, H, W, focal,
chunk=1024*16, rays=None, c2w=None, ndc=True,
near=0., far=1.,
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])
rays = torch.cat([rays_o, rays_d, near, far], -1)
if use_viewdirs:
rays = torch.cat([rays, viewdirs], -1)
# Render and reshape
all_ret = batchify_rays_sm(img_idx, chain_bwd, chain_5frames,
num_img, 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)
return all_ret
def batchify_rays_sm(img_idx, chain_bwd, chain_5frames,
num_img, rays_flat, chunk=1024*16, **kwargs):
"""Render rays in smaller minibatches to avoid OOM.
"""
all_ret = {}
for i in range(0, rays_flat.shape[0], chunk):
ret = render_rays_sm(img_idx, chain_bwd, chain_5frames,
num_img, 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 raw2rgba_blend_slowmo(raw, raw_blend_w, z_vals, rays_d, raw_noise_std=0):
"""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]).expand(dists[...,:1].shape)], -1) # [N_rays, N_samples]
dists = dists * torch.norm(rays_d[...,None,:], dim=-1)
rgb = torch.sigmoid(raw[...,:3]) # [N_rays, N_samples, 3]
noise = 0.
if raw_noise_std > 0.:
noise = torch.randn(raw[...,3].shape) * raw_noise_std
alpha = raw2alpha(raw[...,3] + noise, dists) * raw_blend_w # [N_rays, N_samples]
return rgb, alpha
def render_rays_sm(img_idx,
chain_bwd,
chain_5frames,
num_img,
ray_batch,
network_fn,
network_query_fn,
rigid_network_query_fn,
N_samples,
retraw=False,
lindisp=False,
perturb=0.,
N_importance=0,
network_rigid=None,
white_bkgd=False,
raw_noise_std=0.,
verbose=False,
pytest=False,
inference=True):
"""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] > 8 else None
bounds = torch.reshape(ray_batch[...,6:8], [-1,1,2])
near, far = bounds[...,0], bounds[...,1] # [-1,1]
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)
z_vals = lower + (upper - lower) * t_rand
pts = rays_o[...,None,:] + rays_d[...,None,:] * z_vals[...,:,None] # [N_rays, N_samples, 3]
img_idx_rep = torch.ones_like(pts[:, :, 0:1]) * img_idx
pts_ref = torch.cat([pts, img_idx_rep], -1)
# query point at time t
rgb_map_rig, depth_map_rig, raw_rgba_rigid, raw_blend_w = get_rigid_outputs(pts_ref, viewdirs,
rigid_network_query_fn,
network_rigid,
z_vals, rays_d,
raw_noise_std)
# query point at time t
raw_ref = network_query_fn(pts_ref, viewdirs, network_fn)
raw_rgba_ref = raw_ref[:, :, :4]
raw_sf_ref2prev = raw_ref[:, :, 4:7]
raw_sf_ref2post = raw_ref[:, :, 7:10]
# raw_blend_w_ref = raw_ref[:, :, 12]
raw_rgb, raw_alpha = raw2rgba_blend_slowmo(raw_rgba_ref, raw_blend_w,
z_vals, rays_d, raw_noise_std)
raw_rgb_rigid, raw_alpha_rigid = raw2rgba_blend_slowmo(raw_rgba_rigid, (1. - raw_blend_w),
z_vals, rays_d, raw_noise_std)
ret = {'raw_rgb': raw_rgb, 'raw_alpha': raw_alpha,
'raw_rgb_rigid':raw_rgb_rigid, 'raw_alpha_rigid':raw_alpha_rigid,
'raw_sf_ref2prev': raw_sf_ref2prev,
'raw_sf_ref2post': raw_sf_ref2post,
'pts_ref':pts_ref, 'z_vals':z_vals}
return ret
def batchify(fn, chunk):
"""Constructs a version of 'fn' that applies to smaller batches.
"""
if chunk is None:
return fn
def ret(inputs):
return torch.cat([fn(inputs[i:i+chunk]) for i in range(0, inputs.shape[0], chunk)], 0)
return ret
def run_network(inputs, viewdirs, fn, embed_fn, embeddirs_fn, netchunk=1024*16):
"""Prepares inputs and applies network 'fn'.
"""
inputs_flat = torch.reshape(inputs, [-1, inputs.shape[-1]])
embedded = embed_fn(inputs_flat)
if viewdirs is not None:
input_dirs = viewdirs[:,None].expand(inputs[:, :, :3].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 = batchify(fn, netchunk)(embedded)
outputs = torch.reshape(outputs_flat,
list(inputs.shape[:-1]) + [outputs_flat.shape[-1]])
return outputs
def batchify_rays(img_idx, chain_bwd, chain_5frames,
num_img, rays_flat, chunk=1024*16,K=5,disp_focus=0.5,blur_kernels=[], **kwargs):
"""Render rays in smaller minibatches to avoid OOM.
"""
all_ret = {}
for i in range(0, rays_flat.shape[0], chunk):
ret = render_rays(img_idx, chain_bwd, chain_5frames,
num_img, rays_flat[i:i+chunk], K=K,disp_focus=disp_focus,blur_kernels=blur_kernels,**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 batchify_rays_mpi_dsk(img_idx, chain_bwd, chain_5frames,
num_img, rays_flat, chunk=1024*16,weight=None,num_pt=None, **kwargs):
"""Render rays in smaller minibatches to avoid OOM.
"""
all_ret = {}
for i in range(0, rays_flat.shape[0], chunk):
ret = render_rays_mpi_dsk(img_idx, chain_bwd, chain_5frames,
num_img, rays_flat[i:i+chunk], weight=weight,num_pt=num_pt,**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(img_idx, chain_bwd, chain_5frames,
num_img, H, W, focal,
chunk=1024*16, rays=None, c2w=None, ndc=True,
near=0., far=1.,
use_viewdirs=False, c2w_staticcam=None,K=5,disp_focus=0.5,blur_kernels=[],
**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])
rays = torch.cat([rays_o, rays_d, near, far], -1)
if use_viewdirs:
rays = torch.cat([rays, viewdirs], -1)
# Render and reshape
all_ret = batchify_rays(img_idx, chain_bwd, chain_5frames,
num_img, rays, chunk,K,disp_focus,blur_kernels=blur_kernels, **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', 'depth_map', 'scene_flow', 'raw_sf_t']
# 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]
return all_ret
def render_mpi_dsk(img_idx, chain_bwd, chain_5frames,
num_img, H, W, focal,
chunk=1024*16, rays=None, c2w=None, ndc=True,
near=0., far=1.,
use_viewdirs=False, c2w_staticcam=None,weight=None,num_pt=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])
rays = torch.cat([rays_o, rays_d, near, far], -1)
if use_viewdirs:
rays = torch.cat([rays, viewdirs], -1)
# Render and reshape
all_ret = batchify_rays_mpi_dsk(img_idx, chain_bwd, chain_5frames,
num_img, rays, chunk,weight=weight,num_pt=num_pt, **kwargs)
for k in all_ret:
k_sh = list(sh[:-1]) + list(all_ret[k].shape[1:])
k_sh[0]=k_sh[0]//num_pt
all_ret[k] = torch.reshape(all_ret[k], k_sh)
return all_ret
def render_bullet_time_new(render_poses, img_idx_embed, num_img,
hwf, chunk, render_kwargs, gt_imgs=None, savedir=None, render_factor=0):
H, W, focal = hwf
if render_factor!=0:
# Render downsampled for speed
H = H//render_factor
W = W//render_factor
focal = focal/render_factor
rgbs = []
disps = []
t = time.time()
for i in range(0, (render_poses.shape[0])):
c2w = render_poses[i]
print(i, time.time() - t)
t = time.time()
ret = render(img_idx_embed, 0, False,
num_img,
H, W, focal,
chunk=1024*32, c2w=c2w[:3,:4],
**render_kwargs)
rgb = ret['rgb_map_ref'].cpu().numpy()
rgbs.append(rgb)
return rgbs
def render_bullet_time(render_poses, img_idx_embed, num_img,
hwf, chunk, render_kwargs, gt_imgs=None, savedir=None, render_factor=0):
H, W, focal = hwf
if render_factor!=0:
# Render downsampled for speed
H = H//render_factor
W = W//render_factor
focal = focal/render_factor
rgbs = []
disps = []
t = time.time()
save_img_dir = os.path.join(savedir, 'images')
# save_depth_dir = os.path.join(savedir, 'depths')
os.makedirs(save_img_dir, exist_ok=True)
# os.makedirs(save_depth_dir, exist_ok=True)
for i in range(0, (render_poses.shape[0])):
c2w = render_poses[i]
print(i, time.time() - t)
t = time.time()
ret = render(img_idx_embed, 0, False,
num_img,
H, W, focal,
chunk=1024*32, c2w=c2w[:3,:4],
**render_kwargs)
depth = torch.clamp(ret['depth_map_ref']/percentile(ret['depth_map_ref'], 97), 0., 1.) #1./disp
rgb = ret['rgb_map_ref'].cpu().numpy()#.append(ret['rgb_map_ref'].cpu().numpy())
if savedir is not None:
rgb8 = to8b(rgb)
depth8 = to8b(depth.unsqueeze(-1).repeat(1, 1, 3).cpu().numpy())
# start_y = (rgb8.shape[1] - 512) // 2
# rgb8 = rgb8[:, start_y:start_y+ 512, :]
# depth8 = depth8[:, start_y:start_y+ 512, :]
filename = os.path.join(save_img_dir, '{:03d}.jpg'.format(i))
imageio.imwrite(filename, rgb8)
rgbs.append(rgb8)
# filename = os.path.join(save_depth_dir, '{:03d}.jpg'.format(i))
# imageio.imwrite(filename, depth8)
imageio.mimwrite(os.path.join(save_img_dir,'bullet.mp4'),
rgbs, fps=25, quality=8, macro_block_size=1)
def create_nerf(args,kernelnet):
"""Instantiate NeRF's MLP model.
"""
# XYZ + T
embed_fn, input_ch = get_embedder(args.multires, args.i_embed, 4)
input_ch_views = 0
embeddirs_fn = None
if args.use_viewdirs:
embeddirs_fn, input_ch_views = get_embedder(args.multires_views, args.i_embed, 3)
output_ch = 5 if args.N_importance > 0 else 4
skips = [4]
model = NeRF(D=args.netdepth, W=args.netwidth,
input_ch=input_ch, output_ch=output_ch, skips=skips,
input_ch_views=input_ch_views, use_viewdirs=args.use_viewdirs).to(device)
device_ids = list(range(torch.cuda.device_count()))
model = torch.nn.DataParallel(model, device_ids=device_ids)
kernelnet = torch.nn.DataParallel(kernelnet, device_ids=device_ids)
grad_vars = list(model.parameters())
grad_vars += list(kernelnet.parameters())
embed_fn_rigid, input_rigid_ch = get_embedder(args.multires, args.i_embed, 3)
model_rigid = Rigid_NeRF(D=args.netdepth, W=args.netwidth,
input_ch=input_rigid_ch, output_ch=output_ch, skips=skips,
input_ch_views=input_ch_views,
use_viewdirs=args.use_viewdirs).to(device)
model_rigid = torch.nn.DataParallel(model_rigid, device_ids=device_ids)
model_fine = None
grad_vars += list(model_rigid.parameters())
network_query_fn = lambda inputs, viewdirs, network_fn : run_network(inputs, viewdirs, network_fn,
embed_fn=embed_fn,
embeddirs_fn=embeddirs_fn,
netchunk=args.netchunk)
rigid_network_query_fn = lambda inputs, viewdirs, network_fn : run_network(inputs, viewdirs, network_fn,
embed_fn=embed_fn_rigid,
embeddirs_fn=embeddirs_fn,
netchunk=args.netchunk)
# Create optimizer
optimizer = torch.optim.Adam(params=grad_vars, lr=args.lrate, betas=(0.9, 0.999))
start = 0
basedir = args.basedir
expname = args.expname
##########################
# Load checkpoints
if args.ft_path is not None and args.ft_path!='None':
ckpts = [args.ft_path]
else:
ckpts = [os.path.join(basedir, expname, f) for f in sorted(os.listdir(os.path.join(basedir, expname))) if 'tar' in f]
if len(ckpts) > 0 and not args.no_reload:
ckpt_path = ckpts[-1]
print('Reloading from', ckpt_path)
ckpt = torch.load(ckpt_path)
start = ckpt['global_step'] + 1
optimizer.load_state_dict(ckpt['optimizer_state_dict'])
model.load_state_dict(ckpt['network_fn_state_dict'])
kernelnet.load_state_dict(ckpt['kernel_state_dict'])
# Load model
print('LOADING SF MODEL!!!!!!!!!!!!!!!!!!!')
model_rigid.load_state_dict(ckpt['network_rigid'])
if model_fine is not None:
model_fine.load_state_dict(ckpt['network_fine_state_dict'])
##########################
render_kwargs_train = {
'network_query_fn' : network_query_fn,
'perturb' : args.perturb,
'N_importance' : args.N_importance,
'rigid_network_query_fn':rigid_network_query_fn,
'network_rigid' : model_rigid,
'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,
'inference': False
}
# NDC only good for LLFF-style forward facing data
if args.dataset_type != 'llff' or args.no_ndc:
print('Not 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.
render_kwargs_test['inference'] = True
return render_kwargs_train, render_kwargs_test, start, grad_vars, optimizer,kernelnet
def raw2outputs_blending(raw_dy,
raw_rigid,
raw_blend_w,
z_vals, rays_d,
raw_noise_std,K=5,disp_focus=0.5):
act_fn = F.relu
dists = z_vals[...,1:] - z_vals[...,:-1]
dists = torch.cat([dists, torch.Tensor([1e10]).expand(dists[...,:1].shape)], -1) # [N_rays, N_samples]
dists = dists * torch.norm(rays_d[...,None,:], dim=-1)
rgb_dy = torch.sigmoid(raw_dy[..., :3]) # [N_rays, N_samples, 3]
rgb_rigid = torch.sigmoid(raw_rigid[..., :3]) # [N_rays, N_samples, 3]
noise = 0.
if raw_noise_std > 0.:
noise = torch.randn(raw_dy[...,3].shape) * raw_noise_std
opacity_dy = act_fn(raw_dy[..., 3] + noise)#.detach() #* raw_blend_w
opacity_rigid = act_fn(raw_rigid[..., 3] + noise)#.detach() #* (1. - raw_blend_w)
# alpha with blending weights
alpha_dy = (1. - torch.exp(-opacity_dy * dists) ) * raw_blend_w
alpha_rig = (1. - torch.exp(-opacity_rigid * dists)) * (1. - raw_blend_w)
Ts = torch.cumprod(torch.cat([torch.ones((alpha_dy.shape[0], 1)),
(1. - alpha_dy) * (1. - alpha_rig) + 1e-10], -1), -1)[:, :-1]
weights_dy = Ts * alpha_dy
weights_rig = Ts * alpha_rig
# union map
rgb_map = torch.sum(weights_dy[..., None] * rgb_dy + \
weights_rig[..., None] * rgb_rigid, -2)
weights_mix = weights_dy + weights_rig
depth_map = torch.sum(weights_mix * z_vals, -1)
disp_map = 1./torch.max(1e-10 * torch.ones_like(depth_map), depth_map / torch.sum(weights_mix, -1))
# compute dynamic depth only
alpha_fg = 1. - torch.exp(-opacity_dy * dists)
weights_fg = alpha_fg * torch.cumprod(torch.cat([torch.ones((alpha_fg.shape[0], 1)),
1.-alpha_fg + 1e-10], -1), -1)[:, :-1]
depth_map_fg = torch.sum(weights_fg * z_vals, -1)
rgb_map_fg = torch.sum(weights_fg[..., None] * rgb_dy, -2)
return rgb_map, depth_map,\
rgb_map_fg, depth_map_fg, weights_fg, \
weights_dy
def raw2outputs_blending_mpi(raw_dy,
raw_rigid,
raw_blend_w,
z_vals, rays_d,
raw_noise_std,K,disp_focus,weight,bokeh=False,num_pt=None):
act_fn = F.relu
dists = z_vals[...,1:] - z_vals[...,:-1]
dists = torch.cat([dists, torch.Tensor([1e10]).expand(dists[...,:1].shape)], -1) # [N_rays, N_samples]
dists = dists * torch.norm(rays_d[...,None,:], dim=-1)
rgb_dy = torch.sigmoid(raw_dy[..., :3]) # [N_rays, N_samples, 3]
rgb_rigid = torch.sigmoid(raw_rigid[..., :3]) # [N_rays, N_samples, 3]
noise = 0.
if raw_noise_std > 0.:
noise = torch.randn(raw_dy[...,3].shape) * raw_noise_std
opacity_dy = act_fn(raw_dy[..., 3] + noise)#.detach() #* raw_blend_w
opacity_rigid = act_fn(raw_rigid[..., 3] + noise)#.detach() #* (1. - raw_blend_w)
# alpha with blending weights
alpha_dy = (1. - torch.exp(-opacity_dy * dists) ) * raw_blend_w
alpha_rig = (1. - torch.exp(-opacity_rigid * dists)) * (1. - raw_blend_w)
Ts = torch.cumprod(torch.cat([torch.ones((alpha_dy.shape[0], 1)),
(1. - alpha_dy) * (1. - alpha_rig) + 1e-10], -1), -1)[:, :-1]
weights_dy = Ts * alpha_dy
weights_rig = Ts * alpha_rig
# union map
rgb_map = torch.sum(weights_dy[..., None] * rgb_dy + \
weights_rig[..., None] * rgb_rigid, -2)
##moidified by lxr
if bokeh:
bokeh_map_ref=MPIBokehRenderer_blending_final(raw_dy,raw_rigid,alpha_dy,alpha_rig,weight=weight,num_pt=num_pt)
weights_mix = weights_dy + weights_rig
depth_map = torch.sum(weights_mix * z_vals, -1)
# compute dynamic depth only
alpha_fg = 1. - torch.exp(-opacity_dy * dists)