mmselfsup/tests/test_utils/test_clustering.py
Ming-Yang Ho aa1e65cee3
[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>
2022-01-24 19:10:29 +08:00

45 lines
1.2 KiB
Python

from unittest.mock import patch
import numpy as np
import pytest
from mmselfsup.utils.clustering import PIC, 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, verbose=verbose)
assert loss is not None
with pytest.raises(AssertionError):
loss = kmeans.cluster(np.random.rand(10, 8), verbose=verbose)
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)
assert res == 0