-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
438 lines (377 loc) · 19.5 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
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
#!/usr/bin/env python
# coding: utf-8
'''
'''
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import numpy as np
import torch
import os, time
import argparse
import shutil
from torch.utils.data import DataLoader
from src.Models import DSTA
from src.eval_tools import evaluation_P_R80, print_results, vis_results
import ipdb
import matplotlib.pyplot as plt
from tensorboardX import SummaryWriter
from tqdm import tqdm
from sklearn.metrics import average_precision_score
seed = 123
np.random.seed(seed)
torch.manual_seed(seed)
ROOT_PATH = os.path.dirname(__file__)
def average_losses(losses_all):
total_loss, cross_entropy, aux_loss = 0, 0, 0
losses_mean = {}
for losses in losses_all:
total_loss += losses['total_loss']
cross_entropy += losses['cross_entropy']
aux_loss += losses['auxloss']
losses_mean['total_loss'] = total_loss / len(losses_all)
losses_mean['cross_entropy'] = cross_entropy / len(losses_all)
losses_mean['auxloss'] = aux_loss / len(losses_all)
return losses_mean
def test_all(testdata_loader, model):
all_pred = []
all_labels = []
all_toas = []
losses_all = []
with torch.no_grad():
for i, (batch_xs, batch_ys, batch_toas) in enumerate(testdata_loader):
# run forward inference
losses, all_outputs, hiddens, alphas = model(batch_xs, batch_ys, batch_toas,
hidden_in=None, nbatch=len(testdata_loader), testing=False)
# make total loss
losses['total_loss'] = losses['cross_entropy']
losses['total_loss'] += p.loss_beta * losses['auxloss']
losses_all.append(losses)
num_frames = batch_xs.size()[1]
batch_size = batch_xs.size()[0]
pred_frames = np.zeros((batch_size, num_frames), dtype=np.float32)
# run inference
for t in range(num_frames):
# pred = all_outputs[t]['pred_mean']
pred = all_outputs[t]
pred = pred.cpu().numpy() if pred.is_cuda else pred.detach().numpy()
pred_frames[:, t] = np.exp(pred[:, 1]) / np.sum(np.exp(pred), axis=1)
# gather results and ground truth
all_pred.append(pred_frames)
label_onehot = batch_ys.cpu().numpy()
label = np.reshape(label_onehot[:, 1], [batch_size,])
all_labels.append(label)
toas = np.squeeze(batch_toas.cpu().numpy()).astype(np.int)
all_toas.append(toas)
all_pred = np.vstack((np.vstack(all_pred[:-1]), all_pred[-1]))
all_labels = np.hstack((np.hstack(all_labels[:-1]), all_labels[-1]))
all_toas = np.hstack((np.hstack(all_toas[:-1]), all_toas[-1]))
return all_pred, all_labels, all_toas, losses_all
def test_all_vis(testdata_loader, model, vis=True, multiGPU=False, device=torch.device('cuda')):
if multiGPU:
model = torch.nn.DataParallel(model)
model = model.to(device=device)
model.eval()
all_pred = []
all_labels = []
all_toas = []
vis_data = []
with torch.no_grad():
for i, (batch_xs, batch_ys, batch_toas, detections, video_ids) in tqdm(enumerate(testdata_loader), desc="batch progress", total=len(testdata_loader)):
# run forward inference
losses, all_outputs, hiddens, alphas = model(batch_xs, batch_ys, batch_toas,
hidden_in=None, nbatch=len(testdata_loader), testing=False)
num_frames = batch_xs.size()[1]
batch_size = batch_xs.size()[0]
pred_frames = np.zeros((batch_size, num_frames), dtype=np.float32)
# run inference
for t in range(num_frames):
# prediction
# pred = all_outputs[t]['pred_mean'] # B x 2
pred = all_outputs[t]
pred = pred.cpu().numpy() if pred.is_cuda else pred.detach().numpy()
pred_frames[:, t] = np.exp(pred[:, 1]) / np.sum(np.exp(pred), axis=1)
# gather results and ground truth
all_pred.append(pred_frames)
label_onehot = batch_ys.cpu().numpy()
label = np.reshape(label_onehot[:, 1], [batch_size,])
all_labels.append(label)
toas = np.squeeze(batch_toas.cpu().numpy()).astype(np.int)
all_toas.append(toas)
if vis:
# gather data for visualization
vis_data.append({'pred_frames': pred_frames, 'label': label,
'toa': toas, 'detections': detections, 'video_ids': video_ids})
all_pred = np.vstack((np.vstack(all_pred[:-1]), all_pred[-1]))
all_labels = np.hstack((np.hstack(all_labels[:-1]), all_labels[-1]))
all_toas = np.hstack((np.hstack(all_toas[:-1]), all_toas[-1]))
return all_pred, all_labels, all_toas, vis_data
def write_scalars(logger, cur_epoch, cur_iter, losses, lr):
# fetch results
total_loss = losses['total_loss'].mean().item()
cross_entropy = losses['cross_entropy'].mean()
aux_loss = losses['auxloss'].mean().item()
# write to tensorboard
logger.add_scalars("train/losses/total_loss", {'total_loss': total_loss}, cur_iter)
logger.add_scalars("train/losses/cross_entropy", {'cross_entropy': cross_entropy}, cur_iter)
logger.add_scalars("train/losses/aux_loss", {'aux_loss': aux_loss}, cur_iter)
# write learning rate
logger.add_scalars("train/learning_rate/lr", {'lr': lr}, cur_iter)
def write_test_scalars(logger, cur_epoch, cur_iter, losses, metrics):
# fetch results
total_loss = losses['total_loss'].mean().item()
cross_entropy = losses['cross_entropy'].mean()
# write to tensorboard
loss_info = {'total_loss': total_loss, 'cross_entropy': cross_entropy}
aux_loss = losses['auxloss'].mean().item()
loss_info.update({'aux_loss': aux_loss})
logger.add_scalars("test/losses/total_loss", loss_info, cur_iter)
logger.add_scalars("test/accuracy/AP", {'AP': metrics['AP'],'P_R80': metrics['P_R80']}, cur_iter)
logger.add_scalars("test/accuracy/time-to-accident", {'mTTA': metrics['mTTA'],
'TTA_R80': metrics['TTA_R80']}, cur_iter)
def load_checkpoint(model, optimizer=None, filename='checkpoint.pth.tar', isTraining=True):
# Note: Input model & optimizer should be pre-defined. This routine only updates their states.
start_epoch = 0
if os.path.isfile(filename):
checkpoint = torch.load(filename)
start_epoch = checkpoint['epoch']
model.load_state_dict(checkpoint['model'])
if isTraining:
optimizer.load_state_dict(checkpoint['optimizer'])
print("=> loaded checkpoint '{}' (epoch {})".format(filename, checkpoint['epoch']))
else:
print("=> no checkpoint found at '{}'".format(filename))
return model, optimizer, start_epoch
def train_eval():
### --- CONFIG PATH ---
data_path = os.path.join(ROOT_PATH, p.data_path, p.dataset)
# model snapshots
model_dir = os.path.join(p.output_dir, p.dataset, 'snapshot')
if not os.path.exists(model_dir):
os.makedirs(model_dir)
# tensorboard logging
logs_dir = os.path.join(p.output_dir, p.dataset, 'logs')
if not os.path.exists(logs_dir):
os.makedirs(logs_dir)
logger = SummaryWriter(logs_dir)
# gpu options
gpu_ids = [int(id) for id in p.gpus.split(',')]
print("Using GPU devices: ", gpu_ids)
os.environ['CUDA_VISIBLE_DEVICES'] = p.gpus
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
# create data loader
if p.dataset == 'dad':
from src.DataLoader import DADDataset
train_data = DADDataset(data_path, p.feature_name, 'training', toTensor=True, device=device)
test_data = DADDataset(data_path, p.feature_name, 'testing', toTensor=True, device=device)
elif p.dataset == 'crash':
from src.DataLoader import CrashDataset
train_data = CrashDataset(data_path, p.feature_name, 'train', toTensor=True, device=device)
test_data = CrashDataset(data_path, p.feature_name, 'test', toTensor=True, device=device)
else:
raise NotImplementedError
traindata_loader = DataLoader(dataset=train_data, batch_size=p.batch_size, shuffle=True, drop_last=True)
testdata_loader = DataLoader(dataset=test_data, batch_size=p.batch_size, shuffle=False, drop_last=True)
# building model
model = DSTA(train_data.dim_feature, p.hidden_dim, p.latent_dim,
n_layers=p.num_rnn, n_obj=train_data.n_obj, n_frames=train_data.n_frames, fps=train_data.fps,
with_saa=True)
# optimizer
optimizer = torch.optim.Adam(model.parameters(), lr=p.base_lr)
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=5)
if len(gpu_ids) > 1:
model = torch.nn.DataParallel(model)
model = model.to(device=device)
model.train() # set the model into training status
# resume training
start_epoch = -1
if p.resume:
model, optimizer, start_epoch = load_checkpoint(model, optimizer=optimizer, filename=p.model_file)
# write histograms at line 234
# write_weight_histograms(logger, model, 0)
iter_cur = 0
best_metric = 0
best_P_R80 = 0.5
metrics = {}
metrics['AP']= 0
for k in range(p.epoch):
loop = tqdm(enumerate(traindata_loader),total=len(traindata_loader))
if k <= start_epoch:
iter_cur += len(traindata_loader)
continue
for i, (batch_xs, batch_ys, batch_toas) in loop:
# ipdb.set_trace()
optimizer.zero_grad()
losses, all_outputs, hidden_st, alphas = model(batch_xs, batch_ys, batch_toas, nbatch=len(traindata_loader))
losses['total_loss'] = losses['cross_entropy']
losses['total_loss'] += p.loss_beta * losses['auxloss']
# backward
losses['total_loss'].mean().backward()
# clip gradients
torch.nn.utils.clip_grad_norm_(model.parameters(), 10)
optimizer.step()
loop.set_description(f"Epoch [{k}/{p.epoch}]")
loop.set_postfix(loss= losses['total_loss'].item() )
# write the losses info
lr = optimizer.param_groups[0]['lr']
write_scalars(logger, k, iter_cur, losses, lr)
iter_cur += 1
# test and evaluate the model
if iter_cur % p.test_iter == 0:
model.eval()
all_pred, all_labels, all_toas, losses_all = test_all(testdata_loader, model)
model.train()
loss_val = average_losses(losses_all)
print('----------------------------------')
print("Starting evaluation...")
metrics = {}
metrics['AP'], metrics['mTTA'], metrics['TTA_R80'], metrics['P_R80'] = evaluation_P_R80(all_pred, all_labels, all_toas, fps=test_data.fps)
print('----------------------------------')
# keep track of validation losses
write_test_scalars(logger, k, iter_cur, loss_val, metrics)
# save model
model_file = os.path.join(model_dir, 'bayesian_gcrnn_model_%02d.pth'%(k))
torch.save({'epoch': k,
'model': model.module.state_dict() if len(gpu_ids)>1 else model.state_dict(),
'optimizer': optimizer.state_dict()}, model_file)
if metrics['AP'] > best_metric:
best_metric = metrics['AP']
# update best model file
update_final_model(model_file, os.path.join(model_dir, 'final_model.pth'))
print('Model has been saved as: %s'%(model_file))
scheduler.step(losses['total_loss'])
# write histograms
# write_weight_histograms(logger, model, k+1)
logger.close()
def update_final_model(src_file, dest_file):
# source file must exist
assert os.path.exists(src_file), "src file does not exist!"
# destinate file should be removed first if exists
if os.path.exists(dest_file):
if not os.path.samefile(src_file, dest_file):
os.remove(dest_file)
# copy file
shutil.copyfile(src_file, dest_file)
def test_eval():
### --- CONFIG PATH ---
data_path = os.path.join(ROOT_PATH, p.data_path, p.dataset)
# result path
result_dir = os.path.join(p.output_dir, p.dataset, 'test')
if not os.path.exists(result_dir):
os.makedirs(result_dir)
# visualization results
p.visualize = False if p.evaluate_all else p.visualize
vis_dir = None
if p.visualize:
vis_dir = os.path.join(result_dir, 'vis')
if not os.path.exists(vis_dir):
os.makedirs(vis_dir)
# gpu options
gpu_ids = [int(id) for id in p.gpus.split(',')]
os.environ['CUDA_VISIBLE_DEVICES'] = p.gpus
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
# create data loader
if p.dataset == 'dad':
from src.DataLoader import DADDataset
test_data = DADDataset(data_path, p.feature_name, 'testing', toTensor=True, device=device, vis=True)
elif p.dataset == 'crash':
from src.DataLoader import CrashDataset
test_data = CrashDataset(data_path, p.feature_name, 'test', toTensor=True, device=device, vis=True)
else:
raise NotImplementedError
testdata_loader = DataLoader(dataset=test_data, batch_size=p.batch_size, shuffle=False, drop_last=True)
num_samples = len(test_data)
print("Number of testing samples: %d"%(num_samples))
# building model
model = DSTA(test_data.dim_feature, p.hidden_dim, p.latent_dim,
n_layers=p.num_rnn, n_obj=test_data.n_obj, n_frames=test_data.n_frames, fps=test_data.fps,
with_saa=True)
# start to evaluate
if p.evaluate_all:
model_dir = os.path.join(p.output_dir, p.dataset, 'snapshot')
assert os.path.exists(model_dir)
Epochs, APvid_all, AP_all, mTTA_all, TTA_R80_all = [], [], [], [], []
modelfiles = sorted(os.listdir(model_dir))
for filename in modelfiles:
epoch_str = filename.split("_")[-1].split(".pth")[0]
print("Evaluation for epoch: " + epoch_str)
model_file = os.path.join(model_dir, filename)
model, _, _ = load_checkpoint(model, filename=model_file, isTraining=False)
# run model inference
all_pred, all_labels, all_toas, _ = test_all_vis(testdata_loader, model, vis=False, device=device)
# evaluate results
AP, mTTA, TTA_R80, P_R80 = evaluation_P_R80(all_pred, all_labels, all_toas, fps=test_data.fps)
# mUncertains = np.mean(all_uncertains, axis=(0, 1))
all_vid_scores = [max(pred[:int(toa)]) for toa, pred in zip(all_toas, all_pred)]
AP_video = average_precision_score(all_labels, all_vid_scores)
APvid_all.append(AP_video)
# save
Epochs.append(epoch_str)
AP_all.append(AP)
mTTA_all.append(mTTA)
TTA_R80_all.append(TTA_R80)
# print results to file
print_results(Epochs, APvid_all, AP_all, mTTA_all, TTA_R80_all, result_dir)
else:
result_file = os.path.join(vis_dir, "..", "pred_res.npz")
if not os.path.exists(result_file):
model, _, _ = load_checkpoint(model, filename=p.model_file, isTraining=False)
# run model inference
all_pred, all_labels, all_toas, vis_data = test_all_vis(testdata_loader, model, vis=True, device=device)
# save predictions
np.savez(result_file[:-4], pred=all_pred, label=all_labels, toas=all_toas, vis_data=vis_data)
else:
print("Result file exists. Loaded from cache.")
all_results = np.load(result_file, allow_pickle=True)
all_pred, all_labels, all_toas, vis_data = \
all_results['pred'], all_results['label'], all_results['toas'], all_results['vis_data']
# evaluate results
all_vid_scores = [max(pred[:int(toa)]) for toa, pred in zip(all_toas, all_pred)]
AP_video = average_precision_score(all_labels, all_vid_scores)
print("video-level AP=%.5f"%(AP_video))
AP, mTTA, TTA_R80, P_R80 = evaluation_P_R80(all_pred, all_labels, all_toas, fps=test_data.fps)
# # visualize
vis_results(vis_data, p.batch_size, vis_dir)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data_path', type=str, default='./data',
help='The relative path of dataset.')
parser.add_argument('--dataset', type=str, default='dad', choices=['dad', 'crash'],
help='The name of dataset. Default: dad')
parser.add_argument('--base_lr', type=float, default=1e-3,
help='The base learning rate. Default: 1e-3')
parser.add_argument('--epoch', type=int, default=30,
help='The number of training epoches. Default: 30')
parser.add_argument('--batch_size', type=int, default=10,
help='The batch size in training process. Default: 10')
parser.add_argument('--num_rnn', type=int, default=1,
help='The number of RNN cells for each timestamp. Default: 1')
parser.add_argument('--feature_name', type=str, default='vgg16', choices=['vgg16', 'res101'],
help='The name of feature embedding methods. Default: vgg16')
parser.add_argument('--test_iter', type=int, default=64,
help='The number of iteration to perform a evaluation process. Default: 64')
parser.add_argument('--hidden_dim', type=int, default=512,
help='The dimension of hidden states in RNN. Default: 512')
parser.add_argument('--latent_dim', type=int, default=256,
help='The dimension of latent space. Default: 256')
parser.add_argument('--loss_beta', type=float, default=15,
help='The weighting factor of auxiliary loss. Default: 10')
parser.add_argument('--gpus', type=str, default="0",
help="The delimited list of GPU IDs separated with comma. Default: '0'.")
parser.add_argument('--phase', type=str, choices=['train', 'test'],
help='The state of running the model. Default: train')
parser.add_argument('--evaluate_all', action='store_true',
help='Whether to evaluate models of all epoches. Default: False')
parser.add_argument('--visualize', action='store_true',
help='The visualization flag. Default: False')
parser.add_argument('--resume', action='store_true',
help='If to resume the training. Default: False')
parser.add_argument('--model_file', type=str, default='./output/DSTA/vgg16/dad/snapshot_attention_v18/bayesian_gcrnn_model_23.pth',
help='The trained GCRNN model file for demo test only.')
parser.add_argument('--output_dir', type=str, default='./output_debug/bayes_gcrnn/vgg16',
help='The directory of src need to save in the training.')
p = parser.parse_args()
if p.phase == 'test':
test_eval()
else:
train_eval()