-
Notifications
You must be signed in to change notification settings - Fork 80
/
train.py
194 lines (167 loc) · 7.39 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
import os
import time
import numpy as np
import torch
from config import params
from torch import nn, optim
from torch.utils.data import DataLoader
import torch.backends.cudnn as cudnn
from lib.dataset import VideoDataset
from lib import slowfastnet
from tensorboardX import SummaryWriter
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def accuracy(output, target, topk=(1,)):
"""Computes the precision@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0)
res.append(correct_k.mul_(100.0 / batch_size))
return res
def train(model, train_dataloader, epoch, criterion, optimizer, writer):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
model.train()
end = time.time()
for step, (inputs, labels) in enumerate(train_dataloader):
data_time.update(time.time() - end)
inputs = inputs.cuda()
labels = labels.cuda()
outputs = model(inputs)
loss = criterion(outputs, labels)
# measure accuracy and record loss
prec1, prec5 = accuracy(outputs.data, labels, topk=(1, 5))
losses.update(loss.item(), inputs.size(0))
top1.update(prec1.item(), inputs.size(0))
top5.update(prec5.item(), inputs.size(0))
optimizer.zero_grad()
loss.backward()
optimizer.step()
batch_time.update(time.time() - end)
end = time.time()
if (step+1) % params['display'] == 0:
print('-------------------------------------------------------')
for param in optimizer.param_groups:
print('lr: ', param['lr'])
print_string = 'Epoch: [{0}][{1}/{2}]'.format(epoch, step+1, len(train_dataloader))
print(print_string)
print_string = 'data_time: {data_time:.3f}, batch time: {batch_time:.3f}'.format(
data_time=data_time.val,
batch_time=batch_time.val)
print(print_string)
print_string = 'loss: {loss:.5f}'.format(loss=losses.avg)
print(print_string)
print_string = 'Top-1 accuracy: {top1_acc:.2f}%, Top-5 accuracy: {top5_acc:.2f}%'.format(
top1_acc=top1.avg,
top5_acc=top5.avg)
print(print_string)
writer.add_scalar('train_loss_epoch', losses.avg, epoch)
writer.add_scalar('train_top1_acc_epoch', top1.avg, epoch)
writer.add_scalar('train_top5_acc_epoch', top5.avg, epoch)
def validation(model, val_dataloader, epoch, criterion, optimizer, writer):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
model.eval()
end = time.time()
with torch.no_grad():
for step, (inputs, labels) in enumerate(val_dataloader):
data_time.update(time.time() - end)
inputs = inputs.cuda()
labels = labels.cuda()
outputs = model(inputs)
loss = criterion(outputs, labels)
# measure accuracy and record loss
prec1, prec5 = accuracy(outputs.data, labels, topk=(1, 5))
losses.update(loss.item(), inputs.size(0))
top1.update(prec1.item(), inputs.size(0))
top5.update(prec5.item(), inputs.size(0))
batch_time.update(time.time() - end)
end = time.time()
if (step + 1) % params['display'] == 0:
print('----validation----')
print_string = 'Epoch: [{0}][{1}/{2}]'.format(epoch, step + 1, len(val_dataloader))
print(print_string)
print_string = 'data_time: {data_time:.3f}, batch time: {batch_time:.3f}'.format(
data_time=data_time.val,
batch_time=batch_time.val)
print(print_string)
print_string = 'loss: {loss:.5f}'.format(loss=losses.avg)
print(print_string)
print_string = 'Top-1 accuracy: {top1_acc:.2f}%, Top-5 accuracy: {top5_acc:.2f}%'.format(
top1_acc=top1.avg,
top5_acc=top5.avg)
print(print_string)
writer.add_scalar('val_loss_epoch', losses.avg, epoch)
writer.add_scalar('val_top1_acc_epoch', top1.avg, epoch)
writer.add_scalar('val_top5_acc_epoch', top5.avg, epoch)
def main():
cudnn.benchmark = False
cur_time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
logdir = os.path.join(params['log'], cur_time)
if not os.path.exists(logdir):
os.makedirs(logdir)
writer = SummaryWriter(log_dir=logdir)
print("Loading dataset")
train_dataloader = \
DataLoader(
VideoDataset(params['dataset'], mode='train', clip_len=params['clip_len'], frame_sample_rate=params['frame_sample_rate']),
batch_size=params['batch_size'], shuffle=True, num_workers=params['num_workers'])
val_dataloader = \
DataLoader(
VideoDataset(params['dataset'], mode='validation', clip_len=params['clip_len'], frame_sample_rate=params['frame_sample_rate']),
batch_size=params['batch_size'], shuffle=False, num_workers=params['num_workers'])
print("load model")
model = slowfastnet.resnet50(class_num=params['num_classes'])
if params['pretrained'] is not None:
pretrained_dict = torch.load(params['pretrained'], map_location='cpu')
try:
model_dict = model.module.state_dict()
except AttributeError:
model_dict = model.state_dict()
pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict}
print("load pretrain model")
model_dict.update(pretrained_dict)
model.load_state_dict(model_dict)
model = model.cuda(params['gpu'][0])
model = nn.DataParallel(model, device_ids=params['gpu']) # multi-Gpu
criterion = nn.CrossEntropyLoss().cuda()
optimizer = optim.SGD(model.parameters(), lr=params['learning_rate'], momentum=params['momentum'], weight_decay=params['weight_decay'])
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=params['step'], gamma=0.1)
model_save_dir = os.path.join(params['save_path'], cur_time)
if not os.path.exists(model_save_dir):
os.makedirs(model_save_dir)
for epoch in range(params['epoch_num']):
train(model, train_dataloader, epoch, criterion, optimizer, writer)
if epoch % 2== 0:
validation(model, val_dataloader, epoch, criterion, optimizer, writer)
scheduler.step()
if epoch % 1 == 0:
checkpoint = os.path.join(model_save_dir,
"clip_len_" + str(params['clip_len']) + "frame_sample_rate_" +str(params['frame_sample_rate'])+ "_checkpoint_" + str(epoch) + ".pth.tar")
torch.save(model.module.state_dict(), checkpoint)
writer.close
if __name__ == '__main__':
main()