-
Notifications
You must be signed in to change notification settings - Fork 0
/
vaemodel.py
477 lines (364 loc) · 21.3 KB
/
vaemodel.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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#vaemodel
import copy
import torch
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.autograd as autograd
from torch.utils import data
from data_loader import DATA_LOADER as dataloader
import final_classifier as classifier
import models
import numpy as np
from sklearn.cluster import KMeans
class LINEAR_LOGSOFTMAX(nn.Module):
def __init__(self, input_dim, nclass):
super(LINEAR_LOGSOFTMAX, self).__init__()
self.fc = nn.Linear(input_dim,nclass)
self.logic = nn.LogSoftmax(dim=1)
self.lossfunction = nn.NLLLoss()
def forward(self, x):
o = self.logic(self.fc(x))
return o
class Model(nn.Module):
def __init__(self,hyperparameters):
super(Model,self).__init__()
self.device = hyperparameters['device']
self.auxiliary_data_source = hyperparameters['auxiliary_data_source']
self.all_data_sources = ['resnet_features',self.auxiliary_data_source]
self.DATASET = hyperparameters['dataset']
self.num_shots = hyperparameters['num_shots']
self.latent_size = hyperparameters['latent_size']
self.batch_size = hyperparameters['batch_size']
self.hidden_size_rule = hyperparameters['hidden_size_rule']
self.warmup = hyperparameters['model_specifics']['warmup']
self.generalized = hyperparameters['generalized']
self.classifier_batch_size = 32
self.img_seen_samples = hyperparameters['samples_per_class'][self.DATASET][0]
self.att_seen_samples = hyperparameters['samples_per_class'][self.DATASET][1]
self.att_unseen_samples = hyperparameters['samples_per_class'][self.DATASET][2]
self.img_unseen_samples = hyperparameters['samples_per_class'][self.DATASET][3]
self.reco_loss_function = hyperparameters['loss']
self.nepoch = hyperparameters['epochs']
self.lr_cls = hyperparameters['lr_cls']
self.cross_reconstruction = hyperparameters['model_specifics']['cross_reconstruction']
self.cls_train_epochs = hyperparameters['cls_train_steps']
self.dataset = dataloader( self.DATASET, copy.deepcopy(self.auxiliary_data_source) , device= self.device )
att = np.unique(self.dataset.aux_data.cpu(),axis=0)
kmeans = KMeans(n_clusters=3, random_state=1337).fit(att) ### field number : K=3
att = kmeans.cluster_centers_
self.att = torch.tensor(att).float().cuda()
# self.CM = CooperationModule(self.att)
if self.DATASET=='CUB':
self.num_classes=200
self.num_novel_classes = 50
elif self.DATASET=='SUN':
self.num_classes=717
self.num_novel_classes = 72
elif self.DATASET=='AWA1' or self.DATASET=='AWA2':
self.num_classes=50
self.num_novel_classes = 10
feature_dimensions = [2048, self.dataset.aux_data.size(1)]
# feature_dimensions = [2048, 2048]
# Here, the encoders and decoders for all modalities are created and put into dict
self.encoder = {}
for datatype, dim in zip(self.all_data_sources,feature_dimensions):
self.encoder[datatype] = models.encoder_template(dim,self.latent_size,self.hidden_size_rule[datatype],self.device)
print(str(datatype) + ' ' + str(dim))
self.EN = nn.Linear(2048, self.dataset.aux_data.size(1))
self.decoder = {}
for datatype, dim in zip(self.all_data_sources,feature_dimensions):
self.decoder[datatype] = models.decoder_template(self.latent_size,dim,self.hidden_size_rule[datatype],self.device)
# An optimizer for all encoders and decoders is defined here
parameters_to_optimize = list(self.parameters())
for datatype in self.all_data_sources:
parameters_to_optimize += list(self.encoder[datatype].parameters())
parameters_to_optimize += list(self.decoder[datatype].parameters())
# parameters_to_optimize += list(self.CM.parameters())
parameters_to_optimize += list(self.EN.parameters())
self.optimizer = optim.Adam( parameters_to_optimize ,lr=hyperparameters['lr_gen_model'], betas=(0.9, 0.999), eps=1e-08, weight_decay=0, amsgrad=True)
if self.reco_loss_function=='l2':
self.reconstruction_criterion = nn.MSELoss(size_average=False)
elif self.reco_loss_function=='l1':
self.reconstruction_criterion = nn.L1Loss(size_average=False)
def reparameterize(self, mu, logvar):
if self.reparameterize_with_noise:
sigma = torch.exp(logvar)
eps = torch.cuda.FloatTensor(logvar.size()[0],1).normal_(0,1)
eps = eps.expand(sigma.size())
return mu + sigma*eps
else:
return mu
def forward(self):
pass
def map_label(self,label, classes):
mapped_label = torch.LongTensor(label.size()).to(self.device)
for i in range(classes.size(0)):
mapped_label[label==classes[i]] = i
return mapped_label
def trainstep(self, img, att, label):
##############################################
# Encode image features and additional
# features
##############################################
# att = self.CM(att)
img_t = self.EN(img)
img_K = F.normalize(img_t, 2, -1)
att_K = F.normalize(att, 2, -1)
d = torch.matmul(img_K, att_K.T)
LogSoftmax = nn.LogSoftmax(dim=1)
d = LogSoftmax(d)
a, b = d.shape
loss_dis = 0
for i,j in enumerate(label):
if i>=a or j>=b:
continue
loss_dis+=d[i][j]
mu_img, logvar_img = self.encoder['resnet_features'](img)
z_from_img = self.reparameterize(mu_img, logvar_img)
mu_att, logvar_att = self.encoder[self.auxiliary_data_source](att)
z_from_att = self.reparameterize(mu_att, logvar_att)
##############################################
# Reconstruct inputs
##############################################
img_from_img = self.decoder['resnet_features'](z_from_img)
att_from_att = self.decoder[self.auxiliary_data_source](z_from_att)
reconstruction_loss = self.reconstruction_criterion(img_from_img, img) \
+ self.reconstruction_criterion(att_from_att, att)
##############################################
# Cross Reconstruction Loss
##############################################
img_from_att = self.decoder['resnet_features'](z_from_att)
att_from_img = self.decoder[self.auxiliary_data_source](z_from_img)
# soft = nn.Softmax(dim=1)
# cross_reconstruction_loss = - torch.sum(soft(torch.mm(img_from_att, img.t()))) \
# - torch.sum(soft(torch.mm(att_from_img, att.t())))
# cross_reconstruction_loss = - torch.sum(torch.eye(img.size()[1])*torch.mm(img.t(), img_from_att)) \
# - torch.sum(torch.eye(att.size()[1])*torch.mm(att.t(), att_from_img))
# cross_reconstruction_loss = - torch.sum(torch.eye(img.size()[1]).cuda()*soft(torch.mm(img.t(), img_from_att))) \
# - torch.sum(torch.eye(att.size()[1]).cuda()*soft(torch.mm(att.t(), att_from_img)))
cross_reconstruction_loss = self.reconstruction_criterion(img_from_att, img) \
+ self.reconstruction_criterion(att_from_img, att)
##############################################
# KL-Divergence
##############################################
KLD = (0.5 * torch.sum(1 + logvar_att - mu_att.pow(2) - logvar_att.exp())) \
+ (0.5 * torch.sum(1 + logvar_img - mu_img.pow(2) - logvar_img.exp()))
##############################################
# Distribution Alignment
##############################################
distance = torch.sqrt(torch.sum((mu_img - mu_att) ** 2, dim=1) + \
torch.sum((torch.sqrt(logvar_img.exp()) - torch.sqrt(logvar_att.exp())) ** 2, dim=1))
distance = distance.sum()
##############################################
# scale the loss terms according to the warmup
# schedule
##############################################
f1 = 1.0*(self.current_epoch - self.warmup['cross_reconstruction']['start_epoch'] )/(1.0*( self.warmup['cross_reconstruction']['end_epoch']- self.warmup['cross_reconstruction']['start_epoch']))
f1 = f1*(1.0*self.warmup['cross_reconstruction']['factor'])
cross_reconstruction_factor = torch.cuda.FloatTensor([min(max(f1,0),self.warmup['cross_reconstruction']['factor'])])
# f1 = 1.0 - 1.0*(self.current_epoch - self.warmup['cross_reconstruction']['start_epoch'] )/(1.0*( self.warmup['cross_reconstruction']['end_epoch']- self.warmup['cross_reconstruction']['start_epoch']))
# f1 = f1*(1.0*self.warmup['cross_reconstruction']['factor'])
# cross_reconstruction_factor = torch.cuda.FloatTensor([max(min(f1,self.warmup['cross_reconstruction']['factor']), 0)])
f2 = 1.0 * (self.current_epoch - self.warmup['beta']['start_epoch']) / ( 1.0 * (self.warmup['beta']['end_epoch'] - self.warmup['beta']['start_epoch']))
f2 = f2 * (1.0 * self.warmup['beta']['factor'])
beta = torch.cuda.FloatTensor([min(max(f2, 0), self.warmup['beta']['factor'])])
f3 = 1.0*(self.current_epoch - self.warmup['distance']['start_epoch'] )/(1.0*( self.warmup['distance']['end_epoch']- self.warmup['distance']['start_epoch']))
f3 = f3*(1.0*self.warmup['distance']['factor'])
distance_factor = torch.cuda.FloatTensor([min(max(f3,0),self.warmup['distance']['factor'])])
##############################################
# Put the loss together and call the optimizer
##############################################
# print(cross_reconstruction_factor*cross_reconstruction_loss)
self.optimizer.zero_grad()
loss = reconstruction_loss - beta * KLD
loss += cross_reconstruction_factor*cross_reconstruction_loss
# if cross_reconstruction_loss>0:
# loss += cross_reconstruction_factor*cross_reconstruction_loss
if distance_factor >0:
loss += distance_factor*distance
loss += loss_dis*0.01
loss.backward()
self.optimizer.step()
return loss.item()
def train_vae(self):
losses = []
self.dataloader = data.DataLoader(self.dataset,batch_size= self.batch_size,shuffle= True,drop_last=True)#,num_workers = 4)
self.dataset.novelclasses =self.dataset.novelclasses.long().cuda()
self.dataset.seenclasses =self.dataset.seenclasses.long().cuda()
#leave both statements
self.train()
self.reparameterize_with_noise = True
print('train for reconstruction')
for epoch in range(0, self.nepoch ):
self.current_epoch = epoch
i=-1
for iters in range(0, self.dataset.ntrain, self.batch_size):
i+=1
label, data_from_modalities = self.dataset.next_batch(self.batch_size)
label= label.long().to(self.device)
for j in range(len(data_from_modalities)):
data_from_modalities[j] = data_from_modalities[j].to(self.device)
data_from_modalities[j].requires_grad = False
loss = self.trainstep(data_from_modalities[0], data_from_modalities[1] ,label)
if i%50==0:
print('epoch ' + str(epoch) + ' | iter ' + str(i) + '\t'+
' | loss ' + str(loss)[:5] )
if i%50==0 and i>0:
losses.append(loss)
# turn into evaluation mode:
for key, value in self.encoder.items():
self.encoder[key].eval()
for key, value in self.decoder.items():
self.decoder[key].eval()
return losses
def train_classifier(self, show_plots=False):
if self.num_shots > 0 :
print('================ transfer features from test to train ==================')
self.dataset.transfer_features(self.num_shots, num_queries='num_features')
history = [] # stores accuracies
cls_seenclasses = self.dataset.seenclasses
cls_novelclasses = self.dataset.novelclasses
train_seen_feat = self.dataset.data['train_seen']['resnet_features']
train_seen_label = self.dataset.data['train_seen']['labels']
novelclass_aux_data = self.dataset.novelclass_aux_data # access as novelclass_aux_data['resnet_features'], novelclass_aux_data['attributes']
seenclass_aux_data = self.dataset.seenclass_aux_data
novel_corresponding_labels = self.dataset.novelclasses.long().to(self.device)
seen_corresponding_labels = self.dataset.seenclasses.long().to(self.device)
# The resnet_features for testing the classifier are loaded here
novel_test_feat = self.dataset.data['test_unseen'][
'resnet_features'] # self.dataset.test_novel_feature.to(self.device)
seen_test_feat = self.dataset.data['test_seen'][
'resnet_features'] # self.dataset.test_seen_feature.to(self.device)
test_seen_label = self.dataset.data['test_seen']['labels'] # self.dataset.test_seen_label.to(self.device)
test_novel_label = self.dataset.data['test_unseen']['labels'] # self.dataset.test_novel_label.to(self.device)
train_unseen_feat = self.dataset.data['train_unseen']['resnet_features']
train_unseen_label = self.dataset.data['train_unseen']['labels']
# in ZSL mode:
if self.generalized == False:
# there are only 50 classes in ZSL (for CUB)
# novel_corresponding_labels =list of all novel classes (as tensor)
# test_novel_label = mapped to 0-49 in classifier function
# those are used as targets, they have to be mapped to 0-49 right here:
novel_corresponding_labels = self.map_label(novel_corresponding_labels, novel_corresponding_labels)
if self.num_shots > 0:
# not generalized and at least 1 shot means normal FSL setting (use only unseen classes)
train_unseen_label = self.map_label(train_unseen_label, cls_novelclasses)
# for FSL, we train_seen contains the unseen class examples
# for ZSL, train seen label is not used
# if self.num_shots>0:
# train_seen_label = self.map_label(train_seen_label,cls_novelclasses)
test_novel_label = self.map_label(test_novel_label, cls_novelclasses)
# map cls novelclasses last
cls_novelclasses = self.map_label(cls_novelclasses, cls_novelclasses)
if self.generalized:
print('mode: gzsl')
clf = LINEAR_LOGSOFTMAX(self.latent_size, self.num_classes)
else:
print('mode: zsl')
clf = LINEAR_LOGSOFTMAX(self.latent_size, self.num_novel_classes)
clf.apply(models.weights_init)
with torch.no_grad():
####################################
# preparing the test set
# convert raw test data into z vectors
####################################
self.reparameterize_with_noise = False
mu1, var1 = self.encoder['resnet_features'](novel_test_feat)
test_novel_X = self.reparameterize(mu1, var1).to(self.device).data
test_novel_Y = test_novel_label.to(self.device)
mu2, var2 = self.encoder['resnet_features'](seen_test_feat)
test_seen_X = self.reparameterize(mu2, var2).to(self.device).data
test_seen_Y = test_seen_label.to(self.device)
####################################
# preparing the train set:
# chose n random image features per
# class. If n exceeds the number of
# image features per class, duplicate
# some. Next, convert them to
# latent z features.
####################################
self.reparameterize_with_noise = True
def sample_train_data_on_sample_per_class_basis(features, label, sample_per_class):
sample_per_class = int(sample_per_class)
if sample_per_class != 0 and len(label) != 0:
classes = label.unique()
for i, s in enumerate(classes):
features_of_that_class = features[label == s, :] # order of features and labels must coincide
# if number of selected features is smaller than the number of features we want per class:
multiplier = torch.ceil(torch.cuda.FloatTensor(
[max(1, sample_per_class / features_of_that_class.size(0))])).long().item()
features_of_that_class = features_of_that_class.repeat(multiplier, 1)
if i == 0:
features_to_return = features_of_that_class[:sample_per_class, :]
labels_to_return = s.repeat(sample_per_class)
else:
features_to_return = torch.cat(
(features_to_return, features_of_that_class[:sample_per_class, :]), dim=0)
labels_to_return = torch.cat((labels_to_return, s.repeat(sample_per_class)),
dim=0)
return features_to_return, labels_to_return
else:
return torch.cuda.FloatTensor([]), torch.cuda.LongTensor([])
# some of the following might be empty tensors if the specified number of
# samples is zero :
img_seen_feat, img_seen_label = sample_train_data_on_sample_per_class_basis(
train_seen_feat,train_seen_label,self.img_seen_samples )
img_unseen_feat, img_unseen_label = sample_train_data_on_sample_per_class_basis(
train_unseen_feat, train_unseen_label, self.img_unseen_samples )
att_unseen_feat, att_unseen_label = sample_train_data_on_sample_per_class_basis(
novelclass_aux_data,
novel_corresponding_labels,self.att_unseen_samples )
att_seen_feat, att_seen_label = sample_train_data_on_sample_per_class_basis(
seenclass_aux_data,
seen_corresponding_labels, self.att_seen_samples)
def convert_datapoints_to_z(features, encoder):
if features.size(0) != 0:
mu_, logvar_ = encoder(features)
z = self.reparameterize(mu_, logvar_)
return z
else:
return torch.cuda.FloatTensor([])
z_seen_img = convert_datapoints_to_z(img_seen_feat, self.encoder['resnet_features'])
z_unseen_img = convert_datapoints_to_z(img_unseen_feat, self.encoder['resnet_features'])
# if att_seen_feat.shape[0]!=0:
# att_seen_feat = self.CM(att_seen_feat)
# att_unseen_feat = self.CM(att_unseen_feat)
z_seen_att = convert_datapoints_to_z(att_seen_feat, self.encoder[self.auxiliary_data_source])
z_unseen_att = convert_datapoints_to_z(att_unseen_feat, self.encoder[self.auxiliary_data_source])
train_Z = [z_seen_img, z_unseen_img ,z_seen_att ,z_unseen_att]
train_L = [img_seen_label , img_unseen_label,att_seen_label,att_unseen_label]
# empty tensors are sorted out
train_X = [train_Z[i] for i in range(len(train_Z)) if train_Z[i].size(0) != 0]
train_Y = [train_L[i] for i in range(len(train_L)) if train_Z[i].size(0) != 0]
train_X = torch.cat(train_X, dim=0)
train_Y = torch.cat(train_Y, dim=0)
############################################################
##### initializing the classifier and train one epoch
############################################################
cls = classifier.CLASSIFIER(clf, train_X, train_Y, test_seen_X, test_seen_Y, test_novel_X,
test_novel_Y,
cls_seenclasses, cls_novelclasses,
self.num_classes, self.device, self.lr_cls, 0.5, 1,
self.classifier_batch_size,
self.generalized)
for k in range(self.cls_train_epochs):
if k > 0:
if self.generalized:
cls.acc_seen, cls.acc_novel, cls.H = cls.fit()
else:
cls.acc = cls.fit_zsl()
if self.generalized:
print('[%.1f] novel=%.4f, seen=%.4f, h=%.4f , loss=%.4f' % (
k, cls.acc_novel, cls.acc_seen, cls.H, cls.average_loss))
history.append([torch.tensor(cls.acc_seen).item(), torch.tensor(cls.acc_novel).item(),
torch.tensor(cls.H).item()])
else:
print('[%.1f] acc=%.4f ' % (k, cls.acc))
history.append([0, torch.tensor(cls.acc).item(), 0])
if self.generalized:
return torch.tensor(cls.acc_seen).item(), torch.tensor(cls.acc_novel).item(), torch.tensor(
cls.H).item(), history
else:
return 0, torch.tensor(cls.acc).item(), 0, history