-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain_linear_buffer_domain.py
329 lines (257 loc) · 10.8 KB
/
main_linear_buffer_domain.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
'''
The original source code can be found in
https://github.com/HobbitLong/SupContrast/blob/master/main_linear.py
'''
from __future__ import print_function
import os
import sys
import argparse
import time
import math
import numpy as np
import torch
import torch.backends.cudnn as cudnn
from main_ce_buffer_domain import set_loader
from util import AverageMeter
from util import adjust_learning_rate, warmup_learning_rate, accuracy
from util import set_optimizer
from networks.mlp import SupConMLP, LinearClassifier
from torch.utils.tensorboard import SummaryWriter
try:
import apex
from apex import amp, optimizers
except ImportError:
pass
def parse_option():
parser = argparse.ArgumentParser('argument for training')
parser.add_argument('--target_task', type=int, default=0, help='Use all classes if None else learned tasks so far')
parser.add_argument('--print_freq', type=int, default=500,
help='print frequency')
parser.add_argument('--save_freq', type=int, default=50,
help='save frequency')
parser.add_argument('--batch_size', type=int, default=256,
help='batch_size')
parser.add_argument('--num_workers', type=int, default=16,
help='num of workers to use')
parser.add_argument('--epochs', type=int, default=100,
help='number of training epochs')
# optimization
parser.add_argument('--learning_rate', type=float, default=0.1,
help='learning rate')
parser.add_argument('--lr_decay_epochs', type=str, default='60,75,90',
help='where to decay lr, can be a list')
parser.add_argument('--lr_decay_rate', type=float, default=0.2,
help='decay rate for learning rate')
parser.add_argument('--weight_decay', type=float, default=0,
help='weight decay')
parser.add_argument('--momentum', type=float, default=0.9,
help='momentum')
# model dataset
parser.add_argument('--model', type=str, default='mlp')
parser.add_argument('--dataset', type=str, default='r-mnist',
choices=['r-mnist'], help='dataset')
# other setting
parser.add_argument('--cosine', action='store_true',
help='using cosine annealing')
parser.add_argument('--warm', action='store_true',
help='warm-up for large batch training')
parser.add_argument('--ckpt', type=str, default='',
help='path to pre-trained model')
parser.add_argument('--logpt', type=str, default='',
help='path to pre-trained model')
opt = parser.parse_args()
# set the path according to the environment
opt.data_folder = '~/data/'
opt.tb_folder = os.path.join(opt.ckpt, 'tensorboard', 'linear_eval')
iterations = opt.lr_decay_epochs.split(',')
opt.lr_decay_epochs = list([])
for it in iterations:
opt.lr_decay_epochs.append(int(it))
opt.model_name = '{}_{}_lr_{}_decay_{}_bsz_{}'.\
format(opt.dataset, opt.model, opt.learning_rate, opt.weight_decay,
opt.batch_size)
if opt.cosine:
opt.model_name = '{}_cosine'.format(opt.model_name)
# warm-up for large-batch training,
if opt.warm:
opt.model_name = '{}_warm'.format(opt.model_name)
opt.warmup_from = 0.01
opt.warm_epochs = 10
if opt.cosine:
eta_min = opt.learning_rate * (opt.lr_decay_rate ** 3)
opt.warmup_to = eta_min + (opt.learning_rate - eta_min) * (
1 + math.cos(math.pi * opt.warm_epochs / opt.epochs)) / 2
else:
opt.warmup_to = opt.learning_rate
if opt.dataset == 'r-mnist':
opt.n_cls = 10
opt.cls_per_task = 10
else:
raise ValueError('dataset not supported: {}'.format(opt.dataset))
opt.origin_ckpt = opt.ckpt
opt.ckpt = os.path.join(opt.ckpt, 'last_random_{target_task}.pth'.format(target_task=opt.target_task))
logpt = opt.logpt
opt.logpt = os.path.join(logpt, 'replay_indices_random_{target_task}.npy'.format(target_task=opt.target_task))
opt.logpt2 = os.path.join(logpt, 'replay_degrees_random_{target_task}.npy'.format(target_task=opt.target_task))
opt.logpt3 = os.path.join(logpt, 'degree_list_random_{target_task}.npy'.format(target_task=opt.target_task))
return opt
def set_model(opt):
model = SupConMLP()
criterion = torch.nn.CrossEntropyLoss()
classifier = LinearClassifier(num_classes=opt.n_cls)
ckpt = torch.load(opt.ckpt, map_location='cpu')
state_dict = ckpt['model']
if torch.cuda.is_available():
if torch.cuda.device_count() > 1:
model.encoder = torch.nn.DataParallel(model.encoder)
else:
new_state_dict = {}
for k, v in state_dict.items():
k = k.replace("module.", "")
new_state_dict[k] = v
state_dict = new_state_dict
model = model.cuda()
classifier = classifier.cuda()
criterion = criterion.cuda()
cudnn.benchmark = True
model.load_state_dict(state_dict)
return model, classifier, criterion
def train(train_loader, model, classifier, criterion, optimizer, epoch, opt):
"""one epoch training"""
model.eval()
classifier.train()
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
end = time.time()
acc = 0.0
cnt = 0.0
for idx, (images, labels) in enumerate(train_loader):
data_time.update(time.time() - end)
images = images.cuda(non_blocking=True)
labels = labels.cuda(non_blocking=True)
bsz = labels.shape[0]
# warm-up learning rate
warmup_learning_rate(opt, epoch, idx, len(train_loader), optimizer)
# compute loss
with torch.no_grad():
features = model.encoder(images)
output = classifier(features.detach())
loss = criterion(output, labels)
# update metric
losses.update(loss.item(), bsz)
acc += (output.argmax(1) == labels).float().sum().item()
cnt += bsz
# SGD
optimizer.zero_grad()
loss.backward()
optimizer.step()
# measure elapsed time
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'
'Acc@1 {top1:.3f}'.format(
epoch, idx + 1, len(train_loader), batch_time=batch_time,
data_time=data_time, loss=losses, top1=acc/cnt*100.))
sys.stdout.flush()
return losses.avg, acc/cnt*100.
def validate(val_loader, model, classifier, criterion, opt):
"""validation"""
model.eval()
classifier.eval()
batch_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
corr = torch.zeros(opt.n_cls)
cnt = torch.zeros(opt.n_cls)
with torch.no_grad():
end = time.time()
for idx, (images, labels) in enumerate(val_loader):
images = images.float().cuda()
labels = labels.cuda()
bsz = labels.shape[0]
# forward
output = classifier(model.encoder(images))
loss = criterion(output, labels)
# update metric
losses.update(loss.item(), bsz)
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
correct_all = (output.argmax(1) == labels)
for c in np.unique(labels.cpu().numpy()):
mask = labels == c
corr[c] += correct_all[mask].float().sum().item()
cnt[c] += mask.float().sum().item()
if idx % opt.print_freq == 0:
print('Test: [{0}/{1}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'
'Acc@1 {top1:.3f}'.format(
idx, len(val_loader), batch_time=batch_time,
loss=losses, top1=(corr.sum()/cnt.sum()).item()*100.))
print((torch.sum(corr)/torch.sum(cnt)).item()*100.)
print(' * Acc@1 {top1:.3f}'.format(top1=(corr.sum()/cnt.sum()).item()*100.))
return losses.avg, top1.avg, corr.numpy(), cnt.numpy(), (corr.sum()/cnt.sum()).item()*100.
def main():
best_acc = 0
opt = parse_option()
if opt.target_task is not None:
replay_indices = np.load(opt.logpt)
replay_degrees = np.load(opt.logpt2)
degree_list = np.load(opt.logpt3)
print(np.unique(replay_degrees))
print(len(replay_indices), len(replay_degrees), degree_list)
# build data loader
train_loader, val_loader = set_loader(opt, replay_indices, replay_degrees, degree_list)
print(len(train_loader.dataset))
# build model and criterion
model, classifier, criterion = set_model(opt)
# build optimizer
optimizer = set_optimizer(opt, classifier)
print( optimizer.param_groups[0]['lr'])
# tensorboard
writer = SummaryWriter(log_dir=opt.tb_folder)
# training routine
val_acc_list = []
for epoch in range(1, opt.epochs + 1):
adjust_learning_rate(opt, optimizer, epoch)
# train for one epoch
time1 = time.time()
loss, acc = train(train_loader, model, classifier, criterion,
optimizer, epoch, opt)
time2 = time.time()
print('Train epoch {}, total time {:.2f}, accuracy:{:.2f} {:.3f}'.format(
epoch, time2 - time1, acc, optimizer.param_groups[0]['lr']))
# eval for one epoch
loss, val_acc, val_corr, val_cnt, report_acc = validate(val_loader, model, classifier, criterion, opt)
val_acc = report_acc
val_acc_list.append(val_acc)
if val_acc > best_acc:
best_acc = val_acc
val_acc_stats = {}
for cls, (cr, c) in enumerate(zip(val_corr, val_cnt)):
if c > 0:
val_acc_stats[str(cls)] = cr / c * 100.
writer.add_scalars('val_acc', val_acc_stats, epoch)
with open(os.path.join(opt.origin_ckpt, 'acc_buffer_{}.txt'.format(opt.target_task)), 'w') as f:
out = 'best accuracy: {:.2f}'.format(best_acc)
print(out)
out += '\n'
for k, v in val_acc_stats.items():
print(v)
out += '{}\n'.format(v)
f.write(out)
f.write("====\n")
out = ''
for v in val_acc_list:
out += '{}\n'.format(v)
f.write(out)
if __name__ == '__main__':
main()