audio_set.py 6.4 KB
Newer Older
Paul Primus's avatar
Paul Primus committed
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
import os
import torch.utils.data
import glob
from dcase2020_task2.data_sets import BaseDataSet, CLASS_MAP, INVERSE_CLASS_MAP, TRAINING_ID_MAP, EVALUATION_ID_MAP, ALL_ID_MAP,\
    enumerate_development_datasets, enumerate_evaluation_datasets
import librosa
import numpy as np
from dcase2020_task2.data_sets import MCMDataSet


class AudioSet(BaseDataSet):

    def __init__(
            self,
            data_root=os.path.join(os.path.expanduser('~'), 'shared', 'audioset', 'audiosetdata'),
            context=5,
            num_mel=128,
            n_fft=1024,
            hop_size=512,
            power=2.0,
            fmin=0,
            normalize_raw=True,
            hop_all=False
    ):
        self.data_root = data_root
        self.context = context
        self.num_mel = num_mel
        self.n_fft = n_fft
        self.hop_size = hop_size
        self.power = power
        self.fmin = fmin
        self.hop_all = hop_all
33
        self.normalize_raw = normalize_raw
Paul Primus's avatar
Paul Primus committed
34
35
36
37
38
39
40

        kwargs = {
            'data_root': self.data_root,
            'context': self.context,
            'num_mel': self.num_mel,
            'n_fft': self.n_fft,
            'hop_size': self.hop_size,
41
42
43
44
            'power': self.power,
            'normalize': self.normalize_raw,
            'fmin': self.fmin,
            'hop_all': self.hop_all
Paul Primus's avatar
Paul Primus committed
45
46
        }

47
        class_names = sorted([class_name for class_name in os.listdir(data_root) if os.path.isdir(os.path.join(data_root, class_name))])
Paul Primus's avatar
Paul Primus committed
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

        training_sets = []
        data_arrays = []
        for class_name in class_names:
            training_sets.append(AudioSetClassSubset(class_name, **kwargs))
            data = training_sets[-1].data
            for i, file in enumerate(data):
                data_arrays.append(file)
        data_arrays = np.concatenate(data_arrays, axis=-1)

        self.training_set = torch.utils.data.ConcatDataset(training_sets)
        self.validation_set = None
        self.mean = data_arrays.mean(axis=1, keepdims=True)
        self.std = data_arrays.std(axis=1, keepdims=True)

    @property
    def observation_shape(self) -> tuple:
        return 1, self.num_mel, self.context

    def training_data_set(self):
        return self.training_set

    def validation_data_set(self):
        return self.validation_set

    def mean_std(self):
        return self.mean, self.std


class AudioSetClassSubset(torch.utils.data.Dataset):

    def __init__(
            self,
            class_name,
            data_root=os.path.join(os.path.expanduser('~'), 'shared', 'audioset', 'audiosetdata'),
            context=5,
            num_mel=128,
            n_fft=1024,
            hop_size=512,
            power=2.0,
            normalize=True,
            fmin=0,
            hop_all=False,
91
            max_file_per_class=2,
Paul Primus's avatar
Paul Primus committed
92
93
94
95
96
97
98
99
100
101
102
103
104
            max_file_length=350
    ):

        self.num_mel = num_mel
        self.n_fft = n_fft
        self.hop_size = hop_size
        self.power = power
        self.normalize = normalize
        self.data_root = data_root
        self.context = context
        self.fmin = fmin
        self.hop_all = hop_all
        self.class_name = class_name
105
        self.max_file_per_class = max_file_per_class
Paul Primus's avatar
Paul Primus committed
106
107
108
109
110
111
        self.max_file_length = max_file_length

        files = glob.glob(os.path.join(data_root, class_name, '*.wav'))

        assert len(files) > 0

112
        files = sorted(files)[:max_file_per_class]
Paul Primus's avatar
Paul Primus committed
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
        self.files = files

        self.meta_data = self.__load_meta_data__(files)
        self.data = self.__load_data__(files)

        self.index_map = {}
        ctr = 0
        for i, file in enumerate(self.data):
            for j in range(file.shape[-1] + 1 - context):
                self.index_map[ctr] = (i, j)
                ctr += 1
        self.length = ctr

    def __getitem__(self, item):
        file_idx, offset = self.index_map[item]
        observation = self.data[file_idx][:, offset:offset + self.context]
        meta_data = self.meta_data[file_idx].copy()
        meta_data['observations'] = observation[None]

        return meta_data

    def __len__(self):
        return self.length

    def __load_meta_data__(self, files):
        data = []
        for f in files:
            md = self.__get_meta_data__(f)
            data.append(md)
        return data

    def __load_data__(self, files):
        file_name = "{}_{}_{}_{}_{}_{}_{}.npz".format(
            self.num_mel,
            self.n_fft,
            self.hop_size,
            self.power,
            self.normalize,
            self.fmin,
            self.class_name
        )
        file_path = os.path.join(self.data_root, file_name)

        data = []
        if os.path.exists(file_path):
            print('Loading audio set class {} '.format(self.class_name))
            container = np.load(file_path)
            data = [container[key] for key in container]
        else:
            print('Loading & saving audio set class {} '.format(self.class_name))
            for i, f in enumerate(files):
                file = self.__load_preprocess_file__(f)
                if file.shape[1] > self.max_file_length:
                    print(f'File too long: {f} - {file.shape[1]}')
                    file = file[:, :self.max_file_length]
                data.append(file)
            np.savez(file_path, *data)
        return data

    def __load_preprocess_file__(self, file):
        x, sr = librosa.load(file, sr=16000, mono=True)
        if len(x) > (self.max_file_length + 1 * self.hop_size) + self.n_fft:
            x = x[:(self.max_file_length + 1) * self.hop_size + self.n_fft]
176

Paul Primus's avatar
Paul Primus committed
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
        if self.normalize:
            x = (x - x.mean()) / x.std()

        x = librosa.feature.melspectrogram(
            y=x,
            sr=sr,
            n_fft=self.n_fft,
            hop_length=self.hop_size,
            n_mels=self.num_mel,
            power=self.power,
            fmin=self.fmin
        )

        if self.power == 1:
            x = librosa.core.amplitude_to_db(x)
        elif self.power == 2:
            x = librosa.core.power_to_db(x)
        else:
            raise AttributeError

        return x

    def __get_meta_data__(self, file_path):
        return {
            'targets': 1,
            'machine_types': -1,
            'machine_ids': -1,
            'file_ids': os.sep.join(os.path.normpath(file_path).split(os.sep)[-4:])
        }

207

Paul Primus's avatar
Paul Primus committed
208
209
210
211
212
213
214
215
216
217
if __name__ == '__main__':
    mcmc = MCMDataSet(0, 0)
    a = audio_set = AudioSet(
        normalize=(mcmc.mean, mcmc.std)
    ).training_data_set()[0]

    print(a)