-
Notifications
You must be signed in to change notification settings - Fork 8
/
train.py
280 lines (215 loc) · 9.36 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
from __future__ import print_function
import argparse
import shutil
import time
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim
import torch.utils.data
import torchvision.transforms as transforms
from wresnet_models import *
from h5_dataloaders import *
parser = argparse.ArgumentParser(description='SETI Classifier - Train Model')
parser.add_argument('arch', metavar='PATH',
help='architecture to use')
parser.add_argument('h5data', metavar='PATH',
help='path to hdf5 file with training and validation data')
parser.add_argument('-j', '--workers', default=1, type=int, metavar='N',
help='number of data loading workers (default: 1)')
parser.add_argument('--epochs', default=100, type=int, metavar='N',
help='number of total epochs to run')
parser.add_argument('--start-epoch', default=0, type=int, metavar='N',
help='manual epoch number (useful on restarts)')
parser.add_argument('-b', '--batch-size', default=4 * 3, type=int,
metavar='N', help='mini-batch size')
parser.add_argument('--lr', '--learning-rate', default=0.1, type=float,
metavar='LR', help='initial learning rate')
parser.add_argument('--momentum', default=0.9, type=float, metavar='M',
help='momentum')
parser.add_argument('--weight-decay', '--wd', default=1e-4, type=float,
metavar='W', help='weight decay (default: 1e-4)')
parser.add_argument('--print-freq', '-p', default=10, type=int,
metavar='N', help='print frequency (default: 10)')
parser.add_argument('--resume', default='', type=str, metavar='PATH',
help='path to latest checkpoint (default: none)')
best_acc = 0
epochs_since_improvement = 0
# Available models
# model_archs = ['resnet18', 'resnet34', 'resnet50', 'resnet86', 'resnet101', 'resnet131', 'resnet203', 'resnet152',
# 'resrnn2x2', 'resrnn2x3', 'resrnn3x2', 'resrnn3x3', 'resrnn3x10', 'wresnet28x10', 'wresnet16x8',
# 'wresnet34x2', 'wresnet40x10', 'wresnet28x20', 'densenet161', 'densenet201', 'dpn92', 'dpn98',
# 'dpn131']
model_archs = ['wresnet34x2']
classes = ['brightpixel', 'narrowband', 'narrowbanddrd', 'noise', 'squarepulsedn', 'squiggle',
'squigglesquar']
target_class_index_mapping = {}
for i, c in enumerate(classes):
target_class_index_mapping[c] = i
def main():
"""
Load model's graph, loss function, optimizer, dataloaders.
Perform training one epoch at a time, validating after each epoch. Each epoch's model is written to file,
and the best model thus far is written to a seperate file.
"""
global args, best_acc, epochs_since_improvement
args = parser.parse_args()
print("\n\nChosen args:")
print(args)
assert args.arch in model_archs
model = eval(args.arch + '()')
print("\n\nMODEL ARCHITECTURE:\n\n")
print(model)
model = torch.nn.DataParallel(model).cuda() # data parallelism over GPUs
criterion = nn.CrossEntropyLoss().cuda()
optimizer = torch.optim.SGD(model.parameters(), args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay)
cudnn.benchmark = True
# Normalizer
h = h5py.File(args.h5data, 'r')
mean = torch.FloatTensor(h['mean'][:])
mean = mean.permute(2, 0, 1) # permute to feature dimensions first
std_dev = torch.FloatTensor(h['std_dev'][:])
std_dev = std_dev.permute(2, 0, 1)
h.close()
normalize = transforms.Normalize(mean=mean,
std=std_dev)
# Custom dataloaders
train_loader = torch.utils.data.DataLoader(
h5Dataset(args.h5data, [2, 3, 4, 5], target_class_index_mapping, transforms.Compose([normalize])),
batch_size=args.batch_size, shuffle=True,
num_workers=args.workers, pin_memory=True)
val_loader = torch.utils.data.DataLoader(
h5Dataset(args.h5data, [1], target_class_index_mapping, transforms.Compose([normalize])),
batch_size=args.batch_size, shuffle=True,
num_workers=args.workers, pin_memory=True)
for epoch in range(args.start_epoch, args.epochs):
# Halve learning rate if there is no improvement for 3 consecutive epochs, and terminate training after 8
if epochs_since_improvement == 8:
break
if epochs_since_improvement > 0 and epochs_since_improvement % 3 == 0:
adjust_learning_rate(optimizer, 0.5)
train(train_loader, model, criterion, optimizer, epoch)
acc = validate(val_loader, model, criterion)
is_best = acc > best_acc
if not is_best:
epochs_since_improvement += 1
print("\nEpochs since last improvement: %d\n" % (epochs_since_improvement,))
else:
epochs_since_improvement = 0
best_acc = max(acc, best_acc)
save_checkpoint({
'epoch': epoch + 1,
'state_dict': model.state_dict(),
'best_acc': best_acc,
'optimizer': optimizer.state_dict(),
}, is_best)
def train(train_loader, model, criterion, optimizer, epoch):
"""
Perform one epoch's training.
"""
batch_time = AverageMeter() # forward prop. + gradient descent time this batch
data_time = AverageMeter() # data loading time this batch
losses = AverageMeter() # loss this batch
top1 = AverageMeter() # (top1) accuracy this batch
model.train() # train mode
start = time.time()
for i, (input, target) in enumerate(train_loader):
data_time.update(time.time() - start)
input_var = torch.autograd.Variable(input).cuda()
target = target.cuda(async=True)
target_var = torch.autograd.Variable(target)
output = model(input_var)
loss = criterion(output, target_var)
acc = accuracy(output.data, target)
losses.update(loss.data[0], input.size(0))
top1.update(acc, input.size(0))
optimizer.zero_grad()
loss.backward()
optimizer.step()
batch_time.update(time.time() - start)
start = time.time()
if i % args.print_freq == 0:
print('Epoch: [{0}][{1}/{2}]\t'
'Batch Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Data Load Time {data_time.val:.3f} ({data_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Accuracy {top1.val:.3f} ({top1.avg:.3f})'.format(
epoch, i, len(train_loader), batch_time=batch_time,
data_time=data_time, loss=losses, top1=top1))
def validate(val_loader, model, criterion):
"""
Perform validation after each training cycle.
Returns:
top1.avg (float): Average accuracy on the validation data
"""
batch_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
model.eval() # eval mode
start = time.time()
for i, (input, target) in enumerate(val_loader):
target = target.cuda(async=True)
input_var = torch.autograd.Variable(input, volatile=True).cuda()
target_var = torch.autograd.Variable(target, volatile=True).cuda()
output = model(input_var)
loss = criterion(output, target_var)
acc = accuracy(output.data, target)
losses.update(loss.data[0], input.size(0))
top1.update(acc, input.size(0))
batch_time.update(time.time() - start)
start = time.time()
if i % args.print_freq == 0:
print('Validation: [{0}/{1}]\t'
'Batch Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Accuracy {top1.val:.3f} ({top1.avg:.3f})'.format(
i, len(val_loader), batch_time=batch_time, loss=losses,
top1=top1))
print('\n * Accuracy {top1.avg:.3f}\n'
.format(top1=top1))
return top1.avg
def save_checkpoint(state, is_best):
"""
Saves model state to a checkpoint.
If this is an improved model, also save to a seperate file.
"""
filename = args.arch + '_batchsize' + str(args.batch_size) + '_checkpoint.pth.tar'
torch.save(state, filename)
if is_best:
shutil.copyfile(filename, 'BEST_' + filename)
class AverageMeter(object):
"""
Keeps track of most recent, average, sum, and count of a metric.
"""
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 adjust_learning_rate(optimizer, shrink_factor):
"""
Shrinks learning rate by a specified factor.
"""
print("\nDECAYING learning rate.")
for param_group in optimizer.param_groups:
param_group['lr'] = param_group['lr'] * shrink_factor
print("The new learning rate is %.3f\n" % (optimizer.param_groups[0]['lr'],))
def accuracy(output, target):
"""
Computes accuracy, from predicted and true labels.
"""
batch_size = target.size(0)
_, pred = output.topk(1, 1, True, True)
correct = pred.eq(target.view(-1, 1).expand_as(pred))
correct_total = correct.float().sum()
return correct_total * (100.0 / batch_size)
if __name__ == '__main__':
main()