From aa1e65cee340e148e3b4fe269542ad73fa13ba9c Mon Sep 17 00:00:00 2001 From: Ming-Yang Ho <38325203+Kaminyou@users.noreply.github.com> Date: Mon, 24 Jan 2022 19:10:29 +0800 Subject: [PATCH] [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 --- mmselfsup/utils/clustering.py | 2 +- tests/test_utils/test_clustering.py | 34 +++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/mmselfsup/utils/clustering.py b/mmselfsup/utils/clustering.py index 3db6ac7e..9c0e3c4a 100644 --- a/mmselfsup/utils/clustering.py +++ b/mmselfsup/utils/clustering.py @@ -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] diff --git a/tests/test_utils/test_clustering.py b/tests/test_utils/test_clustering.py index 332de657..acdde87a 100644 --- a/tests/test_utils/test_clustering.py +++ b/tests/test_utils/test_clustering.py @@ -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)