-
Notifications
You must be signed in to change notification settings - Fork 0
/
embeddingreg.py
339 lines (257 loc) · 12.7 KB
/
embeddingreg.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
from utilities import AbstractNetwork, GeneralDatasetLoader
import torch
import warnings
from copy import deepcopy
import random
import torch.nn.functional as F
class embedding(object):
def __init__(self, model: AbstractNetwork, dataset: GeneralDatasetLoader, config, gpu, batch_size):
self.model = model
self.dataset = dataset
self.gpu = gpu
self.is_conv = config.IS_CONVOLUTIONAL
self.device = config.DEVICE
self.batch_size = batch_size
self.first_batch = True
self.incremental = config.IS_INCREMENTAL
self.handle = None
self.sample_size = config.CL_PAR.get('sample_size')
self.memorized_task_size = config.CL_PAR.get('memorized_task_size')
self.importance = config.CL_PAR.get('penalty_importance')
self.c = config.CL_PAR.get('c', 1)
self.distance = config.CL_PAR.get('distance', 'euclidean')
self.supervised = config.CL_PAR.get('supervised', False)
self.normalize = config.CL_PAR.get('normalize', True)
self.mul = config.CL_PAR.get('mul', 1)
self.online = config.CL_PAR.get('online', False)
self.memory_size = config.CL_PAR.get('memory_size', -1)
if 0 < self.memory_size < self.memorized_task_size:
self.memorized_task_size = self.memory_size
# Can be distance, usage, image_similarity or none
self.weights_type = config.CL_PAR.get('weights_type', None)
if self.weights_type == 'image_similarity':
# img_size = self.dataset[0][0].size()
if self.is_conv:
pass
else:
self.encoder = torch.nn.Sequential(
torch.nn.Linear(224*224*3, 300),
torch.nn.ReLU(),
torch.nn.Linear(300, 200),
torch.nn.ReLU(),
torch.nn.Linear(200, 100),
).to(config.DEVICE)
# Can be None, a tuple (a, b) or an integer. IF a tuple (a, b) is given then these are the input and
# output size of the layer, otherwise if a single integer is given then
# it will be the dimension for both input and output
external_embedding = config.CL_PAR.get('external_embedding', None)
if external_embedding is not None:
a = b = None
if isinstance(external_embedding, int):
a = external_embedding
b = a
elif isinstance(external_embedding, (tuple, list)):
a, b = external_embedding
if a is not None:
self.external_embedding_layer = torch.nn.Linear(a, b, bias=False).to(self.device)
torch.nn.init.orthogonal_(self.external_embedding_layer.weight.data)
for param in self.external_embedding_layer.parameters():
param.requires_grad = False
self.model.external_embedding_layer = self.external_embedding_layer
else:
warnings.warn("Possible values for external_embedding are integer or (integer, integer")
else:
self.external_embedding_layer = None
# def hook(module, input, output):
# setattr(module, "_value_hook", output)
#
# for n, m in self.model.named_modules():
# if n != 'proj':
# m.register_forward_hook(hook)
self._current_task = 0
self.sample_size_task = 0
self.batch_count = 0
self.tasks = set()
self.embeddings = {}
self.embeddings_images = {}
self.w = {}
# self.embeddings_images = []
def __call__(self, *args, **kwargs):
if self.importance == 0:
return self, 0
current_task = kwargs['current_task']
penalty = 0
if current_task > 0:
current_batch = kwargs['batch']
self.batch_count += 1
self.embedding_save(current_task)
if current_task == 1:
if not self.first_batch:# and self.batch_count % self.c == 0:
penalty = self.embedding_drive(current_batch)
else:
self.first_batch = False
else:
penalty = self.embedding_drive(current_batch)
return self, penalty
def get_embeddings(self):
return self.embeddings[0].cpu().detach().numpy(), self.embeddings_images[0].cpu().detach().numpy()
def embedding_save(self, current_task):
if current_task-1 not in self.tasks:
self.importance *= self.mul
self.first_batch = True
self.tasks.add(current_task-1)
self.batch_count = 0
self.model.eval()
# self.dataset.train_phase()
# it = self.dataset.getIterator(self.memorized_task_size, task=current_task-1)
# if self.incremental:
# if self.handle is not None:
# self.handle.remove()
#
# mask = np.ones(self.model.output_size)
# for t in self.tasks:
# for i in self.dataset.task_mask(t):
# mask[i] = 0
# self.handle = self.model.classification_layer.register_backward_hook(self.layer_freeze(torch.from_numpy(mask).float().to(self.device)))
# # self.w.extend([1] * self.memorized_task_size)
# images, _ = next(it)
# input = images.to(self.device)
embs = None
with torch.no_grad():
for step, ((y1, y2), _) in enumerate(self.dataset):
y1 = y1.cuda(self.gpu, non_blocking=True)
y2 = y2.cuda(self.gpu, non_blocking=True)
o_y1, o_y2 = self.model.module.embedding(y1, y2)
# compute average for embeddings
mid_out = [o_y1, o_y2]
output = torch.stack(mid_out).mean(dim=0)
# compute average for images
img_mid_output = [y1, y2]
images = torch.stack(img_mid_output).mean(dim=0)
if self.external_embedding_layer is not None:
output = self.external_embedding_layer(output)
if self.normalize:
output = F.normalize(output, p=2, dim=1)
embeddings = output.cpu()
if embs is None:
embs = embeddings
else:
embs = torch.cat((embs, embeddings), 0)
if self.supervised:
self.embeddings[current_task-1] = embs.cpu()
self.embeddings_images[current_task-1] = images.cpu()
c = 1
# if self.weights_type is not None:
# if self.weights_type == 'distance':
# c = 0.1
w = [c] * self.memorized_task_size
for t in self.w.keys():
self.w[t] = deepcopy(w)
self.w[current_task-1] = deepcopy(w)
else:
if len(self.embeddings) == 0:
self.embeddings[0] = embs.cpu()
self.embeddings_images[0] = images.cpu()
else:
self.embeddings[0] = torch.cat((self.embeddings[0], embs.cpu()), 0)
self.embeddings_images[0] = torch.cat((self.embeddings_images[0], images.cpu()), 0)
c = 1
# if self.weights_type is not None:
# if self.weights_type == 'distance':
# c = 0.1
# self.w[0] = [c] * self.embeddings[0].size()[0]
self.w[0] = [c] * self.batch_size
# if self.embeddings is None or self.online:
# self.embeddings = embeddings
# self.embeddings_images = images
# self.w = [1] * self.memorized_task_size
# else:
# self.embeddings = torch.cat((self.embeddings, embeddings), 0)
# self.embeddings_images = torch.cat((self.embeddings_images, images), 0)
# self.w = [1] * self.embeddings.size()[0]
#
# if 0 < self.memory_size < len(self.w):
# self.embeddings = self.embeddings[-self.memory_size:]
# self.embeddings_images = self.embeddings_images[-self.memory_size:]
# self.w = self.w[-self.memory_size:]
self.model.train()
# self.dataset.train_phase()
def embedding_drive(self, current_batch):
self.model.eval()
for t in self.embeddings:
to_back = None
w = self.w[t]
# print("w: ", w)
idx = range(len(w))
# w = self.w
if self.weights_type == 'usage':
# ws = np.sum(self.w)
w = [1/wc for wc in w]
if self.supervised:
ss = self.sample_size
else:
ss = self.sample_size * len(self.tasks)
# print("idx: ", idx)
# print("w: ", w)
idx = random.choices(idx, k=ss, weights=w)
for i in idx:
# print("emb: ", self.embeddings[0].shape, self.embeddings[0])
# print()
# print()
# print("emb_imgs: ", self.embeddings_images[0].shape, self.embeddings_images[0])
img = self.embeddings_images[t][i].unsqueeze(0).to(self.device)
embeddings = self.embeddings[t][i].unsqueeze(0)
new_embeddings1, new_embeddings2 = self.model.module.embedding(img, img)
new_embeddings = torch.stack([new_embeddings1, new_embeddings2]).mean(dim=0)
if self.external_embedding_layer is not None:
new_embeddings = self.external_embedding_layer(new_embeddings)
if self.normalize:
new_embeddings = F.normalize(new_embeddings, p=2, dim=1)
new_embeddings = new_embeddings.cpu()
if self.distance == 'euclidean':
dist = (embeddings - new_embeddings).norm(p=None, dim=1)
elif self.distance == 'cosine':
cosine = torch.nn.functional.cosine_similarity(embeddings, new_embeddings)
dist = 1-cosine
if to_back is None:
to_back = dist
else:
to_back = torch.cat((to_back, dist), 0)
# dist = dist.mean() * self.importance
# dist.backward()
if self.weights_type is not None:
if self.weights_type == 'distance':
dist = dist.detach().cpu().numpy()
# for j, i in enumerate(idx):
self.w[t][i] = float(dist)
elif self.weights_type == 'usage':
# for j, i in enumerate(idx):
self.w[t][i] += 1
elif self.weights_type == 'image_similarity':
# compute images average again
current_batch_avg = torch.stack([current_batch[0], current_batch[1]]).mean(dim=0)
current_batch_avg = torch.reshape(current_batch_avg,
(-1, current_batch_avg.shape[1] * current_batch_avg.shape[2] * current_batch_avg.shape[3]))
current_images = self.encoder(current_batch_avg)
img = torch.reshape(img, (-1, img.shape[1] * img.shape[2] * img.shape[3]))
old_images = self.encoder(img)
with torch.no_grad():
current_images = current_images / current_images.norm(dim=1)[:, None]
old_images = old_images / old_images.norm(dim=1)[:, None]
dist = torch.mm(current_images, old_images.transpose(0, 1))
dist = (1 - dist.mean(dim=1)).cpu().numpy()
# for j, i in enumerate(idx):
self.w[t] = dist
to_back = to_back.mean() * self.importance
# print(to_back, torch.log(to_back))
to_back.backward()
# l = torch.log(-to_back)
# l.backward()
self.model.train()
return to_back.item()
def get_info(self, diz):
self.embeddings[0] = (diz[()]['embeddings'])
self.embeddings[0] = torch.from_numpy(self.embeddings[0])
self.embeddings_images[0] = diz[()]['embeddings_images']
self.embeddings_images[0] = torch.from_numpy(self.embeddings_images[0])
# self.ewc_lambda = ewc_lambda