forked from vyasn30/image_search
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatastore.py
46 lines (29 loc) · 1.13 KB
/
datastore.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
import numpy as np
import json
import faiss
# GPU = faiss.StandardGPUResources()
class Datastore:
def __init__(self, vectors=None, identifiers=None, dimensions=128, gpu=False, inbuilt_index=False):
self.__dimensions = dimensions
self.__vectors = None
self.__identifiers = None
self.representations = dict()
self.__mappings = dict()
# self.index = faiss.read_index("vecdata/vector.index")
self.index = faiss.IndexFlatIP(self.__dimensions) #bruteforce search index
def add(self,vector, identifier, name):
"""
Adds a vector, identifier and name to the index,
NOTE: make this method better, its very hacky
"""
self.representations[str(identifier)] = vector
self.__mappings[str(identifier)] = name
to_add = []
to_add.append(vector)
to_add = np.array(to_add)
to_add = to_add.reshape(1, self.__dimensions)
faiss.normalize_L2(to_add)
self.index.add(to_add)
def search(self, emb):
D, I = self.index.search(emb, 10)
return D,I