-
Notifications
You must be signed in to change notification settings - Fork 13
/
train.py
350 lines (289 loc) · 12.8 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# original code: https://github.com/dyhan0920/PyramidNet-PyTorch/blob/master/train.py
import os
import time
import numpy as np
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim as optim
import torchvision.models as models
import models.resnet as RN
import models.resnet_ap as RNAP
import models.convnet as CN
import models.densenet_cifar as DN
from data import load_data, MEANS, STDS
from misc.utils import random_indices, rand_bbox, AverageMeter, accuracy, get_time, Plotter
from misc.augment import DiffAug
from efficientnet_pytorch import EfficientNet
import time
import warnings
warnings.filterwarnings("ignore")
model_names = sorted(
name for name in models.__dict__
if name.islower() and not name.startswith("__") and callable(models.__dict__[name]))
mean_torch = {}
std_torch = {}
for key, val in MEANS.items():
mean_torch[key] = torch.tensor(val, device='cuda').reshape(1, len(val), 1, 1)
for key, val in STDS.items():
std_torch[key] = torch.tensor(val, device='cuda').reshape(1, len(val), 1, 1)
def define_model(args, nclass, logger=None, size=None):
"""Define neural network models
"""
if size == None:
size = args.size
if args.net_type == 'resnet':
model = RN.ResNet(args.dataset,
args.depth,
nclass,
norm_type=args.norm_type,
size=size,
nch=args.nch)
elif args.net_type == 'resnet_ap':
model = RNAP.ResNetAP(args.dataset,
args.depth,
nclass,
width=args.width,
norm_type=args.norm_type,
size=size,
nch=args.nch)
elif args.net_type == 'efficient':
model = EfficientNet.from_name('efficientnet-b0', num_classes=nclass)
elif args.net_type == 'densenet':
model = DN.densenet_cifar(nclass)
elif args.net_type == 'convnet':
width = int(128 * args.width)
model = CN.ConvNet(nclass,
net_norm=args.norm_type,
net_depth=args.depth,
net_width=width,
channel=args.nch,
im_size=(args.size, args.size))
else:
raise Exception('unknown network architecture: {}'.format(args.net_type))
if logger is not None:
logger(f"=> creating model {args.net_type}-{args.depth}, norm: {args.norm_type}")
# logger('# model parameters: {:.1f}M'.format(
# sum([p.data.nelement() for p in model.parameters()]) / 10**6))
return model
def main(args, logger, repeat=1):
if args.seed >= 0:
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
cudnn.benchmark = True
_, train_loader, val_loader, nclass = load_data(args)
best_acc_l = []
acc_l = []
for i in range(repeat):
logger(f"\nRepeat: {i+1}/{repeat}")
plotter = Plotter(args.save_dir, args.epochs, idx=i)
model = define_model(args, nclass, logger)
best_acc, acc = train(args, model, train_loader, val_loader, plotter, logger)
best_acc_l.append(best_acc)
acc_l.append(acc)
logger(f'\n(Repeat {repeat}) Best, last acc: {np.mean(best_acc_l):.1f} {np.mean(acc_l):.1f}')
def train(args, model, train_loader, val_loader, plotter=None, logger=None):
criterion = nn.CrossEntropyLoss().cuda()
optimizer = optim.SGD(model.parameters(),
args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay)
scheduler = optim.lr_scheduler.MultiStepLR(
optimizer, milestones=[2 * args.epochs // 3, 5 * args.epochs // 6], gamma=0.2)
# Load pretrained
cur_epoch, best_acc1, best_acc5, acc1, acc5 = 0, 0, 0, 0, 0
if args.pretrained:
pretrained = "{}/{}".format(args.save_dir, 'checkpoint.pth.tar')
cur_epoch, best_acc1 = load_checkpoint(pretrained, model, optimizer)
# TODO: optimizer scheduler steps
# model = torch.nn.DataParallel(model).cuda()
model = model.cuda()
if args.dsa:
aug = DiffAug(strategy=args.dsa_strategy, batch=False)
logger(f"Start training with DSA and {args.mixup} mixup")
else:
aug = None
logger(f"Start training with base augmentation and {args.mixup} mixup")
# Start training and validation
# print(get_time())
for epoch in range(cur_epoch + 1, args.epochs + 1):
acc1_tr, _, loss_tr = train_epoch(args,
train_loader,
model,
criterion,
optimizer,
epoch,
logger,
aug,
mixup=args.mixup)
if epoch % args.epoch_print_freq == 0:
acc1, acc5, loss_val = validate(args, val_loader, model, criterion, epoch, logger)
if plotter != None:
plotter.update(epoch, acc1_tr, acc1, loss_tr, loss_val)
is_best = acc1 > best_acc1
if is_best:
best_acc1 = acc1
best_acc5 = acc5
if logger != None:
logger(f'Best accuracy (top-1 and 5): {best_acc1:.1f} {best_acc5:.1f}')
if args.save_ckpt and (is_best or (epoch == args.epochs)):
state = {
'epoch': epoch,
'arch': args.net_type,
'state_dict': model.state_dict(),
'best_acc1': best_acc1,
'best_acc5': best_acc5,
'optimizer': optimizer.state_dict(),
}
save_checkpoint(args.save_dir, state, is_best)
scheduler.step()
return best_acc1, acc1
def train_epoch(args,
train_loader,
model,
criterion,
optimizer,
epoch=0,
logger=None,
aug=None,
mixup='vanilla',
n_data=-1):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
model.train()
end = time.time()
num_exp = 0
for i, (input, target) in enumerate(train_loader):
if train_loader.device == 'cpu':
input = input.cuda()
target = target.cuda()
data_time.update(time.time() - end)
if aug != None:
with torch.no_grad():
input = aug(input)
r = np.random.rand(1)
if r < args.mix_p and mixup == 'cut':
# generate mixed sample
lam = np.random.beta(args.beta, args.beta)
rand_index = random_indices(target, nclass=args.nclass)
target_b = target[rand_index]
bbx1, bby1, bbx2, bby2 = rand_bbox(input.size(), lam)
input[:, :, bbx1:bbx2, bby1:bby2] = input[rand_index, :, bbx1:bbx2, bby1:bby2]
ratio = 1 - ((bbx2 - bbx1) * (bby2 - bby1) / (input.size()[-1] * input.size()[-2]))
output = model(input)
loss = criterion(output, target) * ratio + criterion(output, target_b) * (1. - ratio)
else:
# compute output
output = model(input)
loss = criterion(output, target)
# measure accuracy and record loss
acc1, acc5 = accuracy(output.data, target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(acc1.item(), input.size(0))
top5.update(acc5.item(), input.size(0))
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0 and args.verbose == True:
print('Epoch: [{0}/{1}][{2}/{3}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Top 1-acc {top1.val:.4f} ({top1.avg:.4f})\t'
'Top 5-acc {top5.val:.4f} ({top5.avg:.4f})'.format(epoch,
args.epochs,
i,
len(train_loader),
batch_time=batch_time,
data_time=data_time,
loss=losses,
top1=top1,
top5=top5))
num_exp += len(target)
if (n_data > 0) and (num_exp >= n_data):
break
if (epoch % args.epoch_print_freq == 0) and (logger is not None):
logger(
'(Train) [Epoch {0}/{1}] {2} Top1 {top1.avg:.1f} Top5 {top5.avg:.1f} Loss {loss.avg:.3f}'
.format(epoch, args.epochs, get_time(), top1=top1, top5=top5, loss=losses))
return top1.avg, top5.avg, losses.avg
def validate(args, val_loader, model, criterion, epoch, logger=None):
batch_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to evaluate mode
model.eval()
end = time.time()
for i, (input, target) in enumerate(val_loader):
input = input.cuda()
target = target.cuda()
output = model(input)
loss = criterion(output, target)
# measure accuracy and record loss
acc1, acc5 = accuracy(output.data, target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(acc1.item(), input.size(0))
top5.update(acc5.item(), input.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0 and args.verbose == True:
print('Test (on val set): [{0}/{1}][{2}/{3}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Top 1-acc {top1.val:.4f} ({top1.avg:.4f})\t'
'Top 5-acc {top5.val:.4f} ({top5.avg:.4f})'.format(epoch,
args.epochs,
i,
len(val_loader),
batch_time=batch_time,
loss=losses,
top1=top1,
top5=top5))
if logger is not None:
logger(
'(Test ) [Epoch {0}/{1}] {2} Top1 {top1.avg:.1f} Top5 {top5.avg:.1f} Loss {loss.avg:.3f}'
.format(epoch, args.epochs, get_time(), top1=top1, top5=top5, loss=losses))
return top1.avg, top5.avg, losses.avg
def load_checkpoint(path, model, optimizer):
if os.path.isfile(path):
print("=> loading checkpoint '{}'".format(path))
checkpoint = torch.load(path)
checkpoint['state_dict'] = dict(
(key[7:], value) for (key, value) in checkpoint['state_dict'].items())
model.load_state_dict(checkpoint['state_dict'])
cur_epoch = checkpoint['epoch']
best_acc1 = checkpoint['best_acc1']
optimizer.load_state_dict(checkpoint['optimizer'])
print("=> loaded checkpoint '{}'(epoch: {}, best acc1: {}%)".format(
path, cur_epoch, checkpoint['best_acc1']))
else:
print("=> no checkpoint found at '{}'".format(path))
cur_epoch = 0
best_acc1 = 100
return cur_epoch, best_acc1
def save_checkpoint(save_dir, state, is_best):
os.makedirs(save_dir, exist_ok=True)
if is_best:
ckpt_path = os.path.join(save_dir, 'model_best.pth.tar')
else:
ckpt_path = os.path.join(save_dir, 'checkpoint.pth.tar')
torch.save(state, ckpt_path)
print("checkpoint saved! ", ckpt_path)
if __name__ == '__main__':
from misc.utils import Logger
from argument import args
os.makedirs(args.save_dir, exist_ok=True)
logger = Logger(args.save_dir)
logger(f"Save dir: {args.save_dir}")
main(args, logger, args.repeat)