-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
489 lines (421 loc) · 19.6 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
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
481
482
483
484
485
486
487
488
489
#!/usr/bin/env python
from __future__ import print_function
import inspect
import os
import pickle
import random
import shutil
import sys
import time
from collections import OrderedDict
import traceback
from sklearn.metrics import confusion_matrix
import csv
import numpy as np
import glob
import itertools
# torch
import torch
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.optim as optim
import yaml
from tensorboardX import SummaryWriter
from tqdm import tqdm
import resource
from utils.config import get_parser
from utils.visualize import record_skeleton, wrong_analyze
from utils.cls_loss import build_loss
rlimit = resource.getrlimit(resource.RLIMIT_NOFILE) # 能打开的最大文件数
resource.setrlimit(resource.RLIMIT_NOFILE, (2048, rlimit[1]))
def init_seed(seed):
# 随机种子固定
torch.cuda.manual_seed_all(seed)
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
# torch.backends.cudnn.enabled = False
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def import_class(import_str):
# 动态 import
mod_str, _sep, class_str = import_str.rpartition('.') # 从目标字符串的末尾也就是右边开始搜索分割符
__import__(mod_str)
try:
return getattr(sys.modules[mod_str], class_str)
except AttributeError:
raise ImportError('Class %s cannot be found (%s)' % (class_str, traceback.format_exception(*sys.exc_info())))
def calc_diff_modality(data, bone=False, vel=False):
if data.shape[3] != 25:
# only work for ntu dataset
return data
if bone:
from feeders.bone_pairs import ntu_pairs
ret_data = torch.zeros_like(data).to(data.device)
for v1, v2 in ntu_pairs:
ret_data[:, :, :, v1 - 1] = data[:, :, :, v1 - 1] - data[:, :, :, v2 - 1]
data = ret_data
if vel:
data[:, :, :-1] = data[:, :, 1:] - data[:, :, :-1]
data[:, :, -1] = 0
return data
class Processor:
"""
Processor for Skeleton-based Action Recgnition
"""
def __init__(self, arg):
self.arg = arg
self.save_arg()
if arg.phase == 'train': # train = train + val
if not arg.train_feeder_args['debug']:
arg.model_saved_name = os.path.join(arg.work_dir, 'runs') # work_dir/runs 模型记录
if os.path.isdir(arg.model_saved_name):
print('log_dir: ', arg.model_saved_name, 'already exist')
answer = input('delete it? y/n:')
if answer == 'y':
shutil.rmtree(arg.model_saved_name)
print('Dir removed: ', arg.model_saved_name)
input('Refresh the website of tensorboard by pressing any keys')
else:
print('Dir not removed: ', arg.model_saved_name)
self.train_writer = SummaryWriter(os.path.join(arg.model_saved_name, 'train'), 'train')
self.val_writer = SummaryWriter(os.path.join(arg.model_saved_name, 'val'), 'val')
else:
self.train_writer = self.val_writer = SummaryWriter(os.path.join(arg.model_saved_name, 'test'), 'test')
self.global_step = 0
# pdb.set_trace()
self.load_model()
if self.arg.phase == 'model_size':
pass
else:
self.load_data()
self.load_optimizer()
self.lr = self.arg.base_lr
self.best_acc = 0
self.best_acc_epoch = 0
self.model = self.model.cuda(self.output_device)
if type(self.arg.device) is list:
if len(self.arg.device) > 1:
self.model = nn.DataParallel(
self.model,
device_ids=self.arg.device,
output_device=self.output_device)
def load_data(self):
Feeder = import_class(self.arg.feeder)
self.data_loader = dict()
if self.arg.phase == 'train':
train_dataset = Feeder(**self.arg.train_feeder_args)
self.data_loader['train'] = torch.utils.data.DataLoader(
dataset=train_dataset,
batch_size=self.arg.batch_size,
shuffle=True,
num_workers=self.arg.num_worker,
drop_last=True,
worker_init_fn=init_seed)
self.train_modality = dict(
bone=self.data_loader['train'].dataset.bone,
vel=self.data_loader['train'].dataset.vel,
)
self.data_loader['test'] = torch.utils.data.DataLoader(
dataset=Feeder(**self.arg.test_feeder_args),
batch_size=self.arg.test_batch_size,
shuffle=False,
num_workers=self.arg.num_worker,
drop_last=False,
worker_init_fn=init_seed)
self.test_modality = dict(
bone=self.data_loader['test'].dataset.bone,
vel=self.data_loader['test'].dataset.vel,
)
def load_model(self):
output_device = self.arg.device[0] if type(self.arg.device) is list else self.arg.device
self.output_device = output_device
Model = import_class(self.arg.model)
shutil.copy2(inspect.getfile(Model), self.arg.work_dir)
print(Model)
self.model = Model(**self.arg.model_args, cl_mode=self.arg.cl_mode,
multi_cl_weights=self.arg.w_multi_cl_loss, cl_version=self.arg.cl_version,
pred_threshold=self.arg.pred_threshold, use_p_map=self.arg.use_p_map)
print(self.model)
self.loss = build_loss(self.arg).cuda(output_device)
if self.arg.weights:
self.global_step = 0
try:
self.global_step = int(arg.weights[:-3].split('-')[-1])
except:
pass
self.print_log('Load weights from {}.'.format(self.arg.weights))
if '.pkl' in self.arg.weights:
with open(self.arg.weights, 'r') as f:
weights = pickle.load(f)
else:
weights = torch.load(self.arg.weights)
weights = OrderedDict([[k.split('module.')[-1], v.cuda(output_device)] for k, v in weights.items()])
keys = list(weights.keys())
for w in self.arg.ignore_weights:
for key in keys:
if w in key:
if weights.pop(key, None) is not None:
self.print_log('Sucessfully Remove Weights: {}.'.format(key))
else:
self.print_log('Can Not Remove Weights: {}.'.format(key))
try:
self.model.load_state_dict(weights)
except:
state = self.model.state_dict()
diff = list(set(state.keys()).difference(set(weights.keys())))
print('Can not find these weights:')
for d in diff:
print(' ' + d)
state.update(weights)
self.model.load_state_dict(state, strict=False)
def load_optimizer(self):
if self.arg.optimizer == 'SGD':
self.optimizer = optim.SGD(
self.model.parameters(),
lr=self.arg.base_lr,
momentum=0.9,
nesterov=self.arg.nesterov,
weight_decay=self.arg.weight_decay)
elif self.arg.optimizer == 'Adam':
self.optimizer = optim.Adam(
self.model.parameters(),
lr=self.arg.base_lr,
weight_decay=self.arg.weight_decay)
else:
raise ValueError()
self.print_log('using warm up, epoch: {}'.format(self.arg.warm_up_epoch))
def save_arg(self):
# save arg
arg_dict = vars(self.arg)
if not os.path.exists(self.arg.work_dir):
os.makedirs(self.arg.work_dir)
with open('{}/config.yaml'.format(self.arg.work_dir), 'w') as f:
f.write(f"# command line: {' '.join(sys.argv)}\n\n")
yaml.dump(arg_dict, f)
def adjust_learning_rate(self, epoch):
if self.arg.optimizer == 'SGD' or self.arg.optimizer == 'Adam':
if epoch < self.arg.warm_up_epoch:
lr = self.arg.base_lr * (epoch + 1) / self.arg.warm_up_epoch
else:
lr = self.arg.base_lr * (
self.arg.lr_decay_rate ** np.sum(epoch >= np.array(self.arg.step)))
for param_group in self.optimizer.param_groups:
param_group['lr'] = lr
return lr
else:
raise ValueError()
def print_time(self):
localtime = time.asctime(time.localtime(time.time()))
self.print_log("Local current time : " + localtime)
def print_log(self, str, print_time=True):
if print_time:
localtime = time.asctime(time.localtime(time.time()))
str = "[ " + localtime + ' ] ' + str
print(str)
if self.arg.print_log:
with open('{}/log.txt'.format(self.arg.work_dir), 'a') as f:
print(str, file=f)
def record_time(self):
self.cur_time = time.time()
return self.cur_time
def split_time(self):
split_time = time.time() - self.cur_time
self.record_time()
return split_time
def train(self, epoch, save_model=False):
self.model.train()
self.print_log('Training epoch: {}'.format(epoch + 1))
loader = self.data_loader['train']
self.adjust_learning_rate(epoch)
loss_value = []
acc_value = []
self.train_writer.add_scalar('epoch', epoch, self.global_step)
self.record_time()
timer = dict(dataloader=0.001, model=0.001, statistics=0.001)
process = tqdm(loader, ncols=40)
roll_back_step = self.global_step
# train model with real data
for batch_idx, (data, label, index) in enumerate(process):
self.global_step += 1
with torch.no_grad():
data = data.float().cuda(self.output_device)
label = label.long().cuda(self.output_device)
timer['dataloader'] += self.split_time()
# forward
if self.arg.cl_mode is not None:
output, cl_loss = self.model(calc_diff_modality(data, **self.train_modality), label, get_cl_loss=True)
else:
output = self.model(calc_diff_modality(data, **self.train_modality), label)
loss = self.loss(output, label)
full_loss = loss.mean()
if self.arg.cl_mode is not None:
# cl_loss = self.loss(fn_output, label).mean() + self.loss(fp_output, label).mean()
self.train_writer.add_scalar('cl_loss', cl_loss.mean().data.item(), self.global_step)
if epoch > self.arg.start_cl_epoch:
full_loss += self.arg.w_cl_loss * cl_loss.mean()
# backward
self.optimizer.zero_grad()
full_loss.backward()
self.optimizer.step()
loss_value.append(loss.mean().data.item())
timer['model'] += self.split_time()
value, predict_label = torch.max(output.data, 1)
acc = torch.mean((predict_label == label.data).float())
acc_value.append(acc.data.item())
self.train_writer.add_scalar('acc', acc, self.global_step)
self.train_writer.add_scalar('loss', loss.mean().data.item(), self.global_step)
# statistics
self.lr = self.optimizer.param_groups[0]['lr']
self.train_writer.add_scalar('lr', self.lr, self.global_step)
timer['statistics'] += self.split_time()
# statistics of time consumption and loss
proportion = {
k: '{:02d}%'.format(int(round(v * 100 / sum(timer.values()))))
for k, v in timer.items()
}
self.print_log(
'\tMean training loss: {:.4f}. Mean training acc: {:.2f}%.'.format(np.mean(loss_value),
np.mean(acc_value) * 100))
self.print_log('\tTime consumption: [Data]{dataloader}, [Network]{model}'.format(**proportion))
if save_model:
state_dict = self.model.state_dict()
weights = OrderedDict([[k.split('module.')[-1], v.cpu()] for k, v in state_dict.items()])
torch.save(weights,
self.arg.model_saved_name + '-' + str(epoch + 1) + '-' + str(int(self.global_step)) + '.pt')
def eval(self, epoch, save_score=False, loader_name=['test'], wrong_file=None, result_file=None):
if wrong_file is not None:
f_w = open(wrong_file, 'w')
if result_file is not None:
f_r = open(result_file, 'w')
self.model.eval()
self.print_log('Eval epoch: {}'.format(epoch + 1))
for ln in loader_name:
loss_value = []
score_frag = []
label_list = []
pred_list = []
step = 0
process = tqdm(self.data_loader[ln], ncols=40)
for batch_idx, (data, label, index) in enumerate(process):
label_list.append(label)
with torch.no_grad():
data = data.float().cuda(self.output_device)
label = label.long().cuda(self.output_device)
output = self.model(calc_diff_modality(data, **self.test_modality))
loss = self.loss(output, label).mean()
score_frag.append(output.data.cpu().numpy())
loss_value.append(loss.data.item())
_, predict_label = torch.max(output.data, 1)
pred_list.append(predict_label.data.cpu().numpy())
step += 1
if wrong_file is not None or result_file is not None:
predict = list(predict_label.cpu().numpy())
true = list(label.data.cpu().numpy())
for i, x in enumerate(predict):
if result_file is not None:
f_r.write(str(x) + ',' + str(true[i]) + '\n')
if x != true[i] and wrong_file is not None:
f_w.write(str(index[i]) + ',' + str(x) + ',' + str(true[i]) + '\n')
score = np.concatenate(score_frag)
loss = np.mean(loss_value)
if 'ucla' in self.arg.feeder:
self.data_loader[ln].dataset.sample_name = np.arange(len(score))
accuracy = self.data_loader[ln].dataset.top_k(score, 1)
if accuracy > self.best_acc:
self.best_acc = accuracy
self.best_acc_epoch = epoch + 1
print('Accuracy: ', accuracy, ' model: ', self.arg.model_saved_name)
if self.arg.phase == 'train':
self.val_writer.add_scalar('loss', loss, self.global_step)
self.val_writer.add_scalar('acc', accuracy, self.global_step)
score_dict = dict(
zip(self.data_loader[ln].dataset.sample_name, score))
self.print_log('\tMean {} loss of {} batches: {}.'.format(
ln, len(self.data_loader[ln]), np.mean(loss_value)))
for k in self.arg.show_topk:
self.print_log('\tTop{}: {:.2f}%'.format(
k, 100 * self.data_loader[ln].dataset.top_k(score, k)))
if save_score:
with open('{}/epoch{}_{}_score.pkl'.format(
self.arg.work_dir, epoch + 1, ln), 'wb') as f:
pickle.dump(score_dict, f)
# acc for each class:
label_list = np.concatenate(label_list)
pred_list = np.concatenate(pred_list)
confusion = confusion_matrix(label_list, pred_list)
list_diag = np.diag(confusion)
list_raw_sum = np.sum(confusion, axis=1)
each_acc = list_diag / list_raw_sum
with open('{}/epoch{}_{}_each_class_acc.csv'.format(self.arg.work_dir, epoch + 1, ln), 'w') as f:
writer = csv.writer(f)
writer.writerow(each_acc)
writer.writerows(confusion)
def start(self):
if self.arg.phase == 'train':
self.print_log('Parameters:\n{}\n'.format(str(vars(self.arg))))
self.global_step = self.arg.start_epoch * len(self.data_loader['train']) / self.arg.batch_size
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
self.print_log(f'# Parameters: {count_parameters(self.model)}')
for epoch in range(self.arg.start_epoch, self.arg.num_epoch):
save_model = (((epoch + 1) % self.arg.save_interval == 0) or (
epoch + 1 == self.arg.num_epoch)) and (epoch + 1) > self.arg.save_epoch
self.train(epoch, save_model=save_model)
self.eval(epoch, save_score=self.arg.save_score, loader_name=['test'])
self.print_log(f'Epoch number: {self.best_acc_epoch}')
# test the best model
weights_path = glob.glob(os.path.join(self.arg.work_dir, 'runs-' + str(self.best_acc_epoch) + '*'))[0]
weights = torch.load(weights_path)
if type(self.arg.device) is list:
if len(self.arg.device) > 1:
weights = OrderedDict([['module.' + k, v.cuda(self.output_device)] for k, v in weights.items()])
self.model.load_state_dict(weights)
wf = weights_path.replace('.pt', '_wrong.txt')
rf = weights_path.replace('.pt', '_right.txt')
self.arg.print_log = False
self.eval(epoch=0, save_score=True, loader_name=['test'], wrong_file=wf, result_file=rf)
wrong_analyze(wf, rf)
self.arg.print_log = True
num_params = sum(p.numel() for p in self.model.parameters() if p.requires_grad)
self.print_log(f'Best accuracy: {self.best_acc}')
self.print_log(f'Epoch number: {self.best_acc_epoch}')
self.print_log(f'Model name: {self.arg.work_dir}')
self.print_log(f'Model total number of params: {num_params}')
self.print_log(f'Weight decay: {self.arg.weight_decay}')
self.print_log(f'Base LR: {self.arg.base_lr}')
self.print_log(f'Batch Size: {self.arg.batch_size}')
self.print_log(f'Test Batch Size: {self.arg.test_batch_size}')
self.print_log(f'seed: {self.arg.seed}')
elif self.arg.phase == 'test':
wf = self.arg.weights.replace('.pt', '_wrong.txt')
rf = self.arg.weights.replace('.pt', '_right.txt')
if self.arg.weights is None:
raise ValueError('Please appoint --weights.')
self.arg.print_log = False
self.print_log('Model: {}.'.format(self.arg.model))
self.print_log('Weights: {}.'.format(self.arg.weights))
self.eval(epoch=0, save_score=self.arg.save_score, loader_name=['test'], wrong_file=wf, result_file=rf)
wrong_analyze(wf, rf)
self.print_log('Done.\n')
if __name__ == '__main__':
# os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
parser = get_parser()
# load arg from config file
p = parser.parse_args()
if p.config is not None:
with open(p.config, 'r') as f:
default_arg = yaml.load(f)
key = vars(p).keys()
for k in default_arg.keys():
if k not in key:
print('WRONG ARG: {}'.format(k))
assert (k in key)
parser.set_defaults(**default_arg)
arg = parser.parse_args()
init_seed(arg.seed)
processor = Processor(arg)
try:
processor.start()
except:
processor.print_log(str(traceback.format_exc()), False)