【Faiss】indexes 前(后)处理(五)
Pre and post processing
在某些情形下,需要对Index做前处理或后处理。
ID映射
默认情况下,faiss会为每个输入的向量记录一个次序id,在使用中也可以为向量指定任意我们需要的id。
部分index类型有add_with_ids方法,可以为每个向量对应一个64-bit的id,搜索的时候返回这个指定的id。
#导入faiss
import sys
sys.path.append('/home/maliqi/faiss/python/')
import faiss
import numpy as np #获取数据和Id
d = 512
n_data = 2000
data = np.random.rand(n_data, d).astype('float32')
ids = np.arange(100000, 102000) #id设定为6位数整数
nlist = 10
quantizer = faiss.IndexFlatIP(d)
index = faiss.IndexIVFFlat(quantizer, d, nlist, faiss.METRIC_L2)
index.train(data)
index.add_with_ids(data, ids)
d, i = index.search(data[:5], 5)
print(i) #返回的id应该是我们自己设定的
[[100000 100383 101007 101444 100729][100001 100880 101693 100004 100964][100002 101113 101998 101017 101768][100003 100694 101701 101608 100831][100004 100111 100564 100541 100513]]
但是对有些Index类型,并不支持add_with_ids,因此需要与其他Index类型结合,将默认的id映射到指定id,用IndexIDMap类实现。
指定的ids不能是字符串,只能是整数。
index = faiss.IndexFlatL2(data.shape[1])
index.add_with_ids(data, ids) #报错
--------------------------------------------------------------------------- RuntimeError Traceback (most recent call last) <ipython-input-5-4de928a09ab9> in <module>()1 index = faiss.IndexFlatL2(data.shape[1]) ----> 2 index.add_with_ids(data, ids)/home/maliqi/faiss/python/faiss/__init__.py in replacement_add_with_ids(self, x, ids)104 assert d == self.d105 assert ids.shape == (n, ), 'not same nb of vectors as ids' --> 106 self.add_with_ids_c(n, swig_ptr(x), swig_ptr(ids))107 108 def replacement_assign(self, x, k):/home/maliqi/faiss/python/faiss/swigfaiss.py in add_with_ids(self, n, x, xids)1316 1317 def add_with_ids(self, n, x, xids): -> 1318 return _swigfaiss.Index_add_with_ids(self, n, x, xids)1319 1320 def search(self, n, x, k, distances, labels):RuntimeError: Error in virtual void faiss::Index::add_with_ids(faiss::Index::idx_t, const float*, const long int*) at Index.cpp:46: add_with_ids not implemented for this type of index
index2 = faiss.IndexIDMap(index)
index2.add_with_ids(data, ids) #将index的id映射到index2的id,会维持一个映射表
数据转换
有些时候需要在索引之前转换数据。转换类继承了VectorTransform类,将输入向量转换为输出向量。
1)随机旋转,类名RandomRotationMatri,用以均衡向量中的元素,一般在IndexPQ和IndexLSH之前;
2)PCA,类名PCAMatrix,降维;
3)改变维度,类名RemapDimensionsTransform,可以升高或降低向量维数
举例:PCA降维(通过IndexPreTransform)
输入向量是2048维,需要减少到16byte。
data = np.random.rand(n_data, 2048).astype('float32')
# the IndexIVFPQ will be in 256D not 2048
coarse_quantizer = faiss.IndexFlatL2 (256)
sub_index = faiss.IndexIVFPQ (coarse_quantizer, 256, 16, 16, 8)
# PCA 2048->256
# 降维后随机旋转 (第四个参数)
pca_matrix = faiss.PCAMatrix (2048, 256, 0, True) #- the wrapping index
index = faiss.IndexPreTransform (pca_matrix, sub_index)# will also train the PCA
index.train(data) #数据需要是2048维
# PCA will be applied prior to addition
index.add(data)
举例:升维
有时候需要在向量中插入0升高维度,一般是我们需要 1)d是4的整数倍,有利于举例计算; 2)d是M的整数倍。
d = 512
M = 8 #M是在维度方向上分割的子空间个数
d2 = int((d + M - 1) / M) * M
remapper = faiss.RemapDimensionsTransform (d, d2, True)
index_pq = faiss.IndexPQ(d2, M, 8)
index = faiss.IndexPreTransform (remapper, index_pq) #后续可以添加数据/索引
对搜索结果重新排序
当查询向量时,可以用真实距离值对结果进行重新排序。
在下面的例子中,搜索阶段会首先选取4*10个结果,然后对这些结果计算真实距离值,再从中选取10个结果返回。IndexRefineFlat保存了全部的向量信息,内存开销不容小觑。
data = np.random.rand(n_data, d).astype('float32')
nbits_per_index = 4
q = faiss.IndexPQ (d, M, nbits_per_index)
rq = faiss.IndexRefineFlat (q)
rq.train (data)
rq.add (data)
rq.k_factor = 4
dis, ind = rq.search (data[:5], 10)
print(ind)
[[ 0 434 1647 1501 867 658 822 1164 490 1430][ 1 1035 369 392 866 1645 1961 1469 1946 175][ 2 466 1183 403 427 505 394 759 633 746][ 3 1668 1798 1293 965 1484 755 315 1633 1453][ 4 309 715 1204 996 239 1381 48 707 1311]]
综合多个index返回的结果
当数据集分布在多个index中,需要在每个index中都执行搜索,然后使用IndexShards综合得到结果。同样也适用于index分布在不同的GPU的情况。
【Faiss】indexes 前(后)处理(五)相关推荐
- 售前工程师职业生涯五个段位,从写解决方案到营销大师的蜕变
售前工程师职业生涯五个段位,从写解决方案到营销大师的蜕变 写在前面:别忽悠客户,断送自己的职业生涯 一.入门级级售前:销售助手 二.合格售前段位:销售伙伴 三.高级售前段位:用户伙伴 四.蜕变售前段位 ...
- 安装Windows XP后的五个设置步骤
安装Windows XP后的五个设置步骤 安装Windows XP后的五个设置步骤 当你兴冲冲地买来XP的光盘,经过漫长的文件拷贝.Windows识别硬件和初始化系统后,终于进入Windows XP那 ...
- 小米11 Pro概念图曝光:曲面挖孔屏+后置五摄相机模组
今年以来,小米陆续推出了四款小米10系列机型,包括小米10.小米10 Pro.小米10青春版和小米10至尊纪念版,并且还在海外推出了小米10T系列,在各个价位和多个市场全面开花并均获得了极好的成绩,可 ...
- MySQL 查询最好的前/后3条
MySQL 查询前/后3条 1 查询SQL 2 学生表 3 成绩表 1 查询SQL select a.`subject`,a.score,s.`name` from score a left join ...
- dronekit 控制飞控 上/下/前/后/左/右 或是 上/下/东/西/南/北
1.ardupilot平台 对于ardupilot平台 ,dronekit的python代码可以这样编写实现 上/下/前/后/左/右 from pymavlink import mavutil fro ...
- 华为p40配置鸿蒙系统,华为P40配置曝光 将搭配鸿蒙系统+后置五摄
花火网讯 华为P40做为下部华为旗舰手机,将要在不久之后首发上市,而最近就有关于华为P40的各种配置信息泄露. 根据国外用户在社交网络上爆料,华为P40系列手机将会有鸿蒙系统和安卓系统两个版本.这也符 ...
- 华为鸿蒙拍照,华为P40重新定义拍照手机,屏下摄像后置五摄,可搭载国产鸿蒙OS...
华为的旗舰手机分Mate和P两大系列,Mate系列属于价格比较贵的高端商务手机,P系列则价格更亲民一些,属于时尚拍照旗舰手机的范畴.按照惯例华为P系列一般会在上半年3月份左右发布,而Mate系列会在下 ...
- 华为鸿蒙OS5摄概念机,华为Mate50Pro概念机:后置五摄,预装鸿蒙OS
原标题:华为Mate50Pro概念机:后置五摄,预装鸿蒙OS 华为Mate40系列手机最近很抢手,接下来大家就是期待主打时尚和拍照的华为P50系列了,不过对于喜欢商务气息更加浓重一些的用户来说,华为M ...
- Java 时间,时间戳 往 前/后 加/减 一个小时
Java 时间,时间戳 往前/后叠加一个小时 public Map<Long, Map<String, Object>> statusDeviceStatusByDate(St ...
最新文章
- 搞懂限流算法这一篇就够了
- 高效的找出两个List中的不同元素
- html自动兼容像素密度,解决 HTML Canvas 元素在高像素密度/高分辨率屏幕上显示模糊的问题...
- Dev c++工具将C代码生成dll文件以及如何调用dll文件
- micropython oled中文_micropython esp8266+ssd1306(OLED) 显示中文(示例)
- ios开发 热搜词demo_一场比赛16个热搜,uzi因焕烽躺枪,阿bin评价赛后太揪心
- word activex部件不能创建对象_如何用Word批量制作员工工作证?1分钟搞定1000份!只需三步...
- 滴滴等8家网约车平台将增设“一键叫车”功能 便利老年人打车
- MongoTemplate.findById查不到数据的一种情况:由写入时未指定_id造成的
- SQL注入如何进行攻击
- mysql sum函数后面好多位小数点
- PLSQL下载及安装
- 根据身份证获取性别、生日、后六位
- python中复选框取消修改_Python tkinter复选框双重检查/取消选中(Python tkinter Checkboxes double check/uncheck)...
- 洛谷P4218 [CTSC2010]珠宝商(后缀自动机+点分治)
- 2021年西式面点师(初级)新版试题及西式面点师(初级)考试试卷
- 使用会议管理系统的高效办会思路
- SEVERE: [FATAL] [INS-32038] The operating system group specified for central inventory
- 让员工都是决策者!受到丰田集团启发:让企业少花500万的诀窍
- HBase 事务性 (Transaction)