-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
273 lines (214 loc) · 10 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
#!/usr/bin/env python
# coding: utf-8
#
# Author: Kazuto Nakashima
# URL: https://github.com/kazuto1011
# Created: 2017-04-20
from __future__ import print_function
import argparse
import os.path as osp
from datetime import datetime
import tensorboard_logger as logger
import torch
import torch.nn.functional as F
import torch.optim as optim
import torchvision
from torchvision import transforms
from torch.autograd import Variable
from models import VGG, ResNetCifar10, BKVGG12, CNN_SIFT
from facedata import FaceData
import numpy as np
import os
import time
parser = argparse.ArgumentParser(
description='Place Categorization on Sparse MPO')
parser.add_argument('--batch-size', type=int, default=256, metavar='N')
parser.add_argument('--test-batch-size', type=int, default=256, metavar='N')
parser.add_argument('--epochs', type=int, default=500, metavar='N')
parser.add_argument('--no-cuda', action='store_true', default=False)
parser.add_argument('--seed', type=int, default=1, metavar='S')
parser.add_argument('--log-interval', type=int, default=50, metavar='N')
parser.add_argument('--save-interval', type=int, default=50, metavar='N')
parser.add_argument('--fold', type=int, default=0)
parser.add_argument('--optimizer', type=str, default='adam')
parser.add_argument('--model', type=str, required=True)
parser.add_argument('--lr', type=float, default=0.0001)
parser.add_argument('--lr-decay', type=float, default=0.1)
parser.add_argument('--lr-decay-after', type=float, default=250)
parser.add_argument('--weight-decay', type=float, default=1e-4)
parser.add_argument('--tensorboard', action='store_true')
parser.add_argument('--dropout-rate', type=float, default=0.5)
parser.add_argument('--write-csv', type=bool, default=False)
args = parser.parse_args()
args.cuda = not args.no_cuda and torch.cuda.is_available()
print('Arguments')
for arg in vars(args):
print('{0:20s}: {1}'.format(arg.rjust(20), getattr(args, arg)))
class average_meter(object):
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
train_history = list()
test_history = list()
def train(epoch, model, optimizer, loader):
print("Learning rate " + str(args.lr))
losses = average_meter()
accuracy = average_meter()
model.train()
for batch_idx, (data, target) in enumerate(loader):
#data=data.type(torch.FloatTensor)
#target=target.type(torch.LongTensor)
if args.cuda:
data, target = data.cuda(), target.cuda()
#print("Input Type")
#print(type(data))
data, target = Variable(data).float(), Variable(target)
output = model(data)
loss = F.nll_loss(output, target)
losses.update(loss.data[0], data.size(0))
pred = output.data.max(1)[1]
prec = pred.eq(target.data).cpu().sum()
accuracy.update(float(prec) / data.size(0), data.size(0))
optimizer.zero_grad()
loss.backward()
optimizer.step()
if batch_idx % args.log_interval == 0:
print('Train Epoch: {}\t'
'Batch: [{:5d}/{:5d} ({:3.0f}%)]\t'
'Loss: {:.6f}'.format(
epoch, batch_idx * len(data), len(loader.dataset),
100. * batch_idx / len(loader), losses.val))
print('Training accuracy:', accuracy.val )
if args.tensorboard:
logger.log_value('train_loss', losses.avg, epoch)
logger.log_value('train_accuracy', accuracy.avg, epoch)
train_history.append((epoch, losses.avg, accuracy.avg))
return accuracy.avg
def vldtn(epoch, model, optimizer, loader):
losses = average_meter()
accuracy = average_meter()
model.eval()
for data, target in loader:
if args.cuda:
data, target = data.cuda(), target.cuda()
data, target = Variable(data, volatile=True).float(), Variable(
target, volatile=True)
output = model(data)
loss = F.nll_loss(output, target)
losses.update(loss.data[0], data.size(0))
pred = output.data.max(1)[1]
prec = pred.eq(target.data).cpu().sum()
accuracy.update(float(prec) / data.size(0), data.size(0))
print('\nTest: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)\n'.format(
losses.avg, int(accuracy.sum), len(loader.dataset), 100. * accuracy.avg))
if args.tensorboard:
logger.log_value('test_loss', losses.avg, epoch)
logger.log_value('test_accuracy', accuracy.avg, epoch)
test_history.append((epoch, losses.avg, accuracy.avg))
return accuracy.avg
def test(model, loader):
losses = average_meter()
accuracy = average_meter()
model.eval()
for data, target in loader:
if args.cuda:
data, target = data.cuda(), target.cuda()
data, target = Variable(data, volatile=True).float(), Variable(
target, volatile=True)
bs, ncrops, c, h, w = data.size()
temp_output = model(data.view(-1, c, h, w))
output = temp_output.view(bs, ncrops, -1).mean(1)
loss = F.nll_loss(output, target)
losses.update(loss.data[0], data.size(0))
pred = output.data.max(1)[1]
prec = pred.eq(target.data).cpu().sum()
accuracy.update(float(prec) / data.size(0), data.size(0))
print('\nTest: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)\n'.format(
losses.avg, int(accuracy.sum), len(loader.dataset), 100. * accuracy.avg))
return accuracy.avg
def main():
if args.tensorboard:
logger.configure(
osp.join('log',
'model_{}'.format(args.model),
'batchsize_{}'.format(args.batch_size),
'optimizer_{}'.format(args.optimizer),
'lr_{}'.format(args.lr),
'weightdecay_{}'.format(args.weight_decay),
datetime.now().isoformat()), flush_secs=1)
# data augmentation
transform_train = transforms.Compose([transforms.RandomHorizontalFlip(),
transforms.RandomRotation(45),
transforms.RandomResizedCrop(42, scale=(0.875, 1.125), ratio=(1.0, 1.0)),
#transforms.RandomCrop(42)
transforms.ToTensor(),
transforms.Normalize((0.507395516207, ),(0.255128989415, ))
])
validation_transform = transforms.Compose([transforms.Resize(42),
transforms.ToTensor(),
transforms.Normalize((0.507395516207,), (0.255128989415,))
])
test_transform = transforms.Compose([transforms.TenCrop(42),
transforms.Lambda(lambda crops: torch.stack([transforms.ToTensor()(crop) for crop in crops])),
transforms.Lambda(lambda crops: torch.stack([transforms.Normalize((0.507395516207,), (0.255128989415,))(crop) for crop in crops])),
])
trn_dataset = FaceData(dataset_csv="data/fer2013.csv", dataset_type='Training', transform=transform_train)
val_dataset = FaceData(dataset_csv="data/fer2013.csv", dataset_type='PublicTest', transform=validation_transform)
tst_dataset = FaceData(dataset_csv="data/fer2013.csv", dataset_type='PrivateTest', transform=test_transform)
train_loader = torch.utils.data.DataLoader(trn_dataset, batch_size=args.batch_size, shuffle=False, num_workers=4)
valid_loader = torch.utils.data.DataLoader(val_dataset, batch_size=args.test_batch_size, shuffle=False, num_workers=4)
test1_loader = torch.utils.data.DataLoader(tst_dataset, batch_size=args.test_batch_size, shuffle=False, num_workers=4)
model = {
'vgg': VGG(),
'resnet20': ResNetCifar10(n_block=3),
'resnet32': ResNetCifar10(n_block=4),
'resnet44': ResNetCifar10(n_block=5),
'resnet56': ResNetCifar10(n_block=6),
'resnet110': ResNetCifar10(n_block=18),
'bkvgg12': BKVGG12(7, dropout_rate=args.dropout_rate),
"cnn_sift": CNN_SIFT(7, args.cuda)
}.get(args.model)
if args.cuda:
model.cuda()
optimizer = {
'adam': optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay),
'momentum_sgd': optim.SGD(model.parameters(), args.lr, momentum=0.9, weight_decay=args.weight_decay),
'nesterov_sgd': optim.SGD(model.parameters(), args.lr, momentum=0.9, weight_decay=args.weight_decay, nesterov=True),
}.get(args.optimizer)
results = []
best_model = model
best_accuray = 0.0
for epoch in range(1, args.epochs + 1):
lr = args.lr * (0.1 ** (epoch // args.lr_decay_after))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
train_accuracy = train(epoch, model, optimizer, train_loader)
val_accuracy = vldtn(epoch, model, optimizer, valid_loader)
results.append((model, train_accuracy, val_accuracy))
if epoch % args.save_interval == 0:
torch.save(model.state_dict(),
'log/{}_epoch{}.model'.format(args.model, epoch))
if best_accuray < val_accuracy:
best_model = model
best_accuray = val_accuracy
if(args.write_csv):
key = time.time()
directory = ('training%f').format(str(key))
os.makedirs(key)
np.savetxt(directory + "/train_history.csv", train_history, delimiter=",", header="epoch,loss, accuracy", comments="")
np.savetxt(directory + "/test_history.csv", test_history, delimiter=",", header="epoch,loss, accuracy", comments="")
print ("The best model has an accuracy of " + str(best_accuray))
torch.save(best_model.state_dict(), 'best.model')
#Test on Private Test
test(best_model, test1_loader)
if __name__ == '__main__':
main()