-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun_tripletization.py
More file actions
375 lines (321 loc) · 12 KB
/
Copy pathrun_tripletization.py
File metadata and controls
375 lines (321 loc) · 12 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import re
import random
import itertools
import joblib
import h5py
import numpy as np
from pathlib import Path
from tomlparse import argparse
from collections import Counter
from dataclasses import dataclass
from scipy.spatial.distance import cdist
from typing import Union, Callable, Optional, Iterable
Array = np.ndarray
def parse_args():
parser = argparse.ArgumentParser(
description="Extract features and tripletize from a dataset using a pretrained model and module"
)
parser.add_argument(
"--file_path",
type=str,
default="./data/image_data/things",
help="Path to the representation used to extract triplets",
)
parser.add_argument(
"--key",
type=str,
help="""If the file path is a .pkl file, specify the key to load the data
(use dot-separated keys for nested dictionaries)""",
)
parser.add_argument(
"--out_path",
type=str,
default="./data/triplets",
help="Path to save the triplets",
)
parser.add_argument(
"--adaptive",
action="store_true",
default=False,
help="Adaptively sample triplets",
)
parser.add_argument(
"--n_samples",
type=int,
default=int(2e7),
help="Number of samples to use for tripletization",
)
parser.add_argument(
"--seed", type=int, default=0, help="Random seed for tripletization"
)
parser.add_argument(
"--triplet_path",
type=str,
default=None,
help="Path to any triplets to use for sampling, i.e. compute ooo. based on these triplets",
)
parser.add_argument(
"--similarity",
type=str,
default="dot",
choices=["dot", "cosine", "euclidean"],
help="Similarity metric to use for tripletization",
)
parser.add_argument(
"--k",
type=int,
default=3,
help="Number of nearest neighbors to use. 3 = triplet, 2 pairwise",
choices=[2, 3],
)
parser.add_argument(
"--train_fraction",
type=float,
default=0.9,
help="Fraction of data to use for training",
)
return parser.parse_args()
def cosine_matrix(
X: Array, a_min: float = -1.0, a_max: float = 1.0, eps: float = 1e-8
) -> Array:
"""Compute cosine-similarity matrix."""
num = X @ X.T
l2_norms = np.linalg.norm(X, axis=1)
denom = np.outer(l2_norms, l2_norms) + eps
S = (num / denom).clip(min=a_min, max=a_max)
return S
def dot_matrix(X: Array) -> Array:
"""Compute dot-product matrix."""
S = X @ X.T
return S
def euclidean_matrix(X: Array) -> Array:
"""Compute euclidean similarity matrix."""
D = cdist(X, X, "euclidean")
S = 1 / (1 + D)
return S
def get_similarity(X: Array, similarity: Union[str, Callable] = "dot") -> Array:
similarity_functions = {
"cosine": cosine_matrix,
"dot": dot_matrix,
"euclidean": euclidean_matrix,
}
if callable(similarity):
S = similarity(X)
elif isinstance(similarity, str):
try:
S = similarity_functions[similarity](X)
except KeyError:
raise ValueError(f"Similarity metric {similarity} not supported")
else:
raise TypeError("The 'similarity' must be either a string or a function")
return S
def get_nested_data(X: dict, nested_key: str) -> Array:
keys = nested_key.split(".")
for key in keys:
X = X[key]
return X
def load_domain(path: str, key: Optional[str] = None) -> Array:
"""Load features from a file can either be a .npy or .txt file or a dict"""
search = re.search(r"(npy|txt|pkl|h5)$", path)
if not os.path.exists(path):
raise FileNotFoundError(f"File {path} does not exist")
if not search:
raise ValueError("Input file must be a .npy or .txt file")
if re.search(r"(npy)$", path):
return np.load(path)
elif re.search(r"(txt)$", path):
return np.loadtxt(path)
elif re.search(r"(pkl)$", path):
data = joblib.load(path)
return get_nested_data(data, key)
elif re.search(r"(h5)$", path):
with h5py.File(path, "r") as handler:
if key:
data = get_nested_data(handler, key)
else:
data = handler
return data[:]
@dataclass(init=True, repr=True)
class Sampler(object):
file_path: str
out_path: str
n_samples: int
k: int = 3
train_fraction: float = 0.9
seed: int = 42
sample_type: str = "random"
similarity: Union[str, Callable] = "dot"
transforms: Optional[Callable] = None
triplet_path: Optional[str] = None
key: Optional[str] = None
def __post_init__(self):
if self.k not in [2, 3]:
raise ValueError(
"Only triplets (k=3) and pairwise (k=2) are supported at the moment"
)
if self.train_fraction > 1 or self.train_fraction < 0:
raise ValueError("Train fraction must be between 0 and 1")
if self.sample_type not in ["random", "adaptive"]:
raise ValueError("Sample type must be either 'random' or 'adaptive'")
if not os.path.exists(self.out_path):
os.makedirs(self.out_path)
self.X = load_domain(self.file_path, self.key)
if self.transforms:
self.X = self.transforms(self.X)
else:
self.X = self.default_transforms(self.X)
self.S = get_similarity(self.X, similarity=self.similarity)
self.n_objects, self.n_features = self.X.shape
def default_transforms(self, X: Array) -> Array:
X = np.maximum(0, X)
nan_indices = np.isnan(X[:, :]).any(axis=1)
X = X[~nan_indices]
return X
def softmax(self, z: Array) -> Array:
proba = np.exp(z) / np.sum(np.exp(z))
return proba
def get_choice(self, S: Array, triplet: Array) -> Array:
combs = list(itertools.combinations(triplet, 2))
sims = [S[comb[0], comb[1]] for comb in combs]
positive = combs[np.argmax(sims)]
ooo = list(set(triplet).difference(set(positive)))
choice = np.hstack((positive, ooo))
return choice
def log_softmax_scaled(self, X: Array, const: float = 0.0) -> Array:
"""see https://www.xarg.org/2016/06/the-log-sum-exp-trick-in-machine-learning/"""
X = X - const
scaled_proba = np.exp(X) / np.sum(np.exp(X))
scaled_log_proba = const + np.log(scaled_proba)
return scaled_log_proba
def find_triplet_argmax(self, S: Array, triplet: Array) -> Array:
combs = list(itertools.combinations(triplet, 2))
sims = [S[comb[0], comb[1]] for comb in combs]
positive = combs[np.argmax(sims)]
ooo = list(set(triplet).difference(set(positive)))
choice = np.hstack((positive, ooo))
return choice
def select_odd_one_outs(self, triplets: Iterable) -> Array:
ooo = np.zeros((self.n_samples, self.k), dtype=int)
for i, triplet in enumerate(triplets):
ooo[i] = self.find_triplet_argmax(self.S, triplet)
return ooo
def sample_adaptive(self):
"""Create similarity judgements."""
unique_triplets = set()
count = Counter()
count.update({x: 0 for x in range(self.n_objects)})
# At the start all classes have zero counts and we sample uniformly
p_per_item = [1 / self.n_objects for _ in range(self.n_objects)]
sample_idx, n_iter = 1, 1
while sample_idx < self.n_samples + 1:
n_iter += 1
print(
f"{n_iter} samples drawn, {sample_idx}/{self.n_samples} added", end="\r"
)
triplet = np.random.choice(
range(self.n_objects), 3, replace=False, p=p_per_item
)
# Using this we can avoid duplicate triplets when adding to the set
triplet.sort()
triplet = tuple(triplet)
# Add to set and increase count if triplet is still unique
if triplet not in unique_triplets:
count.update(triplet)
unique_triplets.add(triplet)
sample_idx += 1
# Update histogram of each class and sample random choices with the inverse of the actual distribution
if sample_idx % 100_000 == 0:
sum_count = sum(count.values())
sorted_count = sorted(count.items())
# Make smallest proba the largest
inverse_probas_per_item = [1 - s[1] / sum_count for s in sorted_count]
# Correct uniform distribution
norm_probas = [
float(i) / sum(inverse_probas_per_item)
for i in inverse_probas_per_item
]
p_per_item = norm_probas
ooo_choices = self.select_odd_one_outs(unique_triplets)
return ooo_choices
def random_combination(self, iterable: Iterable, r: int):
"Random selection from itertools.combinations(iterable, r)"
pool = tuple(iterable)
n = len(pool)
indices = sorted(
random.sample(range(n), r)
) # sorting prevents adding duplicates!
return tuple(pool[i] for i in indices)
def sample_random(self) -> Array:
"""Sample triplets based on the similarity matrix."""
unique_triplets = set()
items = list(range(self.n_objects))
n_triplets = 0
while n_triplets < self.n_samples:
print(f"{n_triplets}/{self.n_samples} added", end="\r")
sample = self.random_combination(items, 3)
unique_triplets.add(sample)
n_triplets = len(unique_triplets)
ooo_choices = self.select_odd_one_outs(unique_triplets)
return ooo_choices
def sample_pairs(self) -> Array:
combs = np.array(list(itertools.combinations(range(self.n_objects), self.k)))
random_sample = combs[
np.random.choice(
np.arange(combs.shape[0]), size=self.n_samples, replace=False
)
]
return random_sample
def train_test_split(self, ooo_choices: Union[list, Array]):
"""Split triplet data into train and test splits."""
random.seed(0)
np.random.shuffle(ooo_choices)
N = ooo_choices.shape[0]
frac = int(N * self.train_fraction)
train_split = ooo_choices[:frac]
test_split = ooo_choices[frac:]
return train_split, test_split
def run(self) -> None:
self()
def __call__(self) -> None:
"""Sample triplets and save them to disk."""
if self.k == 2:
choices = self.sample_pairs()
# If the triplets are already provided, just load them and select the odd one out
if self.triplet_path:
unique_triplets = load_domain(self.triplet_path)
unique_triplets = unique_triplets.astype(int)
self.n_samples = unique_triplets.shape[0]
choices = self.select_odd_one_outs(unique_triplets)
fname = Path(self.triplet_path).stem + ".npy"
with open(os.path.join(self.out_path, fname), "wb") as f:
np.save(f, choices)
return
if self.sample_type == "adaptive":
choices = self.sample_adaptive()
else:
choices = self.sample_random()
train_split, test_split = self.train_test_split(choices)
percentage = int(self.train_fraction * 100)
with open(os.path.join(self.out_path, f"train_{percentage}.npy"), "wb") as f:
np.save(f, train_split)
with open(
os.path.join(self.out_path, f"test_{100 - percentage}.npy"), "wb"
) as f:
np.save(f, test_split)
if __name__ == "__main__":
args = parse_args()
sampler = Sampler(
args.file_path,
args.out_path,
n_samples=args.n_samples,
k=args.k,
train_fraction=args.train_fraction,
seed=args.seed,
similarity=args.similarity,
triplet_path=args.triplet_path,
key=args.key,
)
sampler.run()