-
Notifications
You must be signed in to change notification settings - Fork 3
/
ivf.py
62 lines (52 loc) · 1.76 KB
/
ivf.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
import numpy as np
import faiss
import struct
import os
source = './'
datasets = ['gist']
# the number of clusters
K = 4096
def read_fvecs(filename, c_contiguous=True):
fv = np.fromfile(filename, dtype=np.float32)
if fv.size == 0:
return np.zeros((0, 0))
dim = fv.view(np.int32)[0]
assert dim > 0
fv = fv.reshape(-1, 1 + dim)
if not all(fv.view(np.int32)[:, 0] == dim):
raise IOError("Non-uniform vector sizes in " + filename)
fv = fv[:, 1:]
if c_contiguous:
fv = fv.copy()
return fv
def to_fvecs(filename, data):
print(f"Writing File - {filename}")
with open(filename, 'wb') as fp:
for y in data:
d = struct.pack('I', len(y))
fp.write(d)
for x in y:
a = struct.pack('f', x)
fp.write(a)
if __name__ == '__main__':
for dataset in datasets:
print(f"Clustering - {dataset}")
# path
path = os.path.join(source, dataset)
data_path = os.path.join(path, f'{dataset}_base.fvecs')
centroids_path = os.path.join(path, f'{dataset}_centroid_{K}.fvecs')
randomzized_cluster_path = os.path.join(path, f"O{dataset}_centroid_{K}.fvecs")
transformation_path = os.path.join(path, 'O.fvecs')
# read data vectors
X = read_fvecs(data_path)
P = read_fvecs(transformation_path)
D = X.shape[1]
# cluster data vectors
index = faiss.index_factory(D, f"IVF{K},Flat")
index.verbose = True
index.train(X)
centroids = index.quantizer.reconstruct_n(0, index.nlist)
to_fvecs(centroids_path, centroids)
# randomized centroids
centroids = np.dot(centroids, P)
to_fvecs(randomzized_cluster_path, centroids)