-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
1,473 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: UTF-8 -*- | ||
''' | ||
@Project :shengxian_retrieval_onnx | ||
@File :faiss_demo.py | ||
@IDE :PyCharm | ||
@Author :haoran | ||
@Date :2022/4/25 17:19 | ||
''' | ||
import numpy as np | ||
import time | ||
import faiss | ||
|
||
|
||
|
||
|
||
|
||
if __name__ == '__main__': | ||
d = 1280 # dimension | ||
nb = 20000 # database size | ||
nq = 1 # nb of queries | ||
np.random.seed(1234) # make reproducible | ||
xb = np.random.random((nb, d)).astype('float32') | ||
xb_len = np.linalg.norm(xb, axis=1, keepdims=True) | ||
xb = xb / xb_len | ||
xq = np.random.random((nq, d)).astype('float32') | ||
xq_len = np.linalg.norm(xq, axis=1, keepdims=True) | ||
xq = xq / xq_len | ||
# CPU | ||
|
||
t1 = time.time() | ||
# index = faiss.IndexFlat(d, faiss.METRIC_INNER_PRODUCT) # 建立索引 | ||
index = faiss.IndexFlatIP(d) | ||
# 或者通过faiss.indexFlatIP(内积)实现 | ||
index.add(xb) # add vectors to the index | ||
nlist = 20000 # we want to see 4 nearest neighbors | ||
for i in range(10): | ||
|
||
D, I = index.search(-xq, nlist) # actual search | ||
t2 = time.time() | ||
print(D[0] + [1 for i in range(len(D[0]))],I) | ||
# print(D[0],I) | ||
|
||
print('faiss spend time %.4f' % ((t2 - t1) / 10)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: UTF-8 -*- | ||
''' | ||
@Project :shengxian_retrieval_onnx | ||
@File :faiss_demo.py | ||
@IDE :PyCharm | ||
@Author :haoran | ||
@Date :2022/4/25 17:19 | ||
''' | ||
import numpy as np | ||
import time | ||
import faiss | ||
|
||
|
||
|
||
|
||
|
||
if __name__ == '__main__': | ||
d = 1280 # dimension | ||
nb = 20000 # database size | ||
nq = 1 # nb of queries | ||
np.random.seed(1234) # make reproducible | ||
xb = np.random.random((nb, d)).astype('float32') | ||
xb_len = np.linalg.norm(xb, axis=1, keepdims=True) | ||
xb = xb / xb_len | ||
xq = np.random.random((nq, d)).astype('float32') | ||
xq_len = np.linalg.norm(xq, axis=1, keepdims=True) | ||
xq = xq / xq_len | ||
|
||
# CPU | ||
quantizer = faiss.IndexFlatIP(d) | ||
# index = faiss.IndexFlat(d, faiss.METRIC_INNER_PRODUCT) # 建立索引 | ||
index = faiss.IndexIVFFlat(quantizer, d, 50, faiss.METRIC_INNER_PRODUCT) | ||
index.train(xb) | ||
index.add(xb) # add vectors to the index | ||
t1 = time.time() | ||
nlist = 5 # we want to see 4 nearest neighbors | ||
for i in range(10): | ||
|
||
# 或者通过faiss.indexFlatIP(内积)实现 | ||
|
||
D, I = index.search(xq, nlist) # actual search | ||
t2 = time.time() | ||
print('faiss spend time %.4f' % ((t2 - t1) / 10)) | ||
|
Oops, something went wrong.