From 140a156ea3ab68ed0d3a77ff31c2fe54d5516e4c Mon Sep 17 00:00:00 2001 From: Vincent Quenneville-Belair Date: Fri, 16 Oct 2020 16:18:55 -0700 Subject: [PATCH 01/11] add speechcommand split. --- torchaudio/datasets/speechcommands.py | 32 ++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/torchaudio/datasets/speechcommands.py b/torchaudio/datasets/speechcommands.py index 7c56a846b9..dabe651d0f 100644 --- a/torchaudio/datasets/speechcommands.py +++ b/torchaudio/datasets/speechcommands.py @@ -48,13 +48,18 @@ class SPEECHCOMMANDS(Dataset): The top-level directory of the dataset. (default: ``"SpeechCommands"``) download (bool, optional): Whether to download the dataset if it is not found at root path. (default: ``False``). + split (str): + Select a subset of the dataset ["all", "training", "validation", "testing"]. + (default: ``"all"``) """ def __init__(self, root: str, url: str = URL, folder_in_archive: str = FOLDER_IN_ARCHIVE, - download: bool = False) -> None: + download: bool = False, + split: str = "all", + ) -> None: if url in [ "speech_commands_v0.01", "speech_commands_v0.02", @@ -79,8 +84,29 @@ def __init__(self, download_url(url, root, hash_value=checksum, hash_type="md5") extract_archive(archive, self._path) - walker = walk_files(self._path, suffix=".wav", prefix=True) - walker = filter(lambda w: HASH_DIVIDER in w and EXCEPT_FOLDER not in w, walker) + if split in ["training", "validation"]: + filepath = os.path.join(self._path, "validation_list.txt") + with open(filepath) as f: + validation_list = f.readline() + + if split in ["training", "testing"]: + filepath = os.path.join(self._path, "testing_list.txt") + with open(filepath) as f: + testing_list = f.readline() + + if split == "validation": + walker = validation_list + elif split == "testing": + walker = testing_list + else: + walker = walk_files(self._path, suffix=".wav", prefix=True) + walker = filter(lambda w: HASH_DIVIDER in w and EXCEPT_FOLDER not in w, walker) + + if split == "training": + walker = filter( + lambda w: not (w in validation_list or w in testing_list), walker + ) + self._walker = list(walker) def __getitem__(self, n: int) -> Tuple[Tensor, int, str, str, int]: From b3b723ce2abc2325543d2842870ad90ed0feb587 Mon Sep 17 00:00:00 2001 From: Vincent Quenneville-Belair Date: Fri, 16 Oct 2020 16:31:59 -0700 Subject: [PATCH 02/11] add split test. --- .../datasets/speechcommands_test.py | 73 ++++++++++++------- 1 file changed, 47 insertions(+), 26 deletions(-) diff --git a/test/torchaudio_unittest/datasets/speechcommands_test.py b/test/torchaudio_unittest/datasets/speechcommands_test.py index 7df1ea2007..ef4e884e94 100644 --- a/test/torchaudio_unittest/datasets/speechcommands_test.py +++ b/test/torchaudio_unittest/datasets/speechcommands_test.py @@ -63,33 +63,37 @@ def setUp(cls): os.makedirs(dataset_dir, exist_ok=True) sample_rate = 16000 # 16kHz sample rate seed = 0 - for label in LABELS: - path = os.path.join(dataset_dir, label) - os.makedirs(path, exist_ok=True) - for j in range(2): - # generate hash ID for speaker - speaker = "{:08x}".format(j) + txt_file = os.path.join(dataset_dir, "testing_list.txt") + with open(txt_file, "w") as txt: + for label in LABELS: + path = os.path.join(dataset_dir, label) + os.makedirs(path, exist_ok=True) + for j in range(2): + # generate hash ID for speaker + speaker = "{:08x}".format(j) - for utterance in range(3): - filename = f"{speaker}{speechcommands.HASH_DIVIDER}{utterance}.wav" - file_path = os.path.join(path, filename) - seed += 1 - data = get_whitenoise( - sample_rate=sample_rate, - duration=0.01, - n_channels=1, - dtype="int16", - seed=seed, - ) - save_wav(file_path, data, sample_rate) - sample = ( - normalize_wav(data), - sample_rate, - label, - speaker, - utterance, - ) - cls.samples.append(sample) + for utterance in range(3): + filename = f"{speaker}{speechcommands.HASH_DIVIDER}{utterance}.wav" + file_path = os.path.join(path, filename) + seed += 1 + data = get_whitenoise( + sample_rate=sample_rate, + duration=0.01, + n_channels=1, + dtype="int16", + seed=seed, + ) + save_wav(file_path, data, sample_rate) + sample = ( + normalize_wav(data), + sample_rate, + label, + speaker, + utterance, + ) + cls.samples.append(sample) + label_filename = os.path.join(label, filename) + txt.write(f'{label_filename}\n') def testSpeechCommands(self): dataset = speechcommands.SPEECHCOMMANDS(self.root_dir) @@ -107,3 +111,20 @@ def testSpeechCommands(self): num_samples += 1 assert num_samples == len(self.samples) + + def testSpeechCommandsSplit(self): + dataset = speechcommands.SPEECHCOMMANDS(self.root_dir, split="testing") + print(dataset._path) + + num_samples = 0 + for i, (data, sample_rate, label, speaker_id, utterance_number) in enumerate( + dataset + ): + self.assertEqual(data, self.samples[i][0], atol=5e-5, rtol=1e-8) + assert sample_rate == self.samples[i][1] + assert label == self.samples[i][2] + assert speaker_id == self.samples[i][3] + assert utterance_number == self.samples[i][4] + num_samples += 1 + + assert len(dataset) == len(self.samples) From 51921bd03e8c6ea209b4804d4ff9f22f4ea90a07 Mon Sep 17 00:00:00 2001 From: Vincent Quenneville-Belair Date: Fri, 16 Oct 2020 16:36:33 -0700 Subject: [PATCH 03/11] add error. --- torchaudio/datasets/speechcommands.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/torchaudio/datasets/speechcommands.py b/torchaudio/datasets/speechcommands.py index dabe651d0f..1368900cfb 100644 --- a/torchaudio/datasets/speechcommands.py +++ b/torchaudio/datasets/speechcommands.py @@ -98,9 +98,13 @@ def __init__(self, walker = validation_list elif split == "testing": walker = testing_list - else: + elif split in ["training", "all"]: walker = walk_files(self._path, suffix=".wav", prefix=True) walker = filter(lambda w: HASH_DIVIDER in w and EXCEPT_FOLDER not in w, walker) + else: + raise ValueError( + f'Expected "split" to be one of "all, "training", "validation", "testing", but got "f{split}".' + ) if split == "training": walker = filter( From fc1837fe4e8e527a57c7b27789e6d663c2e850f1 Mon Sep 17 00:00:00 2001 From: Vincent Quenneville-Belair Date: Fri, 16 Oct 2020 16:48:00 -0700 Subject: [PATCH 04/11] align with GTZAN on naming. --- .../datasets/speechcommands_test.py | 4 +- torchaudio/datasets/speechcommands.py | 41 ++++++++++--------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/test/torchaudio_unittest/datasets/speechcommands_test.py b/test/torchaudio_unittest/datasets/speechcommands_test.py index ef4e884e94..d8dbc1ccb5 100644 --- a/test/torchaudio_unittest/datasets/speechcommands_test.py +++ b/test/torchaudio_unittest/datasets/speechcommands_test.py @@ -112,8 +112,8 @@ def testSpeechCommands(self): assert num_samples == len(self.samples) - def testSpeechCommandsSplit(self): - dataset = speechcommands.SPEECHCOMMANDS(self.root_dir, split="testing") + def testSpeechCommandsSubset(self): + dataset = speechcommands.SPEECHCOMMANDS(self.root_dir, subset="testing") print(dataset._path) num_samples = 0 diff --git a/torchaudio/datasets/speechcommands.py b/torchaudio/datasets/speechcommands.py index 1368900cfb..eee2dc0b08 100644 --- a/torchaudio/datasets/speechcommands.py +++ b/torchaudio/datasets/speechcommands.py @@ -1,5 +1,5 @@ import os -from typing import Tuple +from typing import Tuple, Optional import torchaudio from torch.utils.data import Dataset @@ -48,9 +48,9 @@ class SPEECHCOMMANDS(Dataset): The top-level directory of the dataset. (default: ``"SpeechCommands"``) download (bool, optional): Whether to download the dataset if it is not found at root path. (default: ``False``). - split (str): - Select a subset of the dataset ["all", "training", "validation", "testing"]. - (default: ``"all"``) + subset (Optional[str]): + Select a subset of the dataset [None, "training", "validation", "testing"]. + (default: ``None``) """ def __init__(self, @@ -58,8 +58,14 @@ def __init__(self, url: str = URL, folder_in_archive: str = FOLDER_IN_ARCHIVE, download: bool = False, - split: str = "all", + subset: str = None, ) -> None: + + assert subset is None or subset in ["training", "validation", "testing"], ( + "When `subset` not None, it must take a value from " + + "{'training', 'validation', 'testing'}." + ) + if url in [ "speech_commands_v0.01", "speech_commands_v0.02", @@ -84,29 +90,24 @@ def __init__(self, download_url(url, root, hash_value=checksum, hash_type="md5") extract_archive(archive, self._path) - if split in ["training", "validation"]: + walker = walk_files(self._path, suffix=".wav", prefix=True) + walker = filter(lambda w: HASH_DIVIDER in w and EXCEPT_FOLDER not in w, walker) + + if subset in ["training", "validation"]: filepath = os.path.join(self._path, "validation_list.txt") with open(filepath) as f: - validation_list = f.readline() + validation_list = [os.path.join(self._path, l.strip()) for l in f.readlines()] - if split in ["training", "testing"]: + if subset in ["training", "testing"]: filepath = os.path.join(self._path, "testing_list.txt") with open(filepath) as f: - testing_list = f.readline() + testing_list = [os.path.join(self._path, l.strip()) for l in f.readlines()] - if split == "validation": + if subset == "validation": walker = validation_list - elif split == "testing": + elif subset == "testing": walker = testing_list - elif split in ["training", "all"]: - walker = walk_files(self._path, suffix=".wav", prefix=True) - walker = filter(lambda w: HASH_DIVIDER in w and EXCEPT_FOLDER not in w, walker) - else: - raise ValueError( - f'Expected "split" to be one of "all, "training", "validation", "testing", but got "f{split}".' - ) - - if split == "training": + elif subset == "training": walker = filter( lambda w: not (w in validation_list or w in testing_list), walker ) From 7c28a2e8cf140570381fd7970f2456262c9b5cef Mon Sep 17 00:00:00 2001 From: Vincent Quenneville-Belair Date: Fri, 23 Oct 2020 17:31:54 -0400 Subject: [PATCH 05/11] extend tests. --- .../datasets/speechcommands_test.py | 45 ++++++++++++++----- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/test/torchaudio_unittest/datasets/speechcommands_test.py b/test/torchaudio_unittest/datasets/speechcommands_test.py index d8dbc1ccb5..22c92d8c76 100644 --- a/test/torchaudio_unittest/datasets/speechcommands_test.py +++ b/test/torchaudio_unittest/datasets/speechcommands_test.py @@ -53,6 +53,8 @@ class TestSpeechCommands(TempDirMixin, TorchaudioTestCase): root_dir = None samples = [] + valid_samples = [] + test_samples = [] @classmethod def setUp(cls): @@ -63,12 +65,13 @@ def setUp(cls): os.makedirs(dataset_dir, exist_ok=True) sample_rate = 16000 # 16kHz sample rate seed = 0 - txt_file = os.path.join(dataset_dir, "testing_list.txt") - with open(txt_file, "w") as txt: + valid_file = os.path.join(dataset_dir, "validation_list.txt") + test_file = os.path.join(dataset_dir, "testing_list.txt") + with open(valid_file, "w") as valid, open(test_file, "w") as test: for label in LABELS: path = os.path.join(dataset_dir, label) os.makedirs(path, exist_ok=True) - for j in range(2): + for j in range(6): # generate hash ID for speaker speaker = "{:08x}".format(j) @@ -93,7 +96,12 @@ def setUp(cls): ) cls.samples.append(sample) label_filename = os.path.join(label, filename) - txt.write(f'{label_filename}\n') + if 2 <= j < 4: + valid.write(f'{label_filename}\n') + cls.valid_samples.append(sample) + elif 4 <= j < 6: + test.write(f'{label_filename}\n') + cls.test_samples.append(sample) def testSpeechCommands(self): dataset = speechcommands.SPEECHCOMMANDS(self.root_dir) @@ -112,6 +120,23 @@ def testSpeechCommands(self): assert num_samples == len(self.samples) + def testSpeechCommandsSubsetValid(self): + dataset = speechcommands.SPEECHCOMMANDS(self.root_dir, subset="validation") + print(dataset._path) + + num_samples = 0 + for i, (data, sample_rate, label, speaker_id, utterance_number) in enumerate( + dataset + ): + self.assertEqual(data, self.valid_samples[i][0], atol=5e-5, rtol=1e-8) + assert sample_rate == self.valid_samples[i][1] + assert label == self.valid_samples[i][2] + assert speaker_id == self.valid_samples[i][3] + assert utterance_number == self.valid_samples[i][4] + num_samples += 1 + + assert num_samples == len(self.valid_samples) + def testSpeechCommandsSubset(self): dataset = speechcommands.SPEECHCOMMANDS(self.root_dir, subset="testing") print(dataset._path) @@ -120,11 +145,11 @@ def testSpeechCommandsSubset(self): for i, (data, sample_rate, label, speaker_id, utterance_number) in enumerate( dataset ): - self.assertEqual(data, self.samples[i][0], atol=5e-5, rtol=1e-8) - assert sample_rate == self.samples[i][1] - assert label == self.samples[i][2] - assert speaker_id == self.samples[i][3] - assert utterance_number == self.samples[i][4] + self.assertEqual(data, self.test_samples[i][0], atol=5e-5, rtol=1e-8) + assert sample_rate == self.test_samples[i][1] + assert label == self.test_samples[i][2] + assert speaker_id == self.test_samples[i][3] + assert utterance_number == self.test_samples[i][4] num_samples += 1 - assert len(dataset) == len(self.samples) + assert num_samples == len(self.test_samples) From cb228174b4470b199384275a52880930375bb60c Mon Sep 17 00:00:00 2001 From: Vincent Quenneville-Belair Date: Sat, 24 Oct 2020 00:17:40 -0400 Subject: [PATCH 06/11] add train test. fix setUpClass which should be the class method. --- .../datasets/speechcommands_test.py | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/test/torchaudio_unittest/datasets/speechcommands_test.py b/test/torchaudio_unittest/datasets/speechcommands_test.py index 22c92d8c76..c1f3fe28b0 100644 --- a/test/torchaudio_unittest/datasets/speechcommands_test.py +++ b/test/torchaudio_unittest/datasets/speechcommands_test.py @@ -53,11 +53,12 @@ class TestSpeechCommands(TempDirMixin, TorchaudioTestCase): root_dir = None samples = [] + train_samples = [] valid_samples = [] test_samples = [] @classmethod - def setUp(cls): + def setUpClass(cls): cls.root_dir = cls.get_base_temp_dir() dataset_dir = os.path.join( cls.root_dir, speechcommands.FOLDER_IN_ARCHIVE, speechcommands.URL @@ -102,6 +103,8 @@ def setUp(cls): elif 4 <= j < 6: test.write(f'{label_filename}\n') cls.test_samples.append(sample) + else: + cls.train_samples.append(sample) def testSpeechCommands(self): dataset = speechcommands.SPEECHCOMMANDS(self.root_dir) @@ -120,6 +123,22 @@ def testSpeechCommands(self): assert num_samples == len(self.samples) + def testSpeechCommandsSubsetTrain(self): + dataset = speechcommands.SPEECHCOMMANDS(self.root_dir, subset="training") + + num_samples = 0 + for i, (data, sample_rate, label, speaker_id, utterance_number) in enumerate( + dataset + ): + self.assertEqual(data, self.train_samples[i][0], atol=5e-5, rtol=1e-8) + assert sample_rate == self.train_samples[i][1] + assert label == self.train_samples[i][2] + assert speaker_id == self.train_samples[i][3] + assert utterance_number == self.train_samples[i][4] + num_samples += 1 + + assert num_samples == len(self.train_samples) + def testSpeechCommandsSubsetValid(self): dataset = speechcommands.SPEECHCOMMANDS(self.root_dir, subset="validation") print(dataset._path) From 5b38342a2dbeaa782df60f762f1e3182f1174018 Mon Sep 17 00:00:00 2001 From: Vincent Quenneville-Belair Date: Mon, 26 Oct 2020 10:58:22 -0400 Subject: [PATCH 07/11] change type. --- torchaudio/datasets/speechcommands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/torchaudio/datasets/speechcommands.py b/torchaudio/datasets/speechcommands.py index eee2dc0b08..52f0c769ef 100644 --- a/torchaudio/datasets/speechcommands.py +++ b/torchaudio/datasets/speechcommands.py @@ -58,7 +58,7 @@ def __init__(self, url: str = URL, folder_in_archive: str = FOLDER_IN_ARCHIVE, download: bool = False, - subset: str = None, + subset: Optional[str] = None, ) -> None: assert subset is None or subset in ["training", "validation", "testing"], ( From 8be27adbb6fd44c31d31b9632f5444827fd5b876 Mon Sep 17 00:00:00 2001 From: Vincent Quenneville-Belair Date: Mon, 26 Oct 2020 17:06:02 -0400 Subject: [PATCH 08/11] feedback. --- .../datasets/speechcommands_test.py | 17 ++++----- torchaudio/datasets/speechcommands.py | 38 +++++++++---------- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/test/torchaudio_unittest/datasets/speechcommands_test.py b/test/torchaudio_unittest/datasets/speechcommands_test.py index c1f3fe28b0..6498976227 100644 --- a/test/torchaudio_unittest/datasets/speechcommands_test.py +++ b/test/torchaudio_unittest/datasets/speechcommands_test.py @@ -96,15 +96,14 @@ def setUpClass(cls): utterance, ) cls.samples.append(sample) - label_filename = os.path.join(label, filename) - if 2 <= j < 4: - valid.write(f'{label_filename}\n') + if j < 2: + cls.train_samples.append(sample) + elif j < 4: + valid.write(f'{label}/{filename}\n') cls.valid_samples.append(sample) - elif 4 <= j < 6: - test.write(f'{label_filename}\n') + elif j < 6: + test.write(f'{label}/{filename}\n') cls.test_samples.append(sample) - else: - cls.train_samples.append(sample) def testSpeechCommands(self): dataset = speechcommands.SPEECHCOMMANDS(self.root_dir) @@ -141,7 +140,6 @@ def testSpeechCommandsSubsetTrain(self): def testSpeechCommandsSubsetValid(self): dataset = speechcommands.SPEECHCOMMANDS(self.root_dir, subset="validation") - print(dataset._path) num_samples = 0 for i, (data, sample_rate, label, speaker_id, utterance_number) in enumerate( @@ -156,9 +154,8 @@ def testSpeechCommandsSubsetValid(self): assert num_samples == len(self.valid_samples) - def testSpeechCommandsSubset(self): + def testSpeechCommandsSubsetTest(self): dataset = speechcommands.SPEECHCOMMANDS(self.root_dir, subset="testing") - print(dataset._path) num_samples = 0 for i, (data, sample_rate, label, speaker_id, utterance_number) in enumerate( diff --git a/torchaudio/datasets/speechcommands.py b/torchaudio/datasets/speechcommands.py index 52f0c769ef..7333878b4f 100644 --- a/torchaudio/datasets/speechcommands.py +++ b/torchaudio/datasets/speechcommands.py @@ -49,8 +49,8 @@ class SPEECHCOMMANDS(Dataset): download (bool, optional): Whether to download the dataset if it is not found at root path. (default: ``False``). subset (Optional[str]): - Select a subset of the dataset [None, "training", "validation", "testing"]. - (default: ``None``) + Select a subset of the dataset [None, "training", "validation", "testing"]. None means + the whole dataset. (default: ``None``) """ def __init__(self, @@ -90,29 +90,25 @@ def __init__(self, download_url(url, root, hash_value=checksum, hash_type="md5") extract_archive(archive, self._path) - walker = walk_files(self._path, suffix=".wav", prefix=True) - walker = filter(lambda w: HASH_DIVIDER in w and EXCEPT_FOLDER not in w, walker) - - if subset in ["training", "validation"]: - filepath = os.path.join(self._path, "validation_list.txt") - with open(filepath) as f: - validation_list = [os.path.join(self._path, l.strip()) for l in f.readlines()] - - if subset in ["training", "testing"]: - filepath = os.path.join(self._path, "testing_list.txt") - with open(filepath) as f: - testing_list = [os.path.join(self._path, l.strip()) for l in f.readlines()] + def _load_list(*filenames): + output = [] + for filename in filenames: + filepath = os.path.join(self._path, filename) + with open(filepath) as fileobj: + output += [os.path.join(self._path, line.strip()) for line in fileobj] + return output if subset == "validation": - walker = validation_list + self._walker = _load_list("validation_list.txt") elif subset == "testing": - walker = testing_list + self._walker = _load_list("testing_list.txt") elif subset == "training": - walker = filter( - lambda w: not (w in validation_list or w in testing_list), walker - ) - - self._walker = list(walker) + excludes = set(_load_list("validation_list.txt", "testing_list.txt")) + walker = walk_files(self._path, suffix=".wav", prefix=True) + self._walker = [w for w in walker if HASH_DIVIDER in w and EXCEPT_FOLDER not in w and w not in excludes] + else: + walker = walk_files(self._path, suffix=".wav", prefix=True) + self._walker = [w for w in walker if HASH_DIVIDER in w and EXCEPT_FOLDER not in w] def __getitem__(self, n: int) -> Tuple[Tensor, int, str, str, int]: """Load the n-th sample from the dataset. From c3b1911675e835460b99049012eef9014a2704f9 Mon Sep 17 00:00:00 2001 From: Vincent Quenneville-Belair Date: Tue, 27 Oct 2020 10:24:29 -0400 Subject: [PATCH 09/11] add sum test. normalize path. none test. --- .../datasets/speechcommands_test.py | 25 ++++++++++++++++++- torchaudio/datasets/speechcommands.py | 9 +++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/test/torchaudio_unittest/datasets/speechcommands_test.py b/test/torchaudio_unittest/datasets/speechcommands_test.py index 6498976227..5a331bdeca 100644 --- a/test/torchaudio_unittest/datasets/speechcommands_test.py +++ b/test/torchaudio_unittest/datasets/speechcommands_test.py @@ -107,7 +107,22 @@ def setUpClass(cls): def testSpeechCommands(self): dataset = speechcommands.SPEECHCOMMANDS(self.root_dir) - print(dataset._path) + + num_samples = 0 + for i, (data, sample_rate, label, speaker_id, utterance_number) in enumerate( + dataset + ): + self.assertEqual(data, self.samples[i][0], atol=5e-5, rtol=1e-8) + assert sample_rate == self.samples[i][1] + assert label == self.samples[i][2] + assert speaker_id == self.samples[i][3] + assert utterance_number == self.samples[i][4] + num_samples += 1 + + assert num_samples == len(self.samples) + + def testSpeechCommandsNone(self): + dataset = speechcommands.SPEECHCOMMANDS(self.root_dir, subset=None) num_samples = 0 for i, (data, sample_rate, label, speaker_id, utterance_number) in enumerate( @@ -169,3 +184,11 @@ def testSpeechCommandsSubsetTest(self): num_samples += 1 assert num_samples == len(self.test_samples) + + def testSpeechCommandsSum(self): + dataset_all = speechcommands.SPEECHCOMMANDS(self.root_dir) + dataset_train = speechcommands.SPEECHCOMMANDS(self.root_dir, subset="training") + dataset_valid = speechcommands.SPEECHCOMMANDS(self.root_dir, subset="validation") + dataset_test = speechcommands.SPEECHCOMMANDS(self.root_dir, subset="testing") + + assert len(dataset_train) + len(dataset_valid) + len(dataset_test) == len(dataset_all) diff --git a/torchaudio/datasets/speechcommands.py b/torchaudio/datasets/speechcommands.py index 7333878b4f..0551651365 100644 --- a/torchaudio/datasets/speechcommands.py +++ b/torchaudio/datasets/speechcommands.py @@ -95,7 +95,7 @@ def _load_list(*filenames): for filename in filenames: filepath = os.path.join(self._path, filename) with open(filepath) as fileobj: - output += [os.path.join(self._path, line.strip()) for line in fileobj] + output += [os.path.normpath(os.path.join(self._path, line.strip())) for line in fileobj] return output if subset == "validation": @@ -105,7 +105,12 @@ def _load_list(*filenames): elif subset == "training": excludes = set(_load_list("validation_list.txt", "testing_list.txt")) walker = walk_files(self._path, suffix=".wav", prefix=True) - self._walker = [w for w in walker if HASH_DIVIDER in w and EXCEPT_FOLDER not in w and w not in excludes] + self._walker = [ + w for w in walker + if HASH_DIVIDER in w + and EXCEPT_FOLDER not in w + and os.path.normpath(w) not in excludes + ] else: walker = walk_files(self._path, suffix=".wav", prefix=True) self._walker = [w for w in walker if HASH_DIVIDER in w and EXCEPT_FOLDER not in w] From 355ee4ce8c343de4477778e79ce454fabcdf7f8d Mon Sep 17 00:00:00 2001 From: Vincent Quenneville-Belair Date: Tue, 27 Oct 2020 11:21:39 -0400 Subject: [PATCH 10/11] move _load_list. --- torchaudio/datasets/speechcommands.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/torchaudio/datasets/speechcommands.py b/torchaudio/datasets/speechcommands.py index 0551651365..c05e05496e 100644 --- a/torchaudio/datasets/speechcommands.py +++ b/torchaudio/datasets/speechcommands.py @@ -22,6 +22,15 @@ } +def _load_list(root, *filenames): + output = [] + for filename in filenames: + filepath = os.path.join(root, filename) + with open(filepath) as fileobj: + output += [os.path.normpath(os.path.join(root, line.strip())) for line in fileobj] + return output + + def load_speechcommands_item(filepath: str, path: str) -> Tuple[Tensor, int, str, str, int]: relpath = os.path.relpath(filepath, path) label, filename = os.path.split(relpath) @@ -90,20 +99,12 @@ def __init__(self, download_url(url, root, hash_value=checksum, hash_type="md5") extract_archive(archive, self._path) - def _load_list(*filenames): - output = [] - for filename in filenames: - filepath = os.path.join(self._path, filename) - with open(filepath) as fileobj: - output += [os.path.normpath(os.path.join(self._path, line.strip())) for line in fileobj] - return output - if subset == "validation": - self._walker = _load_list("validation_list.txt") + self._walker = _load_list(self._path, "validation_list.txt") elif subset == "testing": - self._walker = _load_list("testing_list.txt") + self._walker = _load_list(self._path, "testing_list.txt") elif subset == "training": - excludes = set(_load_list("validation_list.txt", "testing_list.txt")) + excludes = set(_load_list(self._path, "validation_list.txt", "testing_list.txt")) walker = walk_files(self._path, suffix=".wav", prefix=True) self._walker = [ w for w in walker From 3e7a071765cc3fcd0ffeebf1f9785f0abe5ea5b8 Mon Sep 17 00:00:00 2001 From: Vincent Quenneville-Belair Date: Tue, 27 Oct 2020 11:37:56 -0400 Subject: [PATCH 11/11] update docstring. --- torchaudio/datasets/speechcommands.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/torchaudio/datasets/speechcommands.py b/torchaudio/datasets/speechcommands.py index c05e05496e..2bcd2d199f 100644 --- a/torchaudio/datasets/speechcommands.py +++ b/torchaudio/datasets/speechcommands.py @@ -59,7 +59,8 @@ class SPEECHCOMMANDS(Dataset): Whether to download the dataset if it is not found at root path. (default: ``False``). subset (Optional[str]): Select a subset of the dataset [None, "training", "validation", "testing"]. None means - the whole dataset. (default: ``None``) + the whole dataset. "validation" and "testing" are defined in "validation_list.txt" and + "testing_list.txt", respectively, and "training" is the rest. (default: ``None``) """ def __init__(self,