Faiss: Visualization of centroids, inverted lists and compact codes?

Created on 15 Jul 2020  路  4Comments  路  Source: facebookresearch/faiss

nlist = 100
k = 4
d = 2
quantizer = faiss.IndexFlatL2(d)  
index = faiss.IndexIVFFlat(quantizer, d, nlist)
index.train(xb)
index.add(xb)  

I want to print coordinates of centroids, inverted lists and compact codes of vectors for visualization. I can easily get if I run kmeans separately by kmeans.centroids. But how to get them in the above example?

question

Most helpful comment

A toy example might seem like this:

# suppose there is an ivf-index, which is already trained and built (add)
index = ...
index.train(...)
index.add(...)

inv = index.invlists
code_size = inv.code_size

# loop through invlists
for i in range(index.nlist):
  # get length of *this* invlist
  list_size = inv.list_size(i)

  # get the centroid of *this* invlist
  centroid = index.quantizer.reconstruct(i)

  # get ids of *this* invlist
  ids = faiss.rev_swig_ptr(inv.get_ids(i), list_size)

  # get codes of *this* invlist; the pointer is represented in `char*` and flattened
  codes = faiss.rev_swig_ptr(inv.get_codes(i), list_size * code_size).view('float32')
  codes.shape = list_size, index.d

  # for `Flat` index, one can assert that codes is equal to the original vectors
  np.all(codes == xb[ids])

All 4 comments

Get the pointer of the object (centroids, invlist, etc.) directly from the index and view it in a proper type. I'll paste an example afterward.

@QwertyJack sure! a (python) example will be good. Thanks

A toy example might seem like this:

# suppose there is an ivf-index, which is already trained and built (add)
index = ...
index.train(...)
index.add(...)

inv = index.invlists
code_size = inv.code_size

# loop through invlists
for i in range(index.nlist):
  # get length of *this* invlist
  list_size = inv.list_size(i)

  # get the centroid of *this* invlist
  centroid = index.quantizer.reconstruct(i)

  # get ids of *this* invlist
  ids = faiss.rev_swig_ptr(inv.get_ids(i), list_size)

  # get codes of *this* invlist; the pointer is represented in `char*` and flattened
  codes = faiss.rev_swig_ptr(inv.get_codes(i), list_size * code_size).view('float32')
  codes.shape = list_size, index.d

  # for `Flat` index, one can assert that codes is equal to the original vectors
  np.all(codes == xb[ids])

Was this page helpful?
0 / 5 - 0 ratings

Related issues

0DF0Arc picture 0DF0Arc  路  3Comments

brunodoamaral picture brunodoamaral  路  3Comments

Ljferrer picture Ljferrer  路  3Comments

zoe-cheung picture zoe-cheung  路  3Comments

zjjott picture zjjott  路  3Comments