-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain_test_alt_ewc.py
439 lines (328 loc) · 13.8 KB
/
train_test_alt_ewc.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
################################################################################
# Copyright (c) 2019. Vincenzo Lomonaco, Massimo Caccia, Pau Rodriguez, #
# Lorenzo Pellegrini. All rights reserved. #
# Copyrights licensed under the CC BY 4.0 License. #
# See the accompanying LICENSE file for terms. #
# #
# Date: 8-11-2019 #
# Author: Vincenzo Lomonaco #
# E-mail: [email protected] #
# Website: vincenzolomonaco.com #
################################################################################
"""
General useful functions for machine learning with Pytorch.
"""
# Python 2-3 compatible
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import numpy as np
import torch
from torch.autograd import Variable
from .common import pad_data, shuffle_in_unison, check_ext_mem, check_ram_usage
def train_net(optimizer, model, criterion, mb_size, x, y, t,
train_ep, preproc=None, use_cuda=True, mask=None):
"""
Train a Pytorch model from pre-loaded tensors.
Args:
optimizer (object): the pytorch optimizer.
model (object): the pytorch model to train.
criterion (func): loss function.
mb_size (int): mini-batch size.
x (tensor): train data.
y (tensor): train labels.
t (int): task label.
train_ep (int): number of training epochs.
preproc (func): test iterations.
use_cuda (bool): if we want to use gpu or cpu.
mask (bool): if we want to maks out some classes from the results.
Returns:
ave_loss (float): average loss across the train set.
acc (float): average accuracy over training.
stats (dict): dictionary of several stats collected.
"""
cur_ep = 0
cur_train_t = t
stats = {"ram": [], "disk": []}
if preproc:
x = preproc(x)
(train_x, train_y), it_x_ep = pad_data(
[x, y], mb_size
)
shuffle_in_unison(
[train_x, train_y], 0, in_place=True
)
model = maybe_cuda(model, use_cuda=use_cuda)
acc = None
ave_loss = 0
train_x = torch.from_numpy(train_x).type(torch.FloatTensor)
train_y = torch.from_numpy(train_y).type(torch.LongTensor)
for ep in range(train_ep):
stats['disk'].append(check_ext_mem("cl_ext_mem"))
stats['ram'].append(check_ram_usage())
model.active_perc_list = []
model.train()
print("training ep: ", ep)
correct_cnt, ave_loss = 0, 0
for it in range(it_x_ep):
start = it * mb_size
end = (it + 1) * mb_size
optimizer.zero_grad()
x_mb = maybe_cuda(train_x[start:end], use_cuda=use_cuda)
y_mb = maybe_cuda(train_y[start:end], use_cuda=use_cuda)
logits = model(x_mb)
_, pred_label = torch.max(logits, 1)
correct_cnt += (pred_label == y_mb).sum()
loss = criterion(logits, y_mb)
ave_loss += loss.item()
loss.backward()
optimizer.step()
acc = correct_cnt.item() / \
((it + 1) * y_mb.size(0))
ave_loss /= ((it + 1) * y_mb.size(0))
if it % 100 == 0:
print(
'==>>> it: {}, avg. loss: {:.6f}, '
'running train acc: {:.3f}'
.format(it, ave_loss, acc)
)
cur_ep += 1
return ave_loss, acc, stats
def preprocess_imgs(img_batch, scale=True, norm=True, channel_first=True):
"""
Here we get a batch of PIL imgs and we return them normalized as for
the pytorch pre-trained models.
Args:
img_batch (tensor): batch of images.
scale (bool): if we want to scale the images between 0 an 1.
channel_first (bool): if the channel dimension is before of after
the other dimensions (width and height).
norm (bool): if we want to normalize them.
Returns:
tensor: pre-processed batch.
"""
if scale:
# convert to float in [0, 1]
img_batch = img_batch / 255
if norm:
# normalize
img_batch[:, :, :, 0] = ((img_batch[:, :, :, 0] - 0.485) / 0.229)
img_batch[:, :, :, 1] = ((img_batch[:, :, :, 1] - 0.456) / 0.224)
img_batch[:, :, :, 2] = ((img_batch[:, :, :, 2] - 0.406) / 0.225)
if channel_first:
# Swap channel dimension to fit the caffe format (c, w, h)
img_batch = np.transpose(img_batch, (0, 3, 1, 2))
return img_batch
def maybe_cuda(what, use_cuda=True, **kw):
"""
Moves `what` to CUDA and returns it, if `use_cuda` and it's available.
Args:
what (object): any object to move to eventually gpu
use_cuda (bool): if we want to use gpu or cpu.
Returns
object: the same object but eventually moved to gpu.
"""
if use_cuda is not False and torch.cuda.is_available():
what = what.cuda()
return what
def test_multitask(
model, test_set, mb_size, preproc=None, use_cuda=True, multi_heads=[], verbose=True):
"""
Test a model considering that the test set is composed of multiple tests
one for each task.
Args:
model (nn.Module): the pytorch model to test.
test_set (list): list of (x,y,t) test tuples.
mb_size (int): mini-batch size.
preproc (func): image preprocess function.
use_cuda (bool): if we want to use gpu or cpu.
multi_heads (list): ordered list of "heads" to be used for each
task.
Returns:
stats (float): collected stasts of the test including average and
per class accuracies.
"""
model.eval()
acc_x_task = []
stats = {'accs': [], 'acc': []}
preds = []
for (x, y), t in test_set:
if preproc:
x = preproc(x)
if multi_heads != [] and len(multi_heads) > t:
# we can use the stored head
if verbose:
print("Using head: ", t)
model.fc = multi_heads[t]
model = maybe_cuda(model, use_cuda=use_cuda)
acc = None
test_x = torch.from_numpy(x).type(torch.FloatTensor)
test_y = torch.from_numpy(y).type(torch.LongTensor)
correct_cnt, ave_loss = 0, 0
with torch.no_grad():
iters = test_y.size(0) // mb_size + 1
for it in range(iters):
start = it * mb_size
end = (it + 1) * mb_size
x_mb = maybe_cuda(test_x[start:end], use_cuda=use_cuda)
y_mb = maybe_cuda(test_y[start:end], use_cuda=use_cuda)
logits = model(x_mb)
_, pred_label = torch.max(logits, 1)
correct_cnt += (pred_label == y_mb).sum()
preds += list(pred_label.data.cpu().numpy())
# print(pred_label)
# print(y_mb)
acc = correct_cnt.item() / test_y.shape[0]
if verbose:
print('TEST Acc. Task {}==>>> acc: {:.3f}'.format(t, acc))
acc_x_task.append(acc)
stats['accs'].append(acc)
stats['acc'].append(np.mean(acc_x_task))
if verbose:
print("------------------------------------------")
print("Avg. acc:", stats['acc'])
print("------------------------------------------")
# reset the head for the next batch
if multi_heads:
if verbose:
print("classifier reset...")
classifier = torch.nn.Linear(512, 50)
return stats, preds
# Start Modification
def train_net_ewc(optimizer, model, criterion, mb_size, x, y, t, fisher_dict, optpar_dict, ewc_lambda,
train_ep, preproc=None, use_cuda=True, mask=None):
"""
Train a Pytorch model from pre-loaded tensors.
Use EWC to normalize training for CL.
Args:
optimizer (object): the pytorch optimizer.
model (object): the pytorch model to train.
criterion (func): loss function.
mb_size (int): mini-batch size. we use 32.
x (tensor): train data.
y (tensor): train labels.
t (int): task label.
train_ep (int): number of training epochs.
preproc (func): test iterations.
use_cuda (bool): if we want to use gpu or cpu.
mask (bool): if we want to maks out some classes from the results.
Returns:
ave_loss (float): average loss across the train set.
acc (float): average accuracy over training.
stats (dict): dictionary of several stats collected.
"""
cur_ep = 0
cur_train_t = t
stats = {"ram": [], "disk": []}
if preproc:
x = preproc(x)
(train_x, train_y), it_x_ep = pad_data(
[x, y], mb_size
)
shuffle_in_unison(
[train_x, train_y], 0, in_place=True
)
model = maybe_cuda(model, use_cuda=use_cuda)
acc = None
ave_loss = 0
train_x = torch.from_numpy(train_x).type(torch.FloatTensor)
train_y = torch.from_numpy(train_y).type(torch.LongTensor)
for ep in range(train_ep):
stats['disk'].append(check_ext_mem("cl_ext_mem"))
stats['ram'].append(check_ram_usage())
model.active_perc_list = []
model.train()
print("training ep: ", ep)
correct_cnt, ave_loss = 0, 0
for it in range(it_x_ep):
start = it * mb_size
end = (it + 1) * mb_size
optimizer.zero_grad()
x_mb = maybe_cuda(train_x[start:end], use_cuda=use_cuda)
y_mb = maybe_cuda(train_y[start:end], use_cuda=use_cuda)
logits = model(x_mb)
_, pred_label = torch.max(logits, 1)
correct_cnt += (pred_label == y_mb).sum()
loss = criterion(logits, y_mb)
ave_loss += loss.item()
# Start Modification
# Add EWC Penalty
if t != 0:
# use EWC
for name, param in model.named_parameters(): # for each weight
fisher = fisher_dict[name] # get the fisher value for the given task and weight
optpar = optpar_dict[name] # get the parameter optimized value for the given task and weight
loss += (fisher * (optpar - param).pow(2)).sum() * ewc_lambda # loss is accumulator # add penalty for current task and weight
# End Modification
loss.backward()
optimizer.step()
acc = correct_cnt.item() / \
((it + 1) * y_mb.size(0))
ave_loss /= ((it + 1) * y_mb.size(0))
if it % 100 == 0:
print(
'==>>> it: {}, avg. loss: {:.6f}, '
'running train acc: {:.3f}'
.format(it, ave_loss, acc)
)
cur_ep += 1
return ave_loss, acc, stats
# Function to comput the fisher information for each weight at the end of each task
def on_task_update(t, x, y, fisher_dict, optpar_dict, model, optimizer, criterion, mb_size, use_cuda=True, mask=None, preproc=None):
"""
INPUT:
task_id: integer representing the task number
x_mem: current x_train values
t_mem: current true y_train values (aka target values)
OUTPUT:
The new values are added to the fisher and optpar dictionaries.
fisher_dict[task_id]
optpar_dict[task_id]
"""
cur_ep = 0
cur_train_t = t
if preproc:
x = preproc(x)
(train_x, train_y), it_x_ep = pad_data(
[x, y], mb_size
)
# shuffle_in_unison(
# [train_x, train_y], 0, in_place=True
# )
model = maybe_cuda(model, use_cuda=use_cuda)
acc = None
ave_loss = 0
train_x = torch.from_numpy(train_x).type(torch.FloatTensor)
train_y = torch.from_numpy(train_y).type(torch.LongTensor)
model.active_perc_list = []
model.train() # model in train mode
# loop through batches
# prepare minibatch
# get loss
print("Updating Fisher values and old parameters")
correct_cnt, ave_loss = 0, 0
for it in range(it_x_ep):
start = it * mb_size
end = (it + 1) * mb_size
optimizer.zero_grad()
x_mb = maybe_cuda(train_x[start:end], use_cuda=use_cuda)
y_mb = maybe_cuda(train_y[start:end], use_cuda=use_cuda)
logits = model(x_mb)
_, pred_label = torch.max(logits, 1)
correct_cnt += (pred_label == y_mb).sum()
loss = criterion(logits, y_mb)
ave_loss += loss.item()
loss.backward()
# Update optpar_dict and fisher_dict for EWC
for name, param in model.named_parameters(): # for every parameter save two values
optpar = param.data.clone() # save optimized gradient value for current task i and current gradient location j
fisher = param.grad.data.clone().pow(2) # save fisher value for current task i and current gradient location j
if t == 0: # first task. Just save weights and fisher values for next round
optpar_dict[name] = optpar
fisher_dict[name] = fisher
else:
optpar_dict[name] = optpar # save weights for next round
fisher_dict[name] = (((fisher_dict[name]/(t+1))*t) + (fisher / (t+1))) # average together old and new fisher values. save for use on next training round.
# End Modification