PaddleClas/deploy/vector_search
Felix 41f527f2f4
Update test.py
2021-06-01 11:22:13 +08:00
..
src Add files via upload 2021-06-01 11:20:46 +08:00
Makefile Add files via upload 2021-06-01 11:18:55 +08:00
README.md Update README.md 2021-06-01 11:17:39 +08:00
index.so Add files via upload 2021-06-01 11:18:55 +08:00
interface.cc Add files via upload 2021-06-01 11:18:55 +08:00
interface.py Add files via upload 2021-06-01 11:18:55 +08:00
test.py Update test.py 2021-06-01 11:22:13 +08:00

README.md

向量检索

简介

一些垂域识别任务如车辆、商品等需要识别的类别数较大往往采用基于检索的方式通过查询向量与底库向量进行快速的最近邻搜索获得匹配的预测类别。向量检索模块提供基础的近似最近邻搜索算法基于百度自研的Möbius算法一种基于图的近似最近邻搜索算法用于最大内积搜索 (MIPS)。 该模块提供python接口支持numpy和 tensor类型向量,支持L2和Inner Product距离计算。

Mobius 算法细节详见论文 Möbius Transformation for Fast Inner Product Search on Graph

安装

若index.so不可用在项目目录下运行以下命令生成新的index.so文件

make index.so

编译环境: g++ 5.4.0 , 9.3.0. 其他版本也可能工作。 请确保您的 C++ 编译器支持 C++11 标准。

快速使用

import numpy as np
from interface import *

# 随机产生样本
index_vectors = np.random.rand(100000,128).astype(np.float32) 
query_vector = np.random.rand(128).astype(np.float32) 
index_docs = ["ID_"+str(i) for i in range(100000)]

# 初始化索引结构
indexer = Graph_Index(dist_type="IP") #支持"IP"和"L2"
indexer.build(gallery_vectors=index_vectors, gallery_docs=index_docs, pq_size=100, index_path='test')

# 查询
scores, docs = indexer.search(query=query_vector, return_k=10, search_budget=100)
print(scores)
print(docs)

# 保存与加载
indexer.dump(index_path="test") 
indexer.load(index_path="test")