File size: 49,925 Bytes
a7c2243 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 | # Copyright (c) 2020, 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 os
import tempfile
import numpy as np
import pytest
import soundfile as sf
import torch.cuda
from omegaconf import OmegaConf
from nemo.collections.asr.parts.utils.manifest_utils import write_manifest
from nemo.collections.audio.data import audio_to_audio_dataset
from nemo.collections.audio.data.audio_to_audio import (
ASRAudioProcessor,
AudioToTargetDataset,
AudioToTargetWithEmbeddingDataset,
AudioToTargetWithReferenceDataset,
_audio_collate_fn,
)
from nemo.collections.audio.data.audio_to_audio_lhotse import (
LhotseAudioToTargetDataset,
convert_manifest_nemo_to_lhotse,
)
from nemo.collections.audio.parts.utils.audio import get_segment_start
from nemo.collections.common.data.lhotse import get_lhotse_dataloader_from_config
class TestAudioDatasets:
@pytest.mark.unit
@pytest.mark.parametrize('num_channels', [1, 2])
@pytest.mark.parametrize('num_targets', [1, 3])
def test_list_to_multichannel(self, num_channels, num_targets):
"""Test conversion of a list of arrays into"""
random_seed = 42
num_samples = 1000
# Generate random signals
_rng = np.random.default_rng(seed=random_seed)
# Multi-channel signal
golden_target = _rng.normal(size=(num_channels * num_targets, num_samples))
# Create a list of num_targets signals with num_channels channels
target_list = [golden_target[n * num_channels : (n + 1) * num_channels, :] for n in range(num_targets)]
# Check the original signal is not modified
assert (ASRAudioProcessor.list_to_multichannel(golden_target) == golden_target).all()
# Check the list is converted back to the original signal
assert (ASRAudioProcessor.list_to_multichannel(target_list) == golden_target).all()
@pytest.mark.unit
@pytest.mark.parametrize('num_channels', [1, 2])
def test_processor_process_audio(self, num_channels):
"""Test signal normalization in process_audio."""
num_samples = 1000
num_examples = 30
signals = ['input_signal', 'target_signal', 'reference_signal']
for normalization_signal in [None] + signals:
# Create processor
processor = ASRAudioProcessor(
sample_rate=16000, random_offset=False, normalization_signal=normalization_signal
)
# Generate random signals
for n in range(num_examples):
example = {signal: torch.randn(num_channels, num_samples) for signal in signals}
processed_example = processor.process_audio(example)
# Expected scale
if normalization_signal:
scale = 1.0 / (example[normalization_signal].abs().max() + processor.eps)
else:
scale = 1.0
# Make sure all signals are scaled as expected
for signal in signals:
assert torch.allclose(
processed_example[signal], example[signal] * scale
), f'Failed example {n} signal {signal}'
@pytest.mark.unit
def test_audio_collate_fn(self):
"""Test `_audio_collate_fn`"""
batch_size = 16
random_seed = 42
atol = 1e-5
# Generate random signals
_rng = np.random.default_rng(seed=random_seed)
signal_to_channels = {
'input_signal': 2,
'target_signal': 1,
'reference_signal': 1,
}
signal_to_length = {
'input_signal': _rng.integers(low=5, high=25, size=batch_size),
'target_signal': _rng.integers(low=5, high=25, size=batch_size),
'reference_signal': _rng.integers(low=5, high=25, size=batch_size),
}
# Generate batch
batch = []
for n in range(batch_size):
item = dict()
for signal, num_channels in signal_to_channels.items():
random_signal = _rng.normal(size=(num_channels, signal_to_length[signal][n]))
random_signal = np.squeeze(random_signal) # get rid of channel dimention for single-channel
item[signal] = torch.tensor(random_signal)
batch.append(item)
# Run UUT
batched = _audio_collate_fn(batch)
batched_signals = {
'input_signal': batched[0].cpu().detach().numpy(),
'target_signal': batched[2].cpu().detach().numpy(),
'reference_signal': batched[4].cpu().detach().numpy(),
}
batched_lengths = {
'input_signal': batched[1].cpu().detach().numpy(),
'target_signal': batched[3].cpu().detach().numpy(),
'reference_signal': batched[5].cpu().detach().numpy(),
}
# Check outputs
for signal, b_signal in batched_signals.items():
for n in range(batch_size):
# Check length
uut_length = batched_lengths[signal][n]
golden_length = signal_to_length[signal][n]
assert (
uut_length == golden_length
), f'Example {n} signal {signal} length mismatch: batched ({uut_length}) != golden ({golden_length})'
uut_signal = b_signal[n][:uut_length, ...]
golden_signal = batch[n][signal][:uut_length, ...].cpu().detach().numpy()
assert np.allclose(
uut_signal, golden_signal, atol=atol
), f'Example {n} signal {signal} value mismatch.'
@pytest.mark.unit
def test_audio_to_target_dataset(self):
"""Test AudioWithTargetDataset in different configurations.
Test below cover the following:
1) no constraints
2) filtering based on signal duration
3) use with channel selector
4) use with fixed audio duration and random subsegments
5) collate a batch of items
In this use case, each line of the manifest file has the following format:
```
{
'input_filepath': 'path/to/input.wav',
'target_filepath': 'path/to/path_to_target.wav',
'duration': duration_of_input,
}
```
"""
# Data setup
random_seed = 42
sample_rate = 16000
num_examples = 25
data_num_channels = {
'input_signal': 4,
'target_signal': 2,
}
data_min_duration = 2.0
data_max_duration = 8.0
data_key = {
'input_signal': 'input_filepath',
'target_signal': 'target_filepath',
}
# Tolerance
atol = 1e-6
# Generate random signals
_rng = np.random.default_rng(seed=random_seed)
# Input and target signals have the same duration
data_duration = np.round(_rng.uniform(low=data_min_duration, high=data_max_duration, size=num_examples), 3)
data_duration_samples = np.floor(data_duration * sample_rate).astype(int)
data = dict()
for signal, num_channels in data_num_channels.items():
data[signal] = []
for n in range(num_examples):
if num_channels == 1:
random_signal = _rng.uniform(low=-0.5, high=0.5, size=(data_duration_samples[n]))
else:
random_signal = _rng.uniform(low=-0.5, high=0.5, size=(num_channels, data_duration_samples[n]))
data[signal].append(random_signal)
with tempfile.TemporaryDirectory() as test_dir:
# Build metadata for manifest
metadata = []
for n in range(num_examples):
meta = dict()
for signal in data:
# filenames
signal_filename = f'{signal}_{n:02d}.wav'
# write audio files
sf.write(os.path.join(test_dir, signal_filename), data[signal][n].T, sample_rate, 'float')
# update metadata
meta[data_key[signal]] = signal_filename
meta['duration'] = data_duration[n]
metadata.append(meta)
# Save manifest
manifest_filepath = os.path.join(test_dir, 'manifest.json')
write_manifest(manifest_filepath, metadata)
# Test 1
# - No constraints on channels or duration
dataset = AudioToTargetDataset(
manifest_filepath=manifest_filepath,
input_key=data_key['input_signal'],
target_key=data_key['target_signal'],
sample_rate=sample_rate,
)
# Also test the corresponding factory
config = {
'manifest_filepath': manifest_filepath,
'input_key': data_key['input_signal'],
'target_key': data_key['target_signal'],
'sample_rate': sample_rate,
}
dataset_factory = audio_to_audio_dataset.get_audio_to_target_dataset(config)
# Prepare lhotse manifest
cuts_path = manifest_filepath.replace('.json', '_cuts.jsonl')
convert_manifest_nemo_to_lhotse(
input_manifest=manifest_filepath,
output_manifest=cuts_path,
input_key=data_key['input_signal'],
target_key=data_key['target_signal'],
)
# Prepare lhotse dataset
config_lhotse = {
'cuts_path': cuts_path,
'use_lhotse': True,
'sample_rate': sample_rate,
'batch_size': 1,
}
dl_lhotse = get_lhotse_dataloader_from_config(
OmegaConf.create(config_lhotse), global_rank=0, world_size=1, dataset=LhotseAudioToTargetDataset()
)
dataset_lhotse = [item for item in dl_lhotse]
# Test number of channels
for signal in data:
assert data_num_channels[signal] == dataset.num_channels(
signal
), f'Num channels not correct for signal {signal}'
assert data_num_channels[signal] == dataset_factory.num_channels(
signal
), f'Num channels not correct for signal {signal}'
# Test returned examples
for n in range(num_examples):
for signal in data:
golden_signal = data[signal][n]
for use_lhotse in [False, True]:
item_signal = (
dataset_lhotse[n][signal].squeeze(0) if use_lhotse else dataset.__getitem__(n)[signal]
)
item_factory_signal = dataset_factory.__getitem__(n)[signal]
assert (
item_signal.shape == golden_signal.shape
), f'Test 1, use_lhotse={use_lhotse}: Signal {signal} item shape {item_signal.shape} not matching reference shape {golden_signal.shape}'
assert np.allclose(
item_signal, golden_signal, atol=atol
), f'Test 1, use_lhotse={use_lhotse}: Failed for example {n}, signal {signal} (random seed {random_seed})'
assert np.allclose(
item_factory_signal, golden_signal, atol=atol
), f'Test 1, use_lhotse={use_lhotse}: Failed for factory example {n}, signal {signal} (random seed {random_seed})'
# Test 2
# - Filtering based on signal duration
min_duration = 3.5
max_duration = 7.5
dataset = AudioToTargetDataset(
manifest_filepath=manifest_filepath,
input_key=data_key['input_signal'],
target_key=data_key['target_signal'],
min_duration=min_duration,
max_duration=max_duration,
sample_rate=sample_rate,
)
# Prepare lhotse dataset
config_lhotse = {
'cuts_path': cuts_path,
'use_lhotse': True,
'min_duration': min_duration,
'max_duration': max_duration,
'sample_rate': sample_rate,
'batch_size': 1,
}
dl_lhotse = get_lhotse_dataloader_from_config(
OmegaConf.create(config_lhotse), global_rank=0, world_size=1, dataset=LhotseAudioToTargetDataset()
)
dataset_lhotse = [item for item in dl_lhotse]
filtered_examples = [n for n, val in enumerate(data_duration) if min_duration <= val <= max_duration]
for n in range(len(dataset)):
for use_lhotse in [False, True]:
for signal in data:
item_signal = (
dataset_lhotse[n][signal].squeeze(0) if use_lhotse else dataset.__getitem__(n)[signal]
)
golden_signal = data[signal][filtered_examples[n]]
assert (
item_signal.shape == golden_signal.shape
), f'Test 2, use_lhotse={use_lhotse}: Signal {signal} item shape {item_signal.shape} not matching reference shape {golden_signal.shape}'
assert np.allclose(
item_signal, golden_signal, atol=atol
), f'Test 2, use_lhotse={use_lhotse}: Failed for example {n}, signal {signal} (random seed {random_seed})'
# Test 3
# - Use channel selector
channel_selector = {
'input_signal': [0, 2],
'target_signal': 1,
}
dataset = AudioToTargetDataset(
manifest_filepath=manifest_filepath,
input_key=data_key['input_signal'],
target_key=data_key['target_signal'],
input_channel_selector=channel_selector['input_signal'],
target_channel_selector=channel_selector['target_signal'],
sample_rate=sample_rate,
)
for n in range(len(dataset)):
item = dataset.__getitem__(n)
for signal in data:
cs = channel_selector[signal]
item_signal = item[signal].cpu().detach().numpy()
golden_signal = data[signal][n][cs, ...]
assert (
item_signal.shape == golden_signal.shape
), f'Signal {signal}: item shape {item_signal.shape} not matching reference shape {golden_signal.shape}'
assert np.allclose(
item_signal, golden_signal, atol=atol
), f'Test 3: Failed for example {n}, signal {signal} (random seed {random_seed})'
# Test 4
# - Use fixed duration (random segment selection)
audio_duration = 4.0
audio_duration_samples = int(np.floor(audio_duration * sample_rate))
filtered_examples = [n for n, val in enumerate(data_duration) if val >= audio_duration]
for random_offset in [True, False]:
# Test subsegments with the default fixed offset and a random offset
dataset = AudioToTargetDataset(
manifest_filepath=manifest_filepath,
input_key=data_key['input_signal'],
target_key=data_key['target_signal'],
sample_rate=sample_rate,
min_duration=audio_duration,
audio_duration=audio_duration,
random_offset=random_offset, # random offset when selecting subsegment
)
# Prepare lhotse dataset
config_lhotse = {
'cuts_path': cuts_path,
'use_lhotse': True,
'min_duration': audio_duration,
'truncate_duration': audio_duration,
'truncate_offset_type': 'random' if random_offset else 'start',
'sample_rate': sample_rate,
'batch_size': 1,
}
dl_lhotse = get_lhotse_dataloader_from_config(
OmegaConf.create(config_lhotse), global_rank=0, world_size=1, dataset=LhotseAudioToTargetDataset()
)
dataset_lhotse = [item for item in dl_lhotse]
for n in range(len(dataset)):
for use_lhotse in [False, True]:
item = dataset_lhotse[n] if use_lhotse else dataset.__getitem__(n)
golden_start = golden_end = None
for signal in data:
item_signal = item[signal].squeeze(0) if use_lhotse else item[signal]
full_golden_signal = data[signal][filtered_examples[n]]
# Find random segment using correlation on the first channel
# of the first signal, and then use it fixed for other signals
if golden_start is None:
golden_start = get_segment_start(
signal=full_golden_signal[0, :], segment=item_signal[0, :]
)
if not random_offset:
assert (
golden_start == 0
), f'Test 4, use_lhotse={use_lhotse}: Expecting the signal to start at 0 when random_offset is False'
golden_end = golden_start + audio_duration_samples
golden_signal = full_golden_signal[..., golden_start:golden_end]
# Test length is correct
assert (
item_signal.shape[-1] == audio_duration_samples
), f'Test 4, use_lhotse={use_lhotse}: Signal length ({item_signal.shape[-1]}) not matching the expected length ({audio_duration_samples})'
assert (
item_signal.shape == golden_signal.shape
), f'Test 4, use_lhotse={use_lhotse}: Signal {signal} item shape {item_signal.shape} not matching reference shape {golden_signal.shape}'
# Test signal values
assert np.allclose(
item_signal, golden_signal, atol=atol
), f'Test 4, use_lhotse={use_lhotse}: Failed for example {n}, signal {signal} (random seed {random_seed})'
# Test 5:
# - Test collate_fn
batch_size = 16
for use_lhotse in [False, True]:
if use_lhotse:
# Get batch from lhotse dataloader
config_lhotse['batch_size'] = batch_size
dl_lhotse = get_lhotse_dataloader_from_config(
OmegaConf.create(config_lhotse),
global_rank=0,
world_size=1,
dataset=LhotseAudioToTargetDataset(),
)
batched = next(iter(dl_lhotse))
else:
# Get examples from dataset and collate into a batch
batch = [dataset.__getitem__(n) for n in range(batch_size)]
batched = dataset.collate_fn(batch)
# Test all shapes and lengths
for n, signal in enumerate(data.keys()):
length = signal.replace('_signal', '_length')
if isinstance(batched, dict):
signal_shape = batched[signal].shape
signal_len = batched[length]
else:
signal_shape = batched[2 * n].shape
signal_len = batched[2 * n + 1]
assert signal_shape == (
batch_size,
data_num_channels[signal],
audio_duration_samples,
), f'Test 5, use_lhotse={use_lhotse}: Unexpected signal {signal} shape {signal_shape}'
assert (
len(signal_len) == batch_size
), f'Test 5, use_lhotse={use_lhotse}: Unexpected length of signal_len ({len(signal_len)})'
assert all(
signal_len == audio_duration_samples
), f'Test 5, use_lhotse={use_lhotse}: Unexpected signal_len {signal_len}'
@pytest.mark.unit
def test_audio_to_target_dataset_with_target_list(self):
"""Test AudioWithTargetDataset when the input manifest has a list
of audio files in the target key.
In this use case, each line of the manifest file has the following format:
```
{
'input_filepath': 'path/to/input.wav',
'target_filepath': ['path/to/path_to_target_ch0.wav', 'path/to/path_to_target_ch1.wav'],
'duration': duration_of_input,
}
```
"""
# Data setup
random_seed = 42
sample_rate = 16000
num_examples = 25
data_num_channels = {
'input_signal': 4,
'target_signal': 2,
}
data_min_duration = 2.0
data_max_duration = 8.0
data_key = {
'input_signal': 'input_filepath',
'target_signal': 'target_filepath',
}
# Tolerance
atol = 1e-6
# Generate random signals
_rng = np.random.default_rng(seed=random_seed)
# Input and target signals have the same duration
data_duration = np.round(_rng.uniform(low=data_min_duration, high=data_max_duration, size=num_examples), 3)
data_duration_samples = np.floor(data_duration * sample_rate).astype(int)
data = dict()
for signal, num_channels in data_num_channels.items():
data[signal] = []
for n in range(num_examples):
if num_channels == 1:
random_signal = _rng.uniform(low=-0.5, high=0.5, size=(data_duration_samples[n]))
else:
random_signal = _rng.uniform(low=-0.5, high=0.5, size=(num_channels, data_duration_samples[n]))
data[signal].append(random_signal)
with tempfile.TemporaryDirectory() as test_dir:
# Build metadata for manifest
metadata = []
for n in range(num_examples):
meta = dict()
for signal in data:
if signal == 'target_signal':
# Save targets as individual files
signal_filename = []
for ch in range(data_num_channels[signal]):
# add current filename
signal_filename.append(f'{signal}_{n:02d}_ch_{ch}.wav')
# write audio file
sf.write(
os.path.join(test_dir, signal_filename[-1]),
data[signal][n][ch, :],
sample_rate,
'float',
)
else:
# single file
signal_filename = f'{signal}_{n:02d}.wav'
# write audio files
sf.write(os.path.join(test_dir, signal_filename), data[signal][n].T, sample_rate, 'float')
# update metadata
meta[data_key[signal]] = signal_filename
meta['duration'] = data_duration[n]
metadata.append(meta)
# Save manifest
manifest_filepath = os.path.join(test_dir, 'manifest.json')
write_manifest(manifest_filepath, metadata)
# Test 1
# - No constraints on channels or duration
dataset = AudioToTargetDataset(
manifest_filepath=manifest_filepath,
input_key=data_key['input_signal'],
target_key=data_key['target_signal'],
sample_rate=sample_rate,
)
config = {
'manifest_filepath': manifest_filepath,
'input_key': data_key['input_signal'],
'target_key': data_key['target_signal'],
'sample_rate': sample_rate,
}
dataset_factory = audio_to_audio_dataset.get_audio_to_target_dataset(config)
# Prepare lhotse manifest
cuts_path = manifest_filepath.replace('.json', '_cuts.jsonl')
convert_manifest_nemo_to_lhotse(
input_manifest=manifest_filepath,
output_manifest=cuts_path,
input_key=data_key['input_signal'],
target_key=data_key['target_signal'],
)
# Prepare lhotse dataset
config_lhotse = {
'cuts_path': cuts_path,
'use_lhotse': True,
'sample_rate': sample_rate,
'batch_size': 1,
}
dl_lhotse = get_lhotse_dataloader_from_config(
OmegaConf.create(config_lhotse), global_rank=0, world_size=1, dataset=LhotseAudioToTargetDataset()
)
dataset_lhotse = [item for item in dl_lhotse]
for n in range(num_examples):
for use_lhotse in [False, True]:
item = dataset_lhotse[n] if use_lhotse else dataset.__getitem__(n)
item_factory = dataset_factory.__getitem__(n)
for signal in data:
item_signal = item[signal].squeeze(0) if use_lhotse else item[signal]
golden_signal = data[signal][n]
assert (
item_signal.shape == golden_signal.shape
), f'Test 1, use_lhotse={use_lhotse}: Signal {signal} item shape {item_signal.shape} not matching reference shape {golden_signal.shape}'
assert np.allclose(
item_signal, golden_signal, atol=atol
), f'Test 1, use_lhotse={use_lhotse}: Failed for example {n}, signal {signal} (random seed {random_seed})'
assert np.allclose(
item_factory[signal], golden_signal, atol=atol
), f'Test 1, use_lhotse={use_lhotse}: Failed for factory example {n}, signal {signal} (random seed {random_seed})'
# Test 2
# Set target as the first channel of input_filepath and all files listed in target_filepath.
# In this case, the target will have 3 channels.
# Note: this is currently not supported by lhotse, so we only test the default dataset here.
dataset = AudioToTargetDataset(
manifest_filepath=manifest_filepath,
input_key=data_key['input_signal'],
target_key=[data_key['input_signal'], data_key['target_signal']],
target_channel_selector=0,
sample_rate=sample_rate,
)
for n in range(num_examples):
item = dataset.__getitem__(n)
for signal in data:
item_signal = item[signal].cpu().detach().numpy()
golden_signal = data[signal][n]
if signal == 'target_signal':
# add the first channel of the input
golden_signal = np.concatenate([data['input_signal'][n][0:1, ...], golden_signal], axis=0)
assert (
item_signal.shape == golden_signal.shape
), f'Signal {signal}: item shape {item_signal.shape} not matching reference shape {golden_signal.shape}'
assert np.allclose(
item_signal, golden_signal, atol=atol
), f'Test 2: Failed for example {n}, signal {signal} (random seed {random_seed})'
@pytest.mark.unit
def test_audio_to_target_dataset_for_inference(self):
"""Test AudioWithTargetDataset when target_key is
not set, i.e., it is `None`. This is the case, e.g., when
running inference, and a target is not available.
In this use case, each line of the manifest file has the following format:
```
{
'input_filepath': 'path/to/input.wav',
'duration': duration_of_input,
}
```
"""
# Data setup
random_seed = 42
sample_rate = 16000
num_examples = 25
data_num_channels = {
'input_signal': 4,
}
data_min_duration = 2.0
data_max_duration = 8.0
data_key = {
'input_signal': 'input_filepath',
}
# Tolerance
atol = 1e-6
# Generate random signals
_rng = np.random.default_rng(seed=random_seed)
# Input and target signals have the same duration
data_duration = np.round(_rng.uniform(low=data_min_duration, high=data_max_duration, size=num_examples), 3)
data_duration_samples = np.floor(data_duration * sample_rate).astype(int)
data = dict()
for signal, num_channels in data_num_channels.items():
data[signal] = []
for n in range(num_examples):
if num_channels == 1:
random_signal = _rng.uniform(low=-0.5, high=0.5, size=(data_duration_samples[n]))
else:
random_signal = _rng.uniform(low=-0.5, high=0.5, size=(num_channels, data_duration_samples[n]))
data[signal].append(random_signal)
with tempfile.TemporaryDirectory() as test_dir:
# Build metadata for manifest
metadata = []
for n in range(num_examples):
meta = dict()
for signal in data:
# filenames
signal_filename = f'{signal}_{n:02d}.wav'
# write audio files
sf.write(os.path.join(test_dir, signal_filename), data[signal][n].T, sample_rate, 'float')
# update metadata
meta[data_key[signal]] = signal_filename
meta['duration'] = data_duration[n]
metadata.append(meta)
# Save manifest
manifest_filepath = os.path.join(test_dir, 'manifest.json')
write_manifest(manifest_filepath, metadata)
# Test 1
# - No constraints on channels or duration
dataset = AudioToTargetDataset(
manifest_filepath=manifest_filepath,
input_key=data_key['input_signal'],
target_key=None, # target_signal will be empty
sample_rate=sample_rate,
)
# Also test the corresponding factory
config = {
'manifest_filepath': manifest_filepath,
'input_key': data_key['input_signal'],
'target_key': None,
'sample_rate': sample_rate,
}
dataset_factory = audio_to_audio_dataset.get_audio_to_target_dataset(config)
# Prepare lhotse manifest
cuts_path = manifest_filepath.replace('.json', '_cuts.jsonl')
convert_manifest_nemo_to_lhotse(
input_manifest=manifest_filepath,
output_manifest=cuts_path,
input_key=data_key['input_signal'],
target_key=None,
)
# Prepare lhotse dataset
config_lhotse = {
'cuts_path': cuts_path,
'use_lhotse': True,
'sample_rate': sample_rate,
'batch_size': 1,
}
dl_lhotse = get_lhotse_dataloader_from_config(
OmegaConf.create(config_lhotse), global_rank=0, world_size=1, dataset=LhotseAudioToTargetDataset()
)
dataset_lhotse = [item for item in dl_lhotse]
for n in range(num_examples):
for label in ['original', 'factory', 'lhotse']:
if label == 'original':
item = dataset.__getitem__(n)
elif label == 'factory':
item = dataset_factory.__getitem__(n)
elif label == 'lhotse':
item = dataset_lhotse[n]
else:
raise ValueError(f'Unknown label {label}')
# Check target is None
if 'target_signal' in item:
assert item['target_signal'].numel() == 0, f'{label}: target_signal is expected to be empty.'
# Check valid signals
for signal in data:
item_signal = item[signal].squeeze(0) if label == 'lhotse' else item[signal]
golden_signal = data[signal][n]
assert (
item_signal.shape == golden_signal.shape
), f'{label} -- Signal {signal}: item shape {item_signal.shape} not matching reference shape {golden_signal.shape}'
assert np.allclose(
item_signal, golden_signal, atol=atol
), f'{label} -- Test 1: Failed for example {n}, signal {signal} (random seed {random_seed})'
@pytest.mark.unit
def test_audio_to_target_with_reference_dataset(self):
"""Test AudioWithTargetWithReferenceDataset in different configurations.
1) reference synchronized with input and target
2) reference not synchronized
In this use case, each line of the manifest file has the following format:
```
{
'input_filepath': 'path/to/input.wav',
'target_filepath': 'path/to/path_to_target.wav',
'reference_filepath': 'path/to/path_to_reference.wav',
'duration': duration_of_input,
}
```
"""
# Data setup
random_seed = 42
sample_rate = 16000
num_examples = 25
data_num_channels = {
'input_signal': 4,
'target_signal': 2,
'reference_signal': 1,
}
data_min_duration = 2.0
data_max_duration = 8.0
data_key = {
'input_signal': 'input_filepath',
'target_signal': 'target_filepath',
'reference_signal': 'reference_filepath',
}
# Tolerance
atol = 1e-6
# Generate random signals
_rng = np.random.default_rng(seed=random_seed)
# Input and target signals have the same duration
data_duration = np.round(_rng.uniform(low=data_min_duration, high=data_max_duration, size=num_examples), 3)
data_duration_samples = np.floor(data_duration * sample_rate).astype(int)
data = dict()
for signal, num_channels in data_num_channels.items():
data[signal] = []
for n in range(num_examples):
if num_channels == 1:
random_signal = _rng.uniform(low=-0.5, high=0.5, size=(data_duration_samples[n]))
else:
random_signal = _rng.uniform(low=-0.5, high=0.5, size=(num_channels, data_duration_samples[n]))
data[signal].append(random_signal)
with tempfile.TemporaryDirectory() as test_dir:
# Build metadata for manifest
metadata = []
for n in range(num_examples):
meta = dict()
for signal in data:
# filenames
signal_filename = f'{signal}_{n:02d}.wav'
# write audio files
sf.write(os.path.join(test_dir, signal_filename), data[signal][n].T, sample_rate, 'float')
# update metadata
meta[data_key[signal]] = signal_filename
meta['duration'] = data_duration[n]
metadata.append(meta)
# Save manifest
manifest_filepath = os.path.join(test_dir, 'manifest.json')
write_manifest(manifest_filepath, metadata)
# Test 1
# - No constraints on channels or duration
# - Reference is not synchronized with input and target, so whole reference signal will be loaded
dataset = AudioToTargetWithReferenceDataset(
manifest_filepath=manifest_filepath,
input_key=data_key['input_signal'],
target_key=data_key['target_signal'],
reference_key=data_key['reference_signal'],
reference_is_synchronized=False,
sample_rate=sample_rate,
)
# Also test the corresponding factory
config = {
'manifest_filepath': manifest_filepath,
'input_key': data_key['input_signal'],
'target_key': data_key['target_signal'],
'reference_key': data_key['reference_signal'],
'reference_is_synchronized': False,
'sample_rate': sample_rate,
}
dataset_factory = audio_to_audio_dataset.get_audio_to_target_with_reference_dataset(config)
for n in range(num_examples):
item = dataset.__getitem__(n)
item_factory = dataset_factory.__getitem__(n)
for signal in data:
item_signal = item[signal].cpu().detach().numpy()
golden_signal = data[signal][n]
assert (
item_signal.shape == golden_signal.shape
), f'Signal {signal}: item shape {item_signal.shape} not matching reference shape {golden_signal.shape}'
assert np.allclose(
item_signal, golden_signal, atol=atol
), f'Test 1: Failed for example {n}, signal {signal} (random seed {random_seed})'
item_factory_signal = item_factory[signal].cpu().detach().numpy()
assert np.allclose(
item_factory_signal, golden_signal, atol=atol
), f'Test 1: Failed for factory example {n}, signal {signal} (random seed {random_seed})'
# Test 2
# - Use fixed duration (random segment selection)
# - Reference is synchronized with input and target, so the same segment of reference signal will be loaded
audio_duration = 4.0
audio_duration_samples = int(np.floor(audio_duration * sample_rate))
dataset = AudioToTargetWithReferenceDataset(
manifest_filepath=manifest_filepath,
input_key=data_key['input_signal'],
target_key=data_key['target_signal'],
reference_key=data_key['reference_signal'],
reference_is_synchronized=True,
sample_rate=sample_rate,
min_duration=audio_duration,
audio_duration=audio_duration,
random_offset=True,
)
filtered_examples = [n for n, val in enumerate(data_duration) if val >= audio_duration]
for n in range(len(dataset)):
item = dataset.__getitem__(n)
golden_start = golden_end = None
for signal in data:
item_signal = item[signal].cpu().detach().numpy()
full_golden_signal = data[signal][filtered_examples[n]]
# Find random segment using correlation on the first channel
# of the first signal, and then use it fixed for other signals
if golden_start is None:
golden_start = get_segment_start(signal=full_golden_signal[0, :], segment=item_signal[0, :])
golden_end = golden_start + audio_duration_samples
golden_signal = full_golden_signal[..., golden_start:golden_end]
# Test length is correct
assert (
item_signal.shape[-1] == audio_duration_samples
), f'Test 2: Signal {signal} length ({item_signal.shape[-1]}) not matching the expected length ({audio_duration_samples})'
# Test signal values
assert np.allclose(
item_signal, golden_signal, atol=atol
), f'Test 2: Failed for example {n}, signal {signal} (random seed {random_seed})'
# Test 3
# - Use fixed duration (random segment selection)
# - Reference is not synchronized with input and target, so whole reference signal will be loaded
audio_duration = 4.0
audio_duration_samples = int(np.floor(audio_duration * sample_rate))
dataset = AudioToTargetWithReferenceDataset(
manifest_filepath=manifest_filepath,
input_key=data_key['input_signal'],
target_key=data_key['target_signal'],
reference_key=data_key['reference_signal'],
reference_is_synchronized=False,
sample_rate=sample_rate,
min_duration=audio_duration,
audio_duration=audio_duration,
random_offset=True,
)
filtered_examples = [n for n, val in enumerate(data_duration) if val >= audio_duration]
for n in range(len(dataset)):
item = dataset.__getitem__(n)
golden_start = golden_end = None
for signal in data:
item_signal = item[signal].cpu().detach().numpy()
full_golden_signal = data[signal][filtered_examples[n]]
if signal == 'reference_signal':
# Complete signal is loaded for reference
golden_signal = full_golden_signal
else:
# Find random segment using correlation on the first channel
# of the first signal, and then use it fixed for other signals
if golden_start is None:
golden_start = get_segment_start(
signal=full_golden_signal[0, :], segment=item_signal[0, :]
)
golden_end = golden_start + audio_duration_samples
golden_signal = full_golden_signal[..., golden_start:golden_end]
# Test length is correct
assert (
item_signal.shape[-1] == audio_duration_samples
), f'Test 3: Signal {signal} length ({item_signal.shape[-1]}) not matching the expected length ({audio_duration_samples})'
assert (
item_signal.shape == golden_signal.shape
), f'Signal {signal}: item shape {item_signal.shape} not matching reference shape {golden_signal.shape}'
# Test signal values
assert np.allclose(
item_signal, golden_signal, atol=atol
), f'Test 3: Failed for example {n}, signal {signal} (random seed {random_seed})'
# Test 4:
# - Test collate_fn
batch_size = 16
batch = [dataset.__getitem__(n) for n in range(batch_size)]
_ = dataset.collate_fn(batch)
@pytest.mark.unit
def test_audio_to_target_with_embedding_dataset(self):
"""Test AudioWithTargetWithEmbeddingDataset.
In this use case, each line of the manifest file has the following format:
```
{
'input_filepath': 'path/to/input.wav',
'target_filepath': 'path/to/path_to_target.wav',
'embedding_filepath': 'path/to/path_to_embedding.npy',
'duration': duration_of_input,
}
```
"""
# Data setup
random_seed = 42
sample_rate = 16000
num_examples = 25
data_num_channels = {
'input_signal': 4,
'target_signal': 2,
'embedding_vector': 1,
}
data_min_duration = 2.0
data_max_duration = 8.0
embedding_length = 64 # 64-dimensional embedding vector
data_key = {
'input_signal': 'input_filepath',
'target_signal': 'target_filepath',
'embedding_vector': 'embedding_filepath',
}
# Tolerance
atol = 1e-6
# Generate random signals
_rng = np.random.default_rng(seed=random_seed)
# Input and target signals have the same duration
data_duration = np.round(_rng.uniform(low=data_min_duration, high=data_max_duration, size=num_examples), 3)
data_duration_samples = np.floor(data_duration * sample_rate).astype(int)
data = dict()
for signal, num_channels in data_num_channels.items():
data[signal] = []
for n in range(num_examples):
data_length = embedding_length if signal == 'embedding_vector' else data_duration_samples[n]
if num_channels == 1:
random_signal = _rng.uniform(low=-0.5, high=0.5, size=(data_length))
else:
random_signal = _rng.uniform(low=-0.5, high=0.5, size=(num_channels, data_length))
data[signal].append(random_signal)
with tempfile.TemporaryDirectory() as test_dir:
# Build metadata for manifest
metadata = []
for n in range(num_examples):
meta = dict()
for signal in data:
if signal == 'embedding_vector':
signal_filename = f'{signal}_{n:02d}.npy'
np.save(os.path.join(test_dir, signal_filename), data[signal][n])
else:
# filenames
signal_filename = f'{signal}_{n:02d}.wav'
# write audio files
sf.write(os.path.join(test_dir, signal_filename), data[signal][n].T, sample_rate, 'float')
# update metadata
meta[data_key[signal]] = signal_filename
meta['duration'] = data_duration[n]
metadata.append(meta)
# Save manifest
manifest_filepath = os.path.join(test_dir, 'manifest.json')
write_manifest(manifest_filepath, metadata)
# Test 1
# - No constraints on channels or duration
dataset = AudioToTargetWithEmbeddingDataset(
manifest_filepath=manifest_filepath,
input_key=data_key['input_signal'],
target_key=data_key['target_signal'],
embedding_key=data_key['embedding_vector'],
sample_rate=sample_rate,
)
# Also test the corresponding factory
config = {
'manifest_filepath': manifest_filepath,
'input_key': data_key['input_signal'],
'target_key': data_key['target_signal'],
'embedding_key': data_key['embedding_vector'],
'sample_rate': sample_rate,
}
dataset_factory = audio_to_audio_dataset.get_audio_to_target_with_embedding_dataset(config)
for n in range(num_examples):
item = dataset.__getitem__(n)
item_factory = dataset_factory.__getitem__(n)
for signal in data:
item_signal = item[signal].cpu().detach().numpy()
golden_signal = data[signal][n]
assert (
item_signal.shape == golden_signal.shape
), f'Signal {signal}: item shape {item_signal.shape} not matching reference shape {golden_signal.shape}'
assert np.allclose(
item_signal, golden_signal, atol=atol
), f'Test 1: Failed for example {n}, signal {signal} (random seed {random_seed})'
item_factory_signal = item_factory[signal].cpu().detach().numpy()
assert np.allclose(
item_factory_signal, golden_signal, atol=atol
), f'Test 1: Failed for factory example {n}, signal {signal} (random seed {random_seed})'
# Test 2:
# - Test collate_fn
batch_size = 16
batch = [dataset.__getitem__(n) for n in range(batch_size)]
_ = dataset.collate_fn(batch)
|