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!
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.
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:
or