-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_depth_for_render.py
executable file
·1263 lines (1096 loc) · 45.6 KB
/
eval_depth_for_render.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
# Lint as: python3
"""Train depth and pose on the carla dataset.
"""
import cv2
import os
import distro
import numpy as np
# Pytorch Imports
import torch
from progress.bar import Bar
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
from data_readers.habitat_data_neuray_ft import HabitatImageGeneratorFT
from lmdb_rw.habitat_data_neuray_ft_lmdb import HabitatImageGeneratorFT_LMDB
from helpers import my_torch_helpers
from helpers.torch_checkpoint_manager import CheckpointManager
from models import loss_lib
from network.omni_mvsnet.pipeline3_model import FullPipeline
import numpy as np
import random
import argparse
from utils.base_utils import load_cfg
def setup_seed(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
class App:
"""Main app class"""
default_cfg={
"script_mode": "train_depth_pose",
"model_name": "",
"checkpoints_dir": "",
"batch_size": 2,
"epochs": 999999,#useless
"learning_rate":0.00006,
"width": 512,
"height": 256,
"opt_beta1": 0.5,
"opt_beta2": 0.999,
"train_tensorboard_interval": 0,
"validation_interval": 200,
"checkpoint_interval":200,
"smoothness_loss_lambda": 1,
"checkpoint_count": 3,
"point_radius": 0.004,
"device": "cuda",
"verbose": True,
"patch_loss_patch_size": 5,
"patch_loss_stride": 1,
"patch_loss_stride_dist": 3,
"inpaint_use_residual": False,
"inpaint_wrap_padding": False,
"loss": "l1",
"inpaint_use_batchnorm":True,
"upscale_point_cloud": True,
"inpaint_one_conv": False,
"interpolation_mode": "bilinear",
"add_depth_noise": 0,
"depth_input_uv": False,
"normalize_depth": False,
"predict_zdepth": False,
"cost_volume": "",
"clip_grad_value": 0,
"model_use_v_input": False,
"debug_mode": False,
"dataset": "m3d",
"carla_min_dist": 2,
"carla_max_dist": 100,
"min_depth": 0.1,
"max_depth": 10,
"turbo_cmap_min": 2,
"m3d_dist": 1,
"depth_type": "one_over",
"depth_loss": "l1",
"load_prepared_test_data": False,
"test_sample_num": 200,
"save_datadir": "/home/chenzheng/nas/PanoNVS/somsi_data/test_m3d",
"total_iter": 100000,
#unifuse
"num_layers": 18, # choices=[2, 18, 34, 50, 101]
"imagenet_pretrained": False,
# ablation settings
"mono_net": "UniFuse",#choices=["UniFuse", "Equi", "ERP+TP", "TP", "Cube", "OmniSyn"]
"fusion":"cee", #choices=["cee", "cat", "biproj"]
"se_in_fusion": False,
"num_workers": 2,
# Multi_view matching hyper_parameters
"MAGNET_sampling_range": 3,
"MAGNET_num_samples": 5,
"MAGNET_mvs_weighting": "CW5",
"DNET_ckpt": None,
"contain_dnet": False,
"use_wrap_padding": False,
"stereo_out_type": "disparity",
"dnet_out_type": "depth",
#fusemodel output type
"out_type": "depth",
"stereonet_ckpt": None,
"fuse_type": "simple", #simple, geometry_emb_only, geometry_emb_scaled, geometry_emb_scaled_masked etc
"patchsize": (128, 128),
"fov": 80,
"nrow": 4,#3,4,5,6
"lr_decay": False,
"lrate_decay": 250,
"aug": False,
"wo_hdh": False,
"use_lmdb": False,
}
def __init__(self):
self.model = None
self.optimizer = None
self.checkpoint_manager = None
self.args = None
self.writer = None
# Attributes to hold training data.
self.train_data = None
self.train_data_loader = None
# Attributes to hold validation data.
self.val_data = None
self.val_data_indices = None
self.input_panos_val = None
self.input_depths_val = None
self.input_rots_val = None
self.input_trans_val = None
def start(self, flags):
"""Starts the training."""
try:
cfg=load_cfg(flags.cfg)
self.cfg={**self.default_cfg,**cfg}
# import ipdb;ipdb.set_trace()
args = self.cfg
self.args = args
self.full_width = args["width"]
self.full_height = args["height"]
seed = 2022
setup_seed(seed)
self.load_training_data()
self.load_validation_data()
self.setup_model()
self.setup_checkpoints()
step = self.checkpoint_manager.step
if args["script_mode"] == "eval_depth_test":
total_params = self.model.get_total_params()
print("Total parameters:", total_params)
# import ipdb;ipdb.set_trace()
self.eval_on_validation_data()
else:
raise ValueError("Unknown script mode: " + str(args["script_mode"]))
except KeyboardInterrupt:
print("Terminating script")
self.writer.close()
def setup_model(self):
"""Sets up the model."""
args = self.args
model = FullPipeline(args,
width=args["width"],
height=args["height"],
layers=5,
raster_resolution=args["width"],
depth_input_images=1,
depth_output_channels=1,
include_poseestimator=True,
verbose=args["verbose"],
input_uv=args["depth_input_uv"],
interpolation_mode=args["interpolation_mode"],
cost_volume=args["cost_volume"],
use_v_input=args["model_use_v_input"],
).to(args["device"])
optimizer = torch.optim.Adam(model.parameters(),
lr=args["learning_rate"],
betas=(args["opt_beta1"], args["opt_beta2"]))
self.model = model
self.optimizer = optimizer
def setup_checkpoints(self):
"""Sets up the checkpoint manager."""
args = self.args
model = self.model
optimizer = self.optimizer
checkpoint_manager = CheckpointManager(args["checkpoints_dir"],
max_to_keep=args["checkpoint_count"])
latest_checkpoint = checkpoint_manager.load_latest_checkpoint()
if latest_checkpoint is not None:
model.load_state_dict(latest_checkpoint['model_state_dict'])
optimizer.load_state_dict(latest_checkpoint['optimizer_state_dict'])
writer = SummaryWriter(log_dir=os.path.join(args["checkpoints_dir"], "logs"))
self.checkpoint_manager = checkpoint_manager
self.writer = writer
def load_training_data(self):
"""Loads training data."""
args = self.args
# Prepare dataset loaders for train and validation datasets.
if args["dataset"] == "carla":
train_data = CarlaReader(
args["carla_path"],
width=self.full_width,
height=self.full_height,
towns=["Town01", "Town02", "Town03", "Town04"],
min_dist=args["carla_min_dist"],
max_dist=args["carla_max_dist"],
seq_len=2,
reference_idx=1,
use_meters_depth=True,
interpolation_mode=args["interpolation_mode"],
sampling_method="dense")
print("Size of training set: %d" % (len(train_data),))
train_dataloader = DataLoader(
train_data,
batch_size=args["batch_size"],
shuffle=True,
num_workers=4)
elif args["dataset"] == "m3d":
# args=cfg,
# split=mode,
# seq_len=seq_len,
# reference_idx=reference_idx,
# full_width=full_width,
# full_height=full_height,
# m3d_dist=m3d_dist
if args["use_lmdb"]:
train_data = HabitatImageGeneratorFT_LMDB(
args,
split="train",
seq_len = args["seq_len"],
reference_idx = args["reference_idx"],
full_width=self.full_width,
full_height=self.full_height,
m3d_dist=args["m3d_dist"],
aug=args["aug"]
)
train_dataloader = DataLoader(
dataset=train_data,
num_workers=args["num_workers"],
batch_size=args["batch_size"],
shuffle=True,
drop_last=True,
pin_memory=True,
prefetch_factor=1,
)
else:
train_data = HabitatImageGeneratorFT(
args,
split="train",
seq_len = args["seq_len"],
reference_idx = args["reference_idx"],
full_width=self.full_width,
full_height=self.full_height,
m3d_dist=args["m3d_dist"],
aug=args["aug"]
)
train_dataloader = DataLoader(
dataset=train_data,
num_workers=args["num_workers"],
batch_size=args["batch_size"],
shuffle=False,
drop_last=True,
pin_memory=True,
prefetch_factor=1,
)
train_data.cache_depth_to_dist(args["height"], args["width"])
self.train_data = train_data
self.train_data_loader = train_dataloader
def load_validation_data(self):
"""Loads validation data."""
args = self.args
if args["dataset"] == "carla":
towns = ["Town05"]
if args["script_mode"] == "eval_depth_test":
towns = ["Town06"]
val_data = CarlaReader(
args["carla_path"],
width=self.full_width,
height=self.full_height,
towns=towns,
min_dist=args["carla_min_dist"],
max_dist=args["carla_max_dist"],
seq_len=2,
reference_idx=1,
use_meters_depth=True,
interpolation_mode=args["interpolation_mode"])
elif args["dataset"] == "m3d":
if args["use_lmdb"]: # == "eval_depth_test":
val_data = HabitatImageGeneratorFT_LMDB(
args,
"val",
seq_len = args["seq_len"],
reference_idx = args["reference_idx"],
full_width=self.full_width,
full_height=self.full_height,
m3d_dist=args["m3d_dist"])
else:
val_data = HabitatImageGeneratorFT(
args,
args["eval_mode"],
seq_len = args["seq_len"],
reference_idx = args["reference_idx"],
full_width=self.full_width,
full_height=self.full_height,
m3d_dist=args["m3d_dist"])
self.val_data = val_data
def run_depth_pose_carla(self, step, panos, depths, rots, trans):
"""Does a single run and returns results.
Args:
step: Current step.
panos: Input panoramas.
depths: GT depths.
rots: GT rotations.
trans: GT translations.
Returns:
Dictionary containing all outputs.
"""
args = self.args
height = args["height"]
width = args["width"]
model = self.model
train_data = self.train_data
batch_size, seq_len = panos.shape[:2]
panos_small = panos.reshape(
(batch_size * seq_len, self.full_height, self.full_width, 3))
panos_small = my_torch_helpers.resize_torch_images(
panos_small, (args["height"], args["width"]), mode=args["interpolation_mode"])
panos_small = panos_small.reshape(batch_size, seq_len, height, width, 3)
depths_height, depths_width = depths.shape[2:4]
depths_small = depths.reshape(
(batch_size * seq_len, depths_height, depths_width, 1))
depths_small = my_torch_helpers.resize_torch_images(
depths_small, (args["height"], args["width"]), mode=args["interpolation_mode"])
depths_small = depths_small.reshape(batch_size, seq_len, height, width, 1)
# rots_pred, trans_pred = model.estimate_pose(panos_small[:, :2, :, :, :])
# if args["cost_volume"]:
outputs = model.estimate_depth_using_cost_volume(panos_small, rots, trans,
min_depth=args["min_depth"],
max_depth=args["max_depth"])
depths_pred = outputs["depth"]
# if args["predict_zdepth"]:
# depths_pred = train_data.zdepth_to_distance_torch(depths_pred)
depths_pred = depths_pred.reshape(
(batch_size, 1, height, width, depths_pred.shape[3]))
assert torch.isfinite(depths_pred).all(), "Nan in depths_pred"
# disp_c1 = None
# depths_c1 = None
# zdepths_small_1 = None
# rect_gt_depth = None
# rect_gt_disp = None
# disp_pred_c1 = None
# if args["cost_volume"] == "v1" or \
# args["cost_volume"] == "v2" or \
# args["cost_volume"] == "v3":
# rect_gt_depth = my_torch_helpers.rotate_equirectangular_image(
# depths_small[:, 1], outputs["rect_rots"][:, 1])
# # rect_gt_disp = model.erp_depth_to_disparity(
# # rect_gt_depth.permute((0, 3, 1, 2)), outputs["trans_norm"])
# # rect_gt_disp = rect_gt_disp.permute((0, 2, 3, 1))
# # unrect_gt_disp = model.unrectify_image(rect_gt_disp,
# # outputs["rect_rots"][:, 1])
# # assert torch.isfinite(rect_gt_disp).all(), "Nan in rect_gt_disp"
# assert torch.isfinite(
# outputs["raw_image_features"]).all(), "Nan in raw image features"
if args["loss"] == "l1_cost_volume_erp":
assert torch.isfinite(depths_small).all(), "Nan in depths_small"
# rect_gt_depth = my_torch_helpers.rotate_equirectangular_image(
# depths_small[:, 1], outputs["rect_rots"][:, 1])
one_over_gt_depth = my_torch_helpers.safe_divide(1.0, depths_small[:, 1]) #todo
if args["out_type"]=="disparity":
loss1 = loss_lib.compute_l1_sphere_loss(
outputs['raw_image_features'],
one_over_gt_depth,
mask=torch.gt(depths_small[:, 1], 0.1))
loss1 = loss1 + 0.5 * loss_lib.compute_l1_sphere_loss(
outputs['raw_image_features_d1'],
one_over_gt_depth,
mask=torch.gt(depths_small[:, 1], 0.1))
elif args["out_type"]=="depth":
loss1 = loss_lib.compute_l1_sphere_loss(
outputs['raw_image_features'],
depths_small[:, 1],
mask=torch.gt(depths_small[:, 1], 0.1))
loss1 = loss1 + 0.5 * loss_lib.compute_l1_sphere_loss(
outputs['raw_image_features_d1'],
depths_small[:, 1],
mask=torch.gt(depths_small[:, 1], 0.1))
else:
raise ValueError("Loss not found: %s" % (args["loss"],))
# rot_loss = torch.mean(torch.abs(rots_pred _ rots[:, 0]))
# trans_loss = torch.mean(torch.abs(trans_pred _ trans[:, 0, :]))
final_loss = loss1 #
depths_pred = torch.clamp(depths_pred, min=0.1)
assert torch.isfinite(loss1).all(), "Nan in depth final_loss"
assert torch.isfinite(final_loss).all(), "Nan in final_loss function"
return {
# "error_flag":error_flag,
"loss1": loss1,
"final_loss": final_loss,
# "rot_loss": rot_loss,
# "trans_loss": trans_loss,
"depths_pred": depths_pred,
"panos_small": panos_small,
"depths_small": depths_small,
"outputs": outputs,
# "rect_gt_depth": rect_gt_depth,
# "rect_gt_disp": rect_gt_disp,
# "depth_smoothness_loss": depth_smoothness_loss,
# "disp_c1": disp_c1,
# "disp_c1_pred": disp_pred_c1,
# "depths_c1": depths_c1,
# "zdepths_small_1": zdepths_small_1,
# "rots_pred": rots_pred,
# "trans_pred": trans_pred
}
def do_validation_run(self, step):
"""Does a validation run.
Args:
step: Current step.
Returns:
None.
"""
args = self.args
model = self.model
writer = self.writer
if step == 1 or \
args["validation_interval"] == 0 or \
step % args["validation_interval"] == 0:
# Calculate validation final_loss.
with torch.no_grad():
panos = self.input_panos_val
depths = self.input_depths_val
rots = self.input_rots_val
trans = self.input_trans_val
batch_size, seq_len = panos.shape[:2]
run_outputs = self.run_depth_pose_carla(step, panos, depths, rots,
trans)
final_loss = run_outputs["final_loss"]
depths_pred = run_outputs["depths_pred"]
depths_small = run_outputs["depths_small"]
panos_small = run_outputs["panos_small"]
writer.add_scalar("val_loss", final_loss.item(), step)
writer.add_scalar("val_image_loss", run_outputs["loss1"].item(), step)
# writer.add_scalar("val_rot_loss", run_outputs["rot_loss"].item(), step)
# writer.add_scalar("val_trans_loss", run_outputs["trans_loss"].item(),
# step)
depths_turbo = my_torch_helpers.depth_to_turbo_colormap(
depths_small[:, 1], min_depth=args["turbo_cmap_min"])
normalized_depth_pred = depths_pred[:, 0]
if args["normalize_depth"]:
std, mean = torch.std_mean(depths_small[:, 1],
dim=(1, 2),
keepdim=True)
normalized_depth_pred = loss_lib.normalize_depth(
normalized_depth_pred, new_std=std, new_mean=mean)
depths_pred_turbo = my_torch_helpers.depth_to_turbo_colormap(
normalized_depth_pred, min_depth=args["turbo_cmap_min"])
# back_warped_1 = model.backwards_warping(panos[:, 0],
# depths_small[:, 1, :, :, 0],
# run_outputs["rots_pred"],
# run_outputs["trans_pred"],
# inv_rot=False)
depth_abs_error_img = torch.abs(depths_small[:, 1] -
normalized_depth_pred)
depth_abs_error_img = depth_abs_error_img.expand(
(batch_size, args["height"], args["width"], 3))
depth_abs_error_img_stacked = torch.cat(
(panos_small[:, 1], depths_turbo, depths_pred_turbo,
depth_abs_error_img),
dim=2)
depth_mae = torch.mean(torch.abs(depths_small[:, 1] -
normalized_depth_pred),
dim=(1, 2, 3))
depth_mse = torch.mean(torch.pow(
depths_small[:, 1] - normalized_depth_pred, 2.0),
dim=(1, 2, 3))
y_stacked = torch.cat((panos_small[:, 0], panos_small[:, 1],
depths_turbo, depths_pred_turbo),
dim=2)
for j in range(len(self.val_data_indices)):
writer.add_image("80_val_image_%02d" % j,
y_stacked[j].clamp(0, 1),
step,
dataformats="HWC")
writer.add_image("82_val_depth_ae_%02d" % j,
depth_abs_error_img_stacked[j].clamp(0, 1),
step,
dataformats="HWC")
writer.add_scalar("84_val_depth_mae_%02d" % j, depth_mae[j], step)
writer.add_scalar("86_val_depth_mse_%02d" % j, depth_mse[j], step)
def log_training_to_tensorboard(self, step, run_outputs):
"""Logs training to tensorboard.
Args:
step: Current step.
run_outputs: Outputs of the training step.
Returns:
None.
"""
args = self.args
model = self.model
writer = self.writer
depths_pred = run_outputs["depths_pred"]
depths_small = run_outputs["depths_small"]
outputs = run_outputs["outputs"]
# rect_gt_depth = run_outputs["rect_gt_depth"]
# rect_gt_disp = run_outputs["rect_gt_disp"]
panos_small = run_outputs["panos_small"]
final_loss = run_outputs["final_loss"]
loss_np = final_loss.detach().cpu().numpy()
average_depth_np = torch.mean(depths_pred).detach().cpu().numpy()
writer.add_scalar("train_loss", loss_np, step)
writer.add_scalar("train_depth", average_depth_np, step)
writer.add_scalar("train_image_loss", run_outputs["loss1"].item(), step)
# writer.add_scalar("train_depth_smoothness_loss",
# run_outputs["depth_smoothness_loss"].item(), step)
# writer.add_scalar("train_rot_loss", run_outputs["rot_loss"].item(), step)
# writer.add_scalar("train_trans_loss", run_outputs["trans_loss"].item(),
# step)
if step == 1 or \
args["train_tensorboard_interval"] == 0 or \
step % args["train_tensorboard_interval"] == 0:
with torch.no_grad():
depths_small_turbo = my_torch_helpers.depth_to_turbo_colormap(
depths_small[:, 1], min_depth=args["turbo_cmap_min"])
depths_scale_factor = 1
normalized_depth_pred = depths_pred[:, 0]
if args["normalize_depth"]:
std, mean = torch.std_mean(depths_small[:, 1], dim=(1, 2),
keepdim=True)
normalized_depth_pred = loss_lib.normalize_depth(
normalized_depth_pred,
new_std=std,
new_mean=mean)
depths_pred_turbo = my_torch_helpers.depth_to_turbo_colormap(
normalized_depth_pred, min_depth=args["turbo_cmap_min"])
stacked_input_panos = torch.cat((panos_small[:, 0], panos_small[:, 1]),
dim=1)
writer.add_images("00_train_inputs",
stacked_input_panos,
step,
dataformats="NHWC")
writer.add_images("05_train_depths_gt",
depths_small_turbo,
step,
dataformats="NHWC")
writer.add_images("10_train_pred_depths",
depths_pred_turbo,
step,
dataformats="NHWC")
if self.args["contain_dnet"]:
mono_depth_ref = outputs["mono_depth_ref"]
mono_depth_ref_turbo = my_torch_helpers.depth_to_turbo_colormap(
mono_depth_ref[:, 0].unsqueeze(3), min_depth=args["turbo_cmap_min"])
writer.add_images("105_mono_depth_ref",
mono_depth_ref_turbo,
step,
dataformats="NHWC")
y_pred = depths_pred[:, 0]
y_true = depths_small[:, 1]
if args["normalize_depth"]:
y_pred = loss_lib.normalize_depth(y_pred)
y_true = loss_lib.normalize_depth(y_true)
depth_loss_image = torch.abs(y_true - y_pred)
writer.add_images("11_train_l1_loss_image",
depth_loss_image.clamp(0, 1),
step,
dataformats="NHWC")
def save_checkpoint(self, step):
"""Saves a checkpoint.
Args:
step: Current step.
Returns:
None.
"""
args = self.args
if args["checkpoint_interval"] == 0 or step % args["checkpoint_interval"] == 0:
# Save a checkpoint
self.checkpoint_manager.save_checkpoint({
"model_state_dict": self.model.state_dict(),
"optimizer_state_dict": self.optimizer.state_dict()
})
self.writer.flush()
def run_training_loop(self):
args = self.args
train_dataloader = self.train_data_loader
optimizer = self.optimizer
checkpoint_manager = self.checkpoint_manager
model = self.model
for epoch in range(args["epochs"]):
for i, data in enumerate(train_dataloader):
optimizer.zero_grad()
step = checkpoint_manager.increment_step()
# if i%100==0:
print("step:", step)
if step >= args["total_iter"]:
print("evaluation...")
self.do_validation_run(step)
self.save_checkpoint(step)
self.eval_on_validation_data(step)
exit()
if step % args["validation_interval"]==0 and step>0:
self.do_validation_run(step)
self.save_checkpoint(step)
self.eval_on_validation_data(step)
if args["debug_mode"]:
assert distro.linux_distribution()[0] == "Ubuntu", "Debug mode is on"
panos = self.input_panos_val
depths = self.input_depths_val
rots = self.input_rots_val
trans = self.input_trans_val
else:
assert distro is not None
panos = data["rgb_panos"].to(args["device"])
depths = data["depth_panos"].to(args["device"])
rots = data["rots"].to(args["device"])
trans = data["trans"].to(args["device"])
# print("panos", panos.dtype, depths.dtype, rots.dtype, trans.dtype)
# print("maxmin", torch.max(panos), torch.min(panos))
run_outputs = self.run_depth_pose_carla(step, panos, depths,
rots,
trans)
# if run_outputs["error_flag"]:
# #args["checkpoints_dir"]
# data0 = run_outputs["i0"]
# data1 = run_outputs["i1"]
# import cv2
# cv2.imwrite(args["checkpoints_dir"]+"/step_"+str(step)+"_0.jpg", data0)
# cv2.imwrite(args["checkpoints_dir"]+"/step_"+str(step)+"_1.jpg", data1)
# continue
self.log_training_to_tensorboard(step, run_outputs)
final_loss = run_outputs["final_loss"]
depths_pred = run_outputs["depths_pred"]
final_loss.backward()
if args["clip_grad_value"] > 1e-10:
# print("Clipping gradients to %f" % args["clip_grad_value"])
torch.nn.utils.clip_grad_value_(model.parameters(),
args["clip_grad_value"])
optimizer.step()
self.do_validation_run(step)
self.save_checkpoint(step)
if i%100==0:
loss_np = final_loss.detach().cpu().numpy()
average_depth_np = torch.mean(depths_pred).detach().cpu().numpy()
print("Step: %d [%d:%d] Loss: %f, average depth %f" %
(step, epoch, i, loss_np, average_depth_np))
if args["lr_decay"]:
# NOTE: IMPORTANT!
### update learning rate ###
decay_rate = 0.1
#250*1000=250000
decay_steps = args["lrate_decay"] * 1000 # iteration: 20w->10w, 1000->500
new_lrate = args["learning_rate"] * (decay_rate ** (step / decay_steps))
for param_group in self.optimizer.param_groups:
param_group['lr'] = new_lrate
def eval_on_training_data(self):
"""Performs evaluation on the whole evaluation dataset.
Returns:
None
"""
args = self.args
train_dataloader = DataLoader(self.train_data,
batch_size=args["batch_size"],
shuffle=False,
num_workers=4)
self.model.eval()
num_iterations = 10
bar = Bar('Eval on training data',
max=num_iterations)
train_iterator = iter(train_dataloader)
min_pred_depth = 9999.9
max_pred_depth = 0.0
min_gt_depth = 999.9
max_gt_depth = 0.0
l1_errors = []
l2_errors = []
wl1_errors = []
wl2_errors = []
with torch.no_grad():
step = 100000
weight = (torch.arange(0, args["height"], device=args["device"],
dtype=torch.float32) + 0.5) * np.pi / args["height"]
weight = torch.sin(weight).view(1, args["height"], 1, 1)
for i in range(num_iterations):
data = next(train_iterator)
panos = data["rgb_panos"].to(args["device"])
depths = data["depth_panos"].to(args["device"])
rots = data["rots"].to(args["device"])
trans = data["trans"].to(args["device"])
run_outputs = self.run_depth_pose_carla(step, panos, depths,
rots,
trans)
m_weight = weight.expand(
panos.shape[0], args["height"], args["width"], 1)
depths_small = run_outputs["depths_small"][:, 1]
depths_pred = run_outputs["depths_pred"][:, 0]
depths_pred = torch.clamp_min(depths_pred, 0.0)
wl1_error = torch.abs(depths_small - depths_pred) * m_weight
wl1_error = torch.sum(wl1_error, dim=(1, 2, 3)) / torch.sum(m_weight,
dim=(
1, 2, 3))
wl1_errors.append(wl1_error.cpu().numpy())
wl2_error = torch.pow(depths_small - depths_pred, 2.0) * m_weight
wl2_error = torch.sum(wl2_error, dim=(1, 2, 3)) / torch.sum(m_weight,
dim=(
1, 2, 3))
wl2_errors.append(wl2_error.cpu().numpy())
l1_error = torch.mean(torch.abs(depths_small - depths_pred),
dim=(1, 2, 3))
l1_errors.append(l1_error.cpu().numpy())
l2_error = torch.mean(torch.pow(depths_small - depths_pred, 2.0),
dim=(1, 2, 3))
l2_errors.append(l2_error.cpu().numpy())
min_pred_depth = min(min_pred_depth, torch.min(depths_pred).item())
max_pred_depth = max(max_pred_depth, torch.max(depths_pred).item())
min_gt_depth = min(min_gt_depth, torch.min(depths_small).item())
max_gt_depth = max(max_gt_depth, torch.max(depths_small).item())
bar.next()
total_l1_errors = np.mean(np.stack(l1_errors))
total_l2_errors = np.mean(np.stack(l2_errors))
total_wl1_errors = np.mean(np.stack(wl1_errors))
total_wl2_errors = np.mean(np.stack(wl2_errors))
bar.finish()
print("Evaluation on training data:")
print("Total l1 error:", total_l1_errors, "Weighted:", total_wl1_errors)
print("Total l2 error:", total_l2_errors, "Weighted:", total_wl2_errors)
print("True depth range", min_gt_depth, max_gt_depth)
print("Pred depth range", min_pred_depth, max_pred_depth)
def eval_on_validation_data(self, step=-1):
"""Performs evaluation on the whole evaluation dataset.
Returns:
None
"""
args = self.args
self.model.eval()
self.load_validation_data()
val_dataloader = DataLoader(self.val_data,
batch_size=1,
shuffle=False,
num_workers=0,
pin_memory=True)
max_examples = len(self.val_data)
if args["dataset"] == 'm3d':
max_examples = min(len(self.val_data), args["test_sample_num"])
if args["script_mode"] == 'eval_depth_test':
results_file = os.path.join(
args["checkpoints_dir"],
"test_eval_results_%02f_%02f.txt" % (
args["carla_min_dist"], args["carla_max_dist"]))
if args["dataset"] == 'm3d':
if step==-1:
results_file = os.path.join(
args["checkpoints_dir"],
"test_eval_results_%02f.txt" % (
args["m3d_dist"]))
else:
results_file = os.path.join(
args["checkpoints_dir"],
"test_eval_results_%02f_%06d.txt" % (
args["m3d_dist"], step))
results_file = open(results_file, "w")
else:
results_file = os.path.join(
args["checkpoints_dir"],
"val_eval_results_%02f_%02f.txt" % (
args["carla_min_dist"], args["carla_max_dist"]))
if args["dataset"] == 'm3d':
if step==-1:
results_file = os.path.join(
args["checkpoints_dir"],
"val_eval_results_%02f.txt" % (
args["m3d_dist"]))
else:
# import ipdb;ipdb.set_trace()
results_file = os.path.join(
args["checkpoints_dir"],
"val_eval_results_%02f_%06d.txt" % (
args["m3d_dist"], step))
results_file = open(results_file, "w")
min_pred_depth = 9999.9
max_pred_depth = 0.0
min_gt_depth = 999.9
max_gt_depth = 0.0
all_errors = {}
with torch.no_grad():
# step = 100000
weight = (torch.arange(0, args["height"], device=args["device"],
dtype=torch.float32) + 0.5) * np.pi / args["height"]
weight = torch.sin(weight).view(1, args["height"], 1, 1)
def load_data(args, idx):
# np.savez(args["save_datadir"]+'/data_'+str(i)+'.npz', panos = panos.data.cpu().numpy(), rots=rots.data.cpu().numpy(), trans=trans.data.cpu().numpy(), depths=depths.data.cpu().numpy())
data = np.load(args["save_datadir"]+'/data_'+str(idx)+'.npz')
return data
#todo
#args["checkpoints_dir"]
if step != -1:
test_imgs_dir=os.path.join(args["checkpoints_dir"], "test_images_"+str(step))
else:
test_imgs_dir=os.path.join(args["checkpoints_dir"], "test_images")
# test_imgs_dir=os.path.join(args["checkpoints_dir"], "test_images")
os.makedirs(test_imgs_dir, exist_ok=True)
if args["load_prepared_test_data"]:
print("load_prepared_test_data:")
#for mono depth: evaluation 1_th(not zero)
for i in range(args["test_sample_num"]):
print("evaluate i:", i)
data = load_data(args, i)
panos = torch.from_numpy(data['panos'])[:, :, ...].to(args["device"])
rots = torch.from_numpy(data['rots'])[:, :, ...].to(args["device"])
trans = torch.from_numpy(data['trans'])[:, :, ...].to(args["device"])
depths = torch.from_numpy(data['depths'])[:, :, ...].to(args["device"])
run_outputs = self.run_depth_pose_carla(step, panos, depths,
rots,
trans)
m_weight = weight.expand(
panos.shape[0], args["height"], args["width"], 1)
depths_small = run_outputs["depths_small"][:, 1]
depths_pred = run_outputs["depths_pred"][:, 0]
depths_pred = torch.clamp_min(depths_pred, 0.0)
#visualize
# test_imgs_dir
# import pdb;pdb.set_trace()
# print("panos.shape:", panos.shape)
rgb = np.uint8(panos[:, 1][0].data.cpu().numpy()*255)
cv2.imwrite(test_imgs_dir+"/"+str(i)+"_rgb.jpg", rgb)
d_pred = depths_pred[0].data.cpu().numpy()
d_gt = depths_small[0].data.cpu().numpy()
def normalize_depth(depth):
d_min = depth.min()
d_max = depth.max()
d_norm = np.uint8((depth-d_min)/(d_max-d_min)*255)
d_rgb = cv2.applyColorMap(d_norm, cv2.COLORMAP_JET)
return d_rgb
d_pred = normalize_depth(d_pred)
d_gt = normalize_depth(d_gt)
cv2.imwrite(test_imgs_dir+"/"+str(i)+"_depth_pred.jpg", d_pred)
cv2.imwrite(test_imgs_dir+"/"+str(i)+"_depth_gt.jpg", d_gt)
erp_errors = self.compute_erp_depth_results(
gt_depth=depths_small,
pred_depth=depths_pred,
m_weight=m_weight
)
cube_errors = self.compute_zdepth_results(
gt_depth=depths[:, 1, :, :, None],
pred_depth=depths_pred
)
for k, v in erp_errors.items():
if k not in all_errors:
all_errors[k] = []
all_errors[k].append(v.detach().cpu().numpy())
for k, v in cube_errors.items():
if k not in all_errors:
all_errors[k] = []
all_errors[k].append(v.detach().cpu().numpy())
else:
print("in val_dataloader:")
for i, data in enumerate(val_dataloader):
print("evaluate i:", i)
if i >= max_examples:
break
panos = data["rgb_panos"].to(args["device"])
depths = data["depth_panos"].to(args["device"])
rots = data["rots"].to(args["device"])
trans = data["trans"].to(args["device"])
run_outputs = self.run_depth_pose_carla(step, panos, depths,
rots,
trans)
m_weight = weight.expand(
panos.shape[0], args["height"], args["width"], 1)
depths_small = run_outputs["depths_small"][:, 1]
depths_pred = run_outputs["depths_pred"][:, 0]
depths_pred = torch.clamp_min(depths_pred, 0.0)
#vis
rgb = np.uint8(panos[:, 1][0].data.cpu().numpy()*255)
cv2.imwrite(test_imgs_dir+"/"+str(i)+"_rgb.jpg", rgb)
d_pred = depths_pred[0].data.cpu().numpy()
d_gt = depths_small[0].data.cpu().numpy()
def normalize_depth(depth):