-
Notifications
You must be signed in to change notification settings - Fork 67
/
train.py
357 lines (235 loc) · 10.7 KB
/
train.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
import os
from datetime import datetime
from argparse import ArgumentParser
from tqdm import tqdm
import torch
import torch.nn as nn
from torch.optim import SGD
from torch.optim.lr_scheduler import MultiStepLR
from torch.utils.tensorboard import SummaryWriter
from src.models.model_factory import build_model, build_criterion
from src.data.data_factory import build_dataloaders
from src.utils.configs import get_default_configuration, load_config
from src.utils.confusion import BinaryConfusionMatrix
from src.data.nuscenes.utils import NUSCENES_CLASS_NAMES
from src.data.argoverse.utils import ARGOVERSE_CLASS_NAMES
from src.utils.visualise import colorise
def train(dataloader, model, criterion, optimiser, summary, config, epoch):
model.train()
# Compute prior probability of occupancy
prior = torch.tensor(config.prior)
prior_log_odds = torch.log(prior / (1 - prior))
# Initialise confusion matrix
confusion = BinaryConfusionMatrix(config.num_class)
# Iterate over dataloader
iteration = (epoch - 1) * len(dataloader)
for i, batch in enumerate(tqdm(dataloader)):
# Move tensors to GPU
if len(config.gpus) > 0:
batch = [t.cuda() for t in batch]
# Predict class occupancy scores and compute loss
image, calib, labels, mask = batch
if config.model == 'ved':
logits, mu, logvar = model(image)
loss = criterion(logits, labels, mask, mu, logvar)
else:
logits = model(image, calib)
loss = criterion(logits, labels, mask)
# Compute gradients and update parameters
optimiser.zero_grad()
loss.backward()
optimiser.step()
# Update confusion matrix
scores = logits.cpu().sigmoid()
confusion.update(scores > config.score_thresh, labels, mask)
# Update tensorboard
if i % config.log_interval == 0:
summary.add_scalar('train/loss', float(loss), iteration)
# Visualise
if i % config.vis_interval == 0:
visualise(summary, image, scores, labels, mask, iteration,
config.train_dataset, split='train')
iteration += 1
# Print and record results
display_results(confusion, config.train_dataset)
log_results(confusion, config.train_dataset, summary, 'train', epoch)
def evaluate(dataloader, model, criterion, summary, config, epoch):
model.eval()
# Compute prior probability of occupancy
prior = torch.tensor(config.prior)
prior_log_odds = torch.log(prior / (1 - prior))
# Initialise confusion matrix
confusion = BinaryConfusionMatrix(config.num_class)
# Iterate over dataset
for i, batch in enumerate(tqdm(dataloader)):
# Move tensors to GPU
if len(config.gpus) > 0:
batch = [t.cuda() for t in batch]
# Predict class occupancy scores and compute loss
image, calib, labels, mask = batch
with torch.no_grad():
if config.model == 'ved':
logits, mu, logvar = model(image)
loss = criterion(logits, labels, mask, mu, logvar)
else:
logits = model(image, calib)
loss = criterion(logits, labels, mask)
# Update confusion matrix
scores = logits.cpu().sigmoid()
confusion.update(scores > config.score_thresh, labels, mask)
# Update tensorboard
if i % config.log_interval == 0:
summary.add_scalar('val/loss', float(loss), epoch)
# Visualise
if i % config.vis_interval == 0:
visualise(summary, image, scores, labels, mask, epoch,
config.train_dataset, split='val')
# Print and record results
display_results(confusion, config.train_dataset)
log_results(confusion, config.train_dataset, summary, 'val', epoch)
return confusion.mean_iou
def visualise(summary, image, scores, labels, mask, step, dataset, split):
class_names = NUSCENES_CLASS_NAMES if dataset == 'nuscenes' \
else ARGOVERSE_CLASS_NAMES
summary.add_image(split + '/image', image[0], step, dataformats='CHW')
summary.add_image(split + '/pred', colorise(scores[0], 'coolwarm', 0, 1),
step, dataformats='NHWC')
summary.add_image(split + '/gt', colorise(labels[0], 'coolwarm', 0, 1),
step, dataformats='NHWC')
# for i, name in enumerate(class_names):
# summary.add_image(split + '/pred/' + name, scores[0, i], step,
# dataformats='HW')
# summary.add_image(split + '/gt/' + name, labels[0, i], step,
# dataformats='HW')
# summary.add_image(split + '/mask', mask[0], step, dataformats='HW')
def display_results(confusion, dataset):
# Display confusion matrix summary
class_names = NUSCENES_CLASS_NAMES if dataset == 'nuscenes' \
else ARGOVERSE_CLASS_NAMES
print('\nResults:')
for name, iou_score in zip(class_names, confusion.iou):
print('{:20s} {:.3f}'.format(name, iou_score))
print('{:20s} {:.3f}'.format('MEAN', confusion.mean_iou))
def log_results(confusion, dataset, summary, split, epoch):
# Display and record epoch IoU scores
class_names = NUSCENES_CLASS_NAMES if dataset == 'nuscenes' \
else ARGOVERSE_CLASS_NAMES
for name, iou_score in zip(class_names, confusion.iou):
summary.add_scalar(f'{split}/iou/{name}', iou_score, epoch)
summary.add_scalar(f'{split}/iou/MEAN', confusion.mean_iou, epoch)
def save_checkpoint(path, model, optimizer, scheduler, epoch, best_iou):
if isinstance(model, nn.DataParallel):
model = model.module
ckpt = {
'model' : model.state_dict(),
'optimizer' : optimizer.state_dict(),
'scheduler' : scheduler.state_dict(),
'epoch' : epoch,
'best_iou' : best_iou
}
torch.save(ckpt, path)
def load_checkpoint(path, model, optimizer, scheduler):
ckpt = torch.load(path)
# Load model weights
if isinstance(model, nn.DataParallel):
model = model.module
model.load_state_dict(ckpt['model'])
# Load optimiser state
optimizer.load_state_dict(ckpt['optimizer'])
# Load scheduler state
scheduler.load_state_dict(ckpt['scheduler'])
return ckpt['epoch'], ckpt['best_iou']
# Load the configuration for this experiment
def get_configuration(args):
# Load config defaults
config = get_default_configuration()
# Load dataset options
config.merge_from_file(f'configs/datasets/{args.dataset}.yml')
# Load model options
config.merge_from_file(f'configs/models/{args.model}.yml')
# Load experiment options
config.merge_from_file(f'configs/experiments/{args.experiment}.yml')
# Restore config from an existing experiment
if args.resume is not None:
config.merge_from_file(os.path.join(args.resume, 'config.yml'))
# Override with command line options
config.merge_from_list(args.options)
# Finalise config
config.freeze()
return config
def create_experiment(config, tag, resume=None):
# Restore an existing experiment if a directory is specified
if resume is not None:
print("\n==> Restoring experiment from directory:\n" + resume)
logdir = resume
else:
# Otherwise, generate a run directory based on the current time
name = datetime.now().strftime('{}_%y-%m-%d--%H-%M-%S').format(tag)
logdir = os.path.join(os.path.expandvars(config.logdir), name)
print("\n==> Creating new experiment in directory:\n" + logdir)
os.makedirs(logdir)
# Display the config options on-screen
print(config.dump())
# Save the current config
with open(os.path.join(logdir, 'config.yml'), 'w') as f:
f.write(config.dump())
return logdir
def main():
parser = ArgumentParser()
parser.add_argument('--tag', type=str, default='run',
help='optional tag to identify the run')
parser.add_argument('--dataset', choices=['nuscenes', 'argoverse'],
default='nuscenes', help='dataset to train on')
parser.add_argument('--model', choices=['pyramid', 'vpn', 'ved'],
default='pyramid', help='model to train')
parser.add_argument('--experiment', default='test',
help='name of experiment config to load')
parser.add_argument('--resume', default=None,
help='path to an experiment to resume')
parser.add_argument('--options', nargs='*', default=[],
help='list of addition config options as key-val pairs')
args = parser.parse_args()
# Load configuration
config = get_configuration(args)
# Create a directory for the experiment
logdir = create_experiment(config, args.tag, args.resume)
# Create tensorboard summary
summary = SummaryWriter(logdir)
# Set default device
if len(config.gpus) > 0:
torch.cuda.set_device(config.gpus[0])
# Setup experiment
model = build_model(config.model, config)
criterion = build_criterion(config.model, config)
train_loader, val_loader = build_dataloaders(config.train_dataset, config)
# Build optimiser and learning rate scheduler
optimiser = SGD(model.parameters(), config.learning_rate,
weight_decay=config.weight_decay)
lr_scheduler = MultiStepLR(optimiser, config.lr_milestones, 0.1)
# Load checkpoint
if args.resume:
epoch, best_iou = load_checkpoint(os.path.join(logdir, 'latest.pth'),
model, optimiser, lr_scheduler)
else:
epoch, best_iou = 1, 0
# Main training loop
while epoch <= config.num_epochs:
print('\n\n=== Beginning epoch {} of {} ==='.format(epoch,
config.num_epochs))
# Train model for one epoch
train(train_loader, model, criterion, optimiser, summary, config, epoch)
# Evaluate on the validation set
val_iou = evaluate(val_loader, model, criterion, summary, config, epoch)
# Update learning rate
lr_scheduler.step()
# Save checkpoints
if val_iou > best_iou:
best_iou = val_iou
save_checkpoint(os.path.join(logdir, 'best.pth'), model,
optimiser, lr_scheduler, epoch, best_iou)
save_checkpoint(os.path.join(logdir, 'latest.pth'), model, optimiser,
lr_scheduler, epoch, best_iou)
epoch += 1
print("\nTraining complete!")
if __name__ == '__main__':
main()