-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.py
377 lines (279 loc) · 13.1 KB
/
main.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 25 14:16:29 2019
@author: abdel62
"""
import os
import sys
import importlib
import time
import datetime
import torch
from torch.optim import SGD, Adam
from torch.optim import lr_scheduler
from torch.utils.tensorboard import SummaryWriter
from utils.args_parser import args_parser, save_args, print_args, initialize_args, compare_args_w_json
from dataloaders.dataloader_creator import create_dataloader
from utils.error_metrics import AverageMeter, create_error_metric, LogFile
from utils.save_output_images import create_out_image_saver, colored_depthmap_tensor
from utils.checkpoints import save_checkpoint
from common.losses import get_loss_fn
from utils.eval_uncertainty import eval_ause
def main():
# Make some variable global
global args, train_csv, test_csv, exp_dir, best_result, device, tb_writer, tb_freq
# Args parser
args = args_parser()
start_epoch = 0
############ EVALUATE MODE ############
if args.evaluate: # Evaluate mode
print('\n==> Evaluation mode!')
# Define paths
chkpt_path = args.evaluate
# Check that the checkpoint file exist
assert os.path.isfile(chkpt_path), "- No checkpoint found at: {}".format(chkpt_path)
# Experiment director
exp_dir = os.path.dirname(os.path.abspath(chkpt_path))
sys.path.append(exp_dir)
# Load checkpoint
print('- Loading checkpoint:', chkpt_path)
# Load the checkpoint
checkpoint = torch.load(chkpt_path)
# Assign some local variables
args = checkpoint['args']
start_epoch = checkpoint['epoch']
best_result = checkpoint['best_result']
print('- Checkpoint was loaded successfully.')
# Compare the checkpoint args with the json file in case I wanted to change some args
compare_args_w_json(args, exp_dir, start_epoch+1)
args.evaluate = chkpt_path
device = torch.device("cuda:" + str(args.gpu) if torch.cuda.is_available() else "cpu")
model = checkpoint['model'].to(device)
print_args(args)
_, val_loader = create_dataloader(args, eval_mode=True)
loss = get_loss_fn(args).to(device)
evaluate_epoch(val_loader, model, loss, start_epoch)
return # End program
############ RESUME MODE ############
elif args.resume: # Resume mode
print('\n==> Resume mode!')
# Define paths
chkpt_path = args.resume
assert os.path.isfile(chkpt_path), "- No checkpoint found at: {}".format(chkpt_path)
# Experiment directory
exp_dir = os.path.dirname(os.path.abspath(chkpt_path))
sys.path.append(exp_dir)
# Load checkpoint
print('- Loading checkpoint:', chkpt_path)
checkpoint = torch.load(chkpt_path)
args = checkpoint['args']
start_epoch = checkpoint['epoch'] + 1
best_result = checkpoint['best_result']
print('- Checkpoint ({}) was loaded successfully!\n'.format(checkpoint['epoch']))
# Compare the checkpoint args with the json file in case I wanted to change some args
compare_args_w_json(args, exp_dir, start_epoch)
args.resume = chkpt_path
device = torch.device("cuda:" + str(args.gpu) if torch.cuda.is_available() else "cpu")
model = checkpoint['model'].to(device)
optimizer = checkpoint['optimizer']
print_args(args)
train_loader, val_loader = create_dataloader(args, eval_mode=False)
############ NEW EXP MODE ############
else: # New Exp
print('\n==> Starting a new experiment "{}" \n'.format(args.exp))
# Check if experiment exists
ws_path = os.path.join('workspace/', args.workspace)
exp = args.exp
exp_dir = os.path.join(ws_path, exp)
assert os.path.isdir(exp_dir), '- Experiment "{}" not found!'.format(exp)
# Which device to use
device = torch.device("cuda:" + str(args.gpu) if torch.cuda.is_available() else "cpu")
# Add the experiment's folder to python path
sys.path.append(exp_dir)
print_args(args)
# Create dataloader
train_loader, val_loader = create_dataloader(args, eval_mode=False)
# import the model
f = importlib.import_module('network')
model = f.CNN().to(device)
print('\n==> Model "{}" was loaded successfully!'.format(model.__name__))
# Optimize only parameters that requires_grad
parameters = filter(lambda p: p.requires_grad, model.parameters())
# Create Optimizer
if args.optimizer.lower() == 'sgd':
optimizer = SGD(parameters, lr=args.lr, momentum=args.momentum,
weight_decay=args.weight_decay)
elif args.optimizer.lower() == 'adam':
optimizer = Adam(parameters, lr=args.lr, weight_decay=args.weight_decay, amsgrad=True)
############ IF RESUME/NEW EXP ############
# Error metrics that are set to the worst
best_result = create_error_metric(args)
best_result.set_to_worst()
# Tensorboard
tb = args.tb_log if hasattr(args, 'tb_log') else False
tb_freq = args.tb_freq if hasattr(args, 'tb_freq') else 1000
tb_writer = None
if tb:
tb_writer = SummaryWriter(os.path.join(exp_dir, 'tb_log', datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')))
# Create Loss
loss = get_loss_fn(args).to(device)
# Define Learning rate decay
lr_decayer = lr_scheduler.StepLR(optimizer, step_size=args.lr_decay_step, gamma=args.lr_decay_factor, last_epoch=start_epoch-1)
# Create or Open Logging files
train_csv = LogFile(os.path.join(exp_dir, 'train.csv'), args)
test_csv = LogFile(os.path.join(exp_dir, 'test.csv'), args)
best_txt = os.path.join(exp_dir, 'best.txt')
save_args(exp_dir, args) # Save args to JSON file
############ TRAINING LOOP ############
for epoch in range(start_epoch, args.epochs):
print('\n==> Training Epoch [{}] (lr={})'.format(epoch, optimizer.param_groups[0]['lr']))
train_err_avg = train_epoch(train_loader, model, optimizer, loss, epoch)
# Learning rate scheduler
lr_decayer.step()
train_csv.update_log(train_err_avg, epoch)
# Save checkpoint in case evaluation crashed
save_checkpoint({
'args': args,
'epoch': epoch,
'model': model,
'best_result': best_result,
'optimizer': optimizer,
}, False, epoch, exp_dir)
# Evaluate the trained epoch
test_err_avg, out_image = evaluate_epoch(val_loader, model, loss, epoch) # evaluate on validation set
# Evaluate Uncerainty
ause = None
if args.eval_uncert:
if args.loss == 'masked_prob_loss_var':
ause, ause_fig = eval_ause(model, val_loader, args, epoch, uncert_type='v')
else:
ause, ause_fig = eval_ause(model, val_loader, args, epoch, uncert_type='c')
# Log to tensorboard if enabled
if tb_writer is not None:
avg_meter = test_err_avg.get_avg()
tb_writer.add_scalar('Loss/val', avg_meter.loss, epoch)
tb_writer.add_scalar('MAE/val', avg_meter.metrics['mae'], epoch)
tb_writer.add_scalar('RMSE/val', avg_meter.metrics['rmse'], epoch)
if ause is not None:
tb_writer.add_scalar('AUSE/val', ause, epoch)
tb_writer.add_images('Prediction', colored_depthmap_tensor(out_image[:, :1, :, :]), epoch)
tb_writer.add_images('Input_Conf_Log_Scale', colored_depthmap_tensor(torch.log(out_image[:, 2:, :, :]+1)), epoch)
tb_writer.add_images('Output_Conf_Log_Scale', colored_depthmap_tensor(torch.log(out_image[:, 1:2, :, :]+1)), epoch)
tb_writer.add_figure('Sparsification_Plot', ause_fig, epoch)
# Update Log files
test_csv.update_log(test_err_avg, epoch, ause)
# Save best model
# TODO: How to decide the best based on dataset?
is_best = test_err_avg.metrics['rmse'] < best_result.metrics['rmse']
if is_best:
best_result = test_err_avg # Save the new best locally
test_err_avg.print_to_txt(best_txt, epoch) # Print to a text file
# Save it again if it is best checkpoint
save_checkpoint({
'args': args,
'epoch': epoch,
'model': model,
'best_result': best_result,
'optimizer': optimizer,
}, is_best, epoch, exp_dir)
# TODO: Do you really need to save the best out_image ??
############ TRAINING FUNCTION ############
def train_epoch(dataloader, model, optimizer, objective, epoch):
"""
Training function
Args:
dataloader: The dataloader object for the dataset
model: The model to be trained
optimizer: The optimizer to be used
objective: The objective function
epoch: What epoch to start from
Returns:
AverageMeter() object.
Raises:
KeyError: Raises an exception.
"""
err = create_error_metric(args)
err_avg = AverageMeter(err.get_metrics()) # Accumulator for the error metrics
model.train() # switch to train mode
start = time.time()
for i, (input, target) in enumerate(dataloader):
input, target = input.to(device), target.to(device)
torch.cuda.synchronize() # Wait for all kernels to finish
data_time = time.time() - start
start = time.time()
optimizer.zero_grad() # Clear the gradients
# Forward pass
out = model(input)
loss = objective(out, target) # Compute the loss
# Backward pass
loss.backward()
optimizer.step() # Update the parameters
gpu_time = time.time() - start
# Calculate Error metrics
err = create_error_metric(args)
err.evaluate(out[:, :1, :, :].data, target.data)
err_avg.update(err.get_results(), loss.item(), gpu_time, data_time, input.size(0))
if (i + 1) % args.print_freq == 0 or i == len(dataloader)-1:
print('[Train] Epoch ({}) [{}/{}]: '.format(
epoch, i+1, len(dataloader)), end='')
print(err_avg)
# Log to Tensorboard if enabled
if tb_writer is not None:
if (i + 1) % tb_freq == 0:
avg_meter = err_avg.get_avg()
tb_writer.add_scalar('Loss/train', avg_meter.loss, epoch * len(dataloader) + i)
tb_writer.add_scalar('MAE/train', avg_meter.metrics['mae'], epoch * len(dataloader) + i)
tb_writer.add_scalar('RMSE/train', avg_meter.metrics['rmse'], epoch * len(dataloader) + i)
start = time.time() # Start counting again for the next iteration
return err_avg
############ EVALUATION FUNCTION ############
def evaluate_epoch(dataloader, model, objective, epoch):
"""
Evluation function
Args:
dataloader: The dataloader object for the dataset
model: The model to be trained
epoch: What epoch to start from
Returns:
AverageMeter() object.
Raises:
KeyError: Raises an exception.
"""
print('\n==> Evaluating Epoch [{}]'.format(epoch))
err = create_error_metric(args)
err_avg = AverageMeter(err.get_metrics()) # Accumulator for the error metrics
model.eval() # Swith to evaluate mode
# Save output images
out_img_saver = create_out_image_saver(exp_dir, args, epoch)
out_image = None
start = time.time()
with torch.no_grad(): # Disable gradients computations
for i, (input, target) in enumerate(dataloader):
input, target = input.to(device), target.to(device)
torch.cuda.synchronize()
data_time = time.time() - start
# Forward Pass
start = time.time()
out = model(input)
# Check if there is cout There is Cout
loss = objective(out, target) # Compute the loss
gpu_time = time.time() - start
# Calculate Error metrics
err = create_error_metric(args)
err.evaluate(out[:, :1, :, :].data, target.data)
err_avg.update(err.get_results(), loss.item(), gpu_time, data_time, input.size(0))
# Save output images
if args.save_val_imgs:
out_image = out_img_saver.update(i, out_image, input, out, target)
if args.evaluate is None:
if tb_writer is not None and i == 1: # Retrun batch 1 for tensorboard logging
out_image = out
if (i + 1) % args.print_freq == 0 or i == len(dataloader)-1:
print('[Eval] Epoch: ({0}) [{1}/{2}]: '.format(
epoch, i + 1, len(dataloader)), end='')
print(err_avg)
start = time.time()
return err_avg, out_image
if __name__ == '__main__':
main()