-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdataset_GBU.py
257 lines (219 loc) · 12 KB
/
dataset_GBU.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
import numpy as np
import scipy.io as sio
from sklearn import preprocessing
import torch
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, opt):
self.finetune = opt.finetune
if opt.dataset in ['FLO_EPGN','CUB_STC']:
if self.finetune:
self.read_fine_tune(opt)
else:
self.read(opt)
elif opt.dataset in ['CUB', 'AWA2', 'APY', 'FLO', 'SUN']:
self.read_matdataset(opt)
self.index_in_epoch = 0
self.epochs_completed = 0
self.feature_dim = self.train_feature.shape[1]
self.att_dim = self.attribute.shape[1]
self.text_dim = self.att_dim
self.tr_cls_centroid = np.zeros([self.seenclasses.shape[0], self.feature_dim], np.float32)
for i in range(self.seenclasses.shape[0]):
self.tr_cls_centroid[i] = np.mean(self.train_feature[self.train_label == i].numpy(), axis=0)
def read_fine_tune(self,opt):
if opt.dataset == "CUB_STC":
opt.dataset = "CUB"
if opt.dataset == "FLO_EPGN":
opt.dataset = "FLO"
# 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()
# matcontent = sio.loadmat(opt.dataroot + "/" + opt.dataset + "/cub_feat.mat")
matcontent = sio.loadmat(opt.dataroot + "/" + opt.dataset + "/" + opt.image_embedding + "_finetuned.mat")
feature = matcontent['features'].T
label = matcontent['labels'].astype(int).squeeze() - 1
# feature = matcontent['features'].T
matcontent = sio.loadmat(opt.dataroot + "/" + opt.dataset + "/data.mat")
# feature = matcontent['features'].T
# label = matcontent['labels'].astype(int).squeeze() - 1
train_att = matcontent['att_train']
seen_pro = matcontent['seen_pro']
attribute = matcontent['attribute']
unseen_pro = matcontent['unseen_pro']
self.attribute = torch.from_numpy(attribute).float()
self.train_att = seen_pro.astype(np.float32)
self.test_att = unseen_pro.astype(np.float32)
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()
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()
self.train_label = map_label(self.train_label, self.seenclasses)
self.test_unseen_label = map_label(self.test_unseen_label, self.unseenclasses)
self.test_seen_label = map_label(self.test_seen_label, self.seenclasses)
self.train_att = self.attribute[self.seenclasses].numpy()
self.test_att = self.attribute[self.unseenclasses].numpy()
def read(self, opt):
if opt.dataset == "CUB_STC":
opt.dataset = "CUB"
if opt.dataset == "FLO_EPGN":
opt.dataset = "FLO"
matcontent = sio.loadmat(opt.dataroot + "/" + opt.dataset + "/data.mat")
train_att = matcontent['att_train']
seen_pro = matcontent['seen_pro']
attribute = matcontent['attribute']
unseen_pro = matcontent['unseen_pro']
self.attribute = torch.from_numpy(attribute).float()
self.train_att = seen_pro.astype(np.float32)
self.test_att = unseen_pro.astype(np.float32)
train_fea = matcontent['train_fea']
test_seen_fea = matcontent['test_seen_fea']
test_unseen_fea = matcontent['test_unseen_fea']
scaler = preprocessing.MinMaxScaler()
_train_feature = scaler.fit_transform(train_fea)
_test_seen_feature = scaler.transform(test_seen_fea)
_test_unseen_feature = scaler.transform(test_unseen_fea)
mx = _train_feature.max()
train_fea = train_fea * (1 / mx)
test_seen_fea = test_seen_fea * (1 / mx)
test_unseen_fea = test_unseen_fea * (1 / mx)
self.train_feature = torch.from_numpy(train_fea).float()
self.test_seen_feature = torch.from_numpy(test_seen_fea).float()
self.test_unseen_feature = torch.from_numpy(test_unseen_fea).float()
matcontent = sio.loadmat(opt.dataroot + "/" + opt.dataset + "/label.mat")
train_idx = matcontent['train_idx'] - 1
train_label = matcontent['train_label_new']
test_unseen_idex = matcontent['test_unseen_idex'] - 1
test_seen_idex = matcontent['test_seen_idex'] - 1
self.train_label = torch.from_numpy(train_idx.squeeze()).long()
self.test_seen_label = torch.from_numpy(test_seen_idex.squeeze()).long()
self.test_unseen_label = torch.from_numpy(test_unseen_idex.squeeze()).long()
self.seenclasses = torch.from_numpy(np.unique(self.test_seen_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()
self.test_unseen_label = map_label(self.test_unseen_label, self.unseenclasses)
self.test_seen_label = map_label(self.test_seen_label, self.seenclasses)
def read_matdataset(self, opt):
matcontent = sio.loadmat(opt.dataroot + "/" + opt.dataset + "/" + opt.image_embedding + ".mat")
label = matcontent['labels'].astype(int).squeeze() - 1
if self.finetune:
matcontent = sio.loadmat(opt.dataroot + "/" + opt.dataset + "/" + opt.image_embedding + "_finetuned.mat")
# label = matcontent['labels'].astype(int).squeeze() - 1
feature = matcontent['features'].T
if opt.dataset == "APY" and self.finetune:
feature = feature.T
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 opt.dataset == "FLO":
# temp_norm = torch.norm(self.attribute, p=2, dim=1).unsqueeze(1).expand_as(self.attribute)
# self.attribute = self.attribute.div(temp_norm + 1e-5)
#
#
# matcontent = sio.loadmat(opt.dataroot + "/" + opt.dataset + "/data.mat")
#
# train_att = matcontent['att_train']
# seen_pro = matcontent['seen_pro']
# attribute = matcontent['attribute']
# unseen_pro = matcontent['unseen_pro']
# self.attribute = torch.from_numpy(attribute).float()
# self.train_att = seen_pro.astype(np.float32)
# self.test_att = unseen_pro.astype(np.float32)
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()
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()
self.train_label = map_label(self.train_label, self.seenclasses)
self.test_unseen_label = map_label(self.test_unseen_label, self.unseenclasses)
self.test_seen_label = map_label(self.test_seen_label, self.seenclasses)
self.train_att = self.attribute[self.seenclasses].numpy()
self.test_att = self.attribute[self.unseenclasses].numpy()
class FeatDataLayer(object):
def __init__(self, label, feat_data, opt):
"""Set the roidb to be used by this layer during training."""
assert len(label) == feat_data.shape[0]
self._opt = opt
self._feat_data = feat_data
self._label = label
self._shuffle_roidb_inds()
self._epoch = 0
def _shuffle_roidb_inds(self):
"""Randomly permute the training roidb."""
self._perm = np.random.permutation(np.arange(len(self._label)))
self._cur = 0
def _get_next_minibatch_inds(self):
"""Return the roidb indices for the next minibatch."""
if self._cur + self._opt.batchsize >= len(self._label):
self._shuffle_roidb_inds()
self._epoch += 1
db_inds = self._perm[self._cur:self._cur + self._opt.batchsize]
self._cur += self._opt.batchsize
return db_inds
def forward(self):
new_epoch = False
if self._cur + self._opt.batchsize >= len(self._label):
self._shuffle_roidb_inds()
self._epoch += 1
new_epoch = True
db_inds = self._perm[self._cur:self._cur + self._opt.batchsize]
self._cur += self._opt.batchsize
minibatch_feat = np.array([self._feat_data[i] for i in db_inds])
minibatch_label = np.array([self._label[i] for i in db_inds])
blobs = {'data': minibatch_feat, 'labels': minibatch_label, 'newEpoch': new_epoch, 'idx': db_inds}
return blobs
def get_whole_data(self):
blobs = {'data': self._feat_data, 'labels': self._label}
return blobs