Faiss: How to get the index type?

Created on 29 Sep 2018  Â·  5Comments  Â·  Source: facebookresearch/faiss

Hi, I use cpu to train and build the index like "OPQ32_128,IMI2x10,PQ32". And for searching ,I read the index like this:
cpu_index = dynamic_cast<faiss::IndexIVFPQ*>(faiss::read_index(faissindex_file));
The cpu_index is null.
But write this:
`cpu_index = faiss::read_index(faissindex_file));‘
The cpu_index is not null.
Because I want to set the nprobe parameter, but it isn't in the faiss::Index. How to solve this problem?
Thanks!

question

Most helpful comment

Python lets you set non-existent fields in objects (it will just add the field), so setting nprobe on an IndexPreTransform does not raise an error.
The equivalent Python code of the above is:

cpu_index = faiss.read_index(faissindex_file)
index_ivf = faiss.downcast_index(cpu_index.index)
index_ivf.nprobe = 123

or

cpu_index = faiss.read_index(faissindex_file);
ParameterSpace().set_index_parameters(cpu_index, "nprobe", 123)

All 5 comments

Due to the OPQ32_128 prefix, the index is not an IndexIVFPQ but an IndexPreTransform.
To set the nprobe there are two possibilities:

auto cpu_index = dynamic_cast<faiss::IndexPreTransform*>(faiss::read_index(faissindex_file));
auto index_ivf = dynamic_cast<faiss::IndexIVF*>(cpu_index->index);
index_ivf->nprobe = 123;

or

auto cpu_index = faiss::read_index(faissindex_file);
ParameterSpace().set_index_parameters(cpu_index, "nprobe", 123);

I think I hit this issue in Python, if I set nprobe to an index with pre-transform, it seemed it had no effect on results. What's the correct way to set nprobe in Python?

Python lets you set non-existent fields in objects (it will just add the field), so setting nprobe on an IndexPreTransform does not raise an error.
The equivalent Python code of the above is:

cpu_index = faiss.read_index(faissindex_file)
index_ivf = faiss.downcast_index(cpu_index.index)
index_ivf.nprobe = 123

or

cpu_index = faiss.read_index(faissindex_file);
ParameterSpace().set_index_parameters(cpu_index, "nprobe", 123)

auto cpu_index = faiss::read_index(faissindex_file); ParameterSpace().set_index_parameters(cpu_index, "nprobe", 123);

The correct code is :
auto cpu_index = faiss::read_index(faissindex_file); ParameterSpace().set_index_parameter(cpu_index, "nprobe", 123);

The right method is set_index_parameter.

This is now a FAQ entry
https://github.com/facebookresearch/faiss/wiki/FAQ#how-can-i-set-nprobe-on-an-opaque-index
Closing.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danny1984 picture danny1984  Â·  3Comments

Ljferrer picture Ljferrer  Â·  3Comments

wwmmqq picture wwmmqq  Â·  3Comments

xxllp picture xxllp  Â·  3Comments

maozezhong picture maozezhong  Â·  3Comments