-
Notifications
You must be signed in to change notification settings - Fork 17
/
train_C2L_dense121.py
548 lines (442 loc) · 19.1 KB
/
train_C2L_dense121.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
"""
Code for C2L
"""
from __future__ import print_function
import os
import sys
import time
import torch
import torch.backends.cudnn as cudnn
import argparse
import socket
#import tensorboard_logger as tb_logger
from torchvision import transforms, datasets
from util import adjust_learning_rate, AverageMeter
from models.resnet_mixup import InsResNet50
from models.resnet_mixup import InsResNet18
from models.DensenetModels import DenseNet121
from NCE.NCEAverage import MemoryInsDis
from NCE.NCEAverage import MemoryC2L
from NCE.NCECriterion import NCECriterion
from NCE.NCECriterion import NCESoftmaxLoss
from read_data import ChestXrayDataSet
from dataset_pretrained import ImageFolderInstance
#from dataset import ImageFolderInstance
from DatasetGenerator import DatasetGenerator
from utils.cutout import Cutout
import numpy as np
import ipdb
try:
from apex import amp, optimizers
except ImportError:
pass
"""
TODO: python 3.6 ModuleNotFoundError
"""
def parse_option():
hostname = socket.gethostname()
parser = argparse.ArgumentParser('argument for training')
parser.add_argument('--print_freq', type=int, default=10, help='print frequency')
parser.add_argument('--tb_freq', type=int, default=500, help='tb frequency')
parser.add_argument('--save_freq', type=int, default=10, help='save frequency')
parser.add_argument('--batch_size', type=int, default=128, help='batch_size')
parser.add_argument('--num_workers', type=int, default=18, help='num of workers to use')
parser.add_argument('--epochs', type=int, default=120, help='number of training epochs')
# optimization
parser.add_argument('--learning_rate', type=float, default=0.03, help='learning rate')
parser.add_argument('--lr_decay_epochs', type=str, default='60,80,100', help='where to decay lr, can be a list')
parser.add_argument('--lr_decay_rate', type=float, default=0.1, help='decay rate for learning rate')
parser.add_argument('--beta1', type=float, default=0.5, help='beta1 for adam')
parser.add_argument('--beta2', type=float, default=0.999, help='beta2 for Adam')
parser.add_argument('--weight_decay', type=float, default=1e-4, help='weight decay')
parser.add_argument('--momentum', type=float, default=0.9, help='momentum')
# crop
parser.add_argument('--crop', type=float, default=0.2, help='minimum crop')
# dataset
parser.add_argument('--dataset', type=str, default='imagenet-200',
choices=['imagenet-200', 'imagenet100', 'imagenet'])
# resume
parser.add_argument('--resume', default='', type=str, metavar='PATH',
help='path to latest checkpoint (default: none)')
# augmentation setting
parser.add_argument('--aug', type=str, default='CJ', choices=['NULL', 'CJ'])
# warm up
parser.add_argument('--warm', action='store_true', help='add warm-up setting')
parser.add_argument('--amp', action='store_true', help='using mixed precision')
parser.add_argument('--opt_level', type=str, default='O2', choices=['O1', 'O2'])
# model definition
parser.add_argument('--model', type=str, default='resnet50',
choices=['densenet121', 'resnet18', 'resnet50', 'resnet50x2', 'resnet50x4'])
# loss function
parser.add_argument('--softmax', action='store_true', help='using softmax contrastive loss rather than NCE')
parser.add_argument('--nce_k', type=int, default=16384)
parser.add_argument('--nce_t', type=float, default=0.07)
parser.add_argument('--nce_m', type=float, default=0.5)
# memory setting
parser.add_argument('--c2l', action='store_true', help='using C2L (otherwise Instance Discrimination)')
parser.add_argument('--alpha', type=float, default=0.999, help='exponential moving average weight')
# GPU setting
parser.add_argument('--gpu', default=None, type=int, help='GPU id to use.')
opt = parser.parse_args()
# set the path according to the environment
#if hostname.startswith('visiongpu'):
opt.data_folder = '/home/C2L/{}_mixup4_pretrained'.format(opt.dataset)
opt.model_path = '/home/C2L/{}_models_mixup4_pretrained'.format(opt.dataset)
opt.tb_path = '/home/C2L/{}_tensorboard_mixup4_pretrained'.format(opt.dataset)
#else:
# raise NotImplementedError('server invalid: {}'.format(hostname))
if opt.dataset == 'imagenet':
if 'alexnet' not in opt.model:
opt.crop = 0.08
iterations = opt.lr_decay_epochs.split(',')
opt.lr_decay_epochs = list([])
for it in iterations:
opt.lr_decay_epochs.append(int(it))
opt.method = 'softmax' if opt.softmax else 'nce'
prefix = 'C2L{}'.format(opt.alpha) if opt.c2l else 'InsDis'
opt.model_name = '{}_{}_{}_{}_lr_{}_decay_{}_bsz_{}_crop_{}'.format(prefix, opt.method, opt.nce_k, opt.model,
opt.learning_rate, opt.weight_decay,
opt.batch_size, opt.crop)
if opt.warm:
opt.model_name = '{}_warm'.format(opt.model_name)
if opt.amp:
opt.model_name = '{}_amp_{}'.format(opt.model_name, opt.opt_level)
opt.model_name = '{}_aug_{}'.format(opt.model_name, opt.aug)
opt.model_folder = os.path.join(opt.model_path, opt.model_name)
if not os.path.isdir(opt.model_folder):
os.makedirs(opt.model_folder)
opt.tb_folder = os.path.join(opt.tb_path, opt.model_name)
if not os.path.isdir(opt.tb_folder):
os.makedirs(opt.tb_folder)
return opt
def moment_update(model, model_ema, m):
""" model_ema = m * model_ema + (1 - m) model """
for p1, p2 in zip(model.parameters(), model_ema.parameters()):
p2.data.mul_(m).add_(1-m, p1.detach().data)
def get_shuffle_ids(bsz):
"""generate shuffle ids for ShuffleBN"""
forward_inds = torch.randperm(bsz).long().cuda()
backward_inds = torch.zeros(bsz).long().cuda()
value = torch.arange(bsz).long().cuda()
backward_inds.index_copy_(0, forward_inds, value)
return forward_inds, backward_inds
def mixup_data(x, y, alpha=1.0, index=None, lam=None, use_cuda=True):
'''Returns mixed inputs, pairs of targets, and lambda'''
if lam is None:
lam = np.random.beta(alpha, alpha)
else:
lam = lam
lam = max(lam, 1-lam)
batch_size = x.size()[0]
if index is None:
index = torch.randperm(batch_size).cuda()
else:
index = index
mixed_x = lam * x + (1 - lam) * x[index, :]
mixed_y = lam * y + (1 - lam) * y[index]
return mixed_x, mixed_y, lam, index
def main():
args = parse_option()
if args.gpu is not None:
print("Use GPU: {} for training".format(args.gpu))
# set the data loader
#data_folder = os.path.join(args.data_folder, 'train')
data_folder = '/home/C2L/CXR/'
image_size = 224
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
normalize = transforms.Normalize(mean=mean, std=std)
if args.aug == 'NULL':
train_transform = transforms.Compose([
transforms.RandomResizedCrop(image_size, scale=(args.crop, 1.)),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])
elif args.aug == 'CJ':
train_transform = transforms.Compose([
transforms.RandomResizedCrop(image_size, scale=(args.crop, 1.)),
transforms.RandomRotation(10),
transforms.RandomGrayscale(p=0.2),
transforms.ColorJitter(0.4, 0.4, 0.4, 0.4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
normalize,
])
else:
raise NotImplemented('augmentation not supported: {}'.format(args.aug))
train_transform.transforms.append(Cutout(n_holes=3, length=32))
train_dataset = ImageFolderInstance(data_folder, transform=train_transform, two_crop=args.c2l)
print(len(train_dataset))
train_sampler = None
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=args.batch_size, shuffle=(train_sampler is None),
num_workers=args.num_workers, pin_memory=True, sampler=train_sampler)
# create model and optimizer
n_data = len(train_dataset)
if args.model == 'resnet50':
model = InsResNet50()
if args.c2l:
model_ema = InsResNet50()
elif args.model == 'resnet50x2':
model = InsResNet50(width=2)
if args.c2l:
model_ema = InsResNet50(width=2)
elif args.model == 'resnet50x4':
model = InsResNet50(width=4)
if args.c2l:
model_ema = InsResNet50(width=4)
elif args.model == 'resnet18':
model = InsResNet18(width=1)
if args.c2l:
model_ema = InsResNet18(width=1)
elif args.model == 'densenet121':
model = DenseNet121(isTrained=False)
if args.c2l:
model_ema = DenseNet121(isTrained=False)
else:
raise NotImplementedError('model not supported {}'.format(args.model))
# copy weights from `model' to `model_ema'
if args.c2l:
moment_update(model, model_ema, 0)
# set the contrast memory and criterion
if args.c2l:
contrast = MemoryC2L(128, n_data, args.nce_k, args.nce_t, args.softmax).cuda(args.gpu)
else:
contrast = MemoryInsDis(128, n_data, args.nce_k, args.nce_t, args.nce_m, args.softmax).cuda(args.gpu)
criterion = NCESoftmaxLoss() if args.softmax else NCECriterion(n_data)
criterion = criterion.cuda(args.gpu)
model = model.cuda()
if args.c2l:
model_ema = model_ema.cuda()
optimizer = torch.optim.SGD(model.parameters(),
lr=args.learning_rate,
momentum=args.momentum,
weight_decay=args.weight_decay)
cudnn.benchmark = True
if args.amp:
model, optimizer = amp.initialize(model, optimizer, opt_level=args.opt_level)
if args.c2l:
optimizer_ema = torch.optim.SGD(model_ema.parameters(),
lr=0,
momentum=0,
weight_decay=0)
model_ema, optimizer_ema = amp.initialize(model_ema, optimizer_ema, opt_level=args.opt_level)
# optionally resume from a checkpoint
args.start_epoch = 1
if args.resume:
if os.path.isfile(args.resume):
print("=> loading checkpoint '{}'".format(args.resume))
checkpoint = torch.load(args.resume, map_location='cpu')
# checkpoint = torch.load(args.resume)
args.start_epoch = checkpoint['epoch'] + 1
model.load_state_dict(checkpoint['model'])
optimizer.load_state_dict(checkpoint['optimizer'])
contrast.load_state_dict(checkpoint['contrast'])
if args.c2l:
model_ema.load_state_dict(checkpoint['model_ema'])
if args.amp and checkpoint['opt'].amp:
print('==> resuming amp state_dict')
amp.load_state_dict(checkpoint['amp'])
print("=> loaded successfully '{}' (epoch {})"
.format(args.resume, checkpoint['epoch']))
del checkpoint
torch.cuda.empty_cache()
else:
print("=> no checkpoint found at '{}'".format(args.resume))
# tensorboard
#logger = tb_logger.Logger(logdir=args.tb_folder, flush_secs=2)
# routine
for epoch in range(args.start_epoch, args.epochs + 1):
adjust_learning_rate(epoch, args, optimizer)
print("==> training...")
time1 = time.time()
if args.c2l:
loss, prob = train_C2L(epoch, train_loader, model, model_ema, contrast, criterion, optimizer, args)
else:
loss, prob = train_ins(epoch, train_loader, model, contrast, criterion, optimizer, args)
time2 = time.time()
print('epoch {}, total time {:.2f}'.format(epoch, time2 - time1))
# tensorboard logger
#logger.log_value('ins_loss', loss, epoch)
#logger.log_value('ins_prob', prob, epoch)
#logger.log_value('learning_rate', optimizer.param_groups[0]['lr'], epoch)
# save model
if epoch % args.save_freq == 0:
print('==> Saving...')
state = {
'opt': args,
'model': model.state_dict(),
'contrast': contrast.state_dict(),
'optimizer': optimizer.state_dict(),
'epoch': epoch,
}
if args.c2l:
state['model_ema'] = model_ema.state_dict()
if args.amp:
state['amp'] = amp.state_dict()
save_file = os.path.join(args.model_folder, 'ckpt_epoch_{epoch}.pth'.format(epoch=epoch))
torch.save(state, save_file)
# help release GPU memory
del state
# saving the model
print('==> Saving...')
state = {
'opt': args,
'model': model.state_dict(),
'contrast': contrast.state_dict(),
'optimizer': optimizer.state_dict(),
'epoch': epoch,
}
if args.c2l:
state['model_ema'] = model_ema.state_dict()
if args.amp:
state['amp'] = amp.state_dict()
save_file = os.path.join(args.model_folder, 'current.pth')
torch.save(state, save_file)
if epoch % args.save_freq == 0:
save_file = os.path.join(args.model_folder, 'ckpt_epoch_{epoch}.pth'.format(epoch=epoch))
torch.save(state, save_file)
# help release GPU memory
del state
torch.cuda.empty_cache()
def train_ins(epoch, train_loader, model, contrast, criterion, optimizer, opt):
"""
one epoch training for instance discrimination
"""
model.train()
batch_time = AverageMeter()
data_time = AverageMeter()
loss_meter = AverageMeter()
prob_meter = AverageMeter()
end = time.time()
for idx, (inputs, _, index) in enumerate(train_loader):
data_time.update(time.time() - end)
bsz = inputs.size(0)
inputs = inputs.float()
if opt.gpu is not None:
inputs = inputs.cuda(opt.gpu, non_blocking=True)
else:
inputs = inputs.cuda()
index = index.cuda(opt.gpu, non_blocking=True)
# ===================forward=====================
feat = model(inputs)
out = contrast(feat, index)
loss = criterion(out)
prob = out[:, 0].mean()
# ===================backward=====================
optimizer.zero_grad()
if opt.amp:
with amp.scale_loss(loss, optimizer) as scaled_loss:
scaled_loss.backward()
else:
loss.backward()
optimizer.step()
# ===================meters=====================
loss_meter.update(loss.item(), bsz)
prob_meter.update(prob.item(), bsz)
torch.cuda.synchronize()
batch_time.update(time.time() - end)
end = time.time()
# print info
if (idx + 1) % opt.print_freq == 0:
print('Train: [{0}][{1}/{2}]\t'
'BT {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'DT {data_time.val:.3f} ({data_time.avg:.3f})\t'
'loss {loss.val:.3f} ({loss.avg:.3f})\t'
'prob {prob.val:.3f} ({prob.avg:.3f})'.format(
epoch, idx + 1, len(train_loader), batch_time=batch_time,
data_time=data_time, loss=loss_meter, prob=prob_meter))
print(out.shape)
sys.stdout.flush()
return loss_meter.avg, prob_meter.avg
def Normalize(x):
norm_x = x.pow(2).sum(1, keepdim=True).pow(1. / 2.)
x = x.div(norm_x)
return x
def train_C2L(epoch, train_loader, model, model_ema, contrast, criterion, optimizer, opt):
"""
one epoch training for instance discrimination
"""
model.train()
model_ema.eval()
def set_bn_train(m):
classname = m.__class__.__name__
if classname.find('BatchNorm') != -1:
m.train()
model_ema.apply(set_bn_train)
batch_time = AverageMeter()
data_time = AverageMeter()
loss_meter = AverageMeter()
prob_meter = AverageMeter()
end = time.time()
for idx, (inputs, _, index) in enumerate(train_loader):
data_time.update(time.time() - end)
bsz = inputs.size(0)
inputs = inputs.float()
if opt.gpu is not None:
inputs = inputs.cuda(opt.gpu, non_blocking=True)
else:
inputs = inputs.cuda()
index = index.cuda(opt.gpu, non_blocking=True)
# ===================forward=====================
x1, x2 = torch.split(inputs, [3, 3], dim=1)
# ids for ShuffleBN
shuffle_ids, reverse_ids = get_shuffle_ids(bsz)
feat_q = model(x1)
with torch.no_grad():
x2 = x2[shuffle_ids]
feat_k = model_ema(x2)
feat_k = feat_k[reverse_ids]
x2 = x2[reverse_ids]
out = contrast(Normalize(feat_q), Normalize(feat_k))
#with torch.no_grad():
mixed_x1, mixed_feat1, lam1, index = mixup_data(x1.clone(),
feat_q.clone())
mixed_x2, mixed_feat2, _, _ = mixup_data(x2.clone(), feat_k.clone(),
index=index, lam=lam1)
mixed_feat_q = model(mixed_x1)
with torch.no_grad():
mixed_x2 = mixed_x2[shuffle_ids]
mixed_feat_k = model_ema(mixed_x2)
mixed_feat_k = mixed_feat_k[reverse_ids]
mixed_x2 = mixed_x2[reverse_ids]
mixed_feat_q_norm = Normalize(mixed_feat_q)
mixed_feat_k_norm = Normalize(mixed_feat_k)
mixed_feat1_norm = Normalize(mixed_feat1)
#with torch.no_grad():
mixed_feat2_norm = Normalize(mixed_feat2)
out2 = contrast(mixed_feat_q_norm, mixed_feat_k_norm)
out3 = contrast(mixed_feat_q_norm, mixed_feat2_norm)
criterion2 = torch.nn.MSELoss()
loss = (criterion(out) + criterion(out2) + criterion(out3))/3.
prob = out[:, 0].mean()
# ===================backward=====================
optimizer.zero_grad()
if opt.amp:
with amp.scale_loss(loss, optimizer) as scaled_loss:
scaled_loss.backward()
else:
loss.backward()
optimizer.step()
# ===================meters=====================
loss_meter.update(loss.item(), bsz)
prob_meter.update(prob.item(), bsz)
moment_update(model, model_ema, opt.alpha)
torch.cuda.synchronize()
batch_time.update(time.time() - end)
end = time.time()
# print info
if (idx + 1) % opt.print_freq == 0:
print('Train: [{0}][{1}/{2}]\t'
'BT {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'DT {data_time.val:.3f} ({data_time.avg:.3f})\t'
'loss {loss.val:.3f} ({loss.avg:.3f})\t'
'prob {prob.val:.3f} ({prob.avg:.3f})'.format(
epoch, idx + 1, len(train_loader), batch_time=batch_time,
data_time=data_time, loss=loss_meter, prob=prob_meter))
print(out.shape)
sys.stdout.flush()
return loss_meter.avg, prob_meter.avg
if __name__ == '__main__':
main()