Download tests/collections/speaker_tasks/utils/test_multispeaker_utils.py from echodict/NeMo: direct link, hf CLI and curl.
- Browser
- Download file 16.9 kB
-
https://huggingface.co/datasets/echodict/NeMo/resolve/main/tests/collections/speaker_tasks/utils/test_multispeaker_utils.py
- Command line
-
hf download hf://datasets/echodict/NeMo/tests/collections/speaker_tasks/utils/test_multispeaker_utils.py
-
curl -L -o test_multispeaker_utils.py https://huggingface.co/datasets/echodict/NeMo/resolve/main/tests/collections/speaker_tasks/utils/test_multispeaker_utils.py
16.9 kB
| # Copyright (c) 2023, NVIDIA CORPORATION. All rights reserved. | |
| # | |
| # Licensed under the Apache License, Version 2.0 (the "License"); | |
| # you may not use this file except in compliance with the License. | |
| # You may obtain a copy of the License at | |
| # | |
| # http://www.apache.org/licenses/LICENSE-2.0 | |
| # | |
| # Unless required by applicable law or agreed to in writing, software | |
| # distributed under the License is distributed on an "AS IS" BASIS, | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| # See the License for the specific language governing permissions and | |
| # limitations under the License. | |
| import itertools | |
| import pytest | |
| import torch | |
| from nemo.collections.asr.parts.utils.asr_multispeaker_utils import ( | |
| find_best_permutation, | |
| find_first_nonzero, | |
| get_ats_targets, | |
| get_hidden_length_from_sample_length, | |
| get_pil_targets, | |
| reconstruct_labels, | |
| ) | |
| def reconstruct_labels_forloop(labels: torch.Tensor, batch_perm_inds: torch.Tensor) -> torch.Tensor: | |
| """ | |
| This is a for-loop implementation of reconstruct_labels built for testing purposes. | |
| """ | |
| # Expanding batch_perm_inds to align with labels dimensions | |
| batch_size, num_frames, num_speakers = labels.shape | |
| batch_perm_inds_exp = batch_perm_inds.unsqueeze(1).expand(-1, num_frames, -1) | |
| # Reconstructing the labels using advanced indexing | |
| reconstructed_labels = torch.gather(labels, 2, batch_perm_inds_exp) | |
| return reconstructed_labels | |
| class TestSortingUtils: | |
| def test_find_first_nonzero(self, mat, max_cap_val, thres, expected): | |
| result = find_first_nonzero(mat, max_cap_val, thres) | |
| assert torch.equal(result, expected), f"Expected {expected} but got {result}" | |
| def test_find_best_permutation(self, match_score, speaker_permutations, expected): | |
| result = find_best_permutation(match_score, speaker_permutations) | |
| assert torch.equal(result, expected), f"Expected {expected} but got {result}" | |
| def test_reconstruct_labels_with_forloop_ver(self, batch_size, num_frames, num_speakers): | |
| # Generate random labels and batch_perm_inds tensor for testing | |
| labels = torch.rand(batch_size, num_frames, num_speakers) | |
| batch_perm_inds = torch.stack([torch.randperm(num_speakers) for _ in range(batch_size)]) | |
| # Call both functions | |
| result_matrix = reconstruct_labels(labels, batch_perm_inds) | |
| result_forloop = reconstruct_labels_forloop(labels, batch_perm_inds) | |
| # Assert that both methods return the same result | |
| assert torch.allclose(result_matrix, result_forloop), "The results are not equal!" | |
| def test_reconstruct_labels(self, labels, batch_perm_inds, expected_output): | |
| # Call the reconstruct_labels function | |
| result = reconstruct_labels(labels, batch_perm_inds) | |
| # Assert that the result matches the expected output | |
| assert torch.allclose(result, expected_output), f"Expected {expected_output}, but got {result}" | |
| class TestTargetGenerators: | |
| def test_get_ats_targets(self, labels, preds, num_speakers, expected_output): | |
| # Generate all permutations for the given number of speakers | |
| speaker_inds = list(range(num_speakers)) | |
| speaker_permutations = torch.tensor(list(itertools.permutations(speaker_inds))) | |
| # Call the function under test | |
| result = get_ats_targets(labels, preds, speaker_permutations) | |
| # Assert that the result matches the expected output | |
| assert torch.allclose(result, expected_output), f"Expected {expected_output}, but got {result}" | |
| def test_get_pil_targets(self, labels, preds, num_speakers, expected_output): | |
| # Generate all permutations for the given number of speakers | |
| speaker_inds = list(range(num_speakers)) | |
| speaker_permutations = torch.tensor(list(itertools.permutations(speaker_inds))) | |
| result = get_pil_targets(labels, preds, speaker_permutations) | |
| assert torch.equal(result, expected_output), f"Expected {expected_output} but got {result}" | |
| class TestGetHiddenLengthFromSampleLength: | |
| def test_various_cases( | |
| self, num_samples, num_sample_per_mel_frame, num_mel_frame_per_asr_frame, expected_hidden_length | |
| ): | |
| result = get_hidden_length_from_sample_length( | |
| num_samples, num_sample_per_mel_frame, num_mel_frame_per_asr_frame | |
| ) | |
| assert result == expected_hidden_length | |
| def test_default_parameters(self): | |
| assert get_hidden_length_from_sample_length(160) == 1 | |
| assert get_hidden_length_from_sample_length(1280) == 1 | |
| assert get_hidden_length_from_sample_length(0) == 0 | |
| assert get_hidden_length_from_sample_length(159) == 1 | |
| def test_edge_cases(self): | |
| assert get_hidden_length_from_sample_length(159, 160, 8) == 1 | |
| assert get_hidden_length_from_sample_length(160, 160, 8) == 1 | |
| assert get_hidden_length_from_sample_length(161, 160, 8) == 1 | |
| assert get_hidden_length_from_sample_length(1279, 160, 8) == 1 | |
| def test_real_life_examples(self): | |
| # The samples tried when this function was designed. | |
| assert get_hidden_length_from_sample_length(160000) == 125 | |
| assert get_hidden_length_from_sample_length(159999) == 125 | |
| assert get_hidden_length_from_sample_length(158720) == 124 | |
| assert get_hidden_length_from_sample_length(158719) == 124 | |
| assert get_hidden_length_from_sample_length(158880) == 125 | |
| assert get_hidden_length_from_sample_length(158879) == 125 | |
| assert get_hidden_length_from_sample_length(1600) == 2 | |
| assert get_hidden_length_from_sample_length(1599) == 2 | |