-
Notifications
You must be signed in to change notification settings - Fork 36
/
knn.py
80 lines (69 loc) · 2.52 KB
/
knn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
# -*- coding: utf-8 -*-
" knn module, all credits to faiss! "
import os
import numpy as np
import time
import faiss
from tqdm import tqdm
class BaseKNN(object):
"""KNN base class"""
def __init__(self, database, method):
if database.dtype != np.float32:
database = database.astype(np.float32)
self.N = len(database)
self.D = database[0].shape[-1]
self.database = database if database.flags['C_CONTIGUOUS'] \
else np.ascontiguousarray(database)
def add(self, batch_size=10000):
"""Add data into index"""
if self.N <= batch_size:
self.index.add(self.database)
else:
[self.index.add(self.database[i:i+batch_size])
for i in tqdm(range(0, len(self.database), batch_size),
desc='[index] add')]
def search(self, queries, k):
"""Search
Args:
queries: query vectors
k: get top-k results
Returns:
sims: similarities of k-NN
ids: indexes of k-NN
"""
if not queries.flags['C_CONTIGUOUS']:
queries = np.ascontiguousarray(queries)
if queries.dtype != np.float32:
queries = queries.astype(np.float32)
sims, ids = self.index.search(queries, k)
return sims, ids
class KNN(BaseKNN):
"""KNN class
Args:
database: feature vectors in database
method: distance metric
"""
def __init__(self, database, method):
super().__init__(database, method)
self.index = {'cosine': faiss.IndexFlatIP,
'euclidean': faiss.IndexFlatL2}[method](self.D)
if os.environ.get('CUDA_VISIBLE_DEVICES'):
self.index = faiss.index_cpu_to_all_gpus(self.index)
self.add()
class ANN(BaseKNN):
"""Approximate nearest neighbor search class
Args:
database: feature vectors in database
method: distance metric
"""
def __init__(self, database, method, M=128, nbits=8, nlist=316, nprobe=64):
super().__init__(database, method)
self.quantizer = {'cosine': faiss.IndexFlatIP,
'euclidean': faiss.IndexFlatL2}[method](self.D)
self.index = faiss.IndexIVFPQ(self.quantizer, self.D, nlist, M, nbits)
samples = database[np.random.permutation(np.arange(self.N))[:self.N // 5]]
print("[ANN] train")
self.index.train(samples)
self.add()
self.index.nprobe = nprobe