speechcommands_test.py 4.79 KB
Newer Older
1
import os
2
from pathlib import Path
3
4
5

from torchaudio.datasets import speechcommands

6
from torchaudio_unittest.common_utils import (
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
    TempDirMixin,
    TorchaudioTestCase,
    get_whitenoise,
    normalize_wav,
    save_wav,
)

LABELS = [
    "bed",
    "bird",
    "cat",
    "dog",
    "down",
    "eight",
    "five",
    "follow",
    "forward",
    "four",
    "go",
    "happy",
    "house",
    "learn",
    "left",
    "marvin",
    "nine",
    "no",
    "off",
    "on",
    "one",
    "right",
    "seven",
    "sheila",
    "six",
    "stop",
    "three",
    "tree",
    "two",
    "up",
    "visual",
    "wow",
    "yes",
    "zero",
]


class TestSpeechCommands(TempDirMixin, TorchaudioTestCase):
    backend = "default"

    root_dir = None
    samples = []
57
58
59
    train_samples = []
    valid_samples = []
    test_samples = []
60
61

    @classmethod
62
    def setUpClass(cls):
63
64
65
66
67
68
69
        cls.root_dir = cls.get_base_temp_dir()
        dataset_dir = os.path.join(
            cls.root_dir, speechcommands.FOLDER_IN_ARCHIVE, speechcommands.URL
        )
        os.makedirs(dataset_dir, exist_ok=True)
        sample_rate = 16000  # 16kHz sample rate
        seed = 0
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
        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(6):
                    # 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)
                        if j < 2:
                            cls.train_samples.append(sample)
                        elif j < 4:
                            valid.write(f'{label}/{filename}\n')
                            cls.valid_samples.append(sample)
                        elif j < 6:
                            test.write(f'{label}/{filename}\n')
                            cls.test_samples.append(sample)
108

109
    def _testSpeechCommands(self, dataset, data_samples):
110
111
112
113
        num_samples = 0
        for i, (data, sample_rate, label, speaker_id, utterance_number) in enumerate(
            dataset
        ):
114
115
116
117
118
            self.assertEqual(data, data_samples[i][0], atol=5e-5, rtol=1e-8)
            assert sample_rate == data_samples[i][1]
            assert label == data_samples[i][2]
            assert speaker_id == data_samples[i][3]
            assert utterance_number == data_samples[i][4]
119
120
            num_samples += 1

121
        assert num_samples == len(data_samples)
122

123
124
125
    def testSpeechCommands_str(self):
        dataset = speechcommands.SPEECHCOMMANDS(self.root_dir)
        self._testSpeechCommands(dataset, self.samples)
126

127
128
129
    def testSpeechCommands_path(self):
        dataset = speechcommands.SPEECHCOMMANDS(Path(self.root_dir))
        self._testSpeechCommands(dataset, self.samples)
130
131
132

    def testSpeechCommandsSubsetTrain(self):
        dataset = speechcommands.SPEECHCOMMANDS(self.root_dir, subset="training")
133
        self._testSpeechCommands(dataset, self.train_samples)
134
135
136

    def testSpeechCommandsSubsetValid(self):
        dataset = speechcommands.SPEECHCOMMANDS(self.root_dir, subset="validation")
137
        self._testSpeechCommands(dataset, self.valid_samples)
138
139
140

    def testSpeechCommandsSubsetTest(self):
        dataset = speechcommands.SPEECHCOMMANDS(self.root_dir, subset="testing")
141
        self._testSpeechCommands(dataset, self.test_samples)
142
143
144
145
146
147
148
149

    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)