Faiss: How to get the binary code in LSH?

Created on 27 Jun 2019  路  6Comments  路  Source: facebookresearch/faiss

I use the LSH to build the Index, and I want to save the binary code of gallery dataset. So how can i get the binary code.
I try to one way like this:

will_encode_vec = np.asarray([query_feature], "float32")
print will_encode_vec, np.shape(will_encode_vec), will_encode_vec[0]
xt = lshIndex.apply_preprocess(1, faiss.swig_ptr(will_encode_vec))
codes = np.zeros([1 * num_bits / 8], np.uint8)
faiss.fvecs2bitvecs(xt, faiss.swig_ptr(codes), num_dimension, 1)
print codes

So does the codes represent the binary code?
Looking for your reply!
Thanks

question

Most helpful comment

In the native API, the binary codes are in the std::vector<uint8_t> codes attribute. Although the Python API does not provide an idiomatic property, a standard C++ vector SWIG proxy can be converted to a NumPy array using vector_to_array:

import faiss

index = faiss.IndexLSH(8, 12)
index.add(np.random.normal(0, 1, (10, 8)).astype(np.float32))
faiss.vector_to_array(index.codes)
array([134,  11, 109,   6,  34,   0,  24,  13, 179,   9,  74,   8,  74,
         3,  60,  10, 249,   2, 207,  13], dtype=uint8)

All 6 comments

In the native API, the binary codes are in the std::vector<uint8_t> codes attribute. Although the Python API does not provide an idiomatic property, a standard C++ vector SWIG proxy can be converted to a NumPy array using vector_to_array:

import faiss

index = faiss.IndexLSH(8, 12)
index.add(np.random.normal(0, 1, (10, 8)).astype(np.float32))
faiss.vector_to_array(index.codes)
array([134,  11, 109,   6,  34,   0,  24,  13, 179,   9,  74,   8,  74,
         3,  60,  10, 249,   2, 207,  13], dtype=uint8)

@Enet4
Thank you for your reply.
By the way you provide, we can get the code of training features.
But how to get the binary code of the test feature as well as the query feature?

@UpCoder you get the codes of the added vectors (not the ones used for training).
To get the query vectors, just reset the index, add the query vectors and re-use vector_to_array.

@mdouze Thank you
is the method right? which I mentioned in the question. I write the code by the C++ search resource code.

no activity, closing.

@UpCoder you get the codes of the added vectors (not the ones used for training).
To get the query vectors, just reset the index, add the query vectors and re-use vector_to_array.

@mdouze Can you please provide a working example in python ?

Was this page helpful?
0 / 5 - 0 ratings