-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset.py
220 lines (187 loc) · 10.3 KB
/
dataset.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
import numpy as np
import scipy.io as sio
from termcolor import cprint
import pickle
import sys
from sklearn.decomposition import PCA
class LoadDataset(object):
def __init__(self, opt):
if opt.dataset == 'CUB2011':
self.part_num = 7
txt_feat_path = 'data/CUB2011/CUB_Porter_7551D_TFIDF_new.mat'
train_test_split_dir = 'data/CUB2011/train_test_split_{}.mat'.format(opt.splitmode)
if opt.splitmode == 'easy':
pfc_label_path_train = 'data/CUB2011/labels_train.pkl'
pfc_label_path_test = 'data/CUB2011/labels_test.pkl'
pfc_feat_path_train = 'data/CUB2011/pfc_feat_train.mat'
pfc_feat_path_test = 'data/CUB2011/pfc_feat_test.mat'
train_cls_num = 150
test_cls_num = 50
else:
pfc_label_path_train = 'data/CUB2011/labels_train_hard.pkl'
pfc_label_path_test = 'data/CUB2011/labels_test_hard.pkl'
pfc_feat_path_train = 'data/CUB2011/pfc_feat_train_hard.mat'
pfc_feat_path_test = 'data/CUB2011/pfc_feat_test_hard.mat'
train_cls_num = 160
test_cls_num = 40
else:
self.part_num = 6
txt_feat_path = 'data/NABird/NAB_Porter_13217D_TFIDF_new.mat'
train_cls_num = 323
test_cls_num = 81
train_test_split_dir = 'data/NABird/train_test_split_NABird_{}.mat'.format(opt.splitmode)
if opt.splitmode == 'easy':
pfc_label_path_train = 'data/NABird/labels_train.pkl'
pfc_label_path_test = 'data/NABird/labels_test.pkl'
pfc_feat_path_train = 'data/NABird/pfc_feat_train_easy.mat'
pfc_feat_path_test = 'data/NABird/pfc_feat_test_easy.mat'
else:
pfc_label_path_train = 'data/NABird/labels_train_hard.pkl'
pfc_label_path_test = 'data/NABird/labels_test_hard.pkl'
pfc_feat_path_train = 'data/NABird/pfc_feat_train_hard.mat'
pfc_feat_path_test = 'data/NABird/pfc_feat_test_hard.mat'
self.pfc_feat_data_train = sio.loadmat(pfc_feat_path_train)['pfc_feat'].astype(np.float32)
self.pfc_feat_data_test = sio.loadmat(pfc_feat_path_test)['pfc_feat'].astype(np.float32)
cprint("pfc_feat_file: {} || {} ".format(pfc_feat_path_train, pfc_feat_path_test), 'red')
self.train_cls_num = train_cls_num
self.test_cls_num = test_cls_num
self.feature_dim = self.pfc_feat_data_train.shape[1]
# calculate the corresponding centroid.
with open(pfc_label_path_train, 'rb') as fout1, open(pfc_label_path_test, 'rb') as fout2:
if sys.version_info >= (3, 0):
self.labels_train = pickle.load(fout1, encoding='latin1').astype(int)
self.labels_test = pickle.load(fout2, encoding='latin1')
else:
self.labels_train = pickle.load(fout1)
self.labels_test = pickle.load(fout2)
# Normalize feat_data to zero-centered
mean = self.pfc_feat_data_train.mean()
var = self.pfc_feat_data_train.var()
self.pfc_feat_data_train = (self.pfc_feat_data_train - mean) / var
self.pfc_feat_data_test = (self.pfc_feat_data_test - mean) / var
self.tr_cls_centroid = np.zeros([train_cls_num, self.pfc_feat_data_train.shape[1]]).astype(np.float32)
for i in range(train_cls_num):
self.tr_cls_centroid[i] = np.mean(self.pfc_feat_data_train[self.labels_train == i], axis=0)
self.part_cls_centrild = np.zeros([train_cls_num, self.part_num, 512]).astype(np.float32)
for i in range(train_cls_num):
for p in range(self.part_num):
self.part_cls_centrild[i][p] = np.mean(self.pfc_feat_data_train[self.labels_train==i,
p*512:(p+1)*512],axis=0)
self.mean_intra_dist = np.zeros([train_cls_num, self.part_num]).astype(np.float32)
self.nearest_inter_dist = np.zeros([train_cls_num, self.part_num]).astype(np.float32)
for i in range(train_cls_num):
for p in range(self.part_num):
# num = sum(self.labels_train == i)
current_features = self.pfc_feat_data_train[self.labels_train == i, p*512:(p+1) * 512]
dif = current_features - self.part_cls_centrild[i][p]
inter_dists = np.linalg.norm(dif, axis=1)
self.mean_intra_dist[i][p] = np.mean(inter_dists)
nearest_dist = 100
for y in range(train_cls_num):
dist = np.linalg.norm(self.part_cls_centrild[i][p] - self.part_cls_centrild[y][p])
if dist != 0 and dist < nearest_dist:
nearest_dist = dist
self.nearest_inter_dist[i][p] = nearest_dist
intra_inter_ratia = self.nearest_inter_dist/self.mean_intra_dist
self.weights = []
for p in range(self.part_num):
self.weights.append(intra_inter_ratia[:, p].mean())
import matplotlib.pyplot as plt
print("Computed weights for patches: ",self.weights)
self.train_text_feature, self.test_text_feature = get_text_feature(txt_feat_path, train_test_split_dir)
self.text_dim = self.train_text_feature.shape[1]
# self.att = np.concatenate((self.train_text_feature, self.test_text_feature))
# dim_red = 200 if opt.dataset == "CUB2011" else 400
# pca = PCA(n_components=dim_red)
# pca.fit(self.att)
# self.att = pca.transform(self.att)
# self.train_text_feature = self.att[:train_cls_num]
# self.test_text_feature = self.att[train_cls_num:]
class LoadDataset_NAB(object):
def __init__(self, opt):
txt_feat_path = 'data/NABird/NAB_Porter_13217D_TFIDF_new.mat'
if opt.splitmode == 'easy':
train_test_split_dir = 'data/NABird/train_test_split_NABird_easy.mat'
pfc_label_path_train = 'data/NABird/labels_train.pkl'
pfc_label_path_test = 'data/NABird/labels_test.pkl'
pfc_feat_path_train = 'data/NABird/pfc_feat_train_easy.mat'
pfc_feat_path_test = 'data/NABird/pfc_feat_test_easy.mat'
train_cls_num = 323
test_cls_num = 81
else:
train_test_split_dir = 'data/NABird/train_test_split_NABird_hard.mat'
pfc_label_path_train = 'data/NABird/labels_train_hard.pkl'
pfc_label_path_test = 'data/NABird/labels_test_hard.pkl'
pfc_feat_path_train = 'data/NABird/pfc_feat_train_hard.mat'
pfc_feat_path_test = 'data/NABird/pfc_feat_test_hard.mat'
train_cls_num = 323
test_cls_num = 81
self.pfc_feat_data_train = sio.loadmat(pfc_feat_path_train)['pfc_feat'].astype(np.float32)
self.pfc_feat_data_test = sio.loadmat(pfc_feat_path_test)['pfc_feat'].astype(np.float32)
cprint("pfc_feat_file: {} || {} ".format(pfc_feat_path_train, pfc_feat_path_test), 'red')
self.train_cls_num = train_cls_num
self.test_cls_num = test_cls_num
self.feature_dim = self.pfc_feat_data_train.shape[1]
# calculate the corresponding centroid.
with open(pfc_label_path_train, 'rb') as fout1, open(pfc_label_path_test, 'rb') as fout2:
if sys.version_info >= (3, 0):
self.labels_train = pickle.load(fout1, encoding='latin1')
self.labels_test = pickle.load(fout2, encoding='latin1')
else:
self.labels_train = pickle.load(fout1)
self.labels_test = pickle.load(fout2)
# Normalize feat_data to zero-centered
mean = self.pfc_feat_data_train.mean()
var = self.pfc_feat_data_train.var()
self.pfc_feat_data_train = (self.pfc_feat_data_train - mean) / var
self.pfc_feat_data_test = (self.pfc_feat_data_test - mean) / var
self.tr_cls_centroid = np.zeros([train_cls_num, self.pfc_feat_data_train.shape[1]]).astype(np.float32)
for i in range(train_cls_num):
self.tr_cls_centroid[i] = np.mean(self.pfc_feat_data_train[self.labels_train == i], axis=0)
self.train_text_feature, self.test_text_feature = get_text_feature(txt_feat_path, train_test_split_dir)
self.text_dim = self.train_text_feature.shape[1]
class FeatDataLayer(object):
def __init__(self, label, feat_data, opt):
assert len(label) == feat_data.shape[0]
self._opt = opt
self._feat_data = feat_data
self._label = label
self._shuffle_roidb_inds()
def _shuffle_roidb_inds(self):
"""Randomly permute the training roidb."""
self._perm = np.random.permutation(np.arange(len(self._label)))
# self._perm = np.arange(len(self._roidb))
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()
db_inds = self._perm[self._cur:self._cur + self._opt.batchsize]
self._cur += self._opt.batchsize
return db_inds
def _get_next_minibatch(self):
"""Return the blobs to be used for the next minibatch.
"""
db_inds = self._get_next_minibatch_inds()
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}
return blobs
def forward(self):
"""Get blobs and copy them into this layer's top blob vector."""
blobs = self._get_next_minibatch()
return blobs
def get_whole_data(self):
blobs = {'data': self._feat_data, 'labels': self._label}
return blobs
def get_text_feature(dir, train_test_split_dir):
train_test_split = sio.loadmat(train_test_split_dir)
# get training text feature
train_cid = train_test_split['train_cid'].squeeze()
text_feature = sio.loadmat(dir)['PredicateMatrix']
train_text_feature = text_feature[train_cid - 1] # 0-based index
# get testing text feature
test_cid = train_test_split['test_cid'].squeeze()
text_feature = sio.loadmat(dir)['PredicateMatrix']
test_text_feature = text_feature[test_cid - 1] # 0-based index
return train_text_feature.astype(np.float32), test_text_feature.astype(np.float32)