forked from gzhu06/Cacophony
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_processors.py
289 lines (212 loc) · 9.91 KB
/
dataset_processors.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from abc import ABC, abstractmethod
from typing import Tuple, List
from tqdm import tqdm
import os, csv, glob, json
import pandas as pd
from dataclasses import dataclass
from eval_dataset_configs import VGGSoundConfig, TUTAS2017Config, \
ESC50Config, US8KConfig, AudioCaps16kConfig, Clotho16kConfig, BeatovenConfig
@dataclass
class DatasetProcessor(ABC):
@abstractmethod
def get_filepaths_and_descriptions(self) -> Tuple[List[str], List[List[str]], List[List[str]]]:
pass
class US8KProcessor(DatasetProcessor):
config = US8KConfig()
def get_filepaths_and_descriptions(self, current_split=''):
# init output lists
audio_filepath_list = []
text_dict = {}
synthetic_text_dict = {}
# load audio filepaths
existing_audiopaths = glob.glob(f'{self.config.data_dir}/**/*.wav', recursive=True)
# load meta json file
with open(os.path.join(self.config.data_dir, 'metadata', 'UrbanSound8K.csv'), 'r') as f:
csv_reader = csv.reader(f)
label_dict = {}
for i, row in enumerate(csv_reader):
if i == 0:
continue
label_dict[row[0].split('.wav')[0]] = row[-1].replace('_', ' ')
for audiofile in tqdm(existing_audiopaths[:]):
# get list of text captions
audio_name = audiofile.split('/')[-1].split('.wav')[0]
audio_filepath_list.append(audiofile)
# obtain description item # tags and title+text
text_captions = {}
text_captions['description'] = [label_dict[audio_name]]
text_dict[audio_name] = text_captions
return audio_filepath_list, text_dict, synthetic_text_dict
class ESC50Processor(DatasetProcessor):
# paired wav-json file
config = ESC50Config()
def get_filepaths_and_descriptions(self, current_split=''):
# init output lists
audio_filepath_list = []
text_dict = {}
synthetic_text_dict = {}
# load audio filepaths
existing_audiopaths = glob.glob(f'{self.config.data_dir}/*/*.wav')
# load meta json file
with open(os.path.join(self.config.data_dir, 'esc50.csv'), 'r') as f:
csv_reader = csv.reader(f)
label_dict = {}
for i, row in enumerate(csv_reader):
if i == 0:
continue
label_dict[row[0].split('.wav')[0]] = row[3]
for audiofile in tqdm(existing_audiopaths[:]):
# get list of text captions
audio_name = audiofile.split('/')[-1].split('.wav')[0]
audio_filepath_list.append(audiofile)
# obtain description item # tags and title+text
text_captions = {}
text_captions['description'] = [label_dict[audio_name]]
text_dict[audio_name] = text_captions
return audio_filepath_list, text_dict, synthetic_text_dict
class VGGSoundProcessor(DatasetProcessor):
# paired wav-json file
config = VGGSoundConfig()
def get_filepaths_and_descriptions(self, current_split='test'):
# init output lists
audio_filepath_list = []
text_dict = {}
synthetic_text_dict = {}
# load audio filepaths
existing_audiopaths = glob.glob(f'{self.config.data_dir}/test/*.wav', recursive=True)
# load meta json file
vgg_meta_file = os.path.join(self.config.data_dir, 'vggsound_full.json')
with open(vgg_meta_file, 'r') as f:
vgg_meta_dict = json.load(f)
for audiofile in tqdm(existing_audiopaths[:]):
# get list of text captions
audio_name = audiofile.split('/')[-1].split('.wav')[0]
if audio_name not in vgg_meta_dict:
continue
audio_filepath_list.append(audiofile)
# obtain description item # tags and title+text
text_captions = {}
text_captions['description'] = [vgg_meta_dict[audio_name]]
text_dict[audio_name] = text_captions
return audio_filepath_list, text_dict, synthetic_text_dict
class TUTAS2017Processor(DatasetProcessor):
config = TUTAS2017Config()
def get_filepaths_and_descriptions(self, current_split=''):
# init output lists
audio_filepath_list = []
text_dict = {}
synthetic_text_dict = {}
# load audio filepaths
audio_files = glob.glob(f'{self.config.data_dir}/*/*.wav')
train_json_path = os.path.join(self.config.data_dir, 'meta_train.json')
eval_json_path = os.path.join(self.config.data_dir, 'meta_eval.json')
with open(train_json_path) as f:
train_dict = json.load(f)
with open(eval_json_path) as f:
eval_dict = json.load(f)
# load meta files
for audio_filepath in tqdm(audio_files[:]):
# load audio filepaths
audio_filepath_list.append(audio_filepath)
audio_name = audio_filepath.split('/')[-1].split('.wav')[0]
# get list of text captions
split = audio_filepath.split('/')[-2]
ref_dict = train_dict if split == 'train' else eval_dict
# collecting captions
text_captions = {}
text_captions['description'] = []
text_captions['description'] = [ref_dict[audio_name + '.wav']]
text_dict[audio_name] = text_captions
# obtain computer description item
return audio_filepath_list, text_dict, synthetic_text_dict
class AudioCaps16kProcessor(DatasetProcessor):
# AudioCaps uses a master cvs for each datasplit
config = AudioCaps16kConfig()
def get_filepaths_and_descriptions(self, current_split='test'):
# init output lists
audio_filepath_list = []
text_dict = {}
synthetic_text_dict = {}
# load audio filepaths
audio_files = glob.glob(f'{self.config.data_dir}/{current_split}/*.wav')
with open(os.path.join(self.config.data_dir, current_split + '.csv'), 'r') as f:
csv_reader = csv.reader(f)
meta_info_dict = {}
for i, row in enumerate(csv_reader):
if i == 0:
continue
if row[1] not in meta_info_dict:
meta_info_dict[row[1]] = [row[-1]]
else:
meta_info_dict[row[1]].append(row[-1])
# load meta files
for audio_filepath in tqdm(audio_files[:]):
# load audio filepaths
audio_name = audio_filepath.split('/')[-1].split('.wav')[0]
# get list of text captions
if audio_name not in meta_info_dict:
continue
audio_filepath_list.append(audio_filepath)
# collecting captions
text_captions = {}
text_captions['description'] = meta_info_dict[audio_name]
text_dict[audio_name] = text_captions
# obtain computer description item
return audio_filepath_list, text_dict, synthetic_text_dict
class Clotho16kProcessor(DatasetProcessor):
# clothov2 uses a master cvs for each datasplit instead of paired wav-json
config = Clotho16kConfig()
def get_filepaths_and_descriptions(self, current_split=''):
# init output lists
audio_filepath_list = []
text_dict = {}
synthetic_text_dict = {}
# load audio filepaths
audio_files = glob.glob(f'{self.config.data_dir}/{current_split}/*.wav')
# load meta files
for audio_filepath in tqdm(audio_files[:]):
# load audio filepaths
audio_filepath_list.append(audio_filepath)
audio_name = audio_filepath.split('/')[-1].split('.wav')[0]
# get list of text captions
audio_filename = audio_filepath.split('/')[-1]
split = audio_filepath.split('/')[-2]
if split != current_split:
continue
caption_filename = 'clotho_captions_' + split + '.csv'
caption_path = os.path.join(self.config.data_dir, caption_filename)
split_df = pd.read_csv(caption_path)
data_slice = split_df.loc[split_df['file_name'] == audio_filename]
# collecting captions
text_captions = {}
text_captions['description'] = []
for i in range(5):
text_captions['description'] += data_slice['caption_'+str(i+1)].tolist()
text_dict[audio_name] = text_captions
# obtain computer description item
return audio_filepath_list, text_dict, synthetic_text_dict
class BeatovenProcessor(DatasetProcessor):
# clothov2 uses a master cvs for each datasplit instead of paired wav-json
config = BeatovenConfig()
def get_filepaths_and_descriptions(self, current_split=''):
# init output lists
audio_filepath_list = []
text_dict = {}
synthetic_text_dict = {}
audio_names = []
# load audio filepaths
audio_files = glob.glob(f'{self.config.data_dir}/*.wav')
# load meta files
for audio_filepath in tqdm(audio_files[:]):
# load audio filepaths
audio_filepath_list.append(audio_filepath)
audio_name = audio_filepath.split('/')[-1]
# collecting captions
text_captions = {}
text_captions['description'] = []
text_dict[audio_name] = ''
text_dict[audio_name] = ''
audio_names.append(audio_name)
with open('audio_names_order.json', 'w') as fp:
json.dump(audio_names, fp)
return audio_filepath_list, text_dict, synthetic_text_dict