-
Notifications
You must be signed in to change notification settings - Fork 35
/
datasets.py
277 lines (229 loc) · 12.2 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
274
275
276
277
# -*- coding: utf-8 -*-
"""
Datasets
========
"""
from __future__ import absolute_import
import utils_datasets
import librosa
import numpy as np
import os
def load_jamendo(save_path='datasets', sr=16000, mono=True, duration=None, offset=0.0):
"""Download jamendo http://www.mathieuramona.com/wp/data/jamendo/
It creates `save_path/jamendo` and the sub-directories, `jamendo_lab`, `train`, `valid`, `test`.
It does not remove the downloaded `.tar.gz` files in `save_path/jamendo`.
As it takes quite a while for decoding the audio files, it would be better to store
the returned value as npy/hdf/whatever and use it.
Parameters
----------
save_path: str,
absolute/relative path to store the dataset
sr: int > 0
sampling rate of audio sources.
It is also used to compute label arrays
mono: bool
Whether downmix the audio signals to mono or not.
duration: float [second]
Duration of audio files
offset: float [second]
Offset to load
Returns
-------
srcs: list, length of 3.
| each element is a list of train/valid/test sources
| e.g. ``srcs[0][0].shape = (1, 3959745)`` when ``sr=16000`` and ``mono=True``
ys: list, length of 3.
| each element is a list of train/valid/test groundtruths
| e.g., ``ys[0][0].shape = (3959745, )``
"""
set_names = ['train', 'valid', 'test']
for set in set_names:
datadir = utils_datasets.get_file('jam_{}_audio.tar.gz'.format(set),
'http://www.mathieuramona.com/uploads/Main/jam_{}_audio.tar.gz'.format(set),
save_path, untar=True, cache_subdir='jamendo',
tar_folder_name=set)
datadir = utils_datasets.get_file('jam_annote.tar.gz',
'http://www.mathieuramona.com/uploads/Main/jam_annote.tar.gz',
save_path, untar=True, cache_subdir='jamendo',
tar_folder_name='jamendo_lab')
# load file names in train/valid/test folder
x_filenames = []
y_filenames = []
for set in set_names:
fnames = [f.lstrip('._') for f in os.listdir(os.path.join(save_path, 'jamendo', set)) \
if f.split('.')[-1] in ('ogg', 'mp3')]
x_filenames.append(fnames)
y_filenames.append([f.split('.')[0] + '.lab' for f in fnames])
srcs = []
ys = []
for set, x_fnames, y_fnames in zip(set_names, x_filenames, y_filenames):
srcs_set = []
ys_set = []
for idx, (x_fname, y_fname) in enumerate(zip(x_fnames, y_fnames)):
# process srcs
print('Loading {}/{}: {}...'.format(idx, len(x_fnames), x_fname))
src, _ = librosa.load(os.path.join(save_path, 'jamendo', set, x_fname),
sr=sr, mono=mono, offset=offset, duration=duration)
if mono:
src = src[np.newaxis, :] # to make it (1, N) instead of (N,)
len_src = src.shape[1]
srcs_set.append(src)
# process ys
y = np.zeros((len_src,), dtype=np.bool)
with open(os.path.join(save_path, 'jamendo', 'jamendo_lab', y_fname)) as f_label:
for line in f_label:
start, end, label = line.rstrip('\n').split(' ')
if label == 'sing':
start, end = int(np.round(float(start) * sr)), int(np.round(float(end) * sr))
y[start:end] = True
ys_set.append(y)
srcs.append(srcs_set)
ys.append(ys_set)
# return
return srcs, ys
def load_fma(save_path='datasets', size='small'):
"""Download fma:free music archive (https://github.com/mdeff/fma)
It would be better to download directly from the link for large/full..
Parameters
----------
save_path: str,
absolute/relative path to store the dataset
size: str, 'small', 'medium', 'large', 'huge'
| small: 8,000 tracks of 30s, 8 balanced genres (GTZAN-like) (7.2 GiB)
| medium: 25,000 tracks of 30s, 16 unbalanced genres (22 GiB)
| large: 106,574 tracks of 30s, 161 unbalanced genres (93 GiB)
| full: 106,574 untrimmed tracks, 161 unbalanced genres (879 GiB)
"""
assert size in ('small', 'medium', 'large', 'full')
if size == 'small':
zip_filename = 'fma_small.zip'
zip_path = utils_datasets.get_file(zip_filename, 'https://os.unil.cloud.switch.ch/fma/fma_small.zip',
save_path, untar=False, cache_subdir='fma',
md5_hash='4edb51c99a19d31fe01a7d44d5cac19b')
elif size == 'medium':
zip_filename = 'fma_medium.zip'
zip_path = utils_datasets.get_file(zip_filename, 'https://os.unil.cloud.switch.ch/fma/fma_medium.zip',
save_path, untar=False, cache_subdir='fma')
elif size == 'large':
zip_filename = 'fma_large.zip'
zip_path = utils_datasets.get_file(zip_filename, 'https://os.unil.cloud.switch.ch/fma/fma_large.zip',
save_path, untar=False, cache_subdir='fma')
elif size == 'full':
zip_filename = 'fma_full.zip'
zip_path = utils_datasets.get_file(zip_filename, 'https://os.unil.cloud.switch.ch/fma/fma_full.zip',
save_path, untar=False, cache_subdir='fma')
print("unzipping audio files...")
os.system('unzip {} -d {}'.format(os.path.join(zip_path, zip_filename), zip_path))
metadata_zip_filename = 'fma_metadata.zip'
metadata_zip_path = utils_datasets.get_file(metadata_zip_filename,
'https://os.unil.cloud.switch.ch/fma/fma_metadata.zip',
save_path, untar=False, cache_subdir='fma',
md5_hash='d3ebfd86e283345ee2366a5492495935')
print("unzipping metadata files...")
os.system('unzip {} -d {}'.format(os.path.join(metadata_zip_path, metadata_zip_filename),
metadata_zip_path))
def load_musicnet(save_path='datasets', format='hdf'):
"""Download musicnet (https://homes.cs.washington.edu/~thickstn/start.html)
Parameters
----------
save_path: str,
absolute/relative path to store the dataset
format: str,
Data format to download. Either 'hdf' or 'npz'
"""
assert format in ('hdf', 'npz')
if format == 'hdf':
utils_datasets.get_file('musicnet.h5', 'https://homes.cs.washington.edu/~thickstn/media/musicnet.h5',
save_path, untar=False, cache_subdir='musicnet',
md5_hash='05103753391a8019029b29b790f7e1f7')
else:
utils_datasets.get_file('musicnet.npz', 'https://homes.cs.washington.edu/~thickstn/media/musicnet.npz',
save_path, untar=False, cache_subdir='musicnet',
md5_hash='9303e5338adefd3715c51997553fb45f')
utils_datasets.get_file('musicnet_metadata.csv',
'https://homes.cs.washington.edu/~thickstn/media/musicnet_metadata.csv',
save_path, untar=False, cache_subdir='musicnet',
md5_hash=None)
def load_magnatagatune(save_path='datasets'):
"""Download magnatagatune dataset, concate the zip files, unzip it,
to `save_path`.
Parameters
----------
save_path: absolute or relative path to store the dataset
"""
# 1GB for each
zip_path = utils_datasets.get_file('mp3.zip.001', 'http://mi.soi.city.ac.uk/datasets/magnatagatune/mp3.zip.001',
save_path, untar=False, cache_subdir='magnatagatune',
md5_hash='179c91c8c2a6e9b3da3d4e69d306fd3b')
utils_datasets.get_file('mp3.zip.002', 'http://mi.soi.city.ac.uk/datasets/magnatagatune/mp3.zip.002',
save_path, untar=False, cache_subdir='magnatagatune',
md5_hash='acf8265ff2e35c6ff22210e46457a824')
utils_datasets.get_file('mp3.zip.003', 'http://mi.soi.city.ac.uk/datasets/magnatagatune/mp3.zip.003',
save_path, untar=False, cache_subdir='magnatagatune',
md5_hash='582dc649cabb8cd991f09e14b99349a5')
print("appending zip files...")
os.system('cat {}/mp3.zip.* > {}/mp3s.zip'.format(zip_path, zip_path))
print("unzipping...")
os.system('unzip {} -d {}/mp3s'.format(os.path.join(zip_path, 'mp3s.zip'), zip_path))
# labels
utils_datasets.get_file('clip_info_final.csv',
'http://mi.soi.city.ac.uk/datasets/magnatagatune/clip_info_final.csv',
save_path, untar=False, cache_subdir='magnatagatune',
md5_hash='03ef3cb8ddcfe53fdcdb8e0cda005be2')
utils_datasets.get_file('annotations_final.csv',
'http://mi.soi.city.ac.uk/datasets/magnatagatune/annotations_final.csv',
save_path, untar=False, cache_subdir='magnatagatune',
md5_hash='f04fa01752a8cc64f6e1ca142a0fef1d')
utils_datasets.get_file('comparisons_final.csv',
'http://mi.soi.city.ac.uk/datasets/magnatagatune/comparisons_final.csv',
save_path, untar=False, cache_subdir='magnatagatune')
# echonest feature (377.4 MB)
utils_datasets.get_file('mp3_echonest_xml.zip',
'http://mi.soi.city.ac.uk/datasets/magnatagatune/mp3_echonest_xml.zip',
save_path, untar=False, cache_subdir='magnatagatune',
md5_hash='09be4ac8c682a8c182279276fadb37f9')
def load_gtzan_speechmusic(save_path='datasets'):
"""
Download gtzan speech/music dataset, untar it, and create a helper csv file
Arguments
---------
save_path: str,
Absolute or relative path to store the dataset
"""
datadir = utils_datasets.get_file('gtzan_speechmusic.tar.gz', 'http://opihi.cs.uvic.ca/sound/music_speech.tar.gz',
save_path, untar=True, cache_subdir='gtzan_speechmusic',
md5_hash='b063639094c169062940becacd3108a0')
labels = ['music', 'speech']
rows = utils_datasets.get_rows_from_folders(folder_dataset='music_speech',
folders=labels,
dataroot=datadir)
columns = ['id', 'filepath', 'label']
csv_path = os.path.join(datadir, 'dataset_summary_kapre.csv')
utils_datasets.write_to_csv(rows=rows, column_names=columns,
csv_fname=csv_path)
def load_gtzan_genre(save_path='datasets'):
"""Load gtzan muusic dataset from http://opihi.cs.uvic.ca/sound/genres.tar.gz
It downloads gtzan tarball on save_path/gtzan_music.tar.gz .
After untarring, we got files as below:
```
for genre_name in ['blues', ..., 'rock']:
for idx in xrange(100):
"genres/{}/{}.{:05d}.au".format(genre_name, genre_name, idx)
```
Then it creates a helper csv file.
Parameters
----------
save_path: str,
Absolute or relative path to store the dataset
"""
datadir = utils_datasets.get_file('gtzan_genre.tar.gz', 'http://opihi.cs.uvic.ca/sound/genres.tar.gz',
save_path, untar=True, cache_subdir='gtzan_genre',
md5_hash='fe37942310e589be16b04b6d631790de')
labels = ['blues', 'classical', 'country', 'disco', 'hiphop', 'jazz', 'metal', 'pop', 'reggae', 'rock']
rows = utils_datasets.get_rows_from_folders(folder_dataset='genres',
folders=labels,
dataroot=datadir)
columns = ['id', 'filepath', 'label']
csv_path = os.path.join(datadir, 'dataset_summary_kapre.csv')
utils_datasets.write_to_csv(rows=rows, column_names=columns,
csv_fname=csv_path)