-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_routing.py
347 lines (277 loc) · 11.5 KB
/
train_routing.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
import torch
import argparse
import datetime
import random
import numpy as np
from tqdm import tqdm
from utils.loading import load_config_from_yaml
from utils.setup import *
from utils.loss import RoutingLoss
from modules.routing import ConfidenceRouting
import wandb
def arg_parser():
parser = argparse.ArgumentParser()
parser.add_argument("--config", required=True)
args = parser.parse_args()
return vars(args)
def prepare_input_data(batch, config, device):
for k, sensor_ in enumerate(config.DATA.input):
if k == 0:
inputs = batch[sensor_ + "_depth"].unsqueeze_(1)
else:
inputs = torch.cat((batch[sensor_ + "_depth"].unsqueeze_(1), inputs), 1)
inputs = inputs.to(device)
if config.ROUTING.intensity_grad:
intensity = batch["intensity"].unsqueeze_(1)
grad = batch["gradient"].unsqueeze_(1)
inputs = torch.cat((intensity, grad, inputs), 1)
inputs = inputs.to(device)
target = batch[config.DATA.target] # (batch size, height, width)
target = target.to(device)
target = target.unsqueeze_(1) # (batch size, channels, height, width)
return inputs, target
def train(args, config):
# set seed for reproducibility
if config.SETTINGS.seed:
random.seed(config.SETTINGS.seed)
np.random.seed(config.SETTINGS.seed)
torch.manual_seed(config.SETTINGS.seed)
torch.cuda.manual_seed_all(config.SETTINGS.seed)
torch.backends.cudnn.deterministic = True
torch.cuda.manual_seed_all(config.SETTINGS.seed)
torch.backends.cudnn.benchmark = False
if config.SETTINGS.gpu:
device = torch.device("cuda:0")
else:
device = torch.device("cpu")
config.TIMESTAMP = datetime.datetime.now().strftime("%y%m%d-%H%M%S")
print("model time stamp: ", config.TIMESTAMP)
# initialize weights and biases logging
wandb.init(
config=config,
entity="esandstroem",
project="senfunet-routing",
name=config.TIMESTAMP,
notes="put comment here",
)
# change run name of wandb
wandb.run.name = config.TIMESTAMP
wandb.run.save()
workspace = get_workspace(config)
workspace.save_config(config)
# get train dataset
train_data_config = get_data_config(config, mode="train")
train_dataset = get_data(config.DATA.dataset, train_data_config)
train_loader = torch.utils.data.DataLoader(
train_dataset, config.TRAINING.train_batch_size, config.TRAINING.train_shuffle
)
# get val dataset
val_data_config = get_data_config(config, mode="val")
val_dataset = get_data(config.DATA.dataset, val_data_config)
val_loader = torch.utils.data.DataLoader(
val_dataset, config.TRAINING.val_batch_size, config.TRAINING.val_shuffle
)
# define model
Cin = len(config.DATA.input)
if config.ROUTING.intensity_grad:
Cin += 2
model = ConfidenceRouting(
Cin=Cin, F=config.MODEL.contraction, batchnorms=config.MODEL.normalization
)
model = model.to(device)
# define loss function
criterion = RoutingLoss(config)
criterion = criterion.to(device)
# add weight and gradient tracking in wandb
wandb.watch(model, criterion, log="all", log_freq=1000)
# define optimizer
optimizer = torch.optim.RMSprop(
model.parameters(),
config.OPTIMIZATION.lr,
config.OPTIMIZATION.rho,
config.OPTIMIZATION.eps,
momentum=config.OPTIMIZATION.momentum,
weight_decay=config.OPTIMIZATION.weight_decay,
)
n_train_batches = int(len(train_dataset) / config.TRAINING.train_batch_size)
n_val_batches = int(len(val_dataset) / config.TRAINING.val_batch_size)
val_loss_best = np.infty
# sample validation visualization frames
val_vis_ids = np.random.choice(np.arange(0, n_val_batches), 5, replace=False)
# # define metrics
l1_criterion = torch.nn.L1Loss()
l2_criterion = torch.nn.MSELoss()
for epoch in range(0, config.TRAINING.n_epochs):
print("epoch: ", epoch)
val_loss_t = 0.0
val_loss_l1 = 0.0
val_loss_l2 = 0.0
train_loss_t = 0.0
train_loss_l1 = 0.0
train_loss_l2 = 0.0
train_epoch_loss_t = 0.0
train_epoch_loss_l1 = 0.0
train_epoch_loss_l2 = 0.0
# make ready for training and clear optimizer
model.train()
optimizer.zero_grad()
for i, batch in enumerate(tqdm(train_loader, total=n_train_batches)):
inputs, target = prepare_input_data(batch, config, device)
output = model(inputs)
est = output[:, 0, :, :].unsqueeze_(1)
unc = output[:, 1, :, :].unsqueeze_(1)
if not config.LOSS.completion:
if len(config.DATA.input) == 1:
mask = (
batch[config.DATA.input[0] + "_mask"].to(device).unsqueeze_(1)
)
else:
mask = batch["mask"].to(device).unsqueeze_(1)
target = torch.where(mask == 0.0, torch.zeros_like(target), target)
# compute training loss
loss = criterion.forward(est, unc, target)
loss.backward()
# compute metrics for analysis
loss_l1 = l1_criterion.forward(est, target)
loss_l2 = l2_criterion.forward(est, target)
train_loss_t += loss.item()
train_loss_l1 += loss_l1.item()
train_loss_l2 += loss_l2.item()
train_epoch_loss_t += loss.item()
train_epoch_loss_l1 += loss_l1.item()
train_epoch_loss_l2 += loss_l2.item()
if i % config.OPTIMIZATION.accumulation_steps == 0:
optimizer.step()
optimizer.zero_grad()
if i % config.SETTINGS.log_freq == 0 and i > 0:
# compute avg. loss per frame
train_loss_t /= (
config.SETTINGS.log_freq * config.TRAINING.train_batch_size
)
train_loss_l1 /= (
config.SETTINGS.log_freq * config.TRAINING.train_batch_size
)
train_loss_l2 /= (
config.SETTINGS.log_freq * config.TRAINING.train_batch_size
)
wandb.log(
{
"Train/total loss": train_loss_t,
"Train/l1 loss": train_loss_l1,
"Train/l2 loss": train_loss_l2,
"Train/nbr_frames": (epoch * n_train_batches + i)
* config.TRAINING.train_batch_size,
}
)
train_loss_t = 0
train_loss_l1 = 0
train_loss_l2 = 0
train_epoch_loss_t /= n_train_batches * config.TRAINING.train_batch_size
train_epoch_loss_l1 /= n_train_batches * config.TRAINING.train_batch_size
train_epoch_loss_l2 /= n_train_batches * config.TRAINING.train_batch_size
# log training metrics
workspace.log("Epoch {} Loss {}".format(epoch, train_epoch_loss_t))
workspace.log("Epoch {} L1 Loss {}".format(epoch, train_epoch_loss_l1))
workspace.log("Epoch {} L2 Loss {}".format(epoch, train_epoch_loss_l2))
model.eval()
for i, batch in enumerate(tqdm(val_loader, total=n_val_batches)):
inputs, target = prepare_input_data(batch, config, device)
output = model(inputs)
est = output[:, 0, :, :].unsqueeze_(1)
unc = output[:, 1, :, :].unsqueeze_(1)
# visualize frames
if i in val_vis_ids:
# parse frames and normalize to range 0-1
frame_est = est[0, :, :, :].cpu().detach().numpy().reshape(512, 512, 1)
frame_est /= np.amax(frame_est)
frame_gt = (
target[0, :, :, :].cpu().detach().numpy().reshape(512, 512, 1)
)
frame_gt /= np.amax(frame_gt)
frame_unc = unc[0, :, :, :].cpu().detach().numpy().reshape(512, 512, 1)
frame_conf = np.exp(-1.0 * frame_unc)
frame_unc /= np.amax(frame_unc)
frame_l1 = np.abs(frame_est - frame_gt).reshape(512, 512, 1)
frame_l1 /= np.amax(frame_l1)
wandb.log(
{
"Val/images": [
wandb.Image(
frame_est,
caption="depth estimate {}".format(i),
),
wandb.Image(frame_gt, caption="gt depth {}".format(i)),
wandb.Image(
frame_unc,
caption="uncertainty estimate {}".format(i),
),
wandb.Image(
frame_conf,
caption="confidence estimate {}".format(i),
),
wandb.Image(
frame_l1,
caption="l1 depth error {}".format(i),
),
]
}
)
if not config.LOSS.completion:
if len(config.DATA.input) == 1:
mask = (
batch[config.DATA.input[0] + "_mask"].to(device).unsqueeze_(1)
)
else:
mask = batch["mask"].to(device).unsqueeze_(1)
target = torch.where(mask == 0.0, torch.zeros_like(target), target)
loss_t = criterion.forward(est, unc, target)
loss_l1 = l1_criterion.forward(est, target)
loss_l2 = l2_criterion.forward(est, target)
val_loss_t += loss_t.item()
val_loss_l1 += loss_l1.item()
val_loss_l2 += loss_l2.item()
val_loss_t /= n_val_batches * config.TRAINING.train_batch_size
val_loss_l1 /= n_val_batches * config.TRAINING.train_batch_size
val_loss_l2 /= n_val_batches * config.TRAINING.train_batch_size
# log validation metrics
workspace.log(
"Epoch {} Validation Loss {}".format(epoch, val_loss_t), mode="val"
)
workspace.log(
"Epoch {} Validation L1 Loss {}".format(epoch, val_loss_l1), mode="val"
)
workspace.log(
"Epoch {} Validation L2 Loss {}".format(epoch, val_loss_l2), mode="val"
)
wandb.log(
{
"Val/total loss": val_loss_t,
"Val/l1 loss": val_loss_l1,
"Val/l2 loss": val_loss_l2,
"Val/epoch": epoch,
}
)
# define model state for storing
model_state = {
"epoch": epoch,
"pipeline_state_dict": model.state_dict(),
"optimizer_state_dict": optimizer.state_dict(),
}
if val_loss_t <= val_loss_best:
val_loss_best = val_loss_t
workspace.log(
"Found new best model with loss {} at epoch {}".format(
val_loss_best, epoch
),
mode="val",
)
workspace.save_model_state(model_state, is_best=True)
else:
workspace.save_model_state(model_state, is_best=False)
if __name__ == "__main__":
# get arguments
args = arg_parser()
# get configs
config = load_config_from_yaml(args["config"])
# train
train(args, config)