Skip to main content

Product quantization for nearest neighbor search in a single python file

Project description

Copyright (c) 2018 Yusuke Matsui

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Description: # nanopq

[![Documentation Status](https://readthedocs.org/projects/nanopq/badge/?version=latest)](https://nanopq.readthedocs.io/en/latest/?badge=latest)

Nano Product Quantization (nanopq): product quantization for nearest neighbor search in a single python file.

This package contains a vanilla implementation of Product Quantization (PQ) and Optimized Product Quantization (OPQ) written in pure python without any third party dependencies.


## Installing
You can install the package via pip. This library works with Python 3.6+ on linux.
```
pip install nanopq
```

## [Documentation](https://nanopq.readthedocs.io/en/latest/index.html)
- [Tutorial](https://nanopq.readthedocs.io/en/latest/tutorial.html)
- [API](https://nanopq.readthedocs.io/en/latest/api.html)

## Example

```python
import nanopq
import numpy as np

X = np.random.random((10000, 128)).astype(np.float32)
query = np.random.random((128,)).astype(np.float32)

# Instantiate with M=8 sub-spaces
pq = nanopq.PQ(M=8)

# Train with the top 1000 vectors
pq.fit(X[:1000])

# Encode to pq-codes
X_code = pq.encode(X) # (10000, 8) with dtype=np.uint8

# Results
dtable = pq.dtable(query) # Compute a distance table online
dists = pq.adist(dtable, X_code) # Asymmetric distance
```

## Author
- [Yusuke Matsui](http://yusukematsui.me)


## Reference
- [H. Jegou, M. Douze, and C. Schmid, "Product Quantization for Nearest Neighbor Search", IEEE TPAMI 2011](https://ieeexplore.ieee.org/document/5432202/) (the original paper of PQ)
- [T. Ge, K. He, Q. Ke, and J. Sun, "Optimized Product Quantization", IEEE TPAMI 2014](https://ieeexplore.ieee.org/document/6678503/) (the original paper of OPQ)
- [Y. Matsui, Y. Uchida, H. Jegou, and S. Satoh, "A Survey of Product Quantization", ITE MTA 2018](https://www.jstage.jst.go.jp/article/mta/6/1/6_2/_pdf/) (a survey paper of PQ)
- [PQ in faiss](https://github.com/facebookresearch/faiss/wiki/Faiss-building-blocks:-clustering,-PCA,-quantization#pq-encoding--decoding) (Faiss contains an optimized implementation of PQ. [See the difference to ours here](https://nanopq.readthedocs.io/en/latest/tutorial.html#difference-from-pq-in-faiss))
- [Rayuela.jl](https://github.com/una-dinosauria/Rayuela.jl) (Julia implementation of several encoding algorithms including PQ and OPQ)
- [PQk-means](https://github.com/DwangoMediaVillage/pqkmeans) (clustering on PQ-codes. The implementation of nanopq is compatible to [that of PQk-means](https://github.com/DwangoMediaVillage/pqkmeans/blob/master/tutorial/1_pqkmeans.ipynb))
Platform: UNKNOWN
Description-Content-Type: text/markdown

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nanopq-0.1.1.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nanopq-0.1.1-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file nanopq-0.1.1.tar.gz.

File metadata

  • Download URL: nanopq-0.1.1.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for nanopq-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0c061b5b80ce255030b439b0b25d3445ebede24aaff175e47d77ec29646848ac
MD5 0640893b5e762c3fee3d3ddec073e79a
BLAKE2b-256 f4e9a16c8cbc9578d0471d4f376b1141764e077c4cdc88905db9b0b2a840b463

See more details on using hashes here.

File details

Details for the file nanopq-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for nanopq-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 30810864cb2730462503463fed20a71cab9088d8d004beb182a807e66a10a730
MD5 b9200e70f85e147df9215ade934def7b
BLAKE2b-256 0c3064c722f50e1b2b53905f36d24abfc172e89387a58fb814adb760cc5d48e4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page