-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
480 lines (374 loc) · 17.7 KB
/
trainer.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
import torch
import networks
import torch.optim as optim
from torch.autograd import Variable
from torch.utils.data import DataLoader
from misc.layers import *
from utils.layout_utils import *
from utils import *
import datasets
from tqdm.contrib import tenumerate
class Trainer:
def __init__(self, opt):
self.opt = opt
self.models = {}
self.criterion_d = nn.BCEWithLogitsLoss()
self.parameters_to_train = []
self.parameters_to_train_D = []
# Depth network - TO DO Depending on Network
# Pose network
self.models["pose"] = networks.PoseCNN(2)
# Layout network
self.models["layout_encoder"] = networks.LayoutEncoder(
18, self.opt.height, self.opt.width, True
)
self.models["layout_decoder"] = networks.LayoutDecoder(
self.models["layout_encoder"].resnet_encoder.num_ch_enc
)
self.models["discriminator"] = networks.Discriminator()
# Move to device
for key in self.models.keys():
self.models[key].to(self.device)
# Optimization
self.model_optimizer = optim.Adam(
self.parameters_to_train, self.opt.lr)
self.model_lr_scheduler = optim.lr_scheduler.StepLR(
self.model_optimizer, self.opt.scheduler_step_size, 0.1)
self.model_optimizer_D = optim.Adam(
self.parameters_to_train_D, self.opt.lr)
self.model_lr_scheduler_D = optim.lr_scheduler.StepLR(
self.model_optimizer_D, self.opt.scheduler_step_size, 0.1)
self.patch = (1, self.opt.occ_map_size // 2 **
4, self.opt.occ_map_size // 2**4)
self.valid = Variable(
torch.Tensor(
np.ones(
(self.opt.batch_size,
*self.patch))),
requires_grad=False).float().cuda()
self.fake = Variable(
torch.Tensor(
np.zeros(
(self.opt.batch_size,
*self.patch))),
requires_grad=False).float().cuda()
for key in self.models.keys():
self.models[key].to(self.device)
if "discr" in key:
self.parameters_to_train_D += list(
self.models[key].parameters())
else:
self.parameters_to_train += list(self.models[key].parameters())
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)
self.setup_datasets()
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"]
self.save_opts()
def setup_datasets(self, use_pred_depth=False):
# data
datasets_dict = {
"habitat": datasets.HabitatDataset
}
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 = '.jpg'
num_train_samples = len(train_filenames)
self.num_total_steps = num_train_samples // self.opt.batch_size * self.opt.num_epochs
depth_folder = None
if use_pred_depth:
depth_folder = self.opt.depth_folder
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, depth_folder=depth_folder)
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, depth_folder=depth_folder)
self.train_loader = DataLoader(
train_dataset, self.opt.batch_size, True,
num_workers=self.opt.num_workers, pin_memory=True, drop_last=True)
self.val_loader = DataLoader(
val_dataset, self.opt.batch_size, True,
num_workers=self.opt.num_workers, pin_memory=True, drop_last=True)
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)))
def process_batch(self, batch, batch_idx):
for key, inpt in batch.items():
batch[key] = inpt.to(self.device)
outputs = {}
# Layout forward pass
features = self.models["encoder"](batch["color_aug", 0, "l", 0])
outputs["topview"] = self.models["decoder"](features)
# Pose
outputs.update(self.predict_poses(batch, features))
self.generate_images_pred(batch, outputs)
# Compute losses
losses = self.compute_losses(batch, outputs)
losses["loss_discr"] = torch.zeros(1)
return outputs, losses
def train(self):
for self.epoch in range(self.opt.num_epochs):
loss = self.run_epoch()
print("Epoch: %d | Loss: %.4f | Discriminator Loss: %.4f" %
(self.epoch, loss["loss"], loss["loss_discr"]))
if self.epoch % self.opt.log_frequency == 0:
self.validation()
self.save_model()
def run_epoch(self):
self.model_optimizer.step()
self.model_optimizer_D.step()
loss = {}
loss["loss"], loss["loss_discr"] = 0.0, 0.0
for batch_idx, inputs in tenumerate(self.train_loader):
outputs, losses = self.process_batch(inputs, batch_idx)
self.model_optimizer.zero_grad()
fake_pred = self.models["discriminator"](outputs["topview"])
real_pred = self.models["discriminator"](inputs["discr"].float())
loss_GAN = self.criterion_d(fake_pred, self.valid)
loss_D = self.criterion_d(
fake_pred, self.fake) + self.criterion_d(real_pred, self.valid)
loss_G = self.opt.DISCRIMINATOR.lambda_D * loss_GAN + losses["loss"]
# Train Discriminator
if self.epoch > self.opt.DISCRIMINATOR.discr_train_epoch:
loss_G.backward(retain_graph=True)
self.model_optimizer.step()
self.model_optimizer_D.zero_grad()
loss_D.backward()
self.model_optimizer_D.step()
else:
losses["loss"].backward()
self.model_optimizer.step()
loss["loss"] += losses["loss"].item()
# print(losses['loss'].item(), flush =True)
loss["loss_discr"] += loss_D.item()
loss["loss"] /= len(self.train_loader)
loss["loss_discr"] /= len(self.train_loader)
return loss
def validation(self):
iou, mAP = np.array([0., 0.]), np.array([0., 0.])
for batch_idx, inputs in tenumerate(self.val_loader):
with torch.no_grad():
outputs = self.process_batch(inputs, True)
pred = np.squeeze(
torch.argmax(
outputs["topview"].detach(),
1).cpu().numpy())
true = np.squeeze(
inputs["static"].detach().cpu().numpy())
iou += mean_IU(pred, true)
mAP += mean_precision(pred, true)
iou /= len(self.val_loader)
mAP /= len(self.val_loader)
print(
"Epoch: %d | Validation: mIOU: %.4f mAP: %.4f" %
(self.epoch, iou[1], mAP[1]))
def predict_poses(self, inputs, features):
"""Predict poses between input frames for monocular sequences.
"""
outputs = {}
# Here we input all frames to the pose net (and predict all poses) together
pose_inputs = torch.cat(
[inputs[("color_aug", i, "l", 0)] for i in self.opt.frame_ids if i != "s"], 1)
axisangle, translation = self.models["pose"](pose_inputs)
for i, f_i in enumerate(self.opt.frame_ids[1:]):
if f_i != "s":
outputs[("axisangle", 0, f_i)] = axisangle
outputs[("translation", 0, f_i)] = translation
outputs[("cam_T_cam", 0, f_i)] = transformation_from_parameters(
axisangle[:, i], translation[:, i])
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)]
disp = F.interpolate(
disp, [self.opt.height, self.opt.width], mode="bilinear", align_corners=False)
source_scale = 0
_, depth = disp_to_depth(disp, self.opt.min_depth, self.opt.max_depth)
outputs[("depth", 0, scale)] = depth
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
# 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")
if not self.opt.disable_automasking:
outputs[("color_identity", frame_id, scale)] = \
inputs[("color", frame_id, source_scale)]
def compute_reprojection_loss(self, pred, target):
"""Computes reprojection loss between a batch of predicted and target images
"""
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):
"""
Computes pose error loss and the BEV topview loss
"""
losses = {}
total_loss = 0
# Top-view loss
losses["bev_loss"] = self.compute_topview_loss(
outputs["topview"],
inputs["static"],
self.weight[self.opt.type])
# Pose error
for scale in self.opt.scales:
loss = 0
reprojection_losses = []
if self.opt.v1_multiscale:
source_scale = scale
else:
source_scale = 0
disp = outputs[("disp", scale)]
color = inputs[("color", 0, "l", scale)]
target = inputs[("color", 0, source_scale)]
for frame_id in self.opt.frame_ids[1:]:
pred = outputs[("color", frame_id, "l", scale)]
reprojection_losses.append(self.compute_reprojection_loss(pred, target))
reprojection_losses = torch.cat(reprojection_losses, 1)
if not self.opt.disable_automasking:
identity_reprojection_losses = []
for frame_id in self.opt.frame_ids[1:]:
pred = inputs[("color", frame_id, "l", source_scale)]
identity_reprojection_losses.append(
self.compute_reprojection_loss(pred, target))
identity_reprojection_losses = torch.cat(identity_reprojection_losses, 1)
if self.opt.avg_reprojection:
identity_reprojection_loss = identity_reprojection_losses.mean(1, keepdim=True)
else:
# save both images, and do min all at once below
identity_reprojection_loss = identity_reprojection_losses
elif self.opt.predictive_mask:
# use the predicted mask
mask = outputs["predictive_mask"]["disp", scale]
if not self.opt.v1_multiscale:
mask = F.interpolate(
mask, [self.opt.height, self.opt.width],
mode="bilinear", align_corners=False)
reprojection_losses *= mask
# add a loss pushing mask to 1 (using nn.BCELoss for stability)
weighting_loss = 0.2 * nn.BCELoss()(mask, torch.ones(mask.shape).cuda())
loss += weighting_loss.mean()
if self.opt.avg_reprojection:
reprojection_loss = reprojection_losses.mean(1, keepdim=True)
else:
reprojection_loss = reprojection_losses
if not self.opt.disable_automasking:
# add random numbers to break ties
identity_reprojection_loss += torch.randn(
identity_reprojection_loss.shape).cuda() * 0.00001
combined = torch.cat((identity_reprojection_loss, reprojection_loss), dim=1)
else:
combined = reprojection_loss
if combined.shape[1] == 1:
to_optimise = combined
else:
to_optimise, idxs = torch.min(combined, dim=1)
if not self.opt.disable_automasking:
outputs["identity_selection/{}".format(scale)] = (
idxs > identity_reprojection_loss.shape[1] - 1).float()
loss += to_optimise.mean()
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 += 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 compute_topview_loss(self, outputs, true_top_view, weight):
generated_top_view = outputs
true_top_view = torch.squeeze(true_top_view.long())
loss = nn.CrossEntropyLoss(weight=torch.Tensor([1., weight]).cuda())
output = loss(generated_top_view, true_top_view)
return output.mean()
def save_model(self):
save_path = os.path.join(
self.opt.save_path,
self.opt.model_name,
self.opt.split,
"weights_{}".format(
self.epoch))
if not os.path.exists(save_path):
os.makedirs(save_path)
for model_name, model in self.models.items():
model_path = os.path.join(save_path, "{}.pth".format(model_name))
state_dict = model.state_dict()
if model_name == "encoder":
state_dict["height"] = self.opt.height
state_dict["width"] = self.opt.width
torch.save(state_dict, model_path)
optim_path = os.path.join(save_path, "{}.pth".format("adam"))
torch.save(self.model_optimizer.state_dict(), optim_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 key in self.models.keys():
print("Loading {} weights...".format(key))
path = os.path.join(
self.opt.load_weights_folder,
"{}.pth".format(key))
model_dict = self.models[key].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[key].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("Cannot find Adam weights so Adam is randomly initialized")