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了。