-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_scade_scannet.py
1290 lines (1069 loc) · 58.1 KB
/
run_scade_scannet.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
'''
Mikaela Uy
For Scannet data
Modified from DDP codebase
'''
import os
import shutil
import subprocess
import math
import time
import datetime
from argparse import Namespace
import configargparse
from skimage.metrics import structural_similarity
from lpips import LPIPS
import json
import cv2
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torch.utils.tensorboard import SummaryWriter
from tqdm import tqdm, trange
from model import NeRF, get_embedder, get_rays, sample_pdf, sample_pdf_joint, img2mse, mse2psnr, to8b, \
compute_depth_loss, select_coordinates, to16b, compute_space_carving_loss, \
sample_pdf_return_u, sample_pdf_joint_return_u
from data import create_random_subsets, load_scene_scannet, convert_depth_completion_scaling_to_m, \
convert_m_to_depth_completion_scaling, get_pretrained_normalize, resize_sparse_depth
from train_utils import MeanTracker, update_learning_rate, get_learning_rate
from metric import compute_rmse
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
DEBUG = False
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, embedded_cam, fn, embed_fn, embeddirs_fn, bb_center, bb_scale, netchunk=1024*64):
"""Prepares inputs and applies network 'fn'.
"""
inputs_flat = torch.reshape(inputs, [-1, inputs.shape[-1]])
inputs_flat = (inputs_flat - bb_center) * bb_scale
embedded = embed_fn(inputs_flat) # samples * rays, multires * 2 * 3 + 3
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, embedded_cam.unsqueeze(0).expand(embedded_dirs.shape[0], embedded_cam.shape[0])], -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(rays_flat, chunk=1024*32, use_viewdirs=False, **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], use_viewdirs, **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, intrinsic, chunk=1024*32, rays=None, c2w=None, ndc=True,
near=0., far=1., with_5_9=False, use_viewdirs=False, c2w_staticcam=None,
rays_depth=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.
with_5_9: render with aspect ratio 5.33:9 (one third of 16:9)
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, intrinsic, c2w)
if with_5_9:
W_before = W
W = int(H / 9. * 16. / 3.)
if W % 2 != 0:
W = W - 1
start = (W_before - W) // 2
rays_o = rays_o[:, start:start + W, :]
rays_d = rays_d[:, start:start + W, :]
elif rays.shape[0] == 2:
# use provided ray batch
rays_o, rays_d = rays
else:
rays_o, rays_d, rays_depth = 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, intrinsic, c2w_staticcam)
viewdirs = viewdirs / torch.norm(viewdirs, dim=-1, keepdim=True)
viewdirs = torch.reshape(viewdirs, [-1,3]).float()
sh = rays_d.shape # [..., 3]
# 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)
if rays_depth is not None:
rays_depth = torch.reshape(rays_depth, [-1,3]).float()
rays = torch.cat([rays, rays_depth], -1)
# Render and reshape
all_ret = batchify_rays(rays, chunk, use_viewdirs, **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_hyp(H, W, intrinsic, chunk=1024*32, rays=None, c2w=None, ndc=True,
near=0., far=1., with_5_9=False, use_viewdirs=False, c2w_staticcam=None,
rays_depth=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.
with_5_9: render with aspect ratio 5.33:9 (one third of 16:9)
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, intrinsic, c2w)
if with_5_9:
W_before = W
W = int(H / 9. * 16. / 3.)
if W % 2 != 0:
W = W - 1
start = (W_before - W) // 2
rays_o = rays_o[:, start:start + W, :]
rays_d = rays_d[:, start:start + W, :]
elif rays.shape[0] == 2:
# use provided ray batch
rays_o, rays_d = rays
else:
rays_o, rays_d, rays_depth = 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, intrinsic, c2w_staticcam)
viewdirs = viewdirs / torch.norm(viewdirs, dim=-1, keepdim=True)
viewdirs = torch.reshape(viewdirs, [-1,3]).float()
sh = rays_d.shape # [..., 3]
# 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)
if rays_depth is not None:
rays_depth = torch.reshape(rays_depth, [-1,3]).float()
rays = torch.cat([rays, rays_depth], -1)
# Render and reshape
all_ret = batchify_rays(rays, chunk, use_viewdirs, **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_video(poses, H, W, intrinsics, filename, args, render_kwargs_test, fps=25):
video_dir = os.path.join(args.ckpt_dir, args.expname, 'video_' + filename)
if os.path.exists(video_dir):
shutil.rmtree(video_dir)
os.makedirs(video_dir, exist_ok=True)
depth_scale = render_kwargs_test["far"]
max_depth_in_video = 0
for img_idx in range(0, len(poses), 3):
# for img_idx in range(200):
pose = poses[img_idx, :3,:4]
intrinsic = intrinsics[img_idx, :]
with torch.no_grad():
if args.input_ch_cam > 0:
render_kwargs_test["embedded_cam"] = torch.zeros((args.input_ch_cam), device=device)
# render video in 16:9 with one third rgb, one third depth and one third depth standard deviation
rgb, _, _, extras = render(H, W, intrinsic, chunk=(args.chunk // 2), c2w=pose, with_5_9=True, **render_kwargs_test)
rgb_cpu_numpy_8b = to8b(rgb.cpu().numpy())
video_frame = cv2.cvtColor(rgb_cpu_numpy_8b, cv2.COLOR_RGB2BGR)
max_depth_in_video = max(max_depth_in_video, extras['depth_map'].max())
depth_frame = cv2.applyColorMap(to8b((extras['depth_map'] / depth_scale).cpu().numpy()), cv2.COLORMAP_TURBO)
video_frame = np.concatenate((video_frame, depth_frame), 1)
depth_var = ((extras['z_vals'] - extras['depth_map'].unsqueeze(-1)).pow(2) * extras['weights']).sum(-1)
depth_std = depth_var.clamp(0., 1.).sqrt()
video_frame = np.concatenate((video_frame, cv2.applyColorMap(to8b(depth_std.cpu().numpy()), cv2.COLORMAP_VIRIDIS)), 1)
cv2.imwrite(os.path.join(video_dir, str(img_idx) + '.jpg'), video_frame)
video_file = os.path.join(args.ckpt_dir, args.expname, filename + '.mp4')
subprocess.call(["ffmpeg", "-y", "-framerate", str(fps), "-i", os.path.join(video_dir, "%d.jpg"), "-c:v", "libx264", "-profile:v", "high", "-crf", str(fps), video_file])
print("Maximal depth in video: {}".format(max_depth_in_video))
def optimize_camera_embedding(image, pose, H, W, intrinsic, args, render_kwargs_test):
render_kwargs_test["embedded_cam"] = torch.zeros(args.input_ch_cam, requires_grad=True).to(device)
optimizer = torch.optim.Adam(params=(render_kwargs_test["embedded_cam"],), lr=5e-1)
lr_scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, 'max', factor=0.5, patience=3, verbose=True)
half_W = W
print(" - Optimize camera embedding")
max_psnr = 0
best_embedded_cam = torch.zeros(args.input_ch_cam).to(device)
# make batches
coords = torch.stack(torch.meshgrid(torch.linspace(0, H - 1, H), torch.linspace(0, half_W - 1, half_W), indexing='ij'), -1) # (H, W, 2)
coords = torch.reshape(coords, [-1, 2]).long()
assert(coords[:, 1].max() < half_W)
batches = create_random_subsets(range(len(coords)), 2 * args.N_rand, device=device)
# make rays
rays_o, rays_d = get_rays(H, half_W, intrinsic, pose) # (H, W, 3), (H, W, 3)
start_time = time.time()
for i in range(100):
sum_img_loss = torch.zeros(1)
optimizer.zero_grad()
for b in batches:
curr_coords = coords[b]
curr_rays_o = rays_o[curr_coords[:, 0], curr_coords[:, 1]] # (N_rand, 3)
curr_rays_d = rays_d[curr_coords[:, 0], curr_coords[:, 1]] # (N_rand, 3)
target_s = image[curr_coords[:, 0], curr_coords[:, 1]]
batch_rays = torch.stack([curr_rays_o, curr_rays_d], 0)
rgb, _, _, _ = render(H, half_W, None, chunk=args.chunk, rays=batch_rays, verbose=i < 10, **render_kwargs_test)
img_loss = img2mse(rgb, target_s)
img_loss.backward()
sum_img_loss += img_loss
optimizer.step()
psnr = mse2psnr(sum_img_loss / len(batches))
lr_scheduler.step(psnr)
if psnr > max_psnr:
max_psnr = psnr
best_embedded_cam = render_kwargs_test["embedded_cam"].detach().clone()
print("Step {}: PSNR: {} ({:.2f}min)".format(i, psnr, (time.time() - start_time) / 60))
render_kwargs_test["embedded_cam"] = best_embedded_cam
def render_images_with_metrics(count, indices, images, depths, valid_depths, poses, H, W, intrinsics, lpips_alex, args, render_kwargs_test, \
embedcam_fn=None, with_test_time_optimization=False):
far = render_kwargs_test['far']
if count is None:
# take all images in order
count = len(indices)
img_i = indices
else:
# take random images
img_i = np.random.choice(indices, size=count, replace=False)
rgbs_res = torch.empty(count, 3, H, W)
rgbs0_res = torch.empty(count, 3, H, W)
target_rgbs_res = torch.empty(count, 3, H, W)
depths_res = torch.empty(count, 1, H, W)
depths0_res = torch.empty(count, 1, H, W)
target_depths_res = torch.empty(count, 1, H, W)
target_valid_depths_res = torch.empty(count, 1, H, W, dtype=bool)
mean_metrics = MeanTracker()
mean_depth_metrics = MeanTracker() # track separately since they are not always available
for n, img_idx in enumerate(img_i):
print("Render image {}/{}".format(n + 1, count), end="")
target = images[img_idx]
target_depth = depths[img_idx]
target_valid_depth = valid_depths[img_idx]
pose = poses[img_idx, :3,:4]
intrinsic = intrinsics[img_idx, :]
if args.input_ch_cam > 0:
if embedcam_fn is None:
# use zero embedding at test time or optimize for the latent code
render_kwargs_test["embedded_cam"] = torch.zeros((args.input_ch_cam), device=device)
if with_test_time_optimization:
optimize_camera_embedding(target, pose, H, W, intrinsic, args, render_kwargs_test)
result_dir = os.path.join(args.ckpt_dir, args.expname, "test_latent_codes_" + args.scene_id)
os.makedirs(result_dir, exist_ok=True)
np.savetxt(os.path.join(result_dir, str(img_idx) + ".txt"), render_kwargs_test["embedded_cam"].cpu().numpy())
else:
render_kwargs_test["embedded_cam"] = embedcam_fn[img_idx]
with torch.no_grad():
rgb, _, _, extras = render(H, W, intrinsic, chunk=(args.chunk // 2), c2w=pose, **render_kwargs_test)
# compute depth rmse
depth_rmse = compute_rmse(extras['depth_map'][target_valid_depth], target_depth[:, :, 0][target_valid_depth])
if not torch.isnan(depth_rmse):
depth_metrics = {"depth_rmse" : depth_rmse.item()}
mean_depth_metrics.add(depth_metrics)
### Fit LSTSQ for white balancing
rgb_reshape = rgb.view(1, -1, 3)
target_reshape = target.view(1, -1, 3)
## No intercept
# X = torch.linalg.lstsq(rgb_reshape, target_reshape).solution
# rgb_reshape = rgb_reshape @ X
# rgb_reshape = rgb_reshape.view(rgb.shape)
# rgb = rgb_reshape
# compute color metrics
img_loss = img2mse(rgb, target)
psnr = mse2psnr(img_loss)
print("PSNR: {}".format(psnr))
rgb = torch.clamp(rgb, 0, 1)
ssim = structural_similarity(rgb.cpu().numpy(), target.cpu().numpy(), data_range=1., channel_axis=-1)
lpips = lpips_alex(rgb.permute(2, 0, 1).unsqueeze(0), target.permute(2, 0, 1).unsqueeze(0), normalize=True)[0]
# store result
rgbs_res[n] = rgb.clamp(0., 1.).permute(2, 0, 1).cpu()
target_rgbs_res[n] = target.permute(2, 0, 1).cpu()
depths_res[n] = (extras['depth_map'] / far).unsqueeze(0).cpu()
target_depths_res[n] = (target_depth[:, :, 0] / far).unsqueeze(0).cpu()
target_valid_depths_res[n] = target_valid_depth.unsqueeze(0).cpu()
metrics = {"img_loss" : img_loss.item(), "psnr" : psnr.item(), "ssim" : ssim, "lpips" : lpips[0, 0, 0],}
if 'rgb0' in extras:
img_loss0 = img2mse(extras['rgb0'], target)
psnr0 = mse2psnr(img_loss0)
depths0_res[n] = (extras['depth0'] / far).unsqueeze(0).cpu()
rgbs0_res[n] = torch.clamp(extras['rgb0'], 0, 1).permute(2, 0, 1).cpu()
metrics.update({"img_loss0" : img_loss0.item(), "psnr0" : psnr0.item()})
mean_metrics.add(metrics)
res = { "rgbs" : rgbs_res, "target_rgbs" : target_rgbs_res, "depths" : depths_res, "target_depths" : target_depths_res, \
"target_valid_depths" : target_valid_depths_res}
if 'rgb0' in extras:
res.update({"rgbs0" : rgbs0_res, "depths0" : depths0_res,})
all_mean_metrics = MeanTracker()
all_mean_metrics.add({**mean_metrics.as_dict(), **mean_depth_metrics.as_dict()})
return all_mean_metrics, res
def write_images_with_metrics(images, mean_metrics, far, args, with_test_time_optimization=False):
result_dir = os.path.join(args.ckpt_dir, args.expname, "test_images_" + ("with_optimization_" if with_test_time_optimization else "") + args.scene_id)
os.makedirs(result_dir, exist_ok=True)
for n, (rgb, depth) in enumerate(zip(images["rgbs"].permute(0, 2, 3, 1).cpu().numpy(), \
images["depths"].permute(0, 2, 3, 1).cpu().numpy())):
# write rgb
cv2.imwrite(os.path.join(result_dir, str(n) + "_rgb" + ".jpg"), cv2.cvtColor(to8b(rgb), cv2.COLOR_RGB2BGR))
# write depth
cv2.imwrite(os.path.join(result_dir, str(n) + "_d" + ".png"), to16b(depth))
with open(os.path.join(result_dir, 'metrics.txt'), 'w') as f:
mean_metrics.print(f)
mean_metrics.print()
def load_checkpoint(args):
path = os.path.join(args.ckpt_dir, args.expname)
ckpts = [os.path.join(path, f) for f in sorted(os.listdir(path)) if '000.tar' in f]
print('Found ckpts', ckpts)
ckpt = None
if len(ckpts) > 0 and not args.no_reload:
ckpt_path = ckpts[-1]
print('Reloading from', ckpt_path)
ckpt = torch.load(ckpt_path)
return ckpt
def create_nerf(args, scene_render_params):
"""Instantiate NeRF's MLP model.
"""
embed_fn, input_ch = get_embedder(args.multires, args.i_embed)
input_ch_views = 0
embeddirs_fn = None
if args.use_viewdirs:
embeddirs_fn, input_ch_views = get_embedder(args.multires_views, args.i_embed)
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, input_ch_cam=args.input_ch_cam, use_viewdirs=args.use_viewdirs)
model = nn.DataParallel(model).to(device)
grad_vars = list(model.parameters())
grad_vars = []
grad_names = []
for name, param in model.named_parameters():
grad_vars.append(param)
grad_names.append(name)
model_fine = None
if args.N_importance > 0:
model_fine = NeRF(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_cam=args.input_ch_cam, use_viewdirs=args.use_viewdirs)
model_fine = nn.DataParallel(model_fine).to(device)
for name, param in model_fine.named_parameters():
grad_vars.append(param)
grad_names.append(name)
network_query_fn = lambda inputs, viewdirs, embedded_cam, network_fn : run_network(inputs, viewdirs, embedded_cam, network_fn,
embed_fn=embed_fn,
embeddirs_fn=embeddirs_fn,
bb_center=args.bb_center,
bb_scale=args.bb_scale,
netchunk=args.netchunk_per_gpu*args.n_gpus)
# Create optimizer
optimizer = torch.optim.Adam(params=grad_vars, lr=args.lrate, betas=(0.9, 0.999))
start = 0
##########################
# Load checkpoints
ckpt = load_checkpoint(args)
if ckpt is not None:
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'])
##########################
embedded_cam = torch.tensor((), device=device)
render_kwargs_train = {
'network_query_fn' : network_query_fn,
'embedded_cam' : embedded_cam,
'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,
'raw_noise_std' : args.raw_noise_std,
}
render_kwargs_train.update(scene_render_params)
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, grad_names
def compute_weights(raw, z_vals, rays_d, noise=0.):
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.full_like(dists[...,:1], 1e10, device=device)], -1) # [N_rays, N_samples]
dists = dists * torch.norm(rays_d[...,None,:], dim=-1)
alpha = raw2alpha(raw[...,3] + noise, dists) # [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), device=device), 1.-alpha + 1e-10], -1), -1)[:, :-1]
return weights
def raw2depth(raw, z_vals, rays_d):
weights = compute_weights(raw, z_vals, rays_d)
depth = torch.sum(weights * z_vals, -1)
std = (((z_vals - depth.unsqueeze(-1)).pow(2) * weights).sum(-1)).sqrt()
return depth, std
def raw2outputs(raw, z_vals, rays_d, raw_noise_std=0, 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.
"""
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
# 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)
weights = compute_weights(raw, z_vals, rays_d, noise)
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)
return rgb_map, disp_map, acc_map, weights, depth_map
def perturb_z_vals(z_vals, pytest):
# 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_like(z_vals)
# 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)
z_vals = lower + (upper - lower) * t_rand
return z_vals
def render_rays(ray_batch,
use_viewdirs,
network_fn,
network_query_fn,
N_samples,
precomputed_z_samples=None,
embedded_cam=None,
retraw=False,
lindisp=False,
perturb=0.,
N_importance=0,
network_fine=None,
raw_noise_std=0.,
verbose=False,
pytest=False,
is_joint=False,
cached_u= None):
"""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.
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 = None
depth_range = None
if use_viewdirs:
viewdirs = ray_batch[:,8:11]
if ray_batch.shape[-1] > 11:
depth_range = ray_batch[:,11:14]
else:
if ray_batch.shape[-1] > 8:
depth_range = ray_batch[:,8:11]
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)
# sample and render rays for dense depth priors for nerf
N_samples_half = N_samples // 2
# sample and render rays for nerf
if not lindisp:
# print("Not lindisp")
z_vals = near * (1.-t_vals) + far * (t_vals)
else:
# print("Lindisp")
z_vals = 1./(1./near * (1.-t_vals) + 1./far * (t_vals))
if perturb > 0.:
# print("Perturb.")
z_vals = perturb_z_vals(z_vals, pytest)
pts = rays_o[...,None,:] + rays_d[...,None,:] * z_vals[...,:,None] # [N_rays, N_samples, 3]
raw = network_query_fn(pts, viewdirs, embedded_cam, network_fn)
rgb_map, disp_map, acc_map, weights, depth_map = raw2outputs(raw, z_vals, rays_d, raw_noise_std, pytest=pytest)
### Try without coarse and fine network, but just one network and use additional samples from the distribution of the nerf
if N_importance == 0:
### P_depth from base network
z_vals_mid = .5 * (z_vals[...,1:] + z_vals[...,:-1])
if not is_joint:
z_vals_2 = sample_pdf(z_vals_mid, weights[...,1:-1], N_samples, det=(perturb==0.), pytest=pytest)
else:
z_vals_2 = sample_pdf_joint(z_vals_mid, weights[...,1:-1], N_samples, det=(perturb==0.), pytest=pytest)
#########################
### Forward the rendering network with the additional samples
pts_2 = rays_o[...,None,:] + rays_d[...,None,:] * z_vals_2[...,:,None]
raw_2 = network_query_fn(pts_2, viewdirs, embedded_cam, network_fn)
z_vals = torch.cat((z_vals, z_vals_2), -1)
raw = torch.cat((raw, raw_2), 1)
z_vals, indices = z_vals.sort()
### Concatenated output
raw = torch.gather(raw, 1, indices.unsqueeze(-1).expand_as(raw))
rgb_map, disp_map, acc_map, weights, depth_map = raw2outputs(raw, z_vals, rays_d, raw_noise_std, pytest=pytest)
## Second tier P_depth
z_vals_mid = .5 * (z_vals[...,1:] + z_vals[...,:-1])
if not is_joint:
z_vals_output = sample_pdf(z_vals_mid, weights[...,1:-1], N_samples, det=(perturb==0.), pytest=pytest)
else:
z_vals_output = sample_pdf_joint(z_vals_mid, weights[...,1:-1], N_samples, det=(perturb==0.), pytest=pytest)
pred_depth_hyp = torch.cat((z_vals_2, z_vals_output), -1)
elif N_importance > 0:
rgb_map_0, disp_map_0, acc_map_0, depth_map_0, z_vals_0, weights_0 = rgb_map, disp_map, acc_map, depth_map, z_vals, weights
z_vals_mid = .5 * (z_vals[...,1:] + z_vals[...,:-1])
## Original NeRF uses this
z_samples = sample_pdf(z_vals_mid, weights[...,1:-1], N_importance, det=(perturb==0.), pytest=pytest)
## To model p_depth from coarse network
z_samples_depth = torch.clone(z_samples)
## For fine network sampling
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 = network_query_fn(pts, viewdirs, embedded_cam, run_fn)
rgb_map, disp_map, acc_map, weights, depth_map = raw2outputs(raw, z_vals, rays_d, raw_noise_std, pytest=pytest)
### P_depth from fine network
z_vals_mid = .5 * (z_vals[...,1:] + z_vals[...,:-1])
if not is_joint:
z_samples, u = sample_pdf_return_u(z_vals_mid, weights[...,1:-1], N_importance, det=(perturb==0.), pytest=pytest, load_u=cached_u)
else:
z_samples, u = sample_pdf_joint_return_u(z_vals_mid, weights[...,1:-1], N_importance, det=(perturb==0.), pytest=pytest, load_u=cached_u)
pred_depth_hyp = z_samples
ret = {'rgb_map' : rgb_map, 'disp_map' : disp_map, 'acc_map' : acc_map, 'depth_map' : depth_map, 'z_vals' : z_vals, 'weights' : weights, 'pred_hyp' : pred_depth_hyp,\
'u':u}
if retraw:
ret['raw'] = raw
if N_importance > 0:
ret['rgb0'] = rgb_map_0
ret['disp0'] = disp_map_0
ret['acc0'] = acc_map_0
ret['depth0'] = depth_map_0
ret['z_vals0'] = z_vals_0
ret['weights0'] = weights_0
ret['z_std'] = torch.std(z_samples, dim=-1, unbiased=False) # [N_rays]
# ret['pred_hyp'] = pred_depth_hyp
for k in ret:
if (torch.isnan(ret[k]).any() or torch.isinf(ret[k]).any()) and DEBUG:
print(f"! [Numerical Error] {k} contains nan or inf.")
return ret
def get_ray_batch_from_one_image(H, W, i_train, images, depths, valid_depths, poses, intrinsics, args):
coords = torch.stack(torch.meshgrid(torch.linspace(0, H-1, H), torch.linspace(0, W-1, W), indexing='ij'), -1) # (H, W, 2)
img_i = np.random.choice(i_train)
target = images[img_i]
target_depth = depths[img_i]
target_valid_depth = valid_depths[img_i]
pose = poses[img_i]
intrinsic = intrinsics[img_i, :]
rays_o, rays_d = get_rays(H, W, intrinsic, pose) # (H, W, 3), (H, W, 3)
select_coords = select_coordinates(coords, args.N_rand)
rays_o = rays_o[select_coords[:, 0], select_coords[:, 1]] # (N_rand, 3)
rays_d = rays_d[select_coords[:, 0], select_coords[:, 1]] # (N_rand, 3)
target_s = target[select_coords[:, 0], select_coords[:, 1]] # (N_rand, 3)
target_d = target_depth[select_coords[:, 0], select_coords[:, 1]] # (N_rand, 1) or (N_rand, 2)
target_vd = target_valid_depth[select_coords[:, 0], select_coords[:, 1]] # (N_rand, 1)
batch_rays = torch.stack([rays_o, rays_d], 0) # (2, N_rand, 3)
return batch_rays, target_s, target_d, target_vd, img_i
def get_ray_batch_from_one_image_hypothesis_idx(H, W, img_i, images, depths, valid_depths, poses, intrinsics, all_hypothesis, args, space_carving_idx=None, cached_u=None):
coords = torch.stack(torch.meshgrid(torch.linspace(0, H-1, H), torch.linspace(0, W-1, W), indexing='ij'), -1) # (H, W, 2)
# img_i = np.random.choice(i_train)
target = images[img_i]
target_depth = depths[img_i]
target_valid_depth = valid_depths[img_i]
pose = poses[img_i]
intrinsic = intrinsics[img_i, :]
target_hypothesis = all_hypothesis[img_i]
rays_o, rays_d = get_rays(H, W, intrinsic, pose) # (H, W, 3), (H, W, 3)
select_coords = select_coordinates(coords, args.N_rand)
rays_o = rays_o[select_coords[:, 0], select_coords[:, 1]] # (N_rand, 3)
rays_d = rays_d[select_coords[:, 0], select_coords[:, 1]] # (N_rand, 3)
target_s = target[select_coords[:, 0], select_coords[:, 1]] # (N_rand, 3)
target_d = target_depth[select_coords[:, 0], select_coords[:, 1]] # (N_rand, 1) or (N_rand, 2)
target_vd = target_valid_depth[select_coords[:, 0], select_coords[:, 1]] # (N_rand, 1)
target_h = target_hypothesis[:, select_coords[:, 0], select_coords[:, 1]]
if space_carving_idx is not None:
# print(space_carving_idx.shape)
# print(space_carving_idx[img_i, select_coords[:, 0], select_coords[:, 1]].shape)
target_hypothesis = target_hypothesis.repeat(1, 1, 1, space_carving_idx.shape[-1])
curr_space_carving_idx = space_carving_idx[img_i, select_coords[:, 0], select_coords[:, 1]]
target_h_rays = target_hypothesis[ :, select_coords[:, 0], select_coords[:, 1]]
target_h = torch.gather(target_h_rays, 1, curr_space_carving_idx.unsqueeze(0).long())
if cached_u is not None:
curr_cached_u = cached_u[img_i, select_coords[:, 0], select_coords[:, 1]]
else:
curr_cached_u = None
if args.mask_corners:
### Initialize a masked image
space_carving_mask = torch.ones((target.shape[0], target.shape[1]), dtype=torch.float, device=images.device)
### Mask out the corners
num_pix_to_mask = 20
space_carving_mask[:num_pix_to_mask, :num_pix_to_mask] = 0
space_carving_mask[:num_pix_to_mask, -num_pix_to_mask:] = 0
space_carving_mask[-num_pix_to_mask:, :num_pix_to_mask] = 0
space_carving_mask[-num_pix_to_mask:, -num_pix_to_mask:] = 0
space_carving_mask = space_carving_mask[select_coords[:, 0], select_coords[:, 1]]
else:
space_carving_mask = None
batch_rays = torch.stack([rays_o, rays_d], 0) # (2, N_rand, 3)
return batch_rays, target_s, target_d, target_vd, img_i, target_h, space_carving_mask, curr_cached_u
def train_nerf(images, depths, valid_depths, poses, intrinsics, i_split, args, scene_sample_params, lpips_alex, gt_depths, gt_valid_depths, all_depth_hypothesis, is_init_scales=False, scales_init=None, shifts_init=None):
np.random.seed(0)
torch.manual_seed(0)
torch.cuda.manual_seed(0)
tb = SummaryWriter(log_dir=os.path.join("runs", args.expname))
near, far = scene_sample_params['near'], scene_sample_params['far']
H, W = images.shape[1:3]
i_train, i_val, i_test, i_video = i_split
print('TRAIN views are', i_train)
print('VAL views are', i_val)
print('TEST views are', i_test)
# use ground truth depth for validation and test if available
if gt_depths is not None:
depths[i_test] = gt_depths[i_test]
valid_depths[i_test] = gt_valid_depths[i_test]
depths[i_val] = gt_depths[i_val]
valid_depths[i_val] = gt_valid_depths[i_val]
i_relevant_for_training = np.concatenate((i_train, i_val), 0)
if len(i_test) == 0:
print("Error: There is no test set")
exit()
if len(i_val) == 0:
print("Warning: There is no validation set, test set is used instead")
i_val = i_test
i_relevant_for_training = np.concatenate((i_relevant_for_training, i_val), 0)
# keep test data on cpu until needed
test_images = images[i_test]
test_depths = depths[i_test]
test_valid_depths = valid_depths[i_test]
test_poses = poses[i_test]
test_intrinsics = intrinsics[i_test]
i_test = i_test - i_test[0]
# move training data to gpu
images = torch.Tensor(images[i_relevant_for_training]).to(device)
depths = torch.Tensor(depths[i_relevant_for_training]).to(device)
valid_depths = torch.Tensor(valid_depths[i_relevant_for_training]).bool().to(device)
poses = torch.Tensor(poses[i_relevant_for_training]).to(device)
intrinsics = torch.Tensor(intrinsics[i_relevant_for_training]).to(device)
all_depth_hypothesis = torch.Tensor(all_depth_hypothesis).to(device)
intrinsics = torch.Tensor(intrinsics[i_relevant_for_training]).to(device)
# create nerf model
render_kwargs_train, render_kwargs_test, start, nerf_grad_vars, optimizer, nerf_grad_names = create_nerf(args, scene_sample_params)
##### Initialize depth scale and shift
DEPTH_SCALES = torch.autograd.Variable(torch.ones((images.shape[0], 1), dtype=torch.float, device=images.device)*args.scale_init, requires_grad=True)
DEPTH_SHIFTS = torch.autograd.Variable(torch.ones((images.shape[0], 1), dtype=torch.float, device=images.device)*args.shift_init, requires_grad=True)
print(DEPTH_SCALES)
print()
print(DEPTH_SHIFTS)
print()
print(DEPTH_SCALES.shape)
print(DEPTH_SHIFTS.shape)
optimizer_ss = torch.optim.Adam(params=(DEPTH_SCALES, DEPTH_SHIFTS,), lr=args.scaleshift_lr)
print("Initialized scale and shift.")
################################
# create camera embedding function
embedcam_fn = None
# optimize nerf
print('Begin')
N_iters = args.num_iterations + 1
global_step = start
start = start + 1
init_learning_rate = args.lrate
old_learning_rate = init_learning_rate
# if args.cimle_white_balancing and args.load_pretrained:
if args.load_pretrained:
path = args.pretrained_dir
ckpts = [os.path.join(path, f) for f in sorted(os.listdir(path)) if '000.tar' in f]
print('Found ckpts', ckpts)
ckpt_path = ckpts[-1]
print('Reloading pretrained model from', ckpt_path)
ckpt = torch.load(ckpt_path)
coarse_model_dict = render_kwargs_train["network_fn"].state_dict()
coarse_keys = {k: v for k, v in ckpt['network_fn_state_dict'].items() if k in coarse_model_dict}
fine_model_dict = render_kwargs_train["network_fine"].state_dict()
fine_keys = {k: v for k, v in ckpt['network_fine_state_dict'].items() if k in fine_model_dict}
print(len(coarse_keys.keys()))
print(len(fine_keys.keys()))
print("Num keys loaded:")
coarse_model_dict.update(coarse_keys)
fine_model_dict.update(fine_keys)
## Load scale and shift
DEPTH_SHIFTS = torch.load(ckpt_path)["depth_shifts"]
DEPTH_SCALES = torch.load(ckpt_path)["depth_scales"]
print("Scales:")
print(DEPTH_SCALES)
print()
print("Shifts:")
print(DEPTH_SHIFTS)
print("Loaded depth shift/scale from pretrained model.")
########################################
########################################
for i in trange(start, N_iters):
### Scale the hypotheses by scale and shift
img_i = np.random.choice(i_train)
curr_scale = DEPTH_SCALES[img_i]
curr_shift = DEPTH_SHIFTS[img_i]
## Scale and shift
batch_rays, target_s, target_d, target_vd, img_i, target_h, space_carving_mask, curr_cached_u = get_ray_batch_from_one_image_hypothesis_idx(H, W, img_i, images, depths, valid_depths, poses, \
intrinsics, all_depth_hypothesis, args, None, None)
target_h = target_h*curr_scale + curr_shift
if args.input_ch_cam > 0:
render_kwargs_train['embedded_cam'] = embedcam_fn[img_i]
target_d = target_d.squeeze(-1)
render_kwargs_train["cached_u"] = None
rgb, _, _, extras = render_hyp(H, W, None, chunk=args.chunk, rays=batch_rays, verbose=i < 10, retraw=True, is_joint=args.is_joint, **render_kwargs_train)
# compute loss and optimize
optimizer.zero_grad()
optimizer_ss.zero_grad()
img_loss = img2mse(rgb, target_s)
psnr = mse2psnr(img_loss)
loss = img_loss
if args.space_carving_weight>0. and i>args.warm_start_nerf:
space_carving_loss = compute_space_carving_loss(extras["pred_hyp"], target_h, is_joint=args.is_joint, norm_p=args.norm_p, threshold=args.space_carving_threshold, mask=space_carving_mask)
loss = loss + args.space_carving_weight * space_carving_loss
else:
space_carving_loss = torch.mean(torch.zeros([target_h.shape[0]]).to(target_h.device))
if 'rgb0' in extras:
img_loss0 = img2mse(extras['rgb0'], target_s)
psnr0 = mse2psnr(img_loss0)
loss = loss + img_loss0
loss.backward()
### Update learning rate
learning_rate = get_learning_rate(init_learning_rate, i, args.decay_step, args.decay_rate, staircase=True)
if old_learning_rate != learning_rate:
update_learning_rate(optimizer, learning_rate)
old_learning_rate = learning_rate
optimizer.step()
### Don't optimize scale shift for the last 100k epochs, check whether the appearance will crisp
if i < args.freeze_ss:
optimizer_ss.step()
### Update camera embeddings
if args.input_ch_cam > 0 and args.opt_ch_cam: