[Fix] fix a bug of showing kmeans losses (#182)

* fix a bug of showing kmeans losses

* fix a bug of showing kmeans losses

* isort

* fix typo (#180)

* add unit test

* remove gpu restrict

* restore gpu restrict

* bypass gpu by mock

* mock bypass

* mock bypass

* mock bypass

* mock bypass

* apply flake8

* apply flask8

* typo

* Upgrade isort (#184)

* Fix isort config

* Trigger commit hook

* Trigger commit hook

* Upgrade isort in ci and remove seed-isort-config

* Update test requirement

* Add unit test for build_dataset

* Fix test data

* Fix typo

* Use deepclustering dataset as test dataset config

* fix a bug of showing kmeans losses

* fix a bug of showing kmeans losses

* isort

* add unit test

* remove gpu restrict

* restore gpu restrict

* bypass gpu by mock

* mock bypass

* mock bypass

* mock bypass

* mock bypass

* apply flake8

* apply flask8

* typo

* apply isort

* remove session scope func

Co-authored-by: Uno Wu <st9007a@gmail.com>
This commit is contained in:
Ming-Yang Ho 2022-01-24 19:10:29 +08:00 committed by GitHub
parent 8369d6b0ce
commit aa1e65cee3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 10 deletions

View File

@ -102,7 +102,7 @@ def run_kmeans(x, nmb_clusters, verbose=False):
_, I = index.search(x, 1) # noqa E741
losses = faiss.vector_to_array(clus.obj)
if verbose:
print(f'k-means loss evolution: {losses:0}')
print(f'k-means loss evolution: {losses}')
return [int(n[0]) for n in I], losses[-1]

View File

@ -1,27 +1,43 @@
from unittest.mock import patch
import numpy as np
import pytest
import torch
from mmselfsup.utils.clustering import PIC, Kmeans
@pytest.mark.skipif(
not torch.cuda.is_available(), reason='CUDA is not available.')
def test_kmeans():
@pytest.fixture
def mock_faiss_in_clutering():
with patch('mmselfsup.utils.clustering.faiss') as faiss:
yield faiss
@pytest.fixture
def mock_faiss(mock_faiss_in_clutering):
mock_PCAmatrix = mock_faiss_in_clutering.PCAMatrix.return_value
mock_GpuIndexFlatL2 = mock_faiss_in_clutering.GpuIndexFlatL2.return_value
mock_PCAmatrix.apply_py.return_value = np.random.rand(10, 8)
mock_GpuIndexFlatL2.search.return_value = (
np.random.rand(1000, 6),
np.random.rand(1000, 6),
)
@pytest.mark.parametrize('verbose', [True, False])
def test_kmeans(mock_faiss, verbose):
fake_input = np.random.rand(10, 8).astype(np.float32)
pca_dim = 2
kmeans = Kmeans(2, pca_dim)
loss = kmeans.cluster(fake_input)
loss = kmeans.cluster(fake_input, verbose=verbose)
assert loss is not None
with pytest.raises(AssertionError):
loss = kmeans.cluster(np.random.rand(10, 8))
loss = kmeans.cluster(np.random.rand(10, 8), verbose=verbose)
@pytest.mark.skipif(
not torch.cuda.is_available(), reason='CUDA is not available.')
def test_pic():
def test_pic(mock_faiss):
fake_input = np.random.rand(1000, 16).astype(np.float32)
pic = PIC(pca_dim=8)
res = pic.cluster(fake_input)