forked from louiezzang/faiss-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
faiss_index.py
124 lines (110 loc) · 3.14 KB
/
faiss_index.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
import logging
from os.path import isfile
import faiss
import numpy as np
class FaissIndex:
def __init__(self, dim: int, save_path: str, num_threads: int = None):
"""
Constructor.
:param dim:
:param save_path:
:param num_threads
"""
self.dim = dim
if num_threads is not None and num_threads > 0:
faiss.omp_set_num_threads(num_threads)
if isfile(save_path):
logging.debug("restore: %s", save_path)
self._index = faiss.read_index(save_path)
else:
self._sub_index = faiss.IndexFlat(dim)
self._index = faiss.IndexIDMap2(self._sub_index)
def replace(self, xb, ids):
"""
Replaces the index with new data if the id exists already and adds the index if the id does not exist.
:param xb:
:param ids:
:return:
"""
logging.debug("replace: %s", ids)
# Remove the existing ones
self.remove(ids)
return self._index.add_with_ids(xb, ids)
def rebuild(self, xb, ids):
"""
Rebuilds the index with new data. The existing ones will be all deleted.
:param xb:
:param ids:
:return:
"""
logging.debug("rebuild: %s", ids)
# Reset the index
self._index.reset()
# self._sub_index = faiss.IndexFlat(self.dim)
# self._index = faiss.IndexIDMap2(self._sub_index)
return self._index.add_with_ids(xb, ids)
def add(self, xb, ids):
"""
Adds data with ids.
:param xb:
:param ids:
:return:
"""
return self._index.add_with_ids(xb, ids)
def search(self, xq, k=10):
"""
Searches by vector.
:param xq:
:param k:
:return:
"""
return self._index.search(xq, k)
def search_by_id(self, id, k=10):
"""
Searches by id.
:param id:
:param k:
:return:
"""
try:
x = self._index.reconstruct(id)
xq = np.expand_dims(x, axis=0)
except RuntimeError as e:
if str(e).endswith("not found"):
return ([None], [None])
else:
raise e
return self.search(xq, k)
def ntotal(self):
"""
Returns the total number of rows.
:return:
"""
return self._index.ntotal
def remove(self, ids):
"""
Removes by ids.
:param ids:
:return:
"""
return self._index.remove_ids(ids)
def restore(self, filepath):
"""
Restores with new file path.
:param filepath:
:return:
"""
pre_index = self._index
self._index = faiss.read_index(filepath)
if pre_index:
pre_index.reset()
def save(self, filepath):
"""
Saves the index into the file .
:param filepath:
:return:
"""
if self.ntotal() > 0:
faiss.write_index(self._index, filepath)
def set_nprobe(self, nprobe):
faiss.ParameterSpace().set_index_parameter(self._index, "nprobe", nprobe)