Faiss建立索引并保存(C++)

 Faiss 建立索引并保存。如果用IndexHNSWFlat,就采用IndexIDMap进行映射。

这里的距离不是ip,是l2

#include "index_io.h"
//#include "IndexIVF.h"
//#include "IndexIVFFlat.h"
#include "IndexHNSW.h"
//#include "IndexIVFPQ.h"
#include "MetaIndexes.h"

const char* output_file;
uint64_t fea_dim;
uint64_t img_num;
float *img_fea = new float[fea_dim*img_num]; 

// value the img_fea
// ...

// Method FlatIP
//faiss::IndexFlatIP index(fea_dim);
//index.add(img_num, img_fea);
// Method HNSW
faiss::IndexHNSWFlat index_hsnw(fea_dim, 32);
faiss::IndexIDMap index(&index_hsnw);
long *ids = new long [img_num];
for (auto n=0; n<(long)img_num; n++) {
    ids[n] = (long)n;
}
index.add_with_ids(img_num, img_fea, ids);

FILE *fr;
fr=fopen(output_file,"wb");
faiss::write_index(&index, fr);

delete []img_fea;

 

其中add_with_ids的定义如下,因此可以调用如上。

https://github.com/facebookresearch/faiss/blob/master/MetaIndexes.cpp

template <typename IndexT>
void IndexIDMapTemplate<IndexT>::add_with_ids
    (idx_t n, const typename IndexT::component_t * x,
     const typename IndexT::idx_t *xids)
{
    index->add (n, x);
    for (idx_t i = 0; i < n; i++)
        id_map.push_back (xids[i]);
    this->ntotal = index->ntotal;
}

 期间报错如下

note: virtual void faiss::IndexIDMap::add_with_ids(faiss::Index::idx_t, const float*, const long int*)

需要 long,改了就ok了。

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值