-
Notifications
You must be signed in to change notification settings - Fork 3
/
train_imagenet.py
484 lines (405 loc) · 19.2 KB
/
train_imagenet.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import os
import argparse
import shutil
import builtins
import csv
import random
import time
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
import torch.distributed as dist
import torch.multiprocessing as mp
import torch.nn.parallel
from torch.autograd import Variable
from composite_adv.attacks import *
from composite_adv.utilities import make_dataloader
from composite_adv.data_augmentation import TRAIN_TRANSFORMS_IMAGENET, TEST_TRANSFORMS_IMAGENET
import numpy as np
import warnings
warnings.filterwarnings('ignore')
def list_type(s):
try:
return tuple(map(int, s.split(',')))
except:
raise argparse.ArgumentTypeError("List must be (x,x,....,x) ")
parser = argparse.ArgumentParser(description='PyTorch Tiny ImageNet Natural Training')
parser.add_argument('--batch-size', type=int, default=128, metavar='N',
help='input batch size for training (default: 128)')
parser.add_argument('--epochs', type=int, default=10, metavar='N',
help='number of epochs to train')
parser.add_argument('--weight-decay', '--wd', default=1e-4,
type=float, metavar='W')
parser.add_argument('--lr', type=float, default=0.1, metavar='LR',
help='learning rate')
parser.add_argument('--momentum', type=float, default=0.9, metavar='M',
help='SGD momentum')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
parser.add_argument('--beta', default=6.0, type=float,
help='regularization, i.e., 1/lambda in TRADES')
parser.add_argument('--seed', default=None, type=int,
help='seed for initializing training. ')
parser.add_argument('--print-freq', type=int, default=10, metavar='N',
help='how many batches to wait before logging training status')
parser.add_argument('--arch', default='wideresnet',
help='architecture of model')
parser.add_argument('--model-dir', default='./model-imagenet',
help='directory of model for saving checkpoint')
parser.add_argument('--dist', default='comp', type=str,
help='distance metric')
parser.add_argument('--mode', default='natural', type=str, choices=['natural','adv_train_madry','adv_train_trades'],
help='specify training mode (natural or adv_train)')
parser.add_argument('--debug', action='store_true',
help='Train Only One Epoch and print training images.')
parser.add_argument('--checkpoint', type=str, default=None, help='path of checkpoint')
parser.add_argument('--local_rank', default=-1, type=int, help='node rank for distributed training')
parser.add_argument('--order', default='random', type=str, help='specify the order')
parser.add_argument('--stat-dict', type=str, default=None,
help='key of stat dict in checkpoint')
parser.add_argument("--enable", type=list_type, default=(0, 1), help="list of enabled attacks")
parser.add_argument("--log_filename", default='logfile.csv', help="filename of output log")
parser.add_argument('-j', '--workers', default=4, type=int, metavar='N',
help='number of data loading workers (default: 4)')
parser.add_argument('-e', '--evaluate', dest='evaluate', action='store_true',
help='evaluate model on validation set')
parser.add_argument('--world-size', default=-1, type=int, help='number of nodes for distributed training')
parser.add_argument('--rank', default=-1, type=int, help='node rank for distributed training')
parser.add_argument('--dist-backend', default='nccl', type=str, help='distributed backend')
parser.add_argument('--dist-file', default=None, type=str, help='distributed config')
parser.add_argument('--gpu', default=None, type=int, help='GPU id to use.')
parser.add_argument('--multiprocessing-distributed', action='store_true',
help='Use multi-processing distributed training to launch '
'N processes per node, which has N GPUs. This is the '
'fastest way to use PyTorch for either single node or '
'multi node data parallel training')
epoch = 0
best_acc1 = .0
iteration = 0
train_loader_len = None
start_num = 1
iter_num = 1
inner_iter_num = 7
def find_free_port():
import socket
s = socket.socket()
s.bind(('', 0)) # Bind to a free port provided by the host.
return s.getsockname()[1] # Return the port number assigned.
def main():
# settings
args = parser.parse_args()
try:
if not os.path.exists(args.model_dir):
os.makedirs(args.model_dir)
except FileExistsError:
pass
if args.seed is not None:
torch.manual_seed(args.seed)
np.random.seed(args.seed)
random.seed(args.seed)
cudnn.deterministic = True
warnings.warn('You have chosen to seed training. '
'This will turn on the CUDNN deterministic setting, '
'which can slow down your training considerably! '
'You may see unexpected behavior when restarting '
'from checkpoints.')
# slurm available
if args.world_size == -1 and "SLURM_NPROCS" in os.environ:
args.world_size = int(os.environ["SLURM_NPROCS"])
args.rank = int(os.environ["SLURM_PROCID"])
jobid = os.environ["SLURM_JOBID"]
hostfile = "dist_url." + jobid + ".txt"
if args.dist_file is not None:
args.dist_url = "file://{}.{}".format(os.path.realpath(args.dist_file), jobid)
elif args.rank == 0:
import socket
ip = socket.gethostbyname(socket.gethostname())
port = find_free_port()
args.dist_url = "tcp://{}:{}".format(ip, port)
with open(hostfile, "w") as f:
f.write(args.dist_url)
else:
while not os.path.exists(hostfile):
time.sleep(1)
with open(hostfile, "r") as f:
args.dist_url = f.read()
print("dist-url:{} at PROCID {} / {}".format(args.dist_url, args.rank, args.world_size))
args.distributed = args.world_size > 1 or args.multiprocessing_distributed
ngpus_per_node = torch.cuda.device_count()
if args.multiprocessing_distributed:
args.world_size = ngpus_per_node * args.world_size
mp.spawn(main_worker, nprocs=ngpus_per_node, args=(ngpus_per_node, args))
else:
# Simply call main_worker function
main_worker(args.gpu, ngpus_per_node, args)
def main_worker(gpu, ngpus_per_node, args):
args.gpu = gpu
if args.gpu is not None:
print("Use GPU: {} for training".format(args.gpu))
if args.distributed:
if args.dist_url == "env://" and args.rank == -1:
args.rank = int(os.environ["RANK"])
if args.multiprocessing_distributed:
# For multiprocessing distributed training, rank needs to be the
# global rank among all the processes
args.rank = args.rank * ngpus_per_node + gpu
dist.init_process_group(backend=args.dist_backend, init_method=args.dist_url,
world_size=args.world_size, rank=args.rank)
if args.multiprocessing_distributed:
def print_pass(*args, sep=' ', end='\n', file=None):
pass
builtins.print = print_pass
from composite_adv.utilities import make_model
model = make_model(args.arch, 'imagenet', checkpoint_path=args.checkpoint)
# Uncomment the following if you want to load their checkpoint to finetuning
# from composite_adv.utilities import make_madry_model, make_trades_model
# model = make_madry_model(args.arch, 'imagenet', checkpoint_path=args.checkpoint)
# Send to GPU
if not torch.cuda.is_available():
print('using CPU, this will be slow')
elif args.distributed:
if args.gpu is not None:
torch.cuda.set_device(args.gpu)
model.cuda(args.gpu)
args.batch_size = int(args.batch_size / ngpus_per_node)
args.workers = int((args.workers + ngpus_per_node - 1) / ngpus_per_node)
if args.arch == 'wideresnet':
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu],
find_unused_parameters=True)
else:
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.gpu])
else:
model.cuda()
model = torch.nn.parallel.DistributedDataParallel(model)
elif args.gpu is not None:
torch.cuda.set_device(args.gpu)
model = model.cuda(args.gpu)
else:
# DataParallel will divide and allocate batch_size to all available GPUs
if args.arch.startswith('alexnet') or args.arch.startswith('vgg'):
model.features = torch.nn.DataParallel(model.features)
model.cuda()
else:
model = torch.nn.DataParallel(model).cuda()
criterion = nn.CrossEntropyLoss().cuda(args.gpu)
optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay)
cudnn.benchmark = True
train_loader, train_sampler = make_dataloader('../data/imagenet/train/', 'imagenet', args.batch_size,
TRAIN_TRANSFORMS_IMAGENET, train=True, distributed=args.distributed)
test_loader = make_dataloader('../data/imagenet/val/', 'imagenet', args.batch_size,
TEST_TRANSFORMS_IMAGENET, train=False, distributed=args.distributed)
if args.evaluate:
validate(test_loader, model, criterion, args)
return
train(model, optimizer, criterion, train_loader, train_sampler, test_loader, args, ngpus_per_node)
def train_ep(args, model, train_loader, composite_attack, optimizer, criterion):
global epoch, iteration
batch_time = AverageMeter('Time', ':6.3f')
data_time = AverageMeter('Data', ':6.3f')
losses = AverageMeter('Loss', ':.4e')
top1 = AverageMeter('Acc@1', ':6.2f')
top5 = AverageMeter('Acc@5', ':6.2f')
progress = ProgressMeter(
len(train_loader),
[batch_time, data_time, losses, top1, top5],
prefix="Epoch: [{}]".format(epoch))
model.train()
end = time.time()
for batch_idx, (data, target) in enumerate(train_loader):
adjust_learning_rate(optimizer, epoch, args)
iteration += 1
if args.gpu is not None:
data = data.cuda(args.gpu, non_blocking=True)
target = target.cuda(args.gpu, non_blocking=True)
# clean training
if args.mode == 'natural':
logits = model(data)
loss = criterion(logits, target)
# adv training normal
elif args.mode == 'adv_train_madry':
model.eval()
# generate adversarial example
if args.gpu is not None:
data_adv = data.detach() + 0.001 * torch.randn(data.shape).cuda(args.gpu, non_blocking=True).detach()
else:
data_adv = data.detach() + 0.001 * torch.randn(data.shape).cuda().detach()
data_adv = composite_attack(data_adv, target)
data_adv = Variable(torch.clamp(data_adv, 0.0, 1.0), requires_grad=False)
model.train()
logits = model(data_adv)
loss = criterion(logits, target)
# adv training by trades
elif args.mode == 'adv_train_trades':
# TRADE Loss would require more memory.
model.eval()
batch_size = len(data)
# generate adversarial example
if args.gpu is not None:
data_adv = data.detach() + 0.001 * torch.randn(data.shape).cuda(args.gpu, non_blocking=True).detach()
else:
data_adv = data.detach() + 0.001 * torch.randn(data.shape).cuda().detach()
data_adv = composite_attack(data_adv, target)
data_adv = Variable(torch.clamp(data_adv, 0.0, 1.0), requires_grad=False)
model.train()
# calculate robust loss
logits = model(data)
loss_natural = F.cross_entropy(logits, target)
loss_robust = (1.0 / batch_size) * F.kl_div(F.log_softmax(model(data_adv), dim=1),
F.softmax(model(data), dim=1))
loss = loss_natural + args.beta * loss_robust
else:
print("Not Specify Training Mode.")
raise ValueError()
# measure adv_exp generating time
data_time.update(time.time() - end)
# measure accuracy and record loss
acc1, acc5 = accuracy(logits, target, topk=(1, 5))
losses.update(loss.item(), data.size(0))
top1.update(acc1[0].item(), data.size(0))
top5.update(acc5[0].item(), data.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 batch_idx % args.print_freq == 0:
progress.display(batch_idx)
return top1.avg, top5.avg, losses.avg
def train(model, optimizer, criterion, train_loader, train_sampler, test_loader, args, ngpus_per_node):
global best_acc1, epoch, iteration, train_loader_len
train_loader_len = len(train_loader)
iteration = epoch*train_loader_len
composite_attack = CompositeAttack(model, args.enable, mode='train', dataset='imagenet',
start_num=start_num, iter_num=iter_num, inner_iter_num=inner_iter_num,
multiple_rand_start=True, order_schedule=args.order)
for e in range(epoch, epoch + args.epochs):
epoch = e
if args.distributed:
train_sampler.set_epoch(epoch)
# adversarial training
train_acc1, train_acc5, train_loss = train_ep(args, model, train_loader, composite_attack, optimizer, criterion)
test_acc1, test_acc5, test_loss = validate(test_loader, model, criterion, args)
# remember best acc@1 and save checkpoint
is_best = test_acc1 > best_acc1
best_acc1 = max(test_acc1, best_acc1)
if not args.multiprocessing_distributed or \
(args.multiprocessing_distributed and args.rank <= 0 and args.gpu % ngpus_per_node == 0):
# if args.gpu is None or args.gpu == 0:
print("Best Test Accuracy: {}%".format(best_acc1))
# save checkpoint
save_checkpoint({
'epoch': epoch,
'arch': args.arch,
'state_dict': model.state_dict(),
'best_acc1': best_acc1,
'optimizer': optimizer.state_dict(),
}, is_best, args.model_dir)
with open(args.log_filename, 'a+') as f:
csv_write = csv.writer(f)
data_row = [epoch,
train_loss, train_acc1, train_acc5,
test_loss, test_acc1, test_acc5,
best_acc1]
csv_write.writerow(data_row)
def validate(val_loader, model, criterion, args):
batch_time = AverageMeter('Time', ':6.3f')
losses = AverageMeter('Loss', ':.4e')
top1 = AverageMeter('Acc@1', ':6.2f')
top5 = AverageMeter('Acc@5', ':6.2f')
progress = ProgressMeter(
len(val_loader),
[batch_time, losses, top1, top5],
prefix='Test: ')
# switch to evaluate mode
model.eval()
with torch.no_grad():
end = time.time()
for i, (images, target) in enumerate(val_loader):
if args.gpu is not None:
images = images.cuda(args.gpu, non_blocking=True)
if torch.cuda.is_available():
target = target.cuda(args.gpu, non_blocking=True)
# compute output
output = model(images)
loss = criterion(output, target)
# measure accuracy and record loss
acc1, acc5 = accuracy(output, target, topk=(1, 5))
losses.update(loss.item(), images.size(0))
top1.update(acc1[0].item(), images.size(0))
top5.update(acc5[0].item(), images.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
progress.display(i)
# TODO: this should also be done with the ProgressMeter
print(' * Acc@1 {top1.avg:.3f} Acc@5 {top5.avg:.3f}'
.format(top1=top1, top5=top5))
return top1.avg, top5.avg, losses.avg
def save_checkpoint(state, is_best, model_dir=None):
filename = os.path.join(model_dir, 'model-epoch{}.pt'.format(state['epoch']))
torch.save(state, filename)
print('Save model: {}'.format(filename))
if is_best:
best_cp = os.path.join(model_dir, 'model_best.pth')
print("Save best model (epoch {})!".format(state['epoch']))
shutil.copyfile(filename, best_cp)
print('Save model: {}'.format(best_cp))
print('================================================================')
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self, name, fmt=':f'):
self.name = name
self.fmt = fmt
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 __str__(self):
fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
return fmtstr.format(**self.__dict__)
class ProgressMeter(object):
def __init__(self, num_batches, meters, prefix=""):
self.batch_fmtstr = self._get_batch_fmtstr(num_batches)
self.meters = meters
self.prefix = prefix
def display(self, batch):
entries = [self.prefix + self.batch_fmtstr.format(batch)]
entries += [str(meter) for meter in self.meters]
print('\t'.join(entries))
def _get_batch_fmtstr(self, num_batches):
num_digits = len(str(num_batches // 1))
fmt = '{:' + str(num_digits) + 'd}'
return '[' + fmt + '/' + fmt.format(num_batches) + ']'
def adjust_learning_rate(optimizer, e, args):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
global train_loader_len
lr = args.lr * (0.1 ** (e // 30))
# ramp-up learning rate for SGD
if e < 5 and args.lr >= 0.1:
lr = (iteration + 1) / (5 * train_loader_len) * args.lr
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def accuracy(output, target, topk=(1,)):
"""Computes the accuracy over the k top predictions for the specified values of k"""
with torch.no_grad():
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].reshape(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
if __name__ == '__main__':
main()