-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtrain.py
328 lines (262 loc) · 10.9 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
import argparse
import os
import sys
import shutil
import json
import glob
import signal
import pickle
import torch
import torch.nn as nn
from data_loader import VideoFolder
from callbacks import PlotLearning, MonitorLRDecay, AverageMeter
from model import ConvColumn
from torchvision.transforms import *
str2bool = lambda x: (str(x).lower() == 'true')
parser = argparse.ArgumentParser(
description='PyTorch Jester Training using JPEG')
parser.add_argument('--config', '-c', help='json config file path')
parser.add_argument('--eval_only', '-e', default=False, type=str2bool,
help="evaluate trained model on validation data.")
parser.add_argument('--resume', '-r', default=False, type=str2bool,
help="resume training from given checkpoint.")
parser.add_argument('--use_gpu', default=True, type=str2bool,
help="flag to use gpu or not.")
parser.add_argument('--gpus', '-g', help="gpu ids for use.")
args = parser.parse_args()
if len(sys.argv) < 2:
parser.print_help()
sys.exit(1)
device = torch.device("cuda" if args.use_gpu and torch.cuda.is_available() else "cpu")
if args.use_gpu:
gpus = [int(i) for i in args.gpus.split(',')]
print("=> active GPUs: {}".format(args.gpus))
best_prec1 = 0
# load config file
with open(args.config) as data_file:
config = json.load(data_file)
def main():
global args, best_prec1
# set run output folder
model_name = config["model_name"]
output_dir = config["output_dir"]
print("=> Output folder for this run -- {}".format(model_name))
save_dir = os.path.join(output_dir, model_name)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
os.makedirs(os.path.join(save_dir, 'plots'))
# adds a handler for Ctrl+C
def signal_handler(signal, frame):
"""
Remove the output dir, if you exit with Ctrl+C and
if there are less then 3 files.
It prevents the noise of experimental runs.
"""
num_files = len(glob.glob(save_dir + "/*"))
if num_files < 1:
shutil.rmtree(save_dir)
print('You pressed Ctrl+C!')
sys.exit(0)
# assign Ctrl+C signal handler
signal.signal(signal.SIGINT, signal_handler)
# create model
model = ConvColumn(config['num_classes'])
# multi GPU setting
if args.use_gpu:
model = torch.nn.DataParallel(model, device_ids=gpus).to(device)
# optionally resume from a checkpoint
if args.resume:
if os.path.isfile(config['checkpoint']):
print("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(config['checkpoint'])
args.start_epoch = checkpoint['epoch']
best_prec1 = checkpoint['best_prec1']
model.load_state_dict(checkpoint['state_dict'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(config['checkpoint'], checkpoint['epoch']))
else:
print("=> no checkpoint found at '{}'".format(
config['checkpoint']))
transform = Compose([
CenterCrop(84),
ToTensor(),
Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
train_data = VideoFolder(root=config['train_data_folder'],
csv_file_input=config['train_data_csv'],
csv_file_labels=config['labels_csv'],
clip_size=config['clip_size'],
nclips=1,
step_size=config['step_size'],
is_val=False,
transform=transform,
)
print(" > Using {} processes for data loader.".format(
config["num_workers"]))
train_loader = torch.utils.data.DataLoader(
train_data,
batch_size=config['batch_size'], shuffle=True,
num_workers=config['num_workers'], pin_memory=True,
drop_last=True)
val_data = VideoFolder(root=config['val_data_folder'],
csv_file_input=config['val_data_csv'],
csv_file_labels=config['labels_csv'],
clip_size=config['clip_size'],
nclips=1,
step_size=config['step_size'],
is_val=True,
transform=transform,
)
val_loader = torch.utils.data.DataLoader(
val_data,
batch_size=config['batch_size'], shuffle=False,
num_workers=config['num_workers'], pin_memory=True,
drop_last=False)
assert len(train_data.classes) == config["num_classes"]
# define loss function (criterion) and optimizer
criterion = nn.CrossEntropyLoss().to(device)
# define optimizer
lr = config["lr"]
last_lr = config["last_lr"]
momentum = config['momentum']
weight_decay = config['weight_decay']
optimizer = torch.optim.SGD(model.parameters(), lr,
momentum=momentum,
weight_decay=weight_decay)
if args.eval_only:
validate(val_loader, model, criterion, train_data.classes_dict)
return
# set callbacks
plotter = PlotLearning(os.path.join(
save_dir, "plots"), config["num_classes"])
lr_decayer = MonitorLRDecay(0.6, 3)
val_loss = 9999999
# set end condition by num epochs
num_epochs = int(config["num_epochs"])
if num_epochs == -1:
num_epochs = 999999
print(" > Training is getting started...")
print(" > Training takes {} epochs.".format(num_epochs))
start_epoch = args.start_epoch if args.resume else 0
for epoch in range(start_epoch, num_epochs):
lr = lr_decayer(val_loss, lr)
print(" > Current LR : {}".format(lr))
if lr < last_lr and last_lr > 0:
print(" > Training is done by reaching the last learning rate {}".
format(last_lr))
sys.exit(1)
# train for one epoch
train_loss, train_top1, train_top5 = train(
train_loader, model, criterion, optimizer, epoch)
# evaluate on validation set
val_loss, val_top1, val_top5 = validate(val_loader, model, criterion)
# plot learning
plotter_dict = {}
plotter_dict['loss'] = train_loss
plotter_dict['val_loss'] = val_loss
plotter_dict['acc'] = train_top1
plotter_dict['val_acc'] = val_top1
plotter_dict['learning_rate'] = lr
plotter.plot(plotter_dict)
# remember best prec@1 and save checkpoint
is_best = val_top1 > best_prec1
best_prec1 = max(val_top1, best_prec1)
save_checkpoint({
'epoch': epoch + 1,
'arch': "Conv4Col",
'state_dict': model.state_dict(),
'best_prec1': best_prec1,
}, is_best, config)
def train(train_loader, model, criterion, optimizer, epoch):
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to train mode
model.train()
for i, (input, target) in enumerate(train_loader):
input, target = input.to(device), target.to(device)
model.zero_grad()
# compute output and loss
output = model(input)
loss = criterion(output, target)
# measure accuracy and record loss
prec1, prec5 = accuracy(output.detach(), target.detach().cpu(), topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(prec1.item(), input.size(0))
top5.update(prec5.item(), input.size(0))
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
if i % config["print_freq"] == 0:
print('Epoch: [{0}][{1}/{2}]\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t'
'Prec@5 {top5.val:.3f} ({top5.avg:.3f})'.format(
epoch, i, len(train_loader), loss=losses, top1=top1, top5=top5))
return losses.avg, top1.avg, top5.avg
def validate(val_loader, model, criterion, class_to_idx=None):
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to evaluate mode
model.eval()
logits_matrix = []
targets_list = []
with torch.no_grad():
for i, (input, target) in enumerate(val_loader):
input, target = input.to(device), target.to(device)
# compute output and loss
output = model(input)
loss = criterion(output, target)
if args.eval_only:
logits_matrix.append(output.detach().cpu().numpy())
targets_list.append(target.detach().cpu().numpy())
# measure accuracy and record loss
prec1, prec5 = accuracy(output.detach(), target.detach().cpu(), topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(prec1.item(), input.size(0))
top5.update(prec5.item(), input.size(0))
if i % config["print_freq"] == 0:
print('Test: [{0}/{1}]\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Prec@1 {top1.val:.3f} ({top1.avg:.3f})\t'
'Prec@5 {top5.val:.3f} ({top5.avg:.3f})'.format(
i, len(val_loader), loss=losses, top1=top1, top5=top5))
print(' * Prec@1 {top1.avg:.3f} Prec@5 {top5.avg:.3f}'
.format(top1=top1, top5=top5))
if args.eval_only:
logits_matrix = np.concatenate(logits_matrix)
targets_list = np.concatenate(targets_list)
print(logits_matrix.shape, targets_list.shape)
save_results(logits_matrix, targets_list, class_to_idx, config)
return losses.avg, top1.avg, top5.avg
def save_results(logits_matrix, targets_list, class_to_idx, config):
print("Saving inference results ...")
path_to_save = os.path.join(
config['output_dir'], config['model_name'], "test_results.pkl")
with open(path_to_save, "wb") as f:
pickle.dump([logits_matrix, targets_list, class_to_idx], f)
def save_checkpoint(state, is_best, config, filename='checkpoint.pth.tar'):
checkpoint_path = os.path.join(
config['output_dir'], config['model_name'], filename)
model_path = os.path.join(
config['output_dir'], config['model_name'], 'model_best.pth.tar')
torch.save(state, checkpoint_path)
if is_best:
shutil.copyfile(checkpoint_path, model_path)
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.cpu().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
if __name__ == '__main__':
main()