-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcorruption_runner.py
510 lines (405 loc) · 19.8 KB
/
corruption_runner.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
# Copyright Niantic 2019. Patent Pending. All rights reserved.
#
# This software is licensed under the terms of the Monodepth2 licence
# which allows for non-commercial use only, the full terms of which are made
# available in the LICENSE file.
from __future__ import absolute_import, division, print_function
import numpy as np
import time
import tempfile
import shutil
import torch
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import DataLoader
from tensorboardX import SummaryWriter
import tqdm
import pdb
import json
from utils import *
from layers import *
import datasets
import networks
import mmcv
from IPython import embed
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel as DDP
import cv2
from torch.utils.data import DistributedSampler as _DistributedSampler
import pickle
def get_dist_info(return_gpu_per_machine=False):
if torch.__version__ < '1.0':
initialized = dist._initialized
else:
if dist.is_available():
initialized = dist.is_initialized()
else:
initialized = False
if initialized:
rank = dist.get_rank()
world_size = dist.get_world_size()
else:
rank = 0
world_size = 1
if return_gpu_per_machine:
gpu_per_machine = torch.cuda.device_count()
return rank, world_size, gpu_per_machine
return rank, world_size
class DistributedSampler(_DistributedSampler):
def __init__(self, dataset, num_replicas=None, rank=None, shuffle=True):
super().__init__(dataset, num_replicas=num_replicas, rank=rank)
self.shuffle = shuffle
def __iter__(self):
if self.shuffle:
g = torch.Generator()
g.manual_seed(self.epoch)
indices = torch.randperm(len(self.dataset), generator=g).tolist()
else:
indices = torch.arange(len(self.dataset)).tolist()
indices = indices[self.rank:self.total_size:self.num_replicas]
return iter(indices)
class Runer:
def __init__(self, options):
self.opt = options
self.log_path = os.path.join(self.opt.log_dir, self.opt.model_name)
os.makedirs(os.path.join(self.log_path, 'eval'), exist_ok=True)
os.makedirs(os.path.join(self.log_path, 'models'), exist_ok=True)
# 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.local_rank = self.opt.local_rank
torch.cuda.set_device(self.local_rank)
dist.init_process_group(backend='nccl')
self.device = torch.device("cuda", self.local_rank)
self.num_scales = len(self.opt.scales)
self.num_input_frames = len(self.opt.frame_ids)
self.num_pose_frames = 2 if self.opt.pose_model_input == "pairs" else self.num_input_frames
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")
self.models["encoder"] = torch.nn.SyncBatchNorm.convert_sync_batchnorm(self.models["encoder"])
self.models["encoder"] = (self.models["encoder"]).to(self.device)
self.parameters_to_train += list(self.models["encoder"].parameters())
self.models["depth"] = networks.DepthDecoder(
self.opt, self.models["encoder"].num_ch_enc, self.opt.scales)
self.models["depth"] = torch.nn.SyncBatchNorm.convert_sync_batchnorm(self.models["depth"])
self.models["depth"] = (self.models["depth"]).to(self.device)
self.parameters_to_train += list(self.models["depth"].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"] = torch.nn.SyncBatchNorm.convert_sync_batchnorm(self.models["pose_encoder"])
self.models["pose_encoder"] = 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"] = torch.nn.SyncBatchNorm.convert_sync_batchnorm(self.models["pose"])
self.models["pose"] = (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"] = torch.nn.SyncBatchNorm.convert_sync_batchnorm(self.models["predictive_mask"])
self.models["predictive_mask"] = (self.models["predictive_mask"]).to(self.device)
self.parameters_to_train += list(self.models["predictive_mask"].parameters())
if self.opt.load_weights_folder is not None:
self.load_model()
for key in self.models.keys():
self.models[key] = DDP(self.models[key], device_ids=[self.local_rank], output_device=self.local_rank, find_unused_parameters=True, broadcast_buffers=False)
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)
if self.local_rank == 0:
self.log_print("Training model named: {}".format(self.opt.model_name))
# data
datasets_dict = {"ddad": datasets.DDADDataset,
"nusc": datasets.NuscDataset,
"robodrive": datasets.CorruptionDataset,}
self.dataset = datasets_dict[self.opt.dataset]
self.opt.batch_size = self.opt.batch_size // 6
val_dataset = self.dataset(self.opt,
self.opt.height, self.opt.width,
self.opt.frame_ids, 4, is_train=False)
# debug
data = val_dataset[0]
rank, world_size = get_dist_info()
self.world_size = world_size
val_sampler = DistributedSampler(val_dataset, world_size, rank, shuffle=False)
self.val_loader = DataLoader(
val_dataset, self.opt.batch_size, collate_fn=self.my_collate,
num_workers=4, pin_memory=True, drop_last=False, sampler=val_sampler)
self.val_iter = iter(self.val_loader)
self.num_val = len(val_dataset)
self.opt.batch_size = self.opt.batch_size * 6
self.num_val = self.num_val * 6
if not self.opt.no_ssim:
self.ssim = SSIM()
self.ssim.to(self.device)
self.backproject_depth = {}
self.project_3d = {}
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.depth_metric_names = [
"de/abs_rel", "de/sq_rel", "de/rms", "de/log_rms", "da/a1", "da/a2", "da/a3"]
if self.local_rank == 0:
self.log_print("There are {:d} validation items\n".format(len(val_dataset)))
self.save_opts()
def my_collate(self,batch):
batch_new = {}
keys_list = list(batch[0].keys())
special_key_list = ['id', 'match_spatial']
for key in keys_list:
if key == 'sample_token':
batch_new[key] = [item[key] for item in batch]
elif key not in special_key_list:
batch_new[key] = [item[key] for item in batch]
batch_new[key] = torch.cat(batch_new[key], axis=0)
else:
batch_new[key] = []
for item in batch:
for value in item[key]:
batch_new[key].append(value)
return batch_new
def set_train(self):
"""Convert all models to training mode
"""
for m in self.models.values():
m.train()
def set_eval(self):
"""Convert all models to testing/evaluation mode
"""
for m in self.models.values():
m.eval()
def train(self):
"""Run the entire training pipeline
"""
self.step = 1
if self.opt.eval_only:
self.val()
exit()
else:
raise NotImplementedError
def val(self):
"""Validate the model on a single minibatch
"""
self.set_eval()
self.models["encoder"].eval()
self.models["depth"].eval()
ratios_median = []
if self.local_rank == 0:
pbar = mmcv.ProgressBar(self.num_val / 6)
preds_list = []
gts_list = []
print(f'Evaluating {self.opt.corruption}')
with torch.no_grad():
loader = self.val_loader
for idx, data in enumerate(loader):
if self.local_rank == 0:
for rank in range(self.world_size):
pbar.update()
input_color = data[("color", 0, 0)].cuda()
camera_ids = data["id"]
sample_tokens = data["sample_token"]
assert len(sample_tokens) == 1, 'only support batch size 1 for evaluation'
sample_tokens = sample_tokens[0]
features = self.models["encoder"](input_color)
output = self.models["depth"](features)
pred_disps_tensor, pred_depths = disp_to_depth(output[("disp", 0)], self.opt.min_depth, self.opt.max_depth)
input_color_flip = torch.flip(input_color, [3])
features_flip = self.models["encoder"](input_color_flip)
output_flip = self.models["depth"](features_flip)
pred_disps_flip_tensor, pred_depths_flip = disp_to_depth(output_flip[("disp", 0)], self.opt.min_depth, self.opt.max_depth)
pred_disps_flip = post_process_inv_depth(pred_disps_tensor, pred_disps_flip_tensor)
pred_disps = pred_disps_flip.cpu()[:, 0].numpy()
depth_maps = []
for i in range(pred_disps.shape[0]):
camera_id = camera_ids[i]
pred_disp = pred_disps[i]
pred_depth = 1 / pred_disp
if self.opt.focal:
pred_depth = pred_depth * data[("K", 0, 0)][i, 0, 0].item() / self.opt.focal_scale
depth_maps.append(pred_depth.astype(np.float16))
preds_list.append({sample_tokens: depth_maps})
num_val_multiview = int(self.num_val / 6)
preds_list_collect = collect_results_cpu(preds_list, num_val_multiview)
gts_list_collect = collect_results_cpu(gts_list, num_val_multiview)
if self.local_rank == 0:
assert len(preds_list_collect) == num_val_multiview, \
f'save dict not complete {len(preds_list_collect)} != {num_val_multiview}'
save_path = './pred'
if not os.path.exists(save_path):
os.makedirs(save_path)
with open(os.path.join(save_path, f'{self.opt.corruption}.pkl'), 'wb') as f:
pickle.dump(preds_list_collect, f)
self.set_train()
def to_device(self, inputs):
special_key_list = ['id']
match_key_list = ['match_spatial']
for key, ipt in inputs.items():
if key in special_key_list:
inputs[key] = ipt
elif key in match_key_list:
for i in range(len(inputs[key])):
inputs[key][i] = inputs[key][i].to(self.device)
else:
inputs[key] = ipt.to(self.device)
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:
writer.add_image(
"color_{}_{}/{}".format(frame_id, s, j),
inputs[("color", frame_id, s)][j].data, self.step)
if s == 0 and frame_id != 0:
writer.add_image(
"color_pred_{}_{}/{}".format(frame_id, s, j),
outputs[("color", frame_id, s)][j].data, self.step)
writer.add_image(
"disp_{}/{}".format(s, j),
normalize_image(outputs[("disp", s)][j]), self.step)
if self.opt.predictive_mask:
for f_idx, frame_id in enumerate(self.opt.frame_ids[1:]):
writer.add_image(
"predictive_mask_{}_{}/{}".format(frame_id, s, j),
outputs["predictive_mask"][("disp", s)][j, f_idx][None, ...],
self.step)
elif not self.opt.disable_automasking:
writer.add_image(
"automask_{}/{}".format(s, j),
outputs["identity_selection/{}".format(s)][j][None, ...], 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)
os.makedirs(os.path.join(self.log_path, "eval"), exist_ok=True)
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
"""
if self.local_rank == 0:
save_folder = os.path.join(self.log_path, "models", "weights_{}".format(self.step))
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.module.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)
if self.local_rank == 0:
assert os.path.isdir(self.opt.load_weights_folder), \
"Cannot find folder {}".format(self.opt.load_weights_folder)
self.log_print("loading model from folder {}".format(self.opt.load_weights_folder))
for n in self.opt.models_to_load:
if self.local_rank == 0:
self.log_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, map_location=torch.device('cpu'))
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)
def load_optimizer(self):
# loading adam state
optimizer_load_path = os.path.join(self.opt.load_weights_folder, "adam.pth")
if os.path.isfile(optimizer_load_path):
if self.local_rank == 0:
self.log_print("Loading Adam weights")
optimizer_dict = torch.load(optimizer_load_path)
self.model_optimizer.load_state_dict(optimizer_dict)
else:
self.log_print("Cannot find Adam weights so Adam is randomly initialized")
def log_print(self, str):
print(str)
with open(os.path.join(self.log_path, 'log.txt'), 'a') as f:
f.writelines(str + '\n')
def collect_results_cpu(result_part, size, tmpdir=None):
rank, world_size = get_dist_info()
# create a tmp dir if it is not specified
if tmpdir is None:
MAX_LEN = 512
# 32 is whitespace
dir_tensor = torch.full((MAX_LEN, ),
32,
dtype=torch.uint8,
device='cuda')
if rank == 0:
if not os.path.exists('.dist_test'):
os.makedirs('.dist_test')
tmpdir = tempfile.mkdtemp(dir='.dist_test')
tmpdir = torch.tensor(
bytearray(tmpdir.encode()), dtype=torch.uint8, device='cuda')
dir_tensor[:len(tmpdir)] = tmpdir
dist.broadcast(dir_tensor, 0)
tmpdir = dir_tensor.cpu().numpy().tobytes().decode().rstrip()
# dump the part result to the dir
with open(os.path.join(tmpdir, f'part_{rank}.pkl'), 'wb') as f:
pickle.dump(result_part, f)
dist.barrier()
# collect all parts
if rank != 0:
return None
else:
# load results of all parts from tmp dir
part_list = []
for i in range(world_size):
part_file = os.path.join(tmpdir, f'part_{i}.pkl')
with open(part_file, 'rb') as f:
part_list.append(pickle.load(f))
# sort the results
ordered_results = []
'''
bacause we change the sample of the evaluation stage to make sure that each gpu will handle continuous sample,
'''
#for res in zip(*part_list):
for res in part_list:
ordered_results.extend(list(res))
# the dataloader may pad some samples
ordered_results = ordered_results[:size]
# remove tmp dir
shutil.rmtree(tmpdir)
return ordered_results