-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasets.py
273 lines (213 loc) · 10.8 KB
/
datasets.py
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
# TODO: Remove redundant squeeze unsqueeze
import os
import torch
import torchaudio
from models import GE2E_config
from torch.utils.data import Dataset
class FeatureExtractorDataset(Dataset):
def __init__(self, model_name, data_dir, split):
self.data_dir = os.path.join(data_dir,split)
self.model = model_name
self._index_audios()
self.sampling_rate = 16000
_, org_sample_rate = torchaudio.load(self.audio_files[0])
self.resample_transform = torchaudio.transforms.Resample(org_sample_rate, self.sampling_rate)
if self.model == 'GE2E':
self.mel_transform = torchaudio.transforms.MelSpectrogram(
sample_rate = self.sampling_rate,
n_fft = int(self.sampling_rate * GE2E_config.mel_window_length / 1000),
hop_length = int(self.sampling_rate * GE2E_config.mel_window_step / 1000),
n_mels = GE2E_config.mel_n_channels)
def _index_audios(self):
# Gather all audio file locations
self.audio_files = []
for audiofile in os.listdir(self.data_dir):
filename = os.path.join(self.data_dir,audiofile)
self.audio_files.append(filename)
def __getitem__(self, idx):
file_name = self.audio_files[idx]
audio, sr = torchaudio.load(file_name)
if sr != self.sampling_rate: audio = self.resample_transform(audio)
if audio.size(0)>1: audio = audio.mean(dim=0)
audio = audio.squeeze()
if self.model == 'GE2E':
audio = self.mel_transform(audio).T
length = audio.size(0)
return audio, length, file_name
def __len__(self):
return len(self.audio_files)
def data_collator(self,batch):
audios, seq_lengths, file_paths = zip(*batch)
batch_size = len(batch)
max_seq_len = max(seq_lengths)
if self.model == 'GE2E':
num_channels = audios[0].size(1)
collated_batch = torch.zeros((batch_size, max_seq_len, num_channels))
for idx, sample in enumerate(audios):
collated_batch[idx] = torch.cat([sample, torch.zeros((max_seq_len - seq_lengths[idx], num_channels))])
else:
collated_batch = torch.zeros((batch_size, max_seq_len))
for idx, sample in enumerate(audios):
collated_batch[idx] = torch.cat([sample, torch.zeros((max_seq_len - seq_lengths[idx]))])
return collated_batch, file_paths
class CPUMemoryModeClassifierDataset(Dataset):
def __init__(self, fx_model_name, fx_model, data_dir, datainfo, split):
self.data_dir = os.path.join(data_dir,split)
self.device = 'cpu'
self.model_name = fx_model_name
self.model = fx_model
self.model.to(self.device)
self._index_audios()
self.sampling_rate = 16000
_, org_sample_rate = torchaudio.load(self.audio_files[0])
self.resample_transform = torchaudio.transforms.Resample(org_sample_rate, self.sampling_rate)
if self.model_name == 'GE2E':
self.mel_transform = torchaudio.transforms.MelSpectrogram(
sample_rate = self.sampling_rate,
n_fft = int(self.sampling_rate * GE2E_config.mel_window_length / 1000),
hop_length = int(self.sampling_rate * GE2E_config.mel_window_step / 1000),
n_mels = GE2E_config.mel_n_channels)
self.label_map = datainfo.label_map
self.label_indices = datainfo.label_indices
self.label_to_id = {k:i for k,i in zip(self.label_map.keys(),[*range(len(self.label_map))])}
self.id_to_label = {v:k for k,v in self.label_to_id.items()}
def _index_audios(self):
# Gather all audio file locations
self.audio_files = []
for audiofile in os.listdir(self.data_dir):
filename = os.path.join(self.data_dir,audiofile)
self.audio_files.append(filename)
def __getitem__(self, idx):
file_name = self.audio_files[idx]
audio, sr = torchaudio.load(file_name)
if sr != self.sampling_rate: audio = self.resample_transform(audio)
if audio.size(0)>1: audio = audio.mean(dim=0)
audio = audio.squeeze()
if self.model_name == 'GE2E':
audio = self.mel_transform(audio).T
audio = audio.to(self.device)
with torch.inference_mode():
if self.model_name == 'GE2E':
features = self.model(audio.unsqueeze(dim=0))
features = features.squeeze()
else:
features, _ = self.model.extract_features(audio.unsqueeze(dim=0))
features = torch.stack([*features],dim=1)
features = features.squeeze()
label = self.label_to_id[self.audio_files[idx].split('/')[-1][self.label_indices['from']:self.label_indices['to']]]
if self.model_name == 'GE2E': seq_length = len(features)
else: seq_length = features.shape[1]
return features, seq_length, label
def __len__(self):
return len(self.audio_files)
def data_collator(self,batch):
features, seq_lengths, labels = zip(*batch)
batch_size = len(batch)
max_seq_len = max(seq_lengths)
if self.model_name == 'GE2E':
padded_features = torch.zeros(batch_size, max_seq_len)
for idx, sample in enumerate(batch):
padded_features[idx] = torch.cat([sample[0], torch.zeros((max_seq_len - seq_lengths[idx]))])
else:
layers = features[0].shape[0]
feature_dim = features[0].shape[2]
padded_features = torch.zeros((batch_size, layers, max_seq_len, feature_dim))
for idx, sample in enumerate(batch):
padded_features[idx,:,:seq_lengths[idx], :] = features[idx]
labels = torch.tensor(labels)
seq_lengths = torch.tensor(seq_lengths)
return padded_features, seq_lengths, labels
class GPUMemoryModeClassifierDataset(Dataset):
def __init__(self, fx_model_name, data_dir, datainfo, split):
self.data_dir = os.path.join(data_dir,split)
self.device = 'cpu'
self.model_name = fx_model_name
self._index_audios()
self.sampling_rate = 16000
_, org_sample_rate = torchaudio.load(self.audio_files[0])
self.resample_transform = torchaudio.transforms.Resample(org_sample_rate, self.sampling_rate)
if self.model_name == 'GE2E':
self.mel_transform = torchaudio.transforms.MelSpectrogram(
sample_rate = self.sampling_rate,
n_fft = int(self.sampling_rate * GE2E_config.mel_window_length / 1000),
hop_length = int(self.sampling_rate * GE2E_config.mel_window_step / 1000),
n_mels = GE2E_config.mel_n_channels)
self.label_map = datainfo.label_map
self.label_indices = datainfo.label_indices
self.label_to_id = {k:i for k,i in zip(self.label_map.keys(),[*range(len(self.label_map))])}
self.id_to_label = {v:k for k,v in self.label_to_id.items()}
def _index_audios(self):
# Gather all audio file locations
self.audio_files = []
for audiofile in os.listdir(self.data_dir):
filename = os.path.join(self.data_dir,audiofile)
self.audio_files.append(filename)
def __getitem__(self, idx):
file_name = self.audio_files[idx]
audio, sr = torchaudio.load(file_name)
if sr != self.sampling_rate: audio = self.resample_transform(audio)
if audio.size(0)>1: audio = audio.mean(dim=0)
audio = audio.squeeze()
if self.model_name == 'GE2E':
audio = self.mel_transform(audio).T
length = audio.size(0)
label = self.label_to_id[self.audio_files[idx].split('/')[-1][self.label_indices['from']:self.label_indices['to']]]
return audio, length, label
def __len__(self):
return len(self.audio_files)
def data_collator(self,batch):
audios, seq_lengths, labels = zip(*batch)
batch_size = len(batch)
max_seq_len = max(seq_lengths)
if self.model_name == 'GE2E':
num_channels = audios[0].size(1)
collated_batch = torch.zeros((batch_size, max_seq_len, num_channels))
for idx, sample in enumerate(audios):
collated_batch[idx] = torch.cat([sample, torch.zeros((max_seq_len - seq_lengths[idx], num_channels))])
else:
collated_batch = torch.zeros((batch_size, max_seq_len))
for idx, sample in enumerate(audios):
collated_batch[idx] = torch.cat([sample, torch.zeros((max_seq_len - seq_lengths[idx]))])
labels = torch.tensor(labels)
seq_lengths = torch.tensor(seq_lengths)
return collated_batch, seq_lengths, labels
class GPUDiskModeClassifierDataset(Dataset):
def __init__(self, fx_model_name, data_dir, datainfo, split):
self.data_dir = os.path.join(data_dir,split)
self.fx_model = fx_model_name
self._index_features()
self.label_map = datainfo.label_map
self.label_indices = datainfo.label_indices
self.label_to_id = {k:i for k,i in zip(self.label_map.keys(),[*range(len(self.label_map))])}
self.id_to_label = {v:k for k,v in self.label_to_id.items()}
def _index_features(self):
# Gather all feature file locations
self.feature_files = []
for featurefile in os.listdir(self.data_dir):
filename = os.path.join(self.data_dir,featurefile)
self.feature_files.append(filename)
def __getitem__(self, idx):
feature = torch.load(self.feature_files[idx],map_location='cpu')
label = self.label_to_id[self.feature_files[idx].split('/')[-1][self.label_indices['from']:self.label_indices['to']]]
if self.fx_model == 'GE2E': seq_length = len(feature)
else: seq_length = feature.shape[1]
return feature, seq_length, label
def __len__(self):
return len(self.feature_files)
def data_collator(self,batch):
features, seq_lengths, labels = zip(*batch)
batch_size = len(batch)
max_seq_len = max(seq_lengths)
if self.fx_model == 'GE2E':
padded_features = torch.zeros(batch_size, max_seq_len)
for idx, sample in enumerate(features):
padded_features[idx] = torch.cat([sample, torch.zeros((max_seq_len - seq_lengths[idx]))])
else:
layers = features[0].shape[0]
feature_dim = features[0].shape[2]
padded_features = torch.zeros((batch_size, layers, max_seq_len, feature_dim))
for idx, sample in enumerate(batch):
padded_features[idx,:,:seq_lengths[idx], :] = features[idx]
labels = torch.tensor(labels)
seq_lengths = torch.tensor(seq_lengths)
return padded_features, seq_lengths, labels