faissknn 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- faissknn-0.0.1/LICENSE +21 -0
- faissknn-0.0.1/PKG-INFO +89 -0
- faissknn-0.0.1/README.md +68 -0
- faissknn-0.0.1/faissknn/__init__.py +5 -0
- faissknn-0.0.1/faissknn/knn.py +175 -0
- faissknn-0.0.1/faissknn.egg-info/PKG-INFO +89 -0
- faissknn-0.0.1/faissknn.egg-info/SOURCES.txt +12 -0
- faissknn-0.0.1/faissknn.egg-info/dependency_links.txt +1 -0
- faissknn-0.0.1/faissknn.egg-info/requires.txt +10 -0
- faissknn-0.0.1/faissknn.egg-info/top_level.txt +1 -0
- faissknn-0.0.1/pyproject.toml +59 -0
- faissknn-0.0.1/setup.cfg +60 -0
- faissknn-0.0.1/tests/test_knn.py +25 -0
faissknn-0.0.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 Isaac Corley
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
faissknn-0.0.1/PKG-INFO
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: faissknn
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Faiss implementation of multiclass and multilabel K-Nearest Neighbors Classifiers
|
|
5
|
+
Home-page: https://github.com/isaaccorley/faissknn
|
|
6
|
+
Author: Isaac Corley
|
|
7
|
+
Author-email: isaac.corley@my.utsa.edu
|
|
8
|
+
Keywords: pytorch,machine learning,deep learning
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
17
|
+
Requires-Python: <4,>=3.9
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
Provides-Extra: dev
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
|
|
22
|
+
# FAISSKNN
|
|
23
|
+
`faissknn` contains implementations for both multiclass and multilabel K-Nearest Neighbors Classifier implementations. The classifiers follow the `scikit-learn`: `fit`, `predict`, and `predict_proba` methods.
|
|
24
|
+
|
|
25
|
+
### Install
|
|
26
|
+
|
|
27
|
+
The FAISS authors recommend to install `faiss` through conda e.g. `conda install -c pytorch faiss-gpu`. See [FAISS install page](https://github.com/facebookresearch/faiss/blob/main/INSTALL.md) for more info.
|
|
28
|
+
|
|
29
|
+
Once `faiss` is installed, `faissknn` can be install through pypi:
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
pip install faissknn
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Usage
|
|
36
|
+
|
|
37
|
+
Multiclass:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from sklearn.datasets import make_classification
|
|
41
|
+
from sklearn.model_selection import train_test_split
|
|
42
|
+
|
|
43
|
+
from faissknn import FaissKNNClassifier
|
|
44
|
+
|
|
45
|
+
x, y = make_classification()
|
|
46
|
+
x_train, x_test, y_train, y_test = train_test_split(x, y)
|
|
47
|
+
model = FaissKNNClassifier(
|
|
48
|
+
n_neighbors=5,
|
|
49
|
+
n_classes=None,
|
|
50
|
+
device="cpu"
|
|
51
|
+
)
|
|
52
|
+
model.fit(x_train, y_train)
|
|
53
|
+
|
|
54
|
+
y_pred = model.predict(x_test) # (N,)
|
|
55
|
+
y_proba = model.predict_proba(x_test) # (N, C)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Multilabel:
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
from sklearn.datasets import make_classification
|
|
62
|
+
from sklearn.model_selection import train_test_split
|
|
63
|
+
|
|
64
|
+
from faissknn import FaissKNNMultilabelClassifier
|
|
65
|
+
|
|
66
|
+
x, y = make_multilabel_classification()
|
|
67
|
+
x_train, x_test, y_train, y_test = train_test_split(x, y)
|
|
68
|
+
model = FaissKNNClassifier(
|
|
69
|
+
n_neighbors=5,
|
|
70
|
+
device="cpu"
|
|
71
|
+
)
|
|
72
|
+
model.fit(x_train, y_train)
|
|
73
|
+
|
|
74
|
+
y_pred = model.predict(x_test) # (N, C)
|
|
75
|
+
y_proba = model.predict_proba(x_test) # (N, C)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
GPU/CUDA: `faissknn` also supports running on the GPU to speed up computation. Simply change the device to `cuda` or a specific cuda device `cuda:0`
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
model = FaissKNNClassifier(
|
|
82
|
+
n_neighbors=5,
|
|
83
|
+
device="cuda"
|
|
84
|
+
)
|
|
85
|
+
model = FaissKNNClassifier(
|
|
86
|
+
n_neighbors=5,
|
|
87
|
+
device="cuda:0"
|
|
88
|
+
)
|
|
89
|
+
```
|
faissknn-0.0.1/README.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# FAISSKNN
|
|
2
|
+
`faissknn` contains implementations for both multiclass and multilabel K-Nearest Neighbors Classifier implementations. The classifiers follow the `scikit-learn`: `fit`, `predict`, and `predict_proba` methods.
|
|
3
|
+
|
|
4
|
+
### Install
|
|
5
|
+
|
|
6
|
+
The FAISS authors recommend to install `faiss` through conda e.g. `conda install -c pytorch faiss-gpu`. See [FAISS install page](https://github.com/facebookresearch/faiss/blob/main/INSTALL.md) for more info.
|
|
7
|
+
|
|
8
|
+
Once `faiss` is installed, `faissknn` can be install through pypi:
|
|
9
|
+
|
|
10
|
+
```
|
|
11
|
+
pip install faissknn
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
### Usage
|
|
15
|
+
|
|
16
|
+
Multiclass:
|
|
17
|
+
|
|
18
|
+
```python
|
|
19
|
+
from sklearn.datasets import make_classification
|
|
20
|
+
from sklearn.model_selection import train_test_split
|
|
21
|
+
|
|
22
|
+
from faissknn import FaissKNNClassifier
|
|
23
|
+
|
|
24
|
+
x, y = make_classification()
|
|
25
|
+
x_train, x_test, y_train, y_test = train_test_split(x, y)
|
|
26
|
+
model = FaissKNNClassifier(
|
|
27
|
+
n_neighbors=5,
|
|
28
|
+
n_classes=None,
|
|
29
|
+
device="cpu"
|
|
30
|
+
)
|
|
31
|
+
model.fit(x_train, y_train)
|
|
32
|
+
|
|
33
|
+
y_pred = model.predict(x_test) # (N,)
|
|
34
|
+
y_proba = model.predict_proba(x_test) # (N, C)
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Multilabel:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from sklearn.datasets import make_classification
|
|
41
|
+
from sklearn.model_selection import train_test_split
|
|
42
|
+
|
|
43
|
+
from faissknn import FaissKNNMultilabelClassifier
|
|
44
|
+
|
|
45
|
+
x, y = make_multilabel_classification()
|
|
46
|
+
x_train, x_test, y_train, y_test = train_test_split(x, y)
|
|
47
|
+
model = FaissKNNClassifier(
|
|
48
|
+
n_neighbors=5,
|
|
49
|
+
device="cpu"
|
|
50
|
+
)
|
|
51
|
+
model.fit(x_train, y_train)
|
|
52
|
+
|
|
53
|
+
y_pred = model.predict(x_test) # (N, C)
|
|
54
|
+
y_proba = model.predict_proba(x_test) # (N, C)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
GPU/CUDA: `faissknn` also supports running on the GPU to speed up computation. Simply change the device to `cuda` or a specific cuda device `cuda:0`
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
model = FaissKNNClassifier(
|
|
61
|
+
n_neighbors=5,
|
|
62
|
+
device="cuda"
|
|
63
|
+
)
|
|
64
|
+
model = FaissKNNClassifier(
|
|
65
|
+
n_neighbors=5,
|
|
66
|
+
device="cuda:0"
|
|
67
|
+
)
|
|
68
|
+
```
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"""Modified from https://github.com/scikit-learn-contrib/DESlib/blob/master/deslib/util/faiss_knn_wrapper.py#L19-L203.""" # noqa: E501
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
import faiss
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class FaissKNNClassifier:
|
|
9
|
+
"""A multiclass exact KNN classifier implemented using the FAISS library."""
|
|
10
|
+
|
|
11
|
+
def __init__(
|
|
12
|
+
self, n_neighbors: int, n_classes: Optional[int] = None, device: str = "cpu"
|
|
13
|
+
) -> None:
|
|
14
|
+
"""Instantiate a faiss KNN Classifier.
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
n_neighbors: number of KNN neighbors
|
|
18
|
+
n_classes: (optional) number of dataset classes
|
|
19
|
+
(otherwise derive from the data)
|
|
20
|
+
device: a torch device, e.g. cpu, cuda, cuda:0, etc.
|
|
21
|
+
"""
|
|
22
|
+
self.n_neighbors = n_neighbors
|
|
23
|
+
self.n_classes = n_classes
|
|
24
|
+
|
|
25
|
+
if device == "cpu":
|
|
26
|
+
self.cuda = False
|
|
27
|
+
self.device = None
|
|
28
|
+
else:
|
|
29
|
+
self.cuda = True
|
|
30
|
+
if ":" in device:
|
|
31
|
+
self.device = int(device.split(":")[-1])
|
|
32
|
+
else:
|
|
33
|
+
self.device = 0
|
|
34
|
+
|
|
35
|
+
def create_index(self, d: int) -> None:
|
|
36
|
+
"""Create the faiss index.
|
|
37
|
+
|
|
38
|
+
Args:
|
|
39
|
+
d: feature dimension
|
|
40
|
+
"""
|
|
41
|
+
if self.cuda:
|
|
42
|
+
self.res = faiss.StandardGpuResources()
|
|
43
|
+
self.config = faiss.GpuIndexFlatConfig()
|
|
44
|
+
self.config.device = self.device
|
|
45
|
+
self.index = faiss.GpuIndexFlatL2(self.res, d, self.config)
|
|
46
|
+
else:
|
|
47
|
+
self.index = faiss.IndexFlatL2(d)
|
|
48
|
+
|
|
49
|
+
def fit(self, X: np.ndarray, y: np.ndarray) -> object:
|
|
50
|
+
"""Store train X and y.
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
X: input features (N, d)
|
|
54
|
+
y: input labels (N, ...)
|
|
55
|
+
|
|
56
|
+
Returns:
|
|
57
|
+
self
|
|
58
|
+
"""
|
|
59
|
+
X = np.atleast_2d(X).astype(np.float32)
|
|
60
|
+
X = np.ascontiguousarray(X)
|
|
61
|
+
self.create_index(X.shape[-1])
|
|
62
|
+
self.index.add(X)
|
|
63
|
+
self.y = y.astype(int)
|
|
64
|
+
if self.n_classes is None:
|
|
65
|
+
self.n_classes = len(np.unique(y))
|
|
66
|
+
return self
|
|
67
|
+
|
|
68
|
+
def __del__(self) -> None:
|
|
69
|
+
"""Cleanup helpers."""
|
|
70
|
+
if hasattr(self, "index"):
|
|
71
|
+
self.index.reset()
|
|
72
|
+
del self.index
|
|
73
|
+
if hasattr(self, "res"):
|
|
74
|
+
self.res.noTempMemory()
|
|
75
|
+
del self.res
|
|
76
|
+
|
|
77
|
+
def predict(self, X: np.ndarray) -> np.ndarray:
|
|
78
|
+
"""Predict int labels given X.
|
|
79
|
+
|
|
80
|
+
Args:
|
|
81
|
+
X: input features (N, d)
|
|
82
|
+
|
|
83
|
+
Returns:
|
|
84
|
+
preds: int predicted labels (N,)
|
|
85
|
+
"""
|
|
86
|
+
X = np.atleast_2d(X).astype(np.float32)
|
|
87
|
+
_, idx = self.index.search(X, self.n_neighbors)
|
|
88
|
+
class_idx = self.y[idx]
|
|
89
|
+
counts = np.apply_along_axis(
|
|
90
|
+
lambda x: np.bincount(x, minlength=self.n_classes),
|
|
91
|
+
axis=1,
|
|
92
|
+
arr=class_idx.astype(np.int16),
|
|
93
|
+
)
|
|
94
|
+
preds = np.argmax(counts, axis=1)
|
|
95
|
+
return preds
|
|
96
|
+
|
|
97
|
+
def predict_proba(self, X: np.ndarray) -> np.ndarray:
|
|
98
|
+
"""Predict float probabilities for labels given X.
|
|
99
|
+
|
|
100
|
+
Args:
|
|
101
|
+
X: input features (N, d)
|
|
102
|
+
|
|
103
|
+
Returns:
|
|
104
|
+
preds_proba: float probas per labels (N, c)
|
|
105
|
+
"""
|
|
106
|
+
X = np.atleast_2d(X).astype(np.float32)
|
|
107
|
+
_, idx = self.index.search(X, self.n_neighbors)
|
|
108
|
+
class_idx = self.y[idx]
|
|
109
|
+
counts = np.apply_along_axis(
|
|
110
|
+
lambda x: np.bincount(x, minlength=self.n_classes),
|
|
111
|
+
axis=1,
|
|
112
|
+
arr=class_idx.astype(np.int16),
|
|
113
|
+
)
|
|
114
|
+
|
|
115
|
+
preds_proba = counts / self.n_neighbors
|
|
116
|
+
return preds_proba
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class FaissKNNMultilabelClassifier(FaissKNNClassifier):
|
|
120
|
+
"""A multilabel exact KNN classifier implemented using the FAISS library."""
|
|
121
|
+
|
|
122
|
+
def predict(self, X: np.ndarray) -> np.ndarray:
|
|
123
|
+
"""Predict one-hot int labels given X.
|
|
124
|
+
|
|
125
|
+
Args:
|
|
126
|
+
X: input features (N, d)
|
|
127
|
+
|
|
128
|
+
Returns:
|
|
129
|
+
preds: int predicted labels (N, c)
|
|
130
|
+
"""
|
|
131
|
+
X = np.atleast_2d(X).astype(np.float32)
|
|
132
|
+
_, idx = self.index.search(X, self.n_neighbors)
|
|
133
|
+
class_idx = self.y[idx]
|
|
134
|
+
|
|
135
|
+
preds = []
|
|
136
|
+
for i in range(class_idx.shape[-1]):
|
|
137
|
+
class_idx_i = class_idx[..., i]
|
|
138
|
+
counts_i = np.apply_along_axis(
|
|
139
|
+
lambda x: np.bincount(x, minlength=2),
|
|
140
|
+
axis=1,
|
|
141
|
+
arr=class_idx_i.astype(np.int16),
|
|
142
|
+
)
|
|
143
|
+
preds_i = np.argmax(counts_i, axis=1)
|
|
144
|
+
preds.append(preds_i)
|
|
145
|
+
|
|
146
|
+
preds = np.stack(preds, axis=1)
|
|
147
|
+
return preds
|
|
148
|
+
|
|
149
|
+
def predict_proba(self, X):
|
|
150
|
+
"""Predict float probabilities for labels given X.
|
|
151
|
+
|
|
152
|
+
Args:
|
|
153
|
+
X: input features (N, d)
|
|
154
|
+
|
|
155
|
+
Returns:
|
|
156
|
+
preds_proba: float probas per labels (N, c)
|
|
157
|
+
"""
|
|
158
|
+
X = np.atleast_2d(X).astype(np.float32)
|
|
159
|
+
_, idx = self.index.search(X, self.n_neighbors)
|
|
160
|
+
class_idx = self.y[idx]
|
|
161
|
+
|
|
162
|
+
preds_proba = []
|
|
163
|
+
for i in range(class_idx.shape[-1]):
|
|
164
|
+
class_idx_i = class_idx[..., i]
|
|
165
|
+
counts_i = np.apply_along_axis(
|
|
166
|
+
lambda x: np.bincount(x, minlength=2),
|
|
167
|
+
axis=1,
|
|
168
|
+
arr=class_idx_i.astype(np.int16),
|
|
169
|
+
)
|
|
170
|
+
preds_proba_i = counts_i / self.n_neighbors
|
|
171
|
+
preds_proba_i = preds_proba_i[:, 1]
|
|
172
|
+
preds_proba.append(preds_proba_i)
|
|
173
|
+
|
|
174
|
+
preds_proba = np.stack(preds_proba, axis=1)
|
|
175
|
+
return preds_proba
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: faissknn
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Faiss implementation of multiclass and multilabel K-Nearest Neighbors Classifiers
|
|
5
|
+
Home-page: https://github.com/isaaccorley/faissknn
|
|
6
|
+
Author: Isaac Corley
|
|
7
|
+
Author-email: isaac.corley@my.utsa.edu
|
|
8
|
+
Keywords: pytorch,machine learning,deep learning
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Science/Research
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
17
|
+
Requires-Python: <4,>=3.9
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
Provides-Extra: dev
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
|
|
22
|
+
# FAISSKNN
|
|
23
|
+
`faissknn` contains implementations for both multiclass and multilabel K-Nearest Neighbors Classifier implementations. The classifiers follow the `scikit-learn`: `fit`, `predict`, and `predict_proba` methods.
|
|
24
|
+
|
|
25
|
+
### Install
|
|
26
|
+
|
|
27
|
+
The FAISS authors recommend to install `faiss` through conda e.g. `conda install -c pytorch faiss-gpu`. See [FAISS install page](https://github.com/facebookresearch/faiss/blob/main/INSTALL.md) for more info.
|
|
28
|
+
|
|
29
|
+
Once `faiss` is installed, `faissknn` can be install through pypi:
|
|
30
|
+
|
|
31
|
+
```
|
|
32
|
+
pip install faissknn
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Usage
|
|
36
|
+
|
|
37
|
+
Multiclass:
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from sklearn.datasets import make_classification
|
|
41
|
+
from sklearn.model_selection import train_test_split
|
|
42
|
+
|
|
43
|
+
from faissknn import FaissKNNClassifier
|
|
44
|
+
|
|
45
|
+
x, y = make_classification()
|
|
46
|
+
x_train, x_test, y_train, y_test = train_test_split(x, y)
|
|
47
|
+
model = FaissKNNClassifier(
|
|
48
|
+
n_neighbors=5,
|
|
49
|
+
n_classes=None,
|
|
50
|
+
device="cpu"
|
|
51
|
+
)
|
|
52
|
+
model.fit(x_train, y_train)
|
|
53
|
+
|
|
54
|
+
y_pred = model.predict(x_test) # (N,)
|
|
55
|
+
y_proba = model.predict_proba(x_test) # (N, C)
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Multilabel:
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
from sklearn.datasets import make_classification
|
|
62
|
+
from sklearn.model_selection import train_test_split
|
|
63
|
+
|
|
64
|
+
from faissknn import FaissKNNMultilabelClassifier
|
|
65
|
+
|
|
66
|
+
x, y = make_multilabel_classification()
|
|
67
|
+
x_train, x_test, y_train, y_test = train_test_split(x, y)
|
|
68
|
+
model = FaissKNNClassifier(
|
|
69
|
+
n_neighbors=5,
|
|
70
|
+
device="cpu"
|
|
71
|
+
)
|
|
72
|
+
model.fit(x_train, y_train)
|
|
73
|
+
|
|
74
|
+
y_pred = model.predict(x_test) # (N, C)
|
|
75
|
+
y_proba = model.predict_proba(x_test) # (N, C)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
GPU/CUDA: `faissknn` also supports running on the GPU to speed up computation. Simply change the device to `cuda` or a specific cuda device `cuda:0`
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
model = FaissKNNClassifier(
|
|
82
|
+
n_neighbors=5,
|
|
83
|
+
device="cuda"
|
|
84
|
+
)
|
|
85
|
+
model = FaissKNNClassifier(
|
|
86
|
+
n_neighbors=5,
|
|
87
|
+
device="cuda:0"
|
|
88
|
+
)
|
|
89
|
+
```
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
setup.cfg
|
|
5
|
+
faissknn/__init__.py
|
|
6
|
+
faissknn/knn.py
|
|
7
|
+
faissknn.egg-info/PKG-INFO
|
|
8
|
+
faissknn.egg-info/SOURCES.txt
|
|
9
|
+
faissknn.egg-info/dependency_links.txt
|
|
10
|
+
faissknn.egg-info/requires.txt
|
|
11
|
+
faissknn.egg-info/top_level.txt
|
|
12
|
+
tests/test_knn.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
faissknn
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = [
|
|
3
|
+
# setuptools 42+ required for metadata.license_files support in setup.cfg
|
|
4
|
+
"setuptools>=42,<68",
|
|
5
|
+
]
|
|
6
|
+
build-backend = "setuptools.build_meta"
|
|
7
|
+
|
|
8
|
+
[tool.black]
|
|
9
|
+
target-version = ["py39", "py310"]
|
|
10
|
+
color = true
|
|
11
|
+
skip_magic_trailing_comma = true
|
|
12
|
+
|
|
13
|
+
[tool.isort]
|
|
14
|
+
profile = "black"
|
|
15
|
+
known_first_party = ["tests", "faissknn"]
|
|
16
|
+
skip_gitignore = true
|
|
17
|
+
color_output = true
|
|
18
|
+
|
|
19
|
+
[tool.mypy]
|
|
20
|
+
python_version = "3.10"
|
|
21
|
+
ignore_missing_imports = true
|
|
22
|
+
show_error_codes = true
|
|
23
|
+
exclude = "(build|data|dist|docs/src|images|logo|logs|output)/"
|
|
24
|
+
|
|
25
|
+
# Strict
|
|
26
|
+
warn_unused_configs = true
|
|
27
|
+
disallow_any_generics = true
|
|
28
|
+
disallow_subclassing_any = true
|
|
29
|
+
disallow_untyped_calls = true
|
|
30
|
+
disallow_untyped_defs = true
|
|
31
|
+
disallow_incomplete_defs = true
|
|
32
|
+
check_untyped_defs = true
|
|
33
|
+
disallow_untyped_decorators = true
|
|
34
|
+
no_implicit_optional = true
|
|
35
|
+
warn_redundant_casts = true
|
|
36
|
+
warn_unused_ignores = true
|
|
37
|
+
warn_return_any = true
|
|
38
|
+
no_implicit_reexport = true
|
|
39
|
+
strict_equality = true
|
|
40
|
+
|
|
41
|
+
[tool.pydocstyle]
|
|
42
|
+
convention = "google"
|
|
43
|
+
match_dir = "(faissknn)"
|
|
44
|
+
|
|
45
|
+
[tool.pytest.ini_options]
|
|
46
|
+
# Skip slow tests by default
|
|
47
|
+
addopts = "-m 'not slow'"
|
|
48
|
+
# https://docs.pytest.org/en/latest/how-to/capture-warnings.html
|
|
49
|
+
markers = [
|
|
50
|
+
"slow: marks tests as slow",
|
|
51
|
+
]
|
|
52
|
+
norecursedirs = [
|
|
53
|
+
".ipynb_checkpoints",
|
|
54
|
+
"data",
|
|
55
|
+
"__pycache__",
|
|
56
|
+
]
|
|
57
|
+
testpaths = [
|
|
58
|
+
"tests",
|
|
59
|
+
]
|
faissknn-0.0.1/setup.cfg
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
[metadata]
|
|
2
|
+
name = faissknn
|
|
3
|
+
version = attr: faissknn.__version__
|
|
4
|
+
author = Isaac Corley
|
|
5
|
+
author_email = isaac.corley@my.utsa.edu
|
|
6
|
+
description = Faiss implementation of multiclass and multilabel K-Nearest Neighbors Classifiers
|
|
7
|
+
long_description = file: README.md
|
|
8
|
+
long_description_content_type = text/markdown
|
|
9
|
+
url = https://github.com/isaaccorley/faissknn
|
|
10
|
+
license_files = LICENSE
|
|
11
|
+
classifiers =
|
|
12
|
+
Development Status :: 3 - Alpha
|
|
13
|
+
Intended Audience :: Science/Research
|
|
14
|
+
Programming Language :: Python :: 3
|
|
15
|
+
Programming Language :: Python :: 3.9
|
|
16
|
+
Programming Language :: Python :: 3.10
|
|
17
|
+
License :: OSI Approved :: MIT License
|
|
18
|
+
Operating System :: OS Independent
|
|
19
|
+
Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
20
|
+
keywords = pytorch, machine learning, deep learning
|
|
21
|
+
|
|
22
|
+
[options]
|
|
23
|
+
install_requires =
|
|
24
|
+
numpy
|
|
25
|
+
python_requires = >=3.9,<4
|
|
26
|
+
packages = find:
|
|
27
|
+
|
|
28
|
+
[options.packages.find]
|
|
29
|
+
include = faissknn*
|
|
30
|
+
|
|
31
|
+
[options.extras_require]
|
|
32
|
+
dev =
|
|
33
|
+
black
|
|
34
|
+
isort[colors]
|
|
35
|
+
flake8
|
|
36
|
+
pydocstyle
|
|
37
|
+
pytest
|
|
38
|
+
mypy
|
|
39
|
+
scikit-learn
|
|
40
|
+
|
|
41
|
+
[flake8]
|
|
42
|
+
max-line-length = 88
|
|
43
|
+
extend-ignore =
|
|
44
|
+
E203,
|
|
45
|
+
exclude =
|
|
46
|
+
build/,
|
|
47
|
+
dist/,
|
|
48
|
+
.cache/,
|
|
49
|
+
.mypy_cache/,
|
|
50
|
+
.pytest_cache/,
|
|
51
|
+
__pycache__/,
|
|
52
|
+
*.egg-info/,
|
|
53
|
+
|
|
54
|
+
.git/,
|
|
55
|
+
.github/,
|
|
56
|
+
|
|
57
|
+
[egg_info]
|
|
58
|
+
tag_build =
|
|
59
|
+
tag_date = 0
|
|
60
|
+
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
from typing import Sequence
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
|
|
5
|
+
from faissknn import FaissKNNClassifier, FaissKNNMultilabelClassifier
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def test_multiclass_knn(multiclass_dataset: Sequence[np.ndarray]):
|
|
9
|
+
x_train, y_train, x_test, y_test = multiclass_dataset
|
|
10
|
+
knn = FaissKNNClassifier(n_neighbors=5, n_classes=None, device="cpu")
|
|
11
|
+
knn.fit(x_train, y_train)
|
|
12
|
+
y_pred = knn.predict(x_test)
|
|
13
|
+
y_proba = knn.predict_proba(x_test)
|
|
14
|
+
assert y_pred.ndim == 1
|
|
15
|
+
assert y_proba.ndim == 2
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_multilabel_knn(multilabel_dataset: Sequence[np.ndarray]):
|
|
19
|
+
x_train, y_train, x_test, y_test = multilabel_dataset
|
|
20
|
+
knn = FaissKNNMultilabelClassifier(n_neighbors=5, n_classes=None, device="cpu")
|
|
21
|
+
knn.fit(x_train, y_train)
|
|
22
|
+
y_pred = knn.predict(x_test)
|
|
23
|
+
y_proba = knn.predict_proba(x_test)
|
|
24
|
+
assert y_pred.ndim == 2
|
|
25
|
+
assert y_proba.ndim == 2
|