-
Notifications
You must be signed in to change notification settings - Fork 8
/
data_loader.py
314 lines (239 loc) · 13.6 KB
/
data_loader.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
import numpy as np
import scipy.io as sio
import torch
from sklearn import preprocessing
import sys
import os
from pathlib import Path
import pickle
import copy
def map_label(label, classes):
mapped_label = torch.LongTensor(label.size())
for i in range(classes.size(0)):
mapped_label[label==classes[i]] = i
return mapped_label
class DATA_LOADER(object):
def __init__(self, dataset, aux_datasource, device='cuda'):
print("The current working directory is")
print(os.getcwd())
folder = str(Path(os.getcwd()))
if folder[-5:] == 'model':
project_directory = Path(os.getcwd()).parent
else:
project_directory = folder
print('Project Directory:')
print(project_directory)
data_path = '/home/shimingchen/ZSL/HSVA/data'
print('Data Path')
print(data_path)
sys.path.append(data_path)
self.data_path = data_path
self.device = device
self.dataset = dataset
self.auxiliary_data_source = aux_datasource
self.all_data_sources = ['resnet_features'] + [self.auxiliary_data_source]
if self.dataset == 'CUB':
self.datadir = self.data_path + '/CUB/'
elif self.dataset == 'SUN':
self.datadir = self.data_path + '/SUN/'
elif self.dataset == 'AWA1':
self.datadir = self.data_path + '/AWA1/'
elif self.dataset == 'AWA2':
self.datadir = self.data_path + '/AWA2/'
elif self.dataset == 'APY':
self.datadir = self.data_path + '/APY/'
elif self.dataset == 'FLO':
self.datadir = self.data_path + '/FLO/'
self.read_matdataset()
self.index_in_epoch = 0
self.epochs_completed = 0
def next_batch(self, batch_size):
idx = torch.randperm(self.ntrain)[0:batch_size]
batch_feature = self.data['train_seen']['resnet_features'][idx]
batch_label = self.data['train_seen']['labels'][idx]
batch_att = self.aux_data[batch_label]
return [ batch_feature, batch_att, batch_label]
def next_unseen_batch(self, batch_size):
idx_unseen = torch.randperm(self.ntest_unseen)[0:batch_size]
# batch_unseen_feature = self.data['test_unseen']['resnet_features'][idx_unseen]
batch_unseen_label = self.data['test_unseen']['labels'][idx_unseen]
batch_unseen_att = self.aux_data[batch_unseen_label]
return [batch_unseen_att, batch_unseen_label]
# def next_unseen_batch(self, batch_size):
# idx_unseen = torch.randperm(self.ntest_unseen)[0:batch_size]
# batch_unseen_feature = self.data['test_unseen']['resnet_features'][idx_unseen]
# idx_unseen =torch.randint(0, self.unseenclasses.shape[0], (batch_size,))
# unseen_label=self.unseenclasses[idx_unseen]
# batch_unseen_att=self.aux_data[unseen_label]
# return [batch_unseen_att, unseen_label]
def read_matdataset(self):
path= self.datadir + 'res101.mat'
print('_____')
print(path)
matcontent = sio.loadmat(path)
feature = matcontent['features'].T
label = matcontent['labels'].astype(int).squeeze() - 1
path= self.datadir + 'att_splits.mat'
matcontent = sio.loadmat(path)
# numpy array index starts from 0, matlab starts from 1
trainval_loc = matcontent['trainval_loc'].squeeze() - 1
train_loc = matcontent['train_loc'].squeeze() - 1 #--> train_feature = TRAIN SEEN
val_unseen_loc = matcontent['val_loc'].squeeze() - 1 #--> test_unseen_feature = TEST UNSEEN
test_seen_loc = matcontent['test_seen_loc'].squeeze() - 1
test_unseen_loc = matcontent['test_unseen_loc'].squeeze() - 1
if self.auxiliary_data_source == 'attributes':
self.aux_data = torch.from_numpy(matcontent['att'].T).float().to(self.device)
else:
if self.dataset != 'CUB':
print('the specified auxiliary datasource is not available for this dataset')
else:
with open(self.datadir + 'CUB_supporting_data.p', 'rb') as h:
x = pickle.load(h)
self.aux_data = torch.from_numpy(x[self.auxiliary_data_source]).float().to(self.device)
print('loaded ', self.auxiliary_data_source)
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])
train_feature = torch.from_numpy(train_feature).float().to(self.device)
test_seen_feature = torch.from_numpy(test_seen_feature).float().to(self.device)
test_unseen_feature = torch.from_numpy(test_unseen_feature).float().to(self.device)
self.ntest_unseen = test_unseen_feature.size()[0]
train_label = torch.from_numpy(label[trainval_loc]).long().to(self.device)
test_unseen_label = torch.from_numpy(label[test_unseen_loc]).long().to(self.device)
test_seen_label = torch.from_numpy(label[test_seen_loc]).long().to(self.device)
self.seenclasses = torch.from_numpy(np.unique(train_label.cpu().numpy())).to(self.device)
self.unseenclasses = torch.from_numpy(np.unique(test_unseen_label.cpu().numpy())).to(self.device)
self.ntrain = 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()
self.train_mapped_label = map_label(train_label, self.seenclasses)
self.data = {}
self.data['train_seen'] = {}
self.data['train_seen']['resnet_features'] = train_feature
self.data['train_seen']['labels']= train_label
self.data['train_seen'][self.auxiliary_data_source] = self.aux_data[train_label]
self.data['train_unseen'] = {}
self.data['train_unseen']['resnet_features'] = None
self.data['train_unseen']['labels'] = None
self.data['test_seen'] = {}
self.data['test_seen']['resnet_features'] = test_seen_feature
self.data['test_seen']['labels'] = test_seen_label
self.data['test_unseen'] = {}
self.data['test_unseen']['resnet_features'] = test_unseen_feature
self.data['test_unseen'][self.auxiliary_data_source] = self.aux_data[test_unseen_label]
self.data['test_unseen']['labels'] = test_unseen_label
self.unseenclass_aux_data = self.aux_data[self.unseenclasses]
self.seenclass_aux_data = self.aux_data[self.seenclasses]
def transfer_features(self, n, num_queries='num_features'):
print('size before')
print(self.data['test_unseen']['resnet_features'].size())
print(self.data['train_seen']['resnet_features'].size())
print('o'*100)
print(self.data['test_unseen'].keys())
for i,s in enumerate(self.unseenclasses):
features_of_that_class = self.data['test_unseen']['resnet_features'][self.data['test_unseen']['labels']==s ,:]
if 'attributes' == self.auxiliary_data_source:
attributes_of_that_class = self.data['test_unseen']['attributes'][self.data['test_unseen']['labels']==s ,:]
use_att = True
else:
use_att = False
if 'sentences' == self.auxiliary_data_source:
sentences_of_that_class = self.data['test_unseen']['sentences'][self.data['test_unseen']['labels']==s ,:]
use_stc = True
else:
use_stc = False
if 'word2vec' == self.auxiliary_data_source:
word2vec_of_that_class = self.data['test_unseen']['word2vec'][self.data['test_unseen']['labels']==s ,:]
use_w2v = True
else:
use_w2v = False
if 'glove' == self.auxiliary_data_source:
glove_of_that_class = self.data['test_unseen']['glove'][self.data['test_unseen']['labels']==s ,:]
use_glo = True
else:
use_glo = False
if 'wordnet' == self.auxiliary_data_source:
wordnet_of_that_class = self.data['test_unseen']['wordnet'][self.data['test_unseen']['labels']==s ,:]
use_hie = True
else:
use_hie = False
num_features = features_of_that_class.size(0)
indices = torch.randperm(num_features)
if num_queries!='num_features':
indices = indices[:n+num_queries]
print(features_of_that_class.size())
if i==0:
new_train_unseen = features_of_that_class[ indices[:n] ,:]
if use_att:
new_train_unseen_att = attributes_of_that_class[ indices[:n] ,:]
if use_stc:
new_train_unseen_stc = sentences_of_that_class[ indices[:n] ,:]
if use_w2v:
new_train_unseen_w2v = word2vec_of_that_class[ indices[:n] ,:]
if use_glo:
new_train_unseen_glo = glove_of_that_class[ indices[:n] ,:]
if use_hie:
new_train_unseen_hie = wordnet_of_that_class[ indices[:n] ,:]
new_train_unseen_label = s.repeat(n)
new_test_unseen = features_of_that_class[ indices[n:] ,:]
new_test_unseen_label = s.repeat( len(indices[n:] ))
else:
new_train_unseen = torch.cat(( new_train_unseen , features_of_that_class[ indices[:n] ,:]),dim=0)
new_train_unseen_label = torch.cat(( new_train_unseen_label , s.repeat(n)),dim=0)
new_test_unseen = torch.cat(( new_test_unseen, features_of_that_class[ indices[n:] ,:]),dim=0)
new_test_unseen_label = torch.cat(( new_test_unseen_label ,s.repeat( len(indices[n:]) )) ,dim=0)
if use_att:
new_train_unseen_att = torch.cat(( new_train_unseen_att , attributes_of_that_class[indices[:n] ,:]),dim=0)
if use_stc:
new_train_unseen_stc = torch.cat(( new_train_unseen_stc , sentences_of_that_class[indices[:n] ,:]),dim=0)
if use_w2v:
new_train_unseen_w2v = torch.cat(( new_train_unseen_w2v , word2vec_of_that_class[indices[:n] ,:]),dim=0)
if use_glo:
new_train_unseen_glo = torch.cat(( new_train_unseen_glo , glove_of_that_class[indices[:n] ,:]),dim=0)
if use_hie:
new_train_unseen_hie = torch.cat(( new_train_unseen_hie , wordnet_of_that_class[indices[:n] ,:]),dim=0)
print('new_test_unseen.size(): ', new_test_unseen.size())
print('new_test_unseen_label.size(): ', new_test_unseen_label.size())
print('new_train_unseen.size(): ', new_train_unseen.size())
#print('new_train_unseen_att.size(): ', new_train_unseen_att.size())
print('new_train_unseen_label.size(): ', new_train_unseen_label.size())
print('>> num unseen classes: ' + str(len(self.unseenclasses)))
#######
##
#######
self.data['test_unseen']['resnet_features'] = copy.deepcopy(new_test_unseen)
#self.data['train_seen']['resnet_features'] = copy.deepcopy(new_train_seen)
self.data['test_unseen']['labels'] = copy.deepcopy(new_test_unseen_label)
#self.data['train_seen']['labels'] = copy.deepcopy(new_train_seen_label)
self.data['train_unseen']['resnet_features'] = copy.deepcopy(new_train_unseen)
self.data['train_unseen']['labels'] = copy.deepcopy(new_train_unseen_label)
self.ntrain_unseen = self.data['train_unseen']['resnet_features'].size(0)
if use_att:
self.data['train_unseen']['attributes'] = copy.deepcopy(new_train_unseen_att)
if use_w2v:
self.data['train_unseen']['word2vec'] = copy.deepcopy(new_train_unseen_w2v)
if use_stc:
self.data['train_unseen']['sentences'] = copy.deepcopy(new_train_unseen_stc)
if use_glo:
self.data['train_unseen']['glove'] = copy.deepcopy(new_train_unseen_glo)
if use_hie:
self.data['train_unseen']['wordnet'] = copy.deepcopy(new_train_unseen_hie)
####
self.data['train_seen_unseen_mixed'] = {}
self.data['train_seen_unseen_mixed']['resnet_features'] = torch.cat((self.data['train_seen']['resnet_features'],self.data['train_unseen']['resnet_features']),dim=0)
self.data['train_seen_unseen_mixed']['labels'] = torch.cat((self.data['train_seen']['labels'],self.data['train_unseen']['labels']),dim=0)
self.ntrain_mixed = self.data['train_seen_unseen_mixed']['resnet_features'].size(0)
if use_att:
self.data['train_seen_unseen_mixed']['attributes'] = torch.cat((self.data['train_seen']['attributes'],self.data['train_unseen']['attributes']),dim=0)
if use_w2v:
self.data['train_seen_unseen_mixed']['word2vec'] = torch.cat((self.data['train_seen']['word2vec'],self.data['train_unseen']['word2vec']),dim=0)
if use_stc:
self.data['train_seen_unseen_mixed']['sentences'] = torch.cat((self.data['train_seen']['sentences'],self.data['train_unseen']['sentences']),dim=0)
if use_glo:
self.data['train_seen_unseen_mixed']['glove'] = torch.cat((self.data['train_seen']['glove'],self.data['train_unseen']['glove']),dim=0)
if use_hie:
self.data['train_seen_unseen_mixed']['wordnet'] = torch.cat((self.data['train_seen']['wordnet'],self.data['train_unseen']['wordnet']),dim=0)
#d = DATA_LOADER()