-
Notifications
You must be signed in to change notification settings - Fork 20
/
execute_func_multi.py
366 lines (293 loc) · 15.2 KB
/
execute_func_multi.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
from __future__ import absolute_import, division, print_function
import numpy as np
import os
import torch
import torch.nn.functional as F
from torch.utils.data import DataLoader
torch.backends.cudnn.benchmark = True
from utils import *
from layers import *
import datasets
import networks
class Trainer_multi:
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.device = torch.device("cpu" if self.opt.no_cuda else "cuda")
self.num_scales = len(self.opt.scales)
self.num_input_frames = len(self.opt.frame_ids)
self.num_pose_frames = 2
assert self.opt.frame_ids[0] == 0, "frame_ids must start with 0"
assert len(self.opt.frame_ids) > 1, "frame_ids must have more than 1 frame specified"
print('using adaptive depth binning!')
self.min_depth_tracker = 0.1
self.max_depth_tracker = 10.0
# check the frames we need the dataloader to load
frames_to_load = self.opt.frame_ids.copy()
self.matching_ids = [0]
if self.opt.use_future_frame:
self.matching_ids.append(1)
for idx in range(-1, -1 - self.opt.num_matching_frames, -1):
self.matching_ids.append(idx)
if idx not in frames_to_load:
frames_to_load.append(idx)
print('Loading frames: {}'.format(frames_to_load))
# MODEL SETUP
self.models["encoder"] = networks.ResnetEncoderMatching(
self.opt.num_layers, self.opt.weights_init == "pretrained",
input_height=self.opt.height, input_width=self.opt.width,
adaptive_bins=True, min_depth_bin=0.1, max_depth_bin=20.0,
depth_binning=self.opt.depth_binning, num_depth_bins=self.opt.num_depth_bins)
self.models["encoder"].to(self.device)
self.models["depth"] = networks.DepthDecoder(
self.models["encoder"].num_ch_enc, self.opt.scales)
self.models["depth"].to(self.device)
self.models["mono_encoder"] = \
networks.ResnetEncoder(18, self.opt.weights_init == "pretrained")
self.models["mono_encoder"].to(self.device)
self.models["mono_depth"] = \
networks.DepthDecoder(self.models["mono_encoder"].num_ch_enc, self.opt.scales)
self.models["mono_depth"].to(self.device)
self.models["pose_encoder"] = \
networks.ResnetEncoder(18, self.opt.weights_init == "pretrained",
num_input_images=self.num_pose_frames)
self.models["pose_encoder"].to(self.device)
self.models["pose"] = \
networks.PoseDecoder(self.models["pose_encoder"].num_ch_enc,
num_input_features=1,
num_frames_to_predict_for=2)
self.models["pose"].to(self.device)
if self.opt.load_weights_folder is not None:
self.load_model_multi()
self.dataset = datasets.VADataset
fpath = os.path.join(self.opt.data_path, "{}.txt")
val_filenames = readlines(fpath.format("UE4_left_all"))
val_dataset = self.dataset(
self.opt.data_path, val_filenames, self.opt.height, self.opt.width,
frames_to_load, 4, is_train=False)
self.val_loader = DataLoader(
val_dataset, self.opt.batch_size, False,
num_workers=8, pin_memory=False, drop_last=True)
self.val_iter = iter(self.val_loader)
self.depth_metric_names = [
"de/abs_mn", "de/abs_rel", "de/sq_rel", "de/rms", "de/log_rms", "da/a1", "da/a2", "da/a3"]
print("In reference mode! There are {:d} samples\n".format(len(val_dataset)))
def set_eval(self):
"""Convert all models to testing/evaluation mode
"""
for m in self.models.values():
m.eval()
def eval_measure_multi(self):
self.set_eval()
self.abs_mn = AverageMeter('abs_mean')
self.abs_rel = AverageMeter('abs_rel')
self.sq_rel = AverageMeter('sq_rel')
self.rms = AverageMeter('rms')
self.log_rms = AverageMeter('log_rms')
self.a1 = AverageMeter('a1')
self.a2 = AverageMeter('a2')
self.a3 = AverageMeter('a3')
N = self.opt.batch_size
#count = 0
while True:
try:
inputs = self.val_iter.__next__()
except StopIteration:
break
with torch.no_grad():
outputs, losses = self.process_batch(inputs)
if "depth_gt" in inputs:
self.compute_depth_errors_VA(inputs, outputs, losses)
self.abs_mn.update(losses['de/abs_mn'], N)
self.abs_rel.update(losses['de/abs_rel'], N)
self.sq_rel.update(losses['de/sq_rel'], N)
self.rms.update(losses['de/rms'], N)
self.log_rms.update(losses['de/log_rms'], N)
self.a1.update(losses['da/a1'], N)
self.a2.update(losses['da/a2'], N)
self.a3.update(losses['da/a3'], N)
del inputs, outputs, losses
f = open('evaluation-ckpts-distdepth-101-SimSIN-DPTLegacy-Multi.txt','w')
all_errors = [self.abs_mn, self.abs_rel, self.sq_rel, self.rms, self.log_rms, self.a1, self.a2, self.a3]
for comp in all_errors:
f.write(str(comp))
f.close()
def compute_depth_errors_VA(self, inputs, outputs, losses):
"""
compute depth errors on VA
"""
depth_pred = outputs[("depth", 0, 0)]
depth_pred = torch.clamp(F.interpolate(
depth_pred, [640, 640], mode="bilinear", align_corners=False), 1e-3, 10)
depth_pred = depth_pred.detach()
depth_gt = inputs["depth_gt"]
mask = depth_gt > 0.01
mask = torch.logical_and(depth_gt > 0.001, depth_gt<=10.0)
depth_gt = depth_gt[mask]
depth_pred = depth_pred[mask]
depth_pred *= torch.median(depth_gt) / torch.median(depth_pred)
depth_pred = torch.clamp(depth_pred, min=1e-3, max=10.0)
depth_errors = compute_depth_errors(depth_gt, depth_pred)
if losses is None:
losses = {}
for i, metric in enumerate(self.depth_metric_names):
losses[metric] = np.array(depth_errors[i].cpu())
def predict_poses(self, inputs):
"""Predict poses between input frames for monocular sequences.
"""
outputs = {}
if self.num_pose_frames == 2:
# In this setting, we compute the pose to each source frame via a
# separate forward pass through the pose network.
# predict poses for reprojection loss
# select what features the pose network takes as input
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":
# To maintain ordering we always pass frames in temporal order
if f_i < 0:
pose_inputs = [pose_feats[f_i], pose_feats[0]]
else:
pose_inputs = [pose_feats[0], pose_feats[f_i]]
pose_inputs = [self.models["pose_encoder"](torch.cat(pose_inputs, 1))]
axisangle, translation = self.models["pose"](pose_inputs)
outputs[("axisangle", 0, f_i)] = axisangle
outputs[("translation", 0, f_i)] = translation
# Invert the matrix if the frame id is negative
outputs[("cam_T_cam", 0, f_i)] = transformation_from_parameters(
axisangle[:, 0], translation[:, 0], invert=(f_i < 0))
# now we need poses for matching - compute without gradients
pose_feats = {f_i: inputs["color_aug", f_i, 0] for f_i in self.matching_ids}
with torch.no_grad():
# compute pose from 0->-1, -1->-2, -2->-3 etc and multiply to find 0->-3
for fi in self.matching_ids[1:]:
if fi < 0:
pose_inputs = [pose_feats[fi], pose_feats[fi + 1]]
pose_inputs = [self.models["pose_encoder"](torch.cat(pose_inputs, 1))]
axisangle, translation = self.models["pose"](pose_inputs)
pose = transformation_from_parameters(
axisangle[:, 0], translation[:, 0], invert=True)
# now find 0->fi pose
if fi != -1:
pose = torch.matmul(pose, inputs[('relative_pose', fi + 1)])
else:
pose_inputs = [pose_feats[fi - 1], pose_feats[fi]]
pose_inputs = [self.models["pose_encoder"](torch.cat(pose_inputs, 1))]
axisangle, translation = self.models["pose"](pose_inputs)
pose = transformation_from_parameters(
axisangle[:, 0], translation[:, 0], invert=False)
# now find 0->fi pose
if fi != 1:
pose = torch.matmul(pose, inputs[('relative_pose', fi - 1)])
# set missing images to 0 pose
for batch_idx, feat in enumerate(pose_feats[fi]):
if feat.sum() == 0:
pose[batch_idx] *= 0
inputs[('relative_pose', fi)] = pose
else:
raise NotImplementedError
return outputs
def process_batch(self, inputs, is_train=False):
"""Pass a minibatch through the network and generate images and losses
"""
for key, ipt in inputs.items():
inputs[key] = ipt.to(self.device)
mono_outputs = {}
outputs = {}
with torch.no_grad():
pose_pred = self.predict_poses(inputs)
outputs.update(pose_pred)
mono_outputs.update(pose_pred)
# grab poses + frames and stack for input to the multi frame network
relative_poses = [inputs[('relative_pose', idx)] for idx in self.matching_ids[1:]]
relative_poses = torch.stack(relative_poses, 1)
lookup_frames = [inputs[('color_aug', idx, 0)] for idx in self.matching_ids[1:]]
lookup_frames = torch.stack(lookup_frames, 1) # batch x frames x 3 x h x w
min_depth_bin = self.min_depth_tracker
max_depth_bin = self.max_depth_tracker
# single frame path
with torch.no_grad():
feats = self.models["mono_encoder"](inputs["color_aug", 0, 0])
mono_outputs.update(self.models['mono_depth'](feats))
self.generate_images_pred(inputs, mono_outputs)
# update multi frame outputs dictionary with single frame outputs
for key in list(mono_outputs.keys()):
_key = list(key)
if _key[0] in ['depth', 'disp']:
_key[0] = 'mono_' + key[0]
_key = tuple(_key)
outputs[_key] = mono_outputs[key]
# multi frame path
features, _, _ = self.models["encoder"](inputs["color_aug", 0, 0],
lookup_frames,
relative_poses,
inputs[('K', 2)],
inputs[('inv_K', 2)],
min_depth_bin=min_depth_bin,
max_depth_bin=max_depth_bin)
outputs.update(self.models["depth"](features))
self.generate_images_pred(inputs, outputs, is_multi=True)
losses = {}
return outputs, losses
def generate_images_pred(self, inputs, outputs, is_multi=False):
for scale in self.opt.scales:
disp = outputs[("out", scale)]
disp = F.interpolate(
disp, [self.opt.height, self.opt.width], mode="bilinear", align_corners=False)
depth = output_to_depth(disp, self.opt.min_depth, self.opt.max_depth)
outputs[("depth", 0, scale)] = depth
def load_mono_model(self):
model_list = ['pose_encoder', 'pose', 'mono_encoder', 'mono_depth']
for n in model_list:
print('loading {}'.format(n))
path = os.path.join(self.opt.mono_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)
def load_model_multi(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)
if n == 'encoder':
min_depth_bin = pretrained_dict.get('min_depth_bin')
max_depth_bin = pretrained_dict.get('max_depth_bin')
print('min depth', min_depth_bin, 'max_depth', max_depth_bin)
if min_depth_bin is not None:
# recompute bins
print('setting depth bins!')
self.models['encoder'].compute_depth_bins(min_depth_bin, max_depth_bin)
self.min_depth_tracker = min_depth_bin
self.max_depth_tracker = max_depth_bin
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):
# try:
# print("Loading Adam weights")
# optimizer_dict = torch.load(optimizer_load_path)
# self.model_optimizer.load_state_dict(optimizer_dict)
# except ValueError:
# print("Can't load Adam - using random")
# else:
# print("Cannot find Adam weights so Adam is randomly initialized")