forked from EdwinKim3069/WaRP-CIFSL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
354 lines (262 loc) · 10.7 KB
/
utils.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
import random
import torch
import os
import time
import numpy as np
import pprint as pprint
from torch.utils.data import DataLoader, Sampler
import random
from tqdm import tqdm
from models import *
from collections import OrderedDict, defaultdict
from copy import deepcopy
_utils_pp = pprint.PrettyPrinter()
def pprint(x):
_utils_pp.pprint(x)
def set_seed(seed):
if seed == -1:
print('random seed')
torch.backends.cudnn.benchmark = True
else:
print('manual seed:', seed)
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def set_gpu(args):
gpu_list = [int(x) for x in args.gpu.split(',')]
print('use gpu:', gpu_list)
os.environ['CUDA_DEVICE_ORDER'] = 'PCI_BUS_ID'
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
return gpu_list.__len__()
def ensure_path(path):
if os.path.exists(path):
pass
else:
print('create folder:', path)
os.makedirs(path)
class Averager():
def __init__(self):
self.v = 0
self.acc_v = 0
self.n = 0
def add(self, x, current_n=1):
# self.acc_v += x * current_n
self.acc_v += x
self.n += current_n
self.v = self.acc_v / self.n
def item(self):
try:
return self.acc_v / self.n
except ZeroDivisionError:
return 0.
class Averager_Loss():
def __init__(self):
self.v = 0
self.acc_v = 0
self.n = 0
def add(self, x, current_n=1):
self.acc_v += x * current_n
# self.acc_v += x
self.n += current_n
self.v = self.acc_v / self.n
def item(self):
return self.v
class Timer():
def __init__(self):
self.o = time.time()
def measure(self, p=1):
x = (time.time() - self.o) / p
x = int(x)
if x >= 3600:
return '{:.1f}h'.format(x / 3600)
if x >= 60:
return '{}m'.format(round(x / 60))
return '{}s'.format(x)
def count_acc(logits, label):
pred = torch.argmax(logits, dim=1)
if torch.cuda.is_available():
return (pred == label).sum().item()
else: # only for gpu setup
pass
def save_list_to_txt(name, input_list):
f = open(name, mode='w')
for item in input_list:
f.write(str(item) + '\n')
f.close()
class BatchSampler(Sampler):
def __init__(self, dataset, num_iterations, batch_size):
super().__init__(None)
self.dataset = dataset
self.num_iterations = num_iterations
self.batch_size = batch_size
def __iter__(self):
for _ in range(self.num_iterations):
indices = random.sample(range(len(self.dataset)), self.batch_size)
yield indices
def __len__(self):
return self.num_iterations
def compute_orthonormal(args, net, trainset):
training = net.training
indices = torch.randperm(len(trainset))[:20 * args.batch_size_base]
trainset = torch.utils.data.Subset(trainset, indices)
dl = DataLoader(trainset, shuffle=False, batch_size=args.batch_size_base)
net.eval()
net.forward_covariance = None
net.batch_count = 0
cdmodule_list = [module for module in net.modules() if isinstance(module, WaRPModule)]
for m in cdmodule_list:
m.flag = False
epoch_iter = tqdm(dl)
with torch.no_grad():
for x, y in epoch_iter:
x, y = x.cuda(), y.cuda()
encoded_x = net.module.encode(x)[:, :args.base_class]
for m in cdmodule_list:
m.post_backward()
module_it = tqdm(cdmodule_list)
for m in module_it:
m.flag = True
forward_cov = getattr(m, 'forward_covariance')
V, S, UT_forward = torch.linalg.svd(forward_cov, full_matrices=True)
weight = getattr(m, 'weight')
bias = getattr(m, 'bias')
if weight.ndim != 2:
weight = weight.reshape(weight.shape[0], -1)
UT_backward = torch.eye(weight.shape[0])
UT_backward = same_device(ensure_tensor(UT_backward), weight)
UT_forward = same_device(UT_forward, weight)
basis_coefficients = UT_backward @ weight @ UT_forward.t()
m.UT_forward = UT_forward
m.UT_backward = UT_backward
m.basis_coefficients.data = basis_coefficients.data
m.basis_coeff.data = basis_coefficients.data
for m in module_it:
coefficients = getattr(m, 'basis_coefficients')
UT_forward = getattr(m, 'UT_forward')
UT_backward = getattr(m, 'UT_backward')
if m.weight.ndim != 2:
basis_coefficients = coefficients.reshape(m.weight.shape[0],
UT_forward.shape[0],
1, 1)
m.UT_forward_conv = UT_forward.reshape(UT_forward.shape[0],
m.weight.shape[1],
m.weight.shape[2],
m.weight.shape[3])
m.UT_backward_conv = UT_backward.t().reshape(m.weight.shape[0],
m.weight.shape[0],
1,
1)
m.basis_coeff.data = basis_coefficients.data
net.training = training
def identify_importance(args, model, trainset, batchsize=60, keep_ratio=0.1, session=0, way=10, new_labels=None):
importances = OrderedDict()
temp = OrderedDict()
dl = DataLoader(trainset, shuffle=False, batch_size=batchsize)
model.eval().cuda()
for module in model.modules():
if isinstance(module, WaRPModule):
module.coeff_mask_prev = module.coeff_mask.data
module.coeff_mask.data = torch.zeros(module.coeff_mask.shape).cuda().data
training = model.training
epoch_iter = tqdm(dl)
for i, batch in enumerate(epoch_iter):
if session == 0:
x, y = [_.cuda() for _ in batch]
else:
x, y = batch.cuda(), new_labels.cuda()[i * batchsize:(i+1) * batchsize]
yhat = model(x)[:, :args.base_class + session * way]
loss = nn.CrossEntropyLoss()(yhat, y)
model.zero_grad()
loss.backward()
for module in model.modules():
if isinstance(module, WaRPModule):
temp[module] = module.basis_coeff.grad.abs().detach().cpu().numpy().copy()
for module in model.modules():
if isinstance(module, WaRPModule):
if module not in importances:
importances[module] = temp[module]
else:
importances[module] += temp[module]
flat_importances = flatten_importances_module(importances)
threshold = fraction_threshold(flat_importances, keep_ratio)
masks = importance_masks_module(importances, threshold)
for module in model.modules():
if isinstance(module, WaRPModule):
coeff_mask = masks[module]
coeff_mask = same_device(ensure_tensor(coeff_mask), module.basis_coefficients)
module.coeff_mask.data = 1 - (1 - coeff_mask.data) * (1 - module.coeff_mask_prev.data)
# -------------------------- get accumulative mask ratio ---------------------------------
for module in model.modules():
if isinstance(module, WaRPModule):
masks[module] = module.coeff_mask.data.detach().cpu().numpy().copy()
print(flatten_importances_module(masks).mean())
# ----------------------------------------------------------------------------------------
model.zero_grad()
model.training = training
for module in model.modules():
if hasattr(module, 'weight') and not isinstance(module, WaRPModule):
for param in module.parameters():
param.requires_grad = False
return model
def flatten_importances_module(importances):
return np.concatenate([
params.flatten()
for _, params in importances.items()
])
def map_importances_module_dict(fn, importances):
return {module: fn(params)
for module, params in importances.items()}
def importance_masks_module(importances, threshold):
return map_importances_module_dict(lambda imp: threshold_mask(imp, threshold), importances)
def fraction_threshold(tensor, fraction):
"""Compute threshold quantile for a given scoring function
Given a tensor and a fraction of parameters to keep,
computes the quantile so that only the specified fraction
are larger than said threshold after applying a given scoring
function. By default, magnitude pruning is applied so absolute value
is used.
Arguments:
tensor {numpy.ndarray} -- Tensor to compute threshold for
fraction {float} -- Fraction of parameters to keep
Returns:
float -- Threshold
"""
assert isinstance(tensor, np.ndarray)
threshold = np.quantile(tensor, 1-fraction)
return threshold
def threshold_mask(tensor, threshold):
"""Given a fraction or threshold, compute binary mask
Arguments:
tensor {numpy.ndarray} -- Array to compute the mask for
Keyword Arguments:
threshold {float} -- Absolute threshold for dropping params
Returns:
np.ndarray -- Binary mask
"""
assert isinstance(tensor, np.ndarray)
idx = np.logical_and(tensor < threshold, tensor > -threshold)
mask = np.ones_like(tensor)
mask[idx] = 0
return mask
def restore_weight(net):
cdmodule_list = [module for module in net.modules() if isinstance(module, WaRPModule)]
for module in cdmodule_list:
weight = getattr(module, 'weight')
UT_forward = getattr(module, 'UT_forward')
UT_backward = getattr(module, 'UT_backward')
coeff_mask = getattr(module, 'coeff_mask').reshape(weight.shape[0], -1)
coeff_mask = same_device(ensure_tensor(coeff_mask), module.basis_coeff.data)
weight_res = UT_backward.t() @ module.basis_coeff.data.reshape(coeff_mask.shape) @ UT_forward
weight_res = weight_res.reshape(weight.shape)
module.weight.data = weight_res.data
return net
def compute_accum_ratio(model, session):
masks = OrderedDict()
for module in model.modules():
if isinstance(module, WaRPModule):
masks[module] = module.coeff_mask.data.detach().cpu().numpy().copy()
logs = dict(num_session=session, keep_ratio=flatten_importances_module(masks).mean().item())
return logs