This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
diffusion_dataset.py
321 lines (248 loc) · 9.29 KB
/
diffusion_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
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
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
from __future__ import print_function
import sys
import os
import numpy as np
import faiss
import pdb
import cPickle
import time
from multiprocessing.dummy import Pool as ThreadPool
def normalize_features(M):
sy = M.sum(axis=1)
sy[sy == 0] = 1
M /= sy.reshape(-1, 1)
return M
def parse_ndis(distractors):
assert distractors.startswith('flickr')
distractors = distractors[6:]
mult = 1
if distractors[-1] == 'M':
mult = 10**6
distractors = distractors[:-1]
if distractors[-1] == 'k':
mult = 10**3
distractors = distractors[:-1]
return int(distractors) * mult
def rate_limited_imap(f, l):
"""A threaded imap that does not produce elements faster than they
are consumed"""
pool = ThreadPool(1)
res = None
for i in l:
res_next = pool.apply_async(f, (i, ))
if res:
yield res.get()
res = res_next
yield res.get()
##########################################################
# Choose between the two datasets
##########################################################
class Opt:
pass
class BharathEval:
def __init__(self, Yte, novel_classes):
self.Yte = Yte
self.novel_classes = novel_classes
def remap_labels(self, label_map):
self.Yte = label_map[self.Yte]
self.novel_classes = label_map[self.novel_classes]
def compute_accuracies(self, val_L):
assert val_L.shape[0] == self.Yte.size, pdb.set_trace()
_, labels5 = faiss.kmax(val_L, 5)
# binary vector of elements that are ok in top5
ok = (labels5 == self.Yte.reshape(-1, 1)).sum(axis=1)
N, nclasses = val_L.shape
acc_full = ok.sum() / float(N)
nc_mask = np.zeros(nclasses, dtype=bool)
nc_mask[self.novel_classes] = True
nc_examples = nc_mask[self.Yte]
acc_novel_classes = ok[nc_examples].sum() / float(nc_examples.sum())
return acc_full, acc_novel_classes
def do_eval(self, val_L):
acc_full, acc_novel_classes = self.compute_accuracies(val_L)
return 'top-5 accuracy: %.3f (%.3f on novel)' % (
acc_full, acc_novel_classes)
def __str__(self):
return '%d examples, %d classes, %d novel classes' % (
self.Yte.size, np.bincount(self.Yte).astype(bool).sum(),
self.novel_classes.size)
fv3_dir = os.getenv('DDIR') + '/features/'
def load_bharath_fv3(class_set, include_base_class, split='train'):
label_info = eval(open("./label_idx.json").read())
labels_mask = np.zeros(1000, dtype=bool)
eval_classes_subset = np.array(label_info['novel_classes_%d' % class_set]) - 1
labels_mask[eval_classes_subset] = True
print("nb of eval classes", eval_classes_subset.size)
if include_base_class:
labels_mask[np.array(label_info['base_classes_%d' % class_set]) - 1] = True
h5_filename = fv3_dir + split + '.hdf5'
import h5py
print('open ', h5_filename)
f = h5py.File(h5_filename, 'r')
count= f['count'][0]
labels = f['all_labels'][:count]
if True:
npy_fname = h5_filename[:-5] + '_features.npy'
if not os.path.exists(npy_fname):
features = f['all_feats'][:count]
print("write ", npy_fname)
np.save(npy_fname, features)
else:
print("read ", npy_fname)
t0 = time.time()
features = np.load(npy_fname, mmap_mode='r')
features = np.array(features)
else:
features = f['all_feats'][:count]
print(' features loaded in %.3f s' % (time.time() - t0))
mask = labels_mask[labels]
return features[mask], labels[mask], eval_classes_subset
def load_traintest(nl, class_set=1, seed=1234,
include_base_class=True,
pca256=False, nnonlabeled=0):
# imagenet validation images
Xte, Yte, eval_classes_subset = load_bharath_fv3(
class_set, include_base_class, 'val')
# imagenet train images
Xtr, Ytr, eval_classes_subset = load_bharath_fv3(
class_set, include_base_class, 'train')
Yte = BharathEval(Yte, eval_classes_subset)
# reduce labels to consecutive numbers that start at 0
label_map = np.cumsum(np.bincount(Ytr).astype(bool)) - 1
Ytr = label_map[Ytr]
Yte.remap_labels(label_map)
eval_classes_subset = label_map[eval_classes_subset]
nclasses = Ytr.max() + 1
print("selecting images, seed=%d" % seed)
rs = np.random.RandomState(seed)
perm1 = []
perm0 = []
base = []
for cl in range(nclasses):
imnos = (Ytr == cl).nonzero()[0]
if cl in eval_classes_subset:
rs.shuffle(imnos)
perm1.append(imnos[:nl])
perm0.append(imnos[nl:])
else:
base.append(imnos)
if nnonlabeled == 0:
perm = np.hstack(base + perm1)
else:
perm = np.hstack(base + perm1 + perm0)
nnonlabeled = np.hstack(perm0).size
Ytr = Ytr[perm]
if nnonlabeled != 0:
Ytr[-nnonlabeled:] = -1
Xtr = Xtr[perm]
if pca256:
pca_fname = fv3_dir + 'PCAR256.vt'
print("load", pca_fname)
pcar = faiss.read_VectorTransform(pca_fname)
Xtr = pcar.apply_py(Xtr)
Xte = pcar.apply_py(Xte)
return Xtr, Ytr, Xte, Yte
###############################################################
# Same as above, instead of descriptors returns imnet filenames
def load_bharath_ids(opt, split='train'):
""" Load Bharath's imagenet descriptors and train/test split """
# if 'prn' in os.getenv('HOSTNAME'):
bharath_dataset_dir = '/mnt/vol/gfsai-local/ai-group/users/matthijs/bharath-dataset'
from torch.utils.serialization import load_lua
label_info = eval(open(bharath_dataset_dir + "/label_idx.json").read())
feat_basedir = bharath_dataset_dir + '/baseline50_' + split
features = []
labels = []
labels_mask = np.zeros(1000, dtype=bool)
# novel_classes_2 are the test-test
# novel_classes_1 are the test-val
eval_classes_subset = np.array(label_info['novel_classes_%d' % opt.class_set]) - 1
labels_mask[eval_classes_subset] = True
print("nb of eval classes", eval_classes_subset.size)
if opt.include_base_class:
labels_mask[np.array(label_info['base_classes_%d' % opt.class_set]) - 1] = True
from torch.utils.serialization import load_lua
label_info = eval(open(bharath_dataset_dir + "/label_idx.json").read())
feat_basedir = bharath_dataset_dir + '/baseline50_' + split
if split == 'train':
r = range(1, 128 + 1)
else:
r = range(5)
Tmeta = torchfile.load(
'/mnt/vol/gfsai-east/ai-group/users/bharathh/imagenet_meta/' + split + '.t7')
image_names = Tmeta['image_names']
count = 0
def load_chunk(chunk_no):
fname = '%s/feats_%d.t7' % (feat_basedir, chunk_no)
# print ("load", fname, 'count=', count, ' \r', end=' ')
print("load", chunk_no, ' \r', end=' ')
sys.stdout.flush()
return chunk_no, load_lua(fname)
pool = ThreadPool(10)
i0 = 0
for chunk_no, F in pool.imap(load_chunk, r):
# F = load_lua(fname)
Flabels = F.labels.numpy().astype(int) - 1
# pdb.set_trace()
# Ffeats = F.feats.numpy()
i1 = i0 + len(Flabels)
idx = F.idx.numpy()
Ffeats = image_names[idx - 1]
i0 = i1
mask = labels_mask[Flabels]
features.append(Ffeats[mask])
labels.append(Flabels[mask])
count += mask.sum()
features = np.vstack(features)
labels = np.hstack(labels)
return features, labels, eval_classes_subset
def load_bharath_traintest_ids(nl, class_set=1, seed=1234,
include_base_class=True,
nnonlabeled=0):
opt = Opt()
opt.bharath256 = True
opt.bharath = False
opt.class_set = class_set
opt.include_base_class = include_base_class
Xte, Yte, eval_classes_subset = load_bharath_ids(opt, 'val')
Xtr, Ytr, eval_classes_subset = load_bharath_ids(opt, 'train')
Yte = BharathEval(Yte, eval_classes_subset)
# reduce labels to consecutive numbers that start at 0
label_map = np.cumsum(np.bincount(Ytr).astype(bool)) - 1
Ytr = label_map[Ytr]
# Yte = label_map[Yte]
Yte.remap_labels(label_map)
eval_classes_subset = label_map[eval_classes_subset]
nclasses = Ytr.max() + 1
rs = np.random.RandomState(seed)
perm1 = []
perm0 = []
base = []
for cl in range(nclasses):
imnos = (Ytr == cl).nonzero()[0]
if cl in eval_classes_subset:
rs.shuffle(imnos)
perm1.append(imnos[:nl])
perm0.append(imnos[nl:])
else:
base.append(imnos)
if nnonlabeled == 0:
perm = np.hstack(base + perm1)
else:
perm = np.hstack(base + perm1 + perm0)
nnonlabeled = np.hstack(perm0).size
Ytr = Ytr[perm]
if nnonlabeled != 0:
Ytr[-nnonlabeled:] = -1
Xtr = Xtr[perm]
return Xtr, Ytr, Xte, Yte
def load_bharath_distractors(ndis):
fname = fv3_dir + '/f100m/concatenated_PCAR256.raw'
print("memmapping ", fname)
return np.memmap(fname, shape=(ndis, 256), dtype='float32')