forked from fabro66/Online-Skeleton-based-Action-Recognition
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
299 lines (237 loc) · 9.86 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
import argparse
import time
import shutil
import logging
import os
os.environ["CUDA_VISIBLE_DEVICES"] = '0'
import os.path as osp
import csv
import numpy as np
np.random.seed(1337)
import torch
import torch.nn as nn
import torch.optim as optim
from torch.optim.lr_scheduler import ReduceLROnPlateau, MultiStepLR
from model import ESN, ESNKinetics
from data import NTUDataLoaders, AverageMeter
import fit
from tools.util import make_dir, get_num_classes
from tools.skeleton import Skeleton
parser = argparse.ArgumentParser(description='Skeleton-Based Action Recgnition')
fit.add_fit_args(parser)
parser.set_defaults(
network='ESN',
dataset='NTU60',
case=0,
batch_size=64,
max_epochs=120,
monitor='val_acc',
lr=0.001,
weight_decay=0.0001,
lr_factor=0.1,
workers=24,
print_freq=20,
train=0,
seg=20,
)
args = parser.parse_args()
# Saving the training detail
LOG_FORMAT = '%(asctime)s - %(message)s'
filename = './results/' + 'training_v%d.log' % args.version
if osp.isfile(filename):
os.remove(filename)
logging.basicConfig(filename=filename, level=logging.DEBUG, format=LOG_FORMAT)
def main():
args.num_classes = get_num_classes(args.dataset)
skeleton = Skeleton(args.dataset)
if args.dataset == 'kinetics':
model = ESNKinetics(args.num_classes, skeleton, args.seg)
else:
model = ESN(args.num_classes, skeleton, args.seg)
total = get_n_params(model)
# print(model)
print('The number of parameters: ', total)
print('The modes is:', args.network)
logging.debug('The number of parameters: %d', total)
logging.debug('The modes is: %s', args.network)
if torch.cuda.is_available():
print('It is using GPU!')
model = model.cuda()
criterion = LabelSmoothingLoss(args.num_classes, smoothing=0.1).cuda()
optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
if args.monitor == 'val_acc':
mode = 'max'
monitor_op = np.greater
best = -np.Inf
str_op = 'improve'
elif args.monitor == 'val_loss':
mode = 'min'
monitor_op = np.less
best = np.Inf
str_op = 'reduce'
scheduler = MultiStepLR(optimizer, milestones=[60, 90, 110], gamma=0.1)
# Data loading
ntu_loaders = NTUDataLoaders(args.dataset, args.case, seg=args.seg)
train_loader = ntu_loaders.get_train_loader(args.batch_size, args.workers)
val_loader = ntu_loaders.get_val_loader(args.batch_size, args.workers)
train_size = ntu_loaders.get_train_size()
val_size = ntu_loaders.get_val_size()
test_loader = ntu_loaders.get_test_loader(32, args.workers)
print('Train on %d samples, validate on %d samples' % (train_size, val_size))
logging.debug('Train on %d samples, validate on %d samples', train_size, val_size)
best_epoch = 0
output_dir = make_dir(args.dataset)
save_path = os.path.join(output_dir, args.network)
if not os.path.exists(save_path):
os.makedirs(save_path)
checkpoint = osp.join(save_path, '{}_best_v{}.pth'.format(args.case, args.version))
earlystop_cnt = 0
csv_file = osp.join(save_path, '{}_log_v{}.csv'.format(args.case, args.version))
log_res = list()
lable_path = osp.join(save_path, '{}_label_v{}.txt'.format(args.case, args.version))
pred_path = osp.join(save_path, '{}_pred_v{}.txt'.format(args.case, args.version))
# Training
if args.train == 1:
for epoch in range(args.start_epoch, args.max_epochs):
print(epoch, optimizer.param_groups[0]['lr'])
t_start = time.time()
train_loss, train_acc = train(train_loader, model, criterion, optimizer, epoch)
val_loss, val_acc = validate(val_loader, model, criterion)
log_res += [[train_loss, train_acc.cpu().numpy(), \
val_loss, val_acc.cpu().numpy()]]
print('Epoch-{:<3d} {:.1f}s\t'
'Train: loss {:.4f}\taccu {:.4f}\tValid: loss {:.4f}\taccu {:.4f}'
.format(epoch + 1, time.time() - t_start, train_loss, train_acc, val_loss, val_acc))
logging.debug('Epoch-%3d %.1fs\t Train: loss %.4f\taccu %.4f\tValid: loss %.4f\taccu %.4f',
epoch + 1, time.time() - t_start, train_loss, train_acc, val_loss, val_acc)
current = val_loss if mode == 'min' else val_acc
####### store tensor in cpu
current = current.cpu()
if monitor_op(current, best):
print('Epoch %d: %s %sd from %.4f to %.4f, '
'saving model to %s'
% (epoch + 1, args.monitor, str_op, best, current, checkpoint))
best = current
best_epoch = epoch + 1
save_checkpoint({
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'best': best,
'monitor': args.monitor,
'optimizer': optimizer.state_dict(),
}, checkpoint)
earlystop_cnt = 0
else:
print('Epoch %d: %s did not %s' % (epoch + 1, args.monitor, str_op))
earlystop_cnt += 1
scheduler.step()
print('Best %s: %.4f from epoch-%d' % (args.monitor, best, best_epoch))
logging.debug('Best %s: %.4f from epoch-%d', args.monitor, best, best_epoch)
with open(csv_file, 'w') as fw:
cw = csv.writer(fw)
cw.writerow(['loss', 'acc', 'val_loss', 'val_acc'])
cw.writerows(log_res)
print('Save train and validation log into into %s' % csv_file)
### Test
args.train = 0
if args.dataset == 'kinetics':
model = ESNKinetics(args.num_classes, skeleton, args.seg)
else:
model = ESN(args.num_classes, skeleton, args.seg)
model = model.cuda()
test(test_loader, model, checkpoint, lable_path, pred_path)
def train(train_loader, model, criterion, optimizer, epoch):
losses = AverageMeter()
acces = AverageMeter()
model.train()
for i, (inputs, target) in enumerate(train_loader):
output = model(inputs.cuda())
target = target.cuda(non_blocking=True)
loss = criterion(output, target)
# measure accuracy and record loss
acc = accuracy(output.data, target)
losses.update(loss.item(), inputs.size(0))
acces.update(acc[0], inputs.size(0))
# backward
optimizer.zero_grad() # clear gradients out before each mini-batch
loss.backward()
optimizer.step()
return losses.avg, acces.avg
def validate(val_loader, model, criterion):
losses = AverageMeter()
acces = AverageMeter()
model.eval()
for i, (inputs, target) in enumerate(val_loader):
with torch.no_grad():
output = model(inputs.cuda())
target = target.cuda(non_blocking=True)
with torch.no_grad():
loss = criterion(output, target)
# measure accuracy and record loss
acc = accuracy(output.data, target)
losses.update(loss.item(), inputs.size(0))
acces.update(acc[0], inputs.size(0))
return losses.avg, acces.avg
def test(test_loader, model, checkpoint, lable_path, pred_path):
acces_top1 = AverageMeter()
acces_top5 = AverageMeter()
# load learnt model that obtained best performance on validation set
model.load_state_dict(torch.load(checkpoint)['state_dict'])
model.eval()
label_output = list()
pred_output = list()
t_start = time.time()
for i, (inputs, target) in enumerate(test_loader):
with torch.no_grad():
output = model(inputs.cuda())
output = output.view((-1, inputs.size(0) // target.size(0), output.size(1)))
output = output.mean(1)
label_output.append(target.cpu().numpy())
pred_output.append(output.cpu().numpy())
acc_top1 = accuracy(output.data, target.cuda(non_blocking=True), topk=1)
acc_top5 = accuracy(output.data, target.cuda(non_blocking=True), topk=5)
acces_top1.update(acc_top1[0], inputs.size(0))
acces_top5.update(acc_top5[0], inputs.size(0))
label_output = np.concatenate(label_output, axis=0)
np.savetxt(lable_path, label_output, fmt='%d')
pred_output = np.concatenate(pred_output, axis=0)
np.savetxt(pred_path, pred_output, fmt='%f')
print('Test: accuracy_top1 {:.3f} accuracy_top5 {:.3f}, time: {:.2f}s'
.format(acces_top1.avg, acces_top5.avg, time.time() - t_start))
logging.debug('Test: accuracy_top1 %.3f accuracy_top5 %.3f, time: %.2fs',
acces_top1.avg, acces_top5.avg, time.time() - t_start)
def accuracy(output, target, topk=1):
batch_size = target.size(0)
_, pred = output.topk(topk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
correct = correct.view(-1).float().sum(0, keepdim=True)
return correct.mul_(100.0 / batch_size)
def save_checkpoint(state, filename='checkpoint.pth.tar', is_best=False):
torch.save(state, filename)
if is_best:
shutil.copyfile(filename, 'model_best.pth.tar')
def get_n_params(model):
pp = 0
for p in list(model.parameters()):
nn = 1
for s in list(p.size()):
nn = nn * s
pp += nn
return pp
class LabelSmoothingLoss(nn.Module):
def __init__(self, classes, smoothing=0.0, dim=-1):
super(LabelSmoothingLoss, self).__init__()
self.confidence = 1.0 - smoothing
self.smoothing = smoothing
self.cls = classes
self.dim = dim
def forward(self, pred, target):
pred = pred.log_softmax(dim=self.dim)
with torch.no_grad():
true_dist = torch.zeros_like(pred)
true_dist.fill_(self.smoothing / (self.cls - 1))
true_dist.scatter_(1, target.data.unsqueeze(1), self.confidence)
return torch.mean(torch.sum(-true_dist * pred, dim=self.dim))
if __name__ == '__main__':
main()