-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdata.py
381 lines (299 loc) · 13.5 KB
/
data.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
### Adapted from DEC implementation: https://github.com/XifengGuo/DEC-keras/blob/master/datasets.py
import numpy as np
import os
from os.path import join
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import MinMaxScaler
from keras.preprocessing.image import img_to_array, array_to_img
from keras.applications.vgg16 import preprocess_input, VGG16
from keras.models import Model
from keras import backend as K
from keras.preprocessing.text import Tokenizer
from keras.datasets import reuters
from keras.datasets import mnist
from keras.datasets import fashion_mnist # this requires keras>=2.0.9
from keras.datasets import imdb
from keras.datasets import cifar10
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.datasets import fetch_20newsgroups
def extract_vgg16_features(x):
K.clear_session()
im_h = 224
model = VGG16(include_top=True, weights='imagenet', input_shape=(im_h, im_h, 3))
feature_model = Model(model.input, model.get_layer('fc2').output)
print('Transforming images to 224x224...')
imgs = []
slices = x.shape[0]/1000
for i in range(0, slices):
xs = x[range(i*1000, (i+1)*1000)]
img = [img_to_array(array_to_img(im, scale=False).resize((im_h,im_h)))for im in xs]
img = preprocess_input(np.asarray(img))
img = feature_model.predict(img)
np.save('%s__imgnet' % i, img)
del img, xs
print('%s__imgnet' % i)
for i in range(0, slices):
imgs.extend(np.load('%s__imgnet.npy' % i))
return np.asarray(imgs)
def make_reuters_data(data_dir):
# download reuters data
data_path = data_dir
print('Downloading data...')
os.system('wget http://www.ai.mit.edu/projects/jmlr/papers/volume5/lewis04a/a12-token-files/lyrl2004_tokens_test_pt0.dat.gz -P %s' % data_path)
os.system('wget http://www.ai.mit.edu/projects/jmlr/papers/volume5/lewis04a/a12-token-files/lyrl2004_tokens_test_pt1.dat.gz -P %s' % data_path)
os.system('wget http://www.ai.mit.edu/projects/jmlr/papers/volume5/lewis04a/a12-token-files/lyrl2004_tokens_test_pt2.dat.gz -P %s' % data_path)
os.system('wget http://www.ai.mit.edu/projects/jmlr/papers/volume5/lewis04a/a12-token-files/lyrl2004_tokens_test_pt3.dat.gz -P %s' % data_path)
os.system('wget http://www.ai.mit.edu/projects/jmlr/papers/volume5/lewis04a/a12-token-files/lyrl2004_tokens_train.dat.gz -P %s' % data_path)
os.system('wget http://www.ai.mit.edu/projects/jmlr/papers/volume5/lewis04a/a08-topic-qrels/rcv1-v2.topics.qrels.gz -P %s' % data_path)
print('Unzipping data...')
os.system('gunzip %s/lyrl2004_tokens_test_pt0.dat.gz' % data_path)
os.system('gunzip %s/lyrl2004_tokens_test_pt1.dat.gz' % data_path)
os.system('gunzip %s/lyrl2004_tokens_test_pt2.dat.gz' % data_path)
os.system('gunzip %s/lyrl2004_tokens_test_pt3.dat.gz' % data_path)
os.system('gunzip %s/lyrl2004_tokens_train.dat.gz' % data_path)
os.system('gunzip %s/rcv1-v2.topics.qrels.gz' % data_path)
np.random.seed(1234)
did_to_cat = {}
cat_list = ['CCAT', 'GCAT', 'MCAT', 'ECAT']
with open(os.path.join(data_dir, 'rcv1-v2.topics.qrels')) as fin:
for line in fin.readlines():
line = line.strip().split(' ')
cat = line[0]
did = int(line[1])
if cat in cat_list:
did_to_cat[did] = did_to_cat.get(did, []) + [cat]
# did_to_cat = {k: did_to_cat[k] for k in list(did_to_cat.keys()) if len(did_to_cat[k]) > 1}
for did in list(did_to_cat.keys()):
if len(did_to_cat[did]) > 1:
del did_to_cat[did]
dat_list = ['lyrl2004_tokens_test_pt0.dat',
'lyrl2004_tokens_test_pt1.dat',
'lyrl2004_tokens_test_pt2.dat',
'lyrl2004_tokens_test_pt3.dat',
'lyrl2004_tokens_train.dat']
data = []
target = []
cat_to_cid = {'CCAT': 0, 'GCAT': 1, 'MCAT': 2, 'ECAT': 3}
del did
for dat in dat_list:
with open(os.path.join(data_dir, dat)) as fin:
for line in fin.readlines():
if line.startswith('.I'):
if 'did' in locals():
assert doc != ''
if did in did_to_cat:
data.append(doc)
target.append(cat_to_cid[did_to_cat[did][0]])
did = int(line.strip().split(' ')[1])
doc = ''
elif line.startswith('.W'):
assert doc == ''
else:
doc += line
print(len(data),len(target))
print((len(data), 'and', len(did_to_cat)))
# assert len(data) == len(did_to_cat)
x = CountVectorizer(dtype=np.float64, max_features=2000).fit_transform(data)
y = np.asarray(target)
from sklearn.feature_extraction.text import TfidfTransformer
x = TfidfTransformer(norm='l2', sublinear_tf=True).fit_transform(x)
x = x[:50000].astype(np.float32)
print(x.dtype, x.size)
y = y[:50000]
x = np.asarray(x.todense()) * np.sqrt(x.shape[1])
print('todense succeed')
p = np.random.permutation(x.shape[0])
x = x[p]
y = y[p]
print('permutation finished')
assert x.shape[0] == y.shape[0]
x = x.reshape((x.shape[0], -1))
np.save(os.path.join(data_dir, 'reutersidf10k.npy'), {'data': x, 'label': y})
def load_mnist():
# the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x = np.concatenate((x_train, x_test))
y = np.concatenate((y_train, y_test))
x = x.reshape((x.shape[0], -1))
x = np.divide(x, 255.)
print('MNIST samples', x.shape)
return x, y
def load_fashion_mnist():
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
x = np.concatenate((x_train, x_test))
y = np.concatenate((y_train, y_test))
x = x.reshape((x.shape[0], -1))
x = np.divide(x, 255.)
print('Fashion MNIST samples', x.shape)
return x, y
def load_pendigits(data_path='./data/pendigits'):
if not os.path.exists(data_path + '/pendigits.tra'):
os.system('wget http://mlearn.ics.uci.edu/databases/pendigits/pendigits.tra -P %s' % data_path)
os.system('wget http://mlearn.ics.uci.edu/databases/pendigits/pendigits.tes -P %s' % data_path)
os.system('wget http://mlearn.ics.uci.edu/databases/pendigits/pendigits.names -P %s' % data_path)
# load training data
with open(data_path + '/pendigits.tra') as file:
data = file.readlines()
data = [list(map(float, line.split(','))) for line in data]
data = np.array(data).astype(np.float32)
data_train, labels_train = data[:, :-1], data[:, -1]
print('data_train shape=', data_train.shape)
# load testing data
with open(data_path + '/pendigits.tes') as file:
data = file.readlines()
data = [list(map(float, line.split(','))) for line in data]
data = np.array(data).astype(np.float32)
data_test, labels_test = data[:, :-1], data[:, -1]
print('data_test shape=', data_test.shape)
x = np.concatenate((data_train, data_test)).astype('float32')
y = np.concatenate((labels_train, labels_test))
x /= 100.
print('pendigits samples:', x.shape)
return x, y
def load_usps(data_path='/content/data/usps'):
if not os.path.exists(data_path):
os.makedirs(data_path)
os.system('wget https://web.stanford.edu/~hastie/ElemStatLearn//datasets/zip.train.gz -P %s' % data_path)
os.system('wget https://web.stanford.edu/~hastie/ElemStatLearn//datasets/zip.test.gz -P %s' % data_path)
os.system('gunzip %s/zip.train' % data_path)
os.system('gunzip %s/zip.test' % data_path)
with open(data_path + '/zip.train') as f:
data = f.readlines()
data = data[1:-1]
data = [list(map(float, line.split())) for line in data]
data = np.array(data)
data_train, labels_train = data[:, 1:], data[:, 0]
with open(data_path + '/zip.test') as f:
data = f.readlines()
data = data[1:-1]
data = [list(map(float, line.split())) for line in data]
data = np.array(data)
data_test, labels_test = data[:, 1:], data[:, 0]
x = np.concatenate((data_train, data_test)).astype('float64') / 2.
# scale to [0,1]
from sklearn.preprocessing import MinMaxScaler
x = MinMaxScaler().fit_transform(x)
y = np.concatenate((labels_train, labels_test))
print('USPS samples', x.shape)
return x, y
def load_reuters(data_path='/content/data/reuters'):
if not os.path.exists(os.path.dirname(data_path)):
os.makedirs(os.path.dirname(data_path))
if not os.path.exists(os.path.join(data_path, 'reutersidf10k.npy')):
print('making reuters idf features')
make_reuters_data(data_path)
print(('reutersidf saved to ' + data_path))
data = np.load(os.path.join(data_path, 'reutersidf10k.npy')).item()
# has been shuffled
x = data['data']
y = data['label']
x = x.reshape((x.shape[0], -1)).astype('float64')
# scale to [0,1]
x = MinMaxScaler().fit_transform(x)
y = y.reshape((y.size,))
print(('REUTERSIDF10K samples', x.shape))
return x, y
def load_reuters_keras():
max_words = 1000
print('Loading data...')
(x, y), (_, _) = reuters.load_data(num_words=max_words, test_split=0.)
print(len(x), 'train sequences')
num_classes = np.max(y) + 1
print(num_classes, 'classes')
print('Vectorizing sequence data...')
tokenizer = Tokenizer(num_words=max_words)
x = tokenizer.sequences_to_matrix(x, mode='binary')
print('x_train shape:', x.shape)
return x.astype(float), y
def load_imdb():
max_words = 1000
print('Loading data...')
(x1, y1), (x2, y2) = imdb.load_data(num_words=max_words)
x = np.concatenate((x1, x2))
y = np.concatenate((y1, y2))
print(len(x), 'train sequences')
num_classes = np.max(y) + 1
print(num_classes, 'classes')
print('Vectorizing sequence data...')
tokenizer = Tokenizer(num_words=max_words)
x = tokenizer.sequences_to_matrix(x, mode='binary')
print('x_train shape:', x.shape)
return x.astype(float), y
def load_newsgroups():
newsgroups = fetch_20newsgroups(subset='all', remove=('headers', 'footers', 'quotes'))
vectorizer = TfidfVectorizer(max_features=2000, dtype=np.float64, sublinear_tf=True)
x_sparse = vectorizer.fit_transform(newsgroups.data)
x = np.asarray(x_sparse.todense())
y = newsgroups.target
print('News group data shape ', x.shape)
print("News group number of clusters: ", np.unique(y).size)
return x, y
def load_cifar10(data_path='/content/data/cifar10'):
(train_x, train_y), (test_x, test_y) = cifar10.load_data()
x = np.concatenate((train_x, test_x))
y = np.concatenate((train_y, test_y)).reshape((60000,))
# if features are ready, return them
if os.path.exists(data_path + '/cifar10_features.npy'):
return np.load(data_path + '/cifar10_features.npy'), y
# extract features
features = np.zeros((60000, 4096))
for i in range(6):
idx = range(i*10000, (i+1)*10000)
print("The %dth 10000 samples" % i)
features[idx] = extract_vgg16_features(x[idx])
print('Feature extracted, normalizing.')
# scale to [0,1]
features = MinMaxScaler().fit_transform(features)
# save features
np.save('cifar10_features',features)
np.save(data_path + '/cifar10_features.npy', features)
print('features saved to ' + data_path + '/cifar10_features.npy')
return features, y
def load_stl(data_path='./data/stl'):
if os.path.exists(data_path + '/stl_features.npy') or not os.path.exists(data_path + '/train_X.bin'):
os.system('wget http://ai.stanford.edu/~acoates/stl10/stl10_binary.tar.gz -P %s' % data_path)
os.system('tar -xf ./data/stl/stl10_binary.tar.gz --strip 1 -C %s' % data_path)
# get labels
y1 = np.fromfile(data_path + '/train_y.bin', dtype=np.uint8) - 1
y2 = np.fromfile(data_path + '/test_y.bin', dtype=np.uint8) - 1
y = np.concatenate((y1, y2))
# if features are ready, return them
if os.path.exists(data_path + '/stl_features.npy'):
return np.load(data_path + '/stl_features.npy'), y
# get data
x1 = np.fromfile(data_path + '/train_X.bin', dtype=np.uint8)
x1 = x1.reshape((int(x1.size/3/96/96), 3, 96, 96)).transpose((0, 3, 2, 1))
x2 = np.fromfile(data_path + '/test_X.bin', dtype=np.uint8)
x2 = x2.reshape((int(x2.size/3/96/96), 3, 96, 96)).transpose((0, 3, 2, 1))
x = np.concatenate((x1, x2)).astype(float)
# extract features
print('Extracting features...')
features = extract_vgg16_features(x)
# scale to [0,1]
print('Normalizing features...')
features = MinMaxScaler().fit_transform(features)
# save features
np.save(data_path + '/stl_features.npy', features)
print('features saved to ' + data_path + '/stl_features.npy')
return features, y
def load_data(dataset_name):
if dataset_name == 'mnist':
return load_mnist()
elif dataset_name == 'fmnist':
return load_fashion_mnist()
elif dataset_name == 'usps':
return load_usps()
elif dataset_name == 'pendigits':
return load_pendigits()
elif dataset_name == 'cifar10':
return load_cifar10()
elif dataset_name == 'reuters10k' or dataset_name == 'reuters':
return load_reuters()
elif dataset_name == 'stl':
return load_stl()
elif dataset_name == '20newsgroups':
return load_newsgroups()
else:
print('Not defined for loading', dataset_name)
exit(0)