-
Notifications
You must be signed in to change notification settings - Fork 4
/
Dataloader_dynamic.py
320 lines (288 loc) · 16.7 KB
/
Dataloader_dynamic.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
import numpy as np
import scipy.io as sio
from scipy.stats.stats import scoreatpercentile
import torch
from sklearn import preprocessing
from sklearn.metrics.pairwise import cosine_similarity
import sys
class DATA_LOADER(object):
def __init__(self, opt):
if opt.matdataset:
if opt.dataset == 'imageNet1K':
self.read_matimagenet(opt)
else:
self.read_matdataset(opt)
self.index_in_epoch = 0
self.epochs_completed = 0
self.feature_size = self.train_feature.shape[1]
self.att_size = self.attribute.shape[1]
def read_matdataset(self, opt):
matcontent = sio.loadmat(opt.dataroot + "/" + opt.dataset + "/" + opt.image_embedding + ".mat")
feature = matcontent['features'].T
label = matcontent['labels'].astype(int).squeeze() - 1
matcontent = sio.loadmat(opt.dataroot + "/" + opt.dataset + "/" + opt.class_embedding + "_splits.mat")
trainval_loc = matcontent['trainval_loc'].squeeze() - 1
train_loc = matcontent['train_loc'].squeeze() - 1
val_unseen_loc = matcontent['val_loc'].squeeze() - 1
test_seen_loc = matcontent['test_seen_loc'].squeeze() - 1
test_unseen_loc = matcontent['test_unseen_loc'].squeeze() - 1
self.attribute = torch.from_numpy(matcontent['att'].T).float()
if not opt.validation:
if opt.preprocessing:
if opt.standardization:
print('standardization...')
scaler = preprocessing.StandardScaler()
else:
scaler = preprocessing.MinMaxScaler()
_train_feature = scaler.fit_transform(feature[trainval_loc])
_test_seen_feature = scaler.transform(feature[test_seen_loc])
_test_unseen_feature = scaler.transform(feature[test_unseen_loc])
self.train_feature = torch.from_numpy(_train_feature).float()
mx = self.train_feature.max()
self.train_feature.mul_(1/mx)
self.train_label = torch.from_numpy(label[trainval_loc]).long()
self.test_unseen_feature = torch.from_numpy(_test_unseen_feature).float()
self.test_unseen_feature.mul_(1/mx)
self.test_unseen_label = torch.from_numpy(label[test_unseen_loc]).long()
self.test_seen_feature = torch.from_numpy(_test_seen_feature).float()
self.test_seen_feature.mul_(1/mx)
self.test_seen_label = torch.from_numpy(label[test_seen_loc]).long()
else:
self.train_feature = torch.from_numpy(feature[trainval_loc]).float()
self.train_label = torch.from_numpy(label[trainval_loc]).long()
self.test_unseen_feature = torch.from_numpy(feature[test_unseen_loc]).float()
self.test_unseen_label = torch.from_numpy(label[test_unseen_loc]).long()
self.test_seen_feature = torch.from_numpy(feature[test_seen_loc]).float()
self.test_seen_label = torch.from_numpy(label[test_seen_loc]).long()
else:
self.train_feature = torch.from_numpy(feature[train_loc]).float()
self.train_label = torch.from_numpy(label[train_loc]).long()
self.test_unseen_feature = torch.from_numpy(feature[val_unseen_loc]).float()
self.test_unseen_label = torch.from_numpy(label[val_unseen_loc]).long()
self.seenclasses = torch.from_numpy(np.unique(self.train_label.numpy()))
self.unseenclasses = torch.from_numpy(np.unique(self.test_unseen_label.numpy()))
self.ntrain = self.train_feature.size()[0]
self.ntrain_class = self.seenclasses.size(0)
self.ntest_class = self.unseenclasses.size(0)
self.train_class = self.seenclasses.clone()
self.allclasses = torch.arange(0, self.ntrain_class+self.ntest_class).long()
def semantic_similarity_check(self, Neighbors, train_text_feature, test_text_feature, train_label_seen, seen_classes,novel_classes,task_no):
seen_similarity_matric = cosine_similarity(train_text_feature, train_text_feature)
self.idx_mat = np.argsort(-1 * seen_similarity_matric, axis=1)
self.idx_mat = self.idx_mat[:, 0:Neighbors]
self.semantic_similarity_seen = np.zeros((seen_classes*task_no, Neighbors))
for i in range(seen_classes*task_no):
for j in range(Neighbors):
self.semantic_similarity_seen [i,j] = seen_similarity_matric[i, self.idx_mat[i,j]]
unseen_similarity_matric = cosine_similarity(test_text_feature, train_text_feature)
self.unseen_idx_mat = np.argsort(-1 * unseen_similarity_matric, axis=1)
self.unseen_idx_mat = self.unseen_idx_mat[:, 0:Neighbors]
self.semantic_similarity_unseen = np.zeros((novel_classes*task_no, Neighbors))
for i in range(novel_classes*task_no):
for j in range(Neighbors):
self.semantic_similarity_unseen [i,j] = unseen_similarity_matric[i, self.unseen_idx_mat[i,j]]
def task_train_data(self,task_no,seen_classes,all_classes,novel_classes,num_tasks,Neighbors):
if task_no==1:
self.lab_list = self.seenclasses
self.seencount = 0
else:
self.lab_list = self.lab_list[seen_classes:]
lab_list = self.lab_list[0:seen_classes]
for i in range(len(lab_list)):
idx = np.where(self.train_label == lab_list[i])
train_feat_seen_1 = self.train_feature[idx]
train_label_seen_1 = np.array(self.seencount).repeat(self.train_label[idx].shape[0])
train_att_seen_1 = self.attribute[self.train_label[idx]]
self.seencount += 1
if i==0:
train_feat_seen = train_feat_seen_1
train_label_seen = train_label_seen_1
train_att_seen = train_att_seen_1
else:
train_feat_seen = np.concatenate((train_feat_seen,train_feat_seen_1))
train_label_seen = np.concatenate((train_label_seen,train_label_seen_1))
train_att_seen = np.concatenate((train_att_seen,train_att_seen_1))
if task_no==1:
train_text_feat = self.attribute[self.seenclasses[0:seen_classes*task_no]]
test_text_feat = self.attribute[self.unseenclasses[0:novel_classes*task_no]]
self.semantic_similarity_check(Neighbors, train_text_feat, test_text_feat,train_label_seen, seen_classes,novel_classes,task_no)
prev_cur_sn_map = None
prev_prev_sn_map = None
cur_prev_sn_map = None
prev_cur_sn_sc = None
prev_prev_sn_sc = None
cur_prev_sn_sc = None
else:
train_text_feat = self.attribute[self.seenclasses[0:seen_classes*task_no]]
test_text_feat = self.attribute[self.unseenclasses[0:novel_classes*task_no]]
self.semantic_similarity_check(Neighbors,train_text_feat, test_text_feat,train_label_seen, seen_classes,novel_classes,task_no)
train_label_seen = torch.reshape(torch.tensor(train_label_seen),(train_label_seen.shape[0],1))
return torch.tensor(train_feat_seen), train_label_seen,torch.tensor(train_att_seen)
def attribute_mapping(self,seen_classes,novel_classes,task_no):
for i in range(seen_classes*task_no):
if i == 0:
unique_attribute = self.attribute[self.seenclasses[i]]
else:
unique_attribute = torch.cat((unique_attribute,self.attribute[self.seenclasses[i]]))
for j in range(novel_classes*task_no):
unique_attribute = torch.cat((unique_attribute,self.attribute[self.unseenclasses[j]]))
unique_attributes = torch.reshape(unique_attribute,[(seen_classes+novel_classes)*task_no,-1])
return unique_attributes
def train_attribute_seen_exclusive(self,seen_classes,task_no):
unique_attributes = self.attribute[self.seenclasses[0:seen_classes*task_no]]
unique_attributes = unique_attributes.cuda()
return unique_attributes
def train_attribute_unseen_exclusive(self,novel_classes,task_no):
unique_attributes = self.attribute[self.unseenclasses[novel_classes*(task_no-1):novel_classes*task_no]]
unique_attributes = unique_attributes.cuda()
return unique_attributes
def test_attribute_seen_exclusive(self,seen_classes,task_no):
if task_no==1:
self.seenattr_list = self.seenclasses
unique_attributes = self.attribute[self.seenattr_list[0:seen_classes]]
self.seenattr_list = self.seenattr_list[seen_classes:]
unique_attributes = unique_attributes.cuda()
return unique_attributes
def test_attribute_unseen_exclusive(self,novel_classes,task_no):
if task_no==1:
self.unseenattr_list = self.unseenclasses
unique_attributes = self.attribute[self.unseenattr_list[0:novel_classes]]
self.unseenattr_list = self.unseenattr_list[novel_classes:]
unique_attributes = unique_attributes.cuda()
return unique_attributes
def task_test_data(self,task_no,seen_classes,all_classes,novel_classes,num_tasks):
test_seen_f = {}
test_seen_l = {}
test_seen_a = {}
test_unseen_f = {}
test_unseen_l = {}
test_unseen_a = {}
dup_seenclasses = self.seenclasses
dup_unseenclasses = self.unseenclasses
self.testseen = 0
self.testunseen = seen_classes*task_no
for tno in range(1,task_no+1):
lab_list = dup_seenclasses[:seen_classes]
for i in range(len(lab_list)):
idx = np.where(self.test_seen_label == lab_list[i])
test_feat_seen_1 = self.test_seen_feature[idx]
test_label_seen_1 = np.array(self.testseen).repeat(self.test_seen_label[idx].shape[0])
test_att_seen_1 = self.attribute[self.test_seen_label[idx]]
self.testseen += 1
if i==0:
test_feat_seen = test_feat_seen_1
test_label_seen = test_label_seen_1
test_att_seen = test_att_seen_1
else:
test_feat_seen = np.concatenate((test_feat_seen,test_feat_seen_1))
test_label_seen = np.concatenate((test_label_seen,test_label_seen_1))
test_att_seen = np.concatenate((test_att_seen,test_att_seen_1))
test_seen_f[tno] = torch.tensor(test_feat_seen)
test_seen_l[tno] = torch.tensor(test_label_seen)
test_seen_a[tno] = torch.tensor(test_att_seen)
test_feat_seen = None
test_label_seen = None
test_att_seen = None
dup_seenclasses = dup_seenclasses[seen_classes:]
lab_list_un = dup_unseenclasses[:novel_classes]
for i in range(len(lab_list_un)):
idx = np.where(self.test_unseen_label == lab_list_un[i])
test_feat_unseen_1 = self.test_unseen_feature[idx]
test_label_unseen_1 = np.array(self.testunseen).repeat(self.test_unseen_label[idx].shape[0])
test_att_unseen_1 = (self.attribute[self.test_unseen_label[idx]])
self.testunseen += 1
if i==0:
test_feat_unseen = test_feat_unseen_1
test_label_unseen = test_label_unseen_1
test_att_unseen = test_att_unseen_1
else:
test_feat_unseen = np.concatenate((test_feat_unseen,test_feat_unseen_1))
test_label_unseen = np.concatenate((test_label_unseen,test_label_unseen_1))
test_att_unseen = np.concatenate((test_att_unseen,test_att_unseen_1))
test_unseen_f[tno] = torch.tensor(test_feat_unseen)
test_unseen_l[tno] = torch.tensor(test_label_unseen)
test_unseen_a[tno] = torch.tensor(test_att_unseen)
test_feat_unseen = None
test_label_unseen = None
test_att_unseen = None
dup_unseenclasses = dup_unseenclasses[novel_classes:]
return test_unseen_l,test_unseen_a
def task_test_data_(self,task_no,seen_classes,all_classes,novel_classes,num_tasks):
dup_seenclasses = self.seenclasses
dup_unseenclasses = self.unseenclasses
self.testseen = 0
self.testunseen = seen_classes*task_no
lab_list = dup_seenclasses[:seen_classes*task_no]
for i in range(len(lab_list)):
idx = np.where(self.test_seen_label == lab_list[i])
test_feat_seen_1 = self.test_seen_feature[idx]
test_label_seen_1 = np.array(self.testseen).repeat(self.test_seen_label[idx].shape[0])
test_att_seen_1 = self.attribute[self.test_seen_label[idx]]
self.testseen += 1
if i==0:
test_feat_seen = test_feat_seen_1
test_label_seen = test_label_seen_1
test_att_seen = test_att_seen_1
else:
test_feat_seen = np.concatenate((test_feat_seen,test_feat_seen_1))
test_label_seen = np.concatenate((test_label_seen,test_label_seen_1))
test_att_seen = np.concatenate((test_att_seen,test_att_seen_1))
lab_list_un = dup_unseenclasses[:novel_classes*task_no]
for i in range(len(lab_list_un)):
idx = np.where(self.test_unseen_label == lab_list_un[i])
test_feat_unseen_1 = self.test_unseen_feature[idx]
test_label_unseen_1 = np.array(self.testunseen).repeat(self.test_unseen_label[idx].shape[0])
test_att_unseen_1 = (self.attribute[self.test_unseen_label[idx]])
self.testunseen += 1
if i==0:
test_feat_unseen = test_feat_unseen_1
test_label_unseen = test_label_unseen_1
test_att_unseen = test_att_unseen_1
else:
test_feat_unseen = np.concatenate((test_feat_unseen,test_feat_unseen_1))
test_label_unseen = np.concatenate((test_label_unseen,test_label_unseen_1))
test_att_unseen = np.concatenate((test_att_unseen,test_att_unseen_1))
return torch.tensor(test_feat_seen),torch.tensor(test_label_seen),torch.tensor(test_att_seen),torch.tensor(test_feat_unseen),torch.tensor(test_label_unseen),torch.tensor(test_att_unseen)
def next_batch_seen(self,ninstance,pos_labels,neg_labels,seen_feat,whole_attr,whole_labels):
for i in range(pos_labels.shape[0]):
c1 = np.where(pos_labels[i]==whole_labels)
np.random.shuffle(c1[0])
if i==0:
batch_label = whole_labels[c1[0]][0:ninstance]
batch_feature = seen_feat[c1[0]][0:ninstance]
batch_attr = whole_attr[c1[0]][0:ninstance]
else:
batch_label = torch.cat((batch_label,whole_labels[c1[0]][0:ninstance]))
batch_feature = torch.cat((batch_feature,seen_feat[c1[0]][0:ninstance]))
batch_attr = torch.cat((batch_attr,whole_attr[c1[0]][0:ninstance]))
for i in range(neg_labels.shape[0]):
c1 = np.where(neg_labels[i]==whole_labels)
np.random.shuffle(c1[0])
if i==0:
batch_label_neg = whole_labels[c1[0][0:ninstance]]
batch_feature_neg = seen_feat[c1[0][0:ninstance]]
batch_attr_neg = whole_attr[c1[0][0:ninstance]]
else:
batch_label_neg = torch.cat((batch_label_neg,whole_labels[c1[0][0:ninstance]]))
batch_feature_neg = torch.cat((batch_feature_neg,seen_feat[c1[0][0:ninstance]]))
batch_attr_neg = torch.cat((batch_attr_neg,whole_attr[c1[0][0:ninstance]]))
return [batch_label,batch_attr, batch_feature] , [batch_label_neg, batch_attr_neg,batch_feature_neg ]
def test_batch_unseen(self,nsamp,ninstance,attr_unseen,label_unseen):
a1 = np.unique(label_unseen)
np.random.shuffle(a1)
b1 = a1[0:nsamp]
for i in range(nsamp):
c1 = np.where(b1[i]==label_unseen)
if i==0:
batch_label = label_unseen[c1[0][0:1]]
batch_attr = attr_unseen[c1[0][0:1]]
else:
batch_label = np.concatenate((batch_label,label_unseen[c1[0][0:1]]))
batch_attr = torch.cat((batch_attr,attr_unseen[c1[0][0:1]]))
return [torch.tensor(batch_label), batch_attr]
def next_batch(self,batch_size,label,feature,attributes):
idx = torch.randperm(feature.shape[0])[0:batch_size]
batch_feature = feature[idx]
batch_label = label[idx]
batch_attr = attributes[idx]
return batch_feature, batch_label, batch_attr