高维空间中的ann搜索
hdidx的Python项目详细描述
什么是hdidx?
^ {STR 1 } $ HDDIX <强>是近似最近邻(ANN)的Python包 搜索。最近邻(nn)搜索在 因为*Curse of Dimensionality*的高维空间 问题。hdidx的基本思想是压缩原始特性 向量到紧致二进制码中,并执行近似神经网络搜索 而不是提取nn搜索。这样可以大大减少存储空间 并能显著加快搜索速度。
架构
hdidx有三个主要模块:1)Encoder,它可以压缩 将原始特征向量转换为压缩二进制哈希码,2)Indexer 它可以索引数据库项并搜索近似最近的。 给定查询项的邻居,以及3)Storage模块 封装底层数据存储,可以是内存或nosql 像lmdb这样的数据库,用于Indexer。
当前版本实现以下功能压缩算法:
- Product Quantization[1]。
- Spectral Hashing[2]。
要使用hdidx,首先应该从一些学习中学习Encoder。 向量。然后可以使用 学习了Encoder,并通过 Indexer,它将索引写入指定的存储 中等。当查询向量出现时,它将通过 相同的Encoder和Indexer将找到与 这个查询向量。
安装
hdidx可以通过pip:
安装[sudo] pip install cython [sudo] pip install hdidx
默认情况下,hdidx使用由 *SciPy*。为了更有效率,你可以 安装*OpenCV*的python扩展,其中 可以通过apt-get在ubuntu上安装。对于其他Linux 发行版,例如centos,您需要从源代码编译它。
[sudo] apt-get install python-opencv
hdidx将自动使用*OpenCV* 有空。
Windows指南
一般依赖项:
安装上述软件后,请下载 `stdint.h<;http://msinttypes.googlecode.com/svn/trunk/stdint.h>;。`_ 并将其置于VisualC++的^ {tt15} $文件夹之下,例如 C:\Users\xxx\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\include。 然后可以通过anaconda命令中的pip安装hdidx 提示。
示例
这里有一个简单的例子。看这个 notebook 更多的例子。
# import necessary packagesimporthdidximportnumpyasnp# generating sample datandim=16# dimension of featuresndb=10000# number of dababase itemsnqry=10# number of queriesX_db=np.random.random((ndb,ndim))X_qry=np.random.random((nqry,ndim))# create Product Quantization Indexeridx=hdidx.indexer.IVFPQIndexer()# build indexeridx.build({'vals':X_db,'nsubq':8})# add database items to the indexeridx.add(X_db)# searching in the database, and return top-10 items for each queryids,dis=idx.search(X_qry,10)printidsprintdis
参考值
[1] Jegou, Herve, Matthijs Douze, and Cordelia Schmid. "Product quantization for nearest neighbor search." Pattern Analysis and Machine Intelligence, IEEE Transactions on 33.1 (2011): 117-128. [2] Weiss, Yair, Antonio Torralba, and Rob Fergus. "Spectral hashing." In Advances in neural information processing systems, pp. 1753-1760. 2009.