-
Notifications
You must be signed in to change notification settings - Fork 15
/
trainer_end_to_end.py
803 lines (626 loc) · 34.9 KB
/
trainer_end_to_end.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
from __future__ import absolute_import, division, print_function
import time
import json
import datasets
import networks
import numpy as np
import torch.optim as optim
import torch.nn as nn
from utils import *
from layers import *
from torch.utils.data import DataLoader
from tensorboardX import SummaryWriter
class Trainer:
def __init__(self, options):
self.opt = options
self.log_path = os.path.join(self.opt.log_dir, self.opt.model_name)
# checking height and width are multiples of 32
assert self.opt.height % 32 == 0, "'height' must be a multiple of 32"
assert self.opt.width % 32 == 0, "'width' must be a multiple of 32"
self.models = {} # 字典
self.parameters_to_train = [] # 列表
self.parameters_to_train_0 = []
self.device = torch.device("cpu" if self.opt.no_cuda else "cuda")
self.num_scales = len(self.opt.scales) # 4
self.num_input_frames = len(self.opt.frame_ids) # 3
self.num_pose_frames = 2 if self.opt.pose_model_input == "pairs" else self.num_input_frames # 2
assert self.opt.frame_ids[0] == 0, "frame_ids must start with 0"
self.use_pose_net = not (self.opt.use_stereo and self.opt.frame_ids == [0])
if self.opt.use_stereo:
self.opt.frame_ids.append("s")
self.models["encoder"] = networks.ResnetEncoder(
self.opt.num_layers, self.opt.weights_init == "pretrained") # 18
self.models["encoder"].to(self.device)
self.parameters_to_train += list(self.models["encoder"].parameters())
self.models["depth"] = networks.DepthDecoder(
self.models["encoder"].num_ch_enc, self.opt.scales)
self.models["depth"].to(self.device)
self.parameters_to_train += list(self.models["depth"].parameters())
self.models["position_encoder"] = networks.ResnetEncoder(
self.opt.num_layers, self.opt.weights_init == "pretrained", num_input_images=2) # 18
self.models["position_encoder"].to(self.device)
self.parameters_to_train_0 += list(self.models["position_encoder"].parameters())
self.models["position"] = networks.PositionDecoder(
self.models["position_encoder"].num_ch_enc, self.opt.scales)
self.models["position"].to(self.device)
self.parameters_to_train_0 += list(self.models["position"].parameters())
self.models["transform_encoder"] = networks.ResnetEncoder(
self.opt.num_layers, self.opt.weights_init == "pretrained", num_input_images=2) # 18
self.models["transform_encoder"].to(self.device)
self.parameters_to_train += list(self.models["transform_encoder"].parameters())
self.models["transform"] = networks.TransformDecoder(
self.models["transform_encoder"].num_ch_enc, self.opt.scales)
self.models["transform"].to(self.device)
self.parameters_to_train += list(self.models["transform"].parameters())
if self.use_pose_net:
if self.opt.pose_model_type == "separate_resnet":
self.models["pose_encoder"] = networks.ResnetEncoder(
self.opt.num_layers,
self.opt.weights_init == "pretrained",
num_input_images=self.num_pose_frames)
self.models["pose_encoder"].to(self.device)
self.parameters_to_train += list(self.models["pose_encoder"].parameters())
self.models["pose"] = networks.PoseDecoder(
self.models["pose_encoder"].num_ch_enc,
num_input_features=1,
num_frames_to_predict_for=2)
elif self.opt.pose_model_type == "shared":
self.models["pose"] = networks.PoseDecoder(
self.models["encoder"].num_ch_enc, self.num_pose_frames)
elif self.opt.pose_model_type == "posecnn":
self.models["pose"] = networks.PoseCNN(
self.num_input_frames if self.opt.pose_model_input == "all" else 2)
self.models["pose"].to(self.device)
self.parameters_to_train += list(self.models["pose"].parameters())
if self.opt.predictive_mask:
assert self.opt.disable_automasking, \
"When using predictive_mask, please disable automasking with --disable_automasking"
# Our implementation of the predictive masking baseline has the the same architecture
# as our depth decoder. We predict a separate mask for each source frame.
self.models["predictive_mask"] = networks.DepthDecoder(
self.models["encoder"].num_ch_enc, self.opt.scales,
num_output_channels=(len(self.opt.frame_ids) - 1))
self.models["predictive_mask"].to(self.device)
self.parameters_to_train += list(self.models["predictive_mask"].parameters())
self.model_optimizer = optim.Adam(self.parameters_to_train, self.opt.learning_rate)
self.model_lr_scheduler = optim.lr_scheduler.StepLR(
self.model_optimizer, self.opt.scheduler_step_size, 0.1)
self.model_optimizer_0 = optim.Adam(self.parameters_to_train_0, 1e-4)
self.model_lr_scheduler_0 = optim.lr_scheduler.StepLR(
self.model_optimizer_0, self.opt.scheduler_step_size, 0.1)
if self.opt.load_weights_folder is not None:
self.load_model()
print("Training model named:\n ", self.opt.model_name)
print("Models and tensorboard events files are saved to:\n ", self.opt.log_dir)
print("Training is using:\n ", self.device)
# data
datasets_dict = {"endovis": datasets.SCAREDRAWDataset}
self.dataset = datasets_dict[self.opt.dataset]
fpath = os.path.join(os.path.dirname(__file__), "splits", self.opt.split, "{}_files.txt")
train_filenames = readlines(fpath.format("train"))
val_filenames = readlines(fpath.format("val"))
img_ext = '.png'
num_train_samples = len(train_filenames)
self.num_total_steps = num_train_samples // self.opt.batch_size * self.opt.num_epochs
train_dataset = self.dataset(
self.opt.data_path, train_filenames, self.opt.height, self.opt.width,
self.opt.frame_ids, 4, is_train=True, img_ext=img_ext)
self.train_loader = DataLoader(
train_dataset, self.opt.batch_size, True,
num_workers=self.opt.num_workers, pin_memory=True, drop_last=True)
val_dataset = self.dataset(
self.opt.data_path, val_filenames, self.opt.height, self.opt.width,
self.opt.frame_ids, 4, is_train=False, img_ext=img_ext)
self.val_loader = DataLoader(
val_dataset, self.opt.batch_size, False,
num_workers=1, pin_memory=True, drop_last=True)
self.val_iter = iter(self.val_loader)
self.writers = {}
for mode in ["train", "val"]:
self.writers[mode] = SummaryWriter(os.path.join(self.log_path, mode))
if not self.opt.no_ssim:
self.ssim = SSIM()
self.ssim.to(self.device)
self.spatial_transform = SpatialTransformer((self.opt.height, self.opt.width))
self.spatial_transform.to(self.device)
self.get_occu_mask_backward = get_occu_mask_backward((self.opt.height, self.opt.width))
self.get_occu_mask_backward.to(self.device)
self.get_occu_mask_bidirection = get_occu_mask_bidirection((self.opt.height, self.opt.width))
self.get_occu_mask_bidirection.to(self.device)
self.backproject_depth = {}
self.project_3d = {}
self.position_depth = {}
for scale in self.opt.scales:
h = self.opt.height // (2 ** scale)
w = self.opt.width // (2 ** scale)
self.backproject_depth[scale] = BackprojectDepth(self.opt.batch_size, h, w)
self.backproject_depth[scale].to(self.device)
self.project_3d[scale] = Project3D(self.opt.batch_size, h, w)
self.project_3d[scale].to(self.device)
self.position_depth[scale] = optical_flow((h, w), self.opt.batch_size, h, w)
self.position_depth[scale].to(self.device)
self.depth_metric_names = [
"de/abs_rel", "de/sq_rel", "de/rms", "de/log_rms", "da/a1", "da/a2", "da/a3"]
print("Using split:\n ", self.opt.split)
print("There are {:d} training items and {:d} validation items\n".format(
len(train_dataset), len(val_dataset)))
self.save_opts()
def set_train_0(self):
"""Convert all models to training mode
"""
for param in self.models["position_encoder"].parameters():
param.requires_grad = True
for param in self.models["position"].parameters():
param.requires_grad = True
for param in self.models["encoder"].parameters():
param.requires_grad = False
for param in self.models["depth"].parameters():
param.requires_grad = False
for param in self.models["pose_encoder"].parameters():
param.requires_grad = False
for param in self.models["pose"].parameters():
param.requires_grad = False
for param in self.models["transform_encoder"].parameters():
param.requires_grad = False
for param in self.models["transform"].parameters():
param.requires_grad = False
self.models["position_encoder"].train()
self.models["position"].train()
self.models["encoder"].eval()
self.models["depth"].eval()
self.models["pose_encoder"].eval()
self.models["pose"].eval()
self.models["transform_encoder"].eval()
self.models["transform"].eval()
def set_train(self):
"""Convert all models to training mode
"""
for param in self.models["position_encoder"].parameters():
param.requires_grad = False
for param in self.models["position"].parameters():
param.requires_grad = False
for param in self.models["encoder"].parameters():
param.requires_grad = True
for param in self.models["depth"].parameters():
param.requires_grad = True
for param in self.models["pose_encoder"].parameters():
param.requires_grad = True
for param in self.models["pose"].parameters():
param.requires_grad = True
for param in self.models["transform_encoder"].parameters():
param.requires_grad = True
for param in self.models["transform"].parameters():
param.requires_grad = True
self.models["position_encoder"].eval()
self.models["position"].eval()
self.models["encoder"].train()
self.models["depth"].train()
self.models["pose_encoder"].train()
self.models["pose"].train()
self.models["transform_encoder"].train()
self.models["transform"].train()
def set_eval(self):
"""Convert all models to testing/evaluation mode
"""
self.models["encoder"].eval()
self.models["depth"].eval()
self.models["transform_encoder"].eval()
self.models["transform"].eval()
self.models["pose_encoder"].eval()
self.models["pose"].eval()
def train(self):
"""Run the entire training pipeline
"""
self.epoch = 0
self.step = 0
self.start_time = time.time()
for self.epoch in range(self.opt.num_epochs):
self.run_epoch()
if (self.epoch + 1) % self.opt.save_frequency == 0:
self.save_model()
def run_epoch(self):
"""Run a single epoch of training and validation
"""
print("Training")
for batch_idx, inputs in enumerate(self.train_loader):
before_op_time = time.time()
# position
self.set_train_0()
_, losses_0 = self.process_batch_0(inputs)
self.model_optimizer_0.zero_grad()
losses_0["loss"].backward()
self.model_optimizer_0.step()
# depth, pose, transform
self.set_train()
outputs, losses = self.process_batch(inputs)
self.model_optimizer.zero_grad()
losses["loss"].backward()
self.model_optimizer.step()
duration = time.time() - before_op_time
phase = batch_idx % self.opt.log_frequency == 0
if phase:
self.log_time(batch_idx, duration, losses["loss"].cpu().data)
self.log("train", inputs, outputs, losses)
# self.val()
self.step += 1
self.model_lr_scheduler.step()
self.model_lr_scheduler_0.step()
def process_batch_0(self, inputs):
"""Pass a minibatch through the network and generate images and losses
"""
for key, ipt in inputs.items():
inputs[key] = ipt.to(self.device)
outputs = {}
outputs.update(self.predict_poses_0(inputs))
losses = self.compute_losses_0(inputs, outputs)
return outputs, losses
def predict_poses_0(self, inputs):
"""Predict poses between input frames for monocular sequences.
"""
outputs = {}
if self.num_pose_frames == 2:
pose_feats = {f_i: inputs["color_aug", f_i, 0] for f_i in self.opt.frame_ids}
for f_i in self.opt.frame_ids[1:]:
if f_i != "s":
inputs_all = [pose_feats[f_i], pose_feats[0]]
inputs_all_reverse = [pose_feats[0], pose_feats[f_i]]
# position
position_inputs = self.models["position_encoder"](torch.cat(inputs_all, 1))
position_inputs_reverse = self.models["position_encoder"](torch.cat(inputs_all_reverse, 1))
outputs_0 = self.models["position"](position_inputs)
outputs_1 = self.models["position"](position_inputs_reverse)
for scale in self.opt.scales:
outputs[("position", scale, f_i)] = outputs_0[("position", scale)]
outputs[("position", "high", scale, f_i)] = F.interpolate(
outputs[("position", scale, f_i)], [self.opt.height, self.opt.width], mode="bilinear",
align_corners=False)
outputs[("registration", scale, f_i)] = self.spatial_transform(inputs[("color", f_i, 0)],
outputs[(
"position", "high", scale, f_i)])
outputs[("position_reverse", scale, f_i)] = outputs_1[("position", scale)]
outputs[("position_reverse", "high", scale, f_i)] = F.interpolate(
outputs[("position_reverse", scale, f_i)], [self.opt.height, self.opt.width],
mode="bilinear", align_corners=False)
outputs[("occu_mask_backward", scale, f_i)], _ = self.get_occu_mask_backward(
outputs[("position_reverse", "high", scale, f_i)])
outputs[("occu_map_bidirection", scale, f_i)] = self.get_occu_mask_bidirection(
outputs[("position", "high", scale, f_i)],
outputs[("position_reverse", "high", scale, f_i)])
# transform
transform_input = [outputs[("registration", 0, f_i)], inputs[("color", 0, 0)]]
transform_inputs = self.models["transform_encoder"](torch.cat(transform_input, 1))
outputs_2 = self.models["transform"](transform_inputs)
for scale in self.opt.scales:
outputs[("transform", scale, f_i)] = outputs_2[("transform", scale)]
outputs[("transform", "high", scale, f_i)] = F.interpolate(
outputs[("transform", scale, f_i)], [self.opt.height, self.opt.width], mode="bilinear",
align_corners=False)
outputs[("refined", scale, f_i)] = (outputs[("transform", "high", scale, f_i)] * outputs[
("occu_mask_backward", 0, f_i)].detach() + inputs[("color", 0, 0)])
outputs[("refined", scale, f_i)] = torch.clamp(outputs[("refined", scale, f_i)], min=0.0,
max=1.0)
return outputs
def compute_losses_0(self, inputs, outputs):
losses = {}
total_loss = 0
for scale in self.opt.scales:
loss = 0
loss_smooth_registration = 0
loss_registration = 0
if self.opt.v1_multiscale:
source_scale = scale
else:
source_scale = 0
color = inputs[("color", 0, scale)]
for frame_id in self.opt.frame_ids[1:]:
occu_mask_backward = outputs[("occu_mask_backward", 0, frame_id)].detach()
loss_smooth_registration += (get_smooth_loss(outputs[("position", scale, frame_id)], color))
loss_registration += (
self.compute_reprojection_loss(outputs[("registration", scale, frame_id)], outputs[("refined", scale, frame_id)].detach()) * occu_mask_backward).sum() / occu_mask_backward.sum()
loss += loss_registration / 2.0
loss += self.opt.position_smoothness * (loss_smooth_registration / 2.0) / (2 ** scale)
total_loss += loss
losses["loss/{}".format(scale)] = loss
total_loss /= self.num_scales
losses["loss"] = total_loss
return losses
def process_batch(self, inputs):
"""Pass a minibatch through the network and generate images and losses
"""
for key, ipt in inputs.items():
inputs[key] = ipt.to(self.device)
if self.opt.pose_model_type == "shared":
# If we are using a shared encoder for both depth and pose (as advocated
# in monodepthv1), then all images are fed separately through the depth encoder.
all_color_aug = torch.cat([inputs[("color_aug", i, 0)] for i in self.opt.frame_ids])
all_features = self.models["encoder"](all_color_aug)
all_features = [torch.split(f, self.opt.batch_size) for f in all_features]
features = {}
for i, k in enumerate(self.opt.frame_ids):
features[k] = [f[i] for f in all_features]
outputs = self.models["depth"](features[0])
else:
# Otherwise, we only feed the image with frame_id 0 through the depth encoder
features = self.models["encoder"](inputs["color_aug", 0, 0])
outputs = self.models["depth"](features)
if self.opt.predictive_mask:
outputs["predictive_mask"] = self.models["predictive_mask"](features)
if self.use_pose_net:
outputs.update(self.predict_poses(inputs, features, outputs))
self.generate_images_pred(inputs, outputs)
losses = self.compute_losses(inputs, outputs)
return outputs, losses
def predict_poses(self, inputs, features, disps):
"""Predict poses between input frames for monocular sequences.
"""
outputs = {}
if self.num_pose_frames == 2:
if self.opt.pose_model_type == "shared":
pose_feats = {f_i: features[f_i] for f_i in self.opt.frame_ids}
else:
pose_feats = {f_i: inputs["color_aug", f_i, 0] for f_i in self.opt.frame_ids}
for f_i in self.opt.frame_ids[1:]:
if f_i != "s":
inputs_all = [pose_feats[f_i], pose_feats[0]]
inputs_all_reverse = [pose_feats[0], pose_feats[f_i]]
# position
position_inputs = self.models["position_encoder"](torch.cat(inputs_all, 1))
position_inputs_reverse = self.models["position_encoder"](torch.cat(inputs_all_reverse, 1))
outputs_0 = self.models["position"](position_inputs)
outputs_1 = self.models["position"](position_inputs_reverse)
for scale in self.opt.scales:
outputs[("position", scale, f_i)] = outputs_0[("position", scale)]
outputs[("position", "high", scale, f_i)] = F.interpolate(
outputs[("position", scale, f_i)], [self.opt.height, self.opt.width], mode="bilinear", align_corners=False)
outputs[("registration", scale, f_i)] = self.spatial_transform(inputs[("color", f_i, 0)], outputs[("position", "high", scale, f_i)])
outputs[("position_reverse", scale, f_i)] = outputs_1[("position", scale)]
outputs[("position_reverse", "high", scale, f_i)] = F.interpolate(
outputs[("position_reverse", scale, f_i)], [self.opt.height, self.opt.width], mode="bilinear", align_corners=False)
outputs[("occu_mask_backward", scale, f_i)], outputs[("occu_map_backward", scale, f_i)]= self.get_occu_mask_backward(outputs[("position_reverse", "high", scale, f_i)])
outputs[("occu_map_bidirection", scale, f_i)] = self.get_occu_mask_bidirection(outputs[("position", "high", scale, f_i)],
outputs[("position_reverse", "high", scale, f_i)])
# transform
transform_input = [outputs[("registration", 0, f_i)], inputs[("color", 0, 0)]]
transform_inputs = self.models["transform_encoder"](torch.cat(transform_input, 1))
outputs_2 = self.models["transform"](transform_inputs)
for scale in self.opt.scales:
outputs[("transform", scale, f_i)] = outputs_2[("transform", scale)]
outputs[("transform", "high", scale, f_i)] = F.interpolate(
outputs[("transform", scale, f_i)], [self.opt.height, self.opt.width], mode="bilinear", align_corners=False)
outputs[("refined", scale, f_i)] = (outputs[("transform", "high", scale, f_i)] * outputs[("occu_mask_backward", 0, f_i)].detach() + inputs[("color", 0, 0)])
outputs[("refined", scale, f_i)] = torch.clamp(outputs[("refined", scale, f_i)], min=0.0, max=1.0)
# outputs[("grad_refined", scale, f_i)] = get_gradmap(outputs[("refined", scale, f_i)])
# pose
pose_inputs = [self.models["pose_encoder"](torch.cat(inputs_all, 1))]
axisangle, translation = self.models["pose"](pose_inputs)
outputs[("axisangle", 0, f_i)] = axisangle
outputs[("translation", 0, f_i)] = translation
outputs[("cam_T_cam", 0, f_i)] = transformation_from_parameters(
axisangle[:, 0], translation[:, 0])
return outputs
def generate_images_pred(self, inputs, outputs):
"""Generate the warped (reprojected) color images for a minibatch.
Generated images are saved into the `outputs` dictionary.
"""
for scale in self.opt.scales:
disp = outputs[("disp", scale)]
if self.opt.v1_multiscale:
source_scale = scale
else:
disp = F.interpolate(
disp, [self.opt.height, self.opt.width], mode="bilinear", align_corners=False)
_, depth = disp_to_depth(disp, self.opt.min_depth, self.opt.max_depth)
outputs[("depth", 0, scale)] = depth
source_scale = 0
for i, frame_id in enumerate(self.opt.frame_ids[1:]):
if frame_id == "s":
T = inputs["stereo_T"]
else:
T = outputs[("cam_T_cam", 0, frame_id)]
# from the authors of https://arxiv.org/abs/1712.00175
if self.opt.pose_model_type == "posecnn":
axisangle = outputs[("axisangle", 0, frame_id)]
translation = outputs[("translation", 0, frame_id)]
inv_depth = 1 / depth
mean_inv_depth = inv_depth.mean(3, True).mean(2, True)
T = transformation_from_parameters(
axisangle[:, 0], translation[:, 0] * mean_inv_depth[:, 0], frame_id < 0)
cam_points = self.backproject_depth[source_scale](
depth, inputs[("inv_K", source_scale)])
pix_coords = self.project_3d[source_scale](
cam_points, inputs[("K", source_scale)], T)
outputs[("sample", frame_id, scale)] = pix_coords
outputs[("color", frame_id, scale)] = F.grid_sample(
inputs[("color", frame_id, source_scale)],
outputs[("sample", frame_id, scale)],
padding_mode="border")
outputs[("position_depth", scale, frame_id)] = self.position_depth[source_scale](
cam_points, inputs[("K", source_scale)], T)
def compute_reprojection_loss(self, pred, target):
abs_diff = torch.abs(target - pred)
l1_loss = abs_diff.mean(1, True)
if self.opt.no_ssim:
reprojection_loss = l1_loss
else:
ssim_loss = self.ssim(pred, target).mean(1, True)
reprojection_loss = 0.85 * ssim_loss + 0.15 * l1_loss
return reprojection_loss
def compute_losses(self, inputs, outputs):
losses = {}
total_loss = 0
for scale in self.opt.scales:
loss = 0
loss_reprojection = 0
loss_transform = 0
loss_cvt = 0
if self.opt.v1_multiscale:
source_scale = scale
else:
source_scale = 0
disp = outputs[("disp", scale)]
color = inputs[("color", 0, scale)]
for frame_id in self.opt.frame_ids[1:]:
occu_mask_backward = outputs[("occu_mask_backward", 0, frame_id)].detach()
loss_reprojection += (
self.compute_reprojection_loss(outputs[("color", frame_id, scale)], outputs[("refined", scale, frame_id)]) * occu_mask_backward).sum() / occu_mask_backward.sum()
loss_transform += (
torch.abs(outputs[("refined", scale, frame_id)] - outputs[("registration", 0, frame_id)].detach()).mean(1, True) * occu_mask_backward).sum() / occu_mask_backward.sum()
loss_cvt += get_smooth_bright(
outputs[("transform", "high", scale, frame_id)], inputs[("color", 0, 0)], outputs[("registration", scale, frame_id)].detach(), occu_mask_backward)
mean_disp = disp.mean(2, True).mean(3, True)
norm_disp = disp / (mean_disp + 1e-7)
smooth_loss = get_smooth_loss(norm_disp, color)
loss += loss_reprojection / 2.0
loss += self.opt.transform_constraint * (loss_transform / 2.0)
loss += self.opt.transform_smoothness * (loss_cvt / 2.0)
loss += self.opt.disparity_smoothness * smooth_loss / (2 ** scale)
total_loss += loss
losses["loss/{}".format(scale)] = loss
total_loss /= self.num_scales
losses["loss"] = total_loss
return losses
def val(self):
"""Validate the model on a single minibatch
"""
self.set_eval()
try:
inputs = self.val_iter.next()
except StopIteration:
self.val_iter = iter(self.val_loader)
inputs = self.val_iter.next()
with torch.no_grad():
outputs, losses = self.process_batch_val(inputs)
self.log("val", inputs, outputs, losses)
del inputs, outputs, losses
self.set_train()
def process_batch_val(self, inputs):
"""Pass a minibatch through the network and generate images and losses
"""
for key, ipt in inputs.items():
inputs[key] = ipt.to(self.device)
if self.opt.pose_model_type == "shared":
# If we are using a shared encoder for both depth and pose (as advocated
# in monodepthv1), then all images are fed separately through the depth encoder.
all_color_aug = torch.cat([inputs[("color_aug", i, 0)] for i in self.opt.frame_ids])
all_features = self.models["encoder"](all_color_aug)
all_features = [torch.split(f, self.opt.batch_size) for f in all_features]
features = {}
for i, k in enumerate(self.opt.frame_ids):
features[k] = [f[i] for f in all_features]
outputs = self.models["depth"](features[0])
else:
# Otherwise, we only feed the image with frame_id 0 through the depth encoder
features = self.models["encoder"](inputs["color_aug", 0, 0])
outputs = self.models["depth"](features)
if self.opt.predictive_mask:
outputs["predictive_mask"] = self.models["predictive_mask"](features)
if self.use_pose_net:
outputs.update(self.predict_poses(inputs, features, outputs))
self.generate_images_pred(inputs, outputs)
losses = self.compute_losses_val(inputs, outputs)
return outputs, losses
def compute_losses_val(self, inputs, outputs):
"""Compute the reprojection, perception_loss and smoothness losses for a minibatch
"""
losses = {}
total_loss = 0
for scale in self.opt.scales:
loss = 0
registration_losses = []
target = inputs[("color", 0, 0)]
for frame_id in self.opt.frame_ids[1:]:
registration_losses.append(
ncc_loss(outputs[("registration", scale, frame_id)].mean(1, True), target.mean(1, True)))
registration_losses = torch.cat(registration_losses, 1)
registration_losses, idxs_registration = torch.min(registration_losses, dim=1)
loss += registration_losses.mean()
total_loss += loss
losses["loss/{}".format(scale)] = loss
total_loss /= self.num_scales
losses["loss"] = -1 * total_loss
return losses
def log_time(self, batch_idx, duration, loss):
"""Print a logging statement to the terminal
"""
samples_per_sec = self.opt.batch_size / duration
time_sofar = time.time() - self.start_time
training_time_left = (
self.num_total_steps / self.step - 1.0) * time_sofar if self.step > 0 else 0
print_string = "epoch {:>3} | batch {:>6} | examples/s: {:5.1f}" + \
" | loss: {:.5f} | time elapsed: {} | time left: {}"
print(print_string.format(self.epoch, batch_idx, samples_per_sec, loss,
sec_to_hm_str(time_sofar), sec_to_hm_str(training_time_left)))
def log(self, mode, inputs, outputs, losses):
"""Write an event to the tensorboard events file
"""
writer = self.writers[mode]
for l, v in losses.items():
writer.add_scalar("{}".format(l), v, self.step)
for j in range(min(4, self.opt.batch_size)): # write a maxmimum of four images
for s in self.opt.scales:
for frame_id in self.opt.frame_ids[1:]:
writer.add_image(
"brightness_{}_{}/{}".format(frame_id, s, j),
outputs[("transform", "high", s, frame_id)][j].data, self.step)
writer.add_image(
"registration_{}_{}/{}".format(frame_id, s, j),
outputs[("registration", s, frame_id)][j].data, self.step)
writer.add_image(
"refined_{}_{}/{}".format(frame_id, s, j),
outputs[("refined", s, frame_id)][j].data, self.step)
if s == 0:
writer.add_image(
"occu_mask_backward_{}_{}/{}".format(frame_id, s, j),
outputs[("occu_mask_backward", s, frame_id)][j].data, self.step)
writer.add_image(
"disp_{}/{}".format(s, j),
normalize_image(outputs[("disp", s)][j]), self.step)
def save_opts(self):
"""Save options to disk so we know what we ran this experiment with
"""
models_dir = os.path.join(self.log_path, "models")
if not os.path.exists(models_dir):
os.makedirs(models_dir)
to_save = self.opt.__dict__.copy()
with open(os.path.join(models_dir, 'opt.json'), 'w') as f:
json.dump(to_save, f, indent=2)
def save_model(self):
"""Save model weights to disk
"""
save_folder = os.path.join(self.log_path, "models", "weights_{}".format(self.epoch))
if not os.path.exists(save_folder):
os.makedirs(save_folder)
for model_name, model in self.models.items():
save_path = os.path.join(save_folder, "{}.pth".format(model_name))
to_save = model.state_dict()
if model_name == 'encoder':
# save the sizes - these are needed at prediction time
to_save['height'] = self.opt.height
to_save['width'] = self.opt.width
to_save['use_stereo'] = self.opt.use_stereo
torch.save(to_save, save_path)
save_path = os.path.join(save_folder, "{}.pth".format("adam"))
torch.save(self.model_optimizer.state_dict(), save_path)
def load_model(self):
"""Load model(s) from disk
"""
self.opt.load_weights_folder = os.path.expanduser(self.opt.load_weights_folder)
assert os.path.isdir(self.opt.load_weights_folder), \
"Cannot find folder {}".format(self.opt.load_weights_folder)
print("loading model from folder {}".format(self.opt.load_weights_folder))
for n in self.opt.models_to_load:
print("Loading {} weights...".format(n))
path = os.path.join(self.opt.load_weights_folder, "{}.pth".format(n))
model_dict = self.models[n].state_dict()
pretrained_dict = torch.load(path)
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
model_dict.update(pretrained_dict)
self.models[n].load_state_dict(model_dict)
# loading adam state
# optimizer_load_path = os.path.join(self.opt.load_weights_folder, "adam.pth")
# if os.path.isfile(optimizer_load_path):
# print("Loading Adam weights")
# optimizer_dict = torch.load(optimizer_load_path)
# self.model_optimizer.load_state_dict(optimizer_dict)
# else:
print("Adam is randomly initialized")