-
Notifications
You must be signed in to change notification settings - Fork 12
/
learner.py
312 lines (234 loc) · 12.2 KB
/
learner.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
import os
# import shutil
import torch
from utils import Bar, Logger, AverageMeter, accuracy, mkdir_p, savefig
import torch.optim as optim
import time
import pickle
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import pdb
class Learner():
def __init__(self,model,args,trainloader,testloader,old_model,use_cuda, path, fixed_path, train_path, infer_path):
self.model=model
self.args=args
self.title='cifar-100-' + self.args.arch
self.trainloader=trainloader
self.use_cuda=use_cuda
self.state= {key:value for key, value in self.args.__dict__.items() if not key.startswith('__') and not callable(key)}
self.best_acc = 0
self.testloader=testloader
self.start_epoch=self.args.start_epoch
self.test_loss=0.0
self.path = path
self.fixed_path = fixed_path
self.train_path = train_path
self.infer_path = infer_path
self.test_acc=0.0
self.train_loss, self.train_acc=0.0,0.0
self.old_model=old_model
if self.args.sess > 0: self.old_model.eval()
trainable_params = []
if(self.args.dataset=="MNIST"):
params_set = [self.model.mlp1, self.model.mlp2]
else:
params_set = [self.model.conv1, self.model.conv2, self.model.conv3, self.model.conv4, self.model.conv5, self.model.conv6, self.model.conv7, self.model.conv8, self.model.conv9]
for j, params in enumerate(params_set):
for i, param in enumerate(params):
if(i==self.args.M):
p = {'params': param.parameters()}
trainable_params.append(p)
else:
if(self.train_path[j,i]==1):
p = {'params': param.parameters()}
trainable_params.append(p)
else:
param.requires_grad = False
p = {'params': self.model.final_layers[-1].parameters()}
trainable_params.append(p)
print("Number of layers being trained : " , len(trainable_params))
# self.optimizer = optim.Adadelta(trainable_params)
# self.optimizer = optim.SGD(trainable_params, lr=self.args.lr, momentum=0.96, weight_decay=0)
self.optimizer = optim.Adam(trainable_params, lr=self.args.lr, betas=(0.9, 0.999), eps=1e-08, weight_decay=0, amsgrad=False)
def learn(self):
if self.args.resume:
# Load checkpoint.
print('==> Resuming from checkpoint..')
assert os.path.isfile(self.args.resume), 'Error: no checkpoint directory found!'
self.args.checkpoint = os.path.dirname(self.args.resume)
checkpoint = torch.load(self.args.resume)
self.best_acc = checkpoint['best_acc']
self.start_epoch = checkpoint['epoch']
self.model.load_state_dict(checkpoint['state_dict'])
self.optimizer.load_state_dict(checkpoint['optimizer'])
logger = Logger(os.path.join(self.args.checkpoint, 'log.txt'), title=self.title, resume=True)
else:
logger = Logger(os.path.join(self.args.checkpoint, 'session_'+str(self.args.sess)+'_'+str(self.args.test_case)+'_log.txt'), title=self.title)
logger.set_names(['Learning Rate', 'Train Loss', 'Valid Loss', 'Train Acc.', 'Valid Acc.', 'Best Acc'])
if self.args.evaluate:
print('\nEvaluation only')
self.test(self.start_epoch)
print(' Test Loss: %.8f, Test Acc: %.2f' % (self.test_loss, self.test_acc))
return
for epoch in range(self.args.start_epoch, self.args.epochs):
self.adjust_learning_rate(epoch)
print('\nEpoch: [%d | %d] LR: %f Sess: %d' % (epoch + 1, self.args.epochs, self.state['lr'],self.args.sess))
self.train(epoch, self.infer_path, -1)
self.test(epoch, self.infer_path, -1)
# append logger file
logger.append([self.state['lr'], self.train_loss, self.test_loss, self.train_acc, self.test_acc, self.best_acc])
# save model
is_best = self.test_acc > self.best_acc
self.best_acc = max(self.test_acc, self.best_acc)
self.save_checkpoint({
'epoch': epoch + 1,
'state_dict': self.model.state_dict(),
'acc': self.test_acc,
'best_acc': self.best_acc,
'optimizer' : self.optimizer.state_dict(),
}, is_best, checkpoint=self.args.savepoint,filename='session_'+str(self.args.sess)+'_' + str(self.args.test_case)+'_checkpoint.pth.tar',session=self.args.sess, test_case=self.args.test_case)
logger.close()
logger.plot()
savefig(os.path.join(self.args.checkpoint, 'log.eps'))
print('Best acc:')
print(self.best_acc)
def train(self, epoch, path, last):
# switch to train mode
self.model.train()
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
end = time.time()
bar = Bar('Processing', max=len(self.trainloader))
for batch_idx, (inputs, targets) in enumerate(self.trainloader):
# measure data loading time
data_time.update(time.time() - end)
targets_one_hot = torch.FloatTensor(inputs.shape[0], self.args.num_class)
targets_one_hot.zero_()
targets_one_hot.scatter_(1, targets[:,None], 1)
if self.use_cuda:
inputs, targets_one_hot,targets = inputs.cuda(), targets_one_hot.cuda(),targets.cuda()
inputs, targets_one_hot,targets = torch.autograd.Variable(inputs), torch.autograd.Variable(targets_one_hot),torch.autograd.Variable(targets)
# compute output
outputs = self.model(inputs, path, -1)
preds=outputs.masked_select(targets_one_hot.eq(1))
tar_ce=targets
pre_ce=outputs.clone()
pre_ce=pre_ce[:,0:self.args.class_per_task*(1+self.args.sess)]
loss = F.cross_entropy(pre_ce,tar_ce)
loss_dist = 0
## distillation loss
if self.args.sess > 0:
outputs_old=self.old_model(inputs, path, -1)
t_one_hot=targets_one_hot.clone()
t_one_hot[:,0:self.args.class_per_task*self.args.sess]=outputs_old[:,0:self.args.class_per_task*self.args.sess]
if(self.args.sess in range(1+self.args.jump)):
cx = 1
else:
cx = self.args.rigidness_coff*(self.args.sess-self.args.jump)
loss_dist = ( cx/self.args.train_batch*1.0)* torch.sum(F.kl_div(F.log_softmax(outputs/2.0,dim=1),F.softmax(t_one_hot/2.0,dim=1),reduce=False).clamp(min=0.0))
loss+=loss_dist
# measure accuracy and record loss
if(self.args.dataset=="MNIST"):
prec1, prec5 = accuracy(output=outputs.data[:,0:self.args.class_per_task*(1+self.args.sess)], target=targets.cuda().data, topk=(1, 1))
else:
prec1, prec5 = accuracy(output=outputs.data[:,0:self.args.class_per_task*(1+self.args.sess)], target=targets.cuda().data, topk=(1, 5))
losses.update(loss.item(), inputs.size(0))
top1.update(prec1.item(), inputs.size(0))
top5.update(prec5.item(), inputs.size(0))
# compute gradient and do SGD step
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
# plot progress
bar.suffix = '({batch}/{size}) | Total: {total:} | Loss: {loss:.4f} | Dist: {loss_dist:.4f} | top1: {top1: .4f} | top5: {top5: .4f} '.format(
batch=batch_idx + 1,
size=len(self.trainloader),
# data=data_time.avg,
# bt=batch_time.avg,
total=bar.elapsed_td,
# eta=bar.eta_td,
loss=losses.avg,
loss_dist=loss_dist,
top1=top1.avg,
top5=top5.avg
)
bar.next()
bar.finish()
self.train_loss,self.train_acc=losses.avg, top1.avg
def test(self, epoch, path, last):
batch_time = AverageMeter()
data_time = AverageMeter()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to evaluate mode
self.model.eval()
end = time.time()
bar = Bar('Processing', max=len(self.testloader))
for batch_idx, (inputs, targets) in enumerate(self.testloader):
# measure data loading time
data_time.update(time.time() - end)
targets_one_hot = torch.FloatTensor(inputs.shape[0], self.args.num_class)
targets_one_hot.zero_()
targets_one_hot.scatter_(1, targets[:,None], 1)
if self.use_cuda:
inputs, targets_one_hot,targets = inputs.cuda(), targets_one_hot.cuda(),targets.cuda()
inputs, targets_one_hot, targets = torch.autograd.Variable(inputs), torch.autograd.Variable(targets_one_hot) ,torch.autograd.Variable(targets)
outputs = self.model(inputs, path, -1)
loss = F.cross_entropy(outputs, targets)
# measure accuracy and record loss
if(self.args.dataset=="MNIST"):
prec1, prec5 = accuracy(outputs.data[:,0:self.args.class_per_task*(1+self.args.sess)], targets.cuda().data, topk=(1, 1))
else:
prec1, prec5 = accuracy(outputs.data[:,0:self.args.class_per_task*(1+self.args.sess)], targets.cuda().data, topk=(1, 5))
losses.update(loss.item(), inputs.size(0))
top1.update(prec1.item(), inputs.size(0))
top5.update(prec5.item(), inputs.size(0))
# measure elapsed time
batch_time.update(time.time() - end)
end = time.time()
# plot progress
bar.suffix = '({batch}/{size}) Total: {total:} | Loss: {loss:.4f} | top1: {top1: .4f} | top5: {top5: .4f}'.format(
batch=batch_idx + 1,
size=len(self.testloader),
# data=data_time.avg,
# bt=batch_time.avg,
total=bar.elapsed_td,
# eta=bar.eta_td,
loss=losses.avg,
top1=top1.avg,
top5=top5.avg
)
bar.next()
bar.finish()
self.test_loss= losses.avg;self.test_acc= top1.avg
def save_checkpoint(self,state, is_best, checkpoint='checkpoint', filename='checkpoint.pth.tar',session=0, test_case=0):
# filepath = os.path.join(checkpoint, filename)
# torch.save(state, filepath)
if is_best:
torch.save(state, os.path.join(checkpoint, 'session_'+str(session)+'_'+str(test_case)+'_model_best.pth.tar'))
# shutil.copyfile(filepath, os.path.join(checkpoint, 'session_'+str(session)+'_'+str(test_case)+'_model_best.pth.tar') )
def adjust_learning_rate(self, epoch):
if epoch in self.args.schedule:
self.state['lr'] *= self.args.gamma
for param_group in self.optimizer.param_groups:
param_group['lr'] = self.state['lr']
def get_confusion_matrix(self, path):
confusion_matrix = torch.zeros(100, 100)
with torch.no_grad():
for i, (inputs, targets) in enumerate(self.testloader):
inputs = inputs.cuda()
targets = targets.cuda()
outputs = self.model(inputs, path, -1)
_, preds = torch.max(outputs, 1)
for t, p in zip(targets.view(-1), preds.view(-1)):
confusion_matrix[t.long(), p.long()] += 1
print(confusion_matrix)
return confusion_matrix