-
Notifications
You must be signed in to change notification settings - Fork 12
/
prepare_data.py
206 lines (142 loc) · 5.59 KB
/
prepare_data.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
import ast
import os
import sys
import warnings
import pandas as pd
from pandas.api.types import CategoricalDtype
if not sys.warnoptions:
warnings.simplefilter("ignore")
import json
import numpy as np
from tensorflow.keras.utils import Sequence
from audio_processing import random_crop, random_mask
class DataGenerator(Sequence):
def __init__(self, path_x_label_list, class_mapping, batch_size=32):
self.path_x_label_list = path_x_label_list
self.batch_size = batch_size
self.indexes = np.arange(len(self.path_x_label_list))
self.class_mapping = class_mapping
self.on_epoch_end()
def __len__(self):
return int(np.floor(len(self.path_x_label_list) / self.batch_size / 10))
def __getitem__(self, index):
indexes = self.indexes[index * self.batch_size : (index + 1) * self.batch_size]
batch_samples = [self.path_x_label_list[k] for k in indexes]
x, y = self.__data_generation(batch_samples)
return x, y
def on_epoch_end(self):
np.random.shuffle(self.indexes)
def __data_generation(self, batch_samples):
paths, labels = zip(*batch_samples)
labels = [labels_to_vector(x, self.class_mapping) for x in labels]
crop_size = np.random.randint(128, 256)
X = np.array([random_crop(np.load(x), crop_size=crop_size) for x in paths])
Y = np.array(labels)
return X, Y[..., np.newaxis]
class PretrainGenerator(Sequence):
def __init__(self, path_x_label_list, batch_size=32):
self.path_x_label_list = path_x_label_list
self.batch_size = batch_size
self.indexes = np.arange(len(self.path_x_label_list))
self.on_epoch_end()
def __len__(self):
return int(np.floor(len(self.path_x_label_list) / self.batch_size / 10))
def __getitem__(self, index):
indexes = self.indexes[index * self.batch_size : (index + 1) * self.batch_size]
batch_samples = [self.path_x_label_list[k] for k in indexes]
x, y = self.__data_generation(batch_samples)
return x, y
def on_epoch_end(self):
np.random.shuffle(self.indexes)
def __data_generation(self, batch_samples):
paths, _ = zip(*batch_samples)
crop_size = np.random.randint(128, 256)
X = [random_crop(np.load(x), crop_size=crop_size) for x in paths]
Y = [random_mask(a) for a in X]
X = np.array(X)
Y = np.array(Y)
return X, Y
def load(filepath):
# From https://github.com/mdeff/fma/blob/rc1/utils.py / MIT License
filename = os.path.basename(filepath)
if "features" in filename:
return pd.read_csv(filepath, index_col=0, header=[0, 1, 2])
if "echonest" in filename:
return pd.read_csv(filepath, index_col=0, header=[0, 1, 2])
if "genres" in filename:
return pd.read_csv(filepath, index_col=0)
if "tracks" in filename:
tracks = pd.read_csv(filepath, index_col=0, header=[0, 1])
COLUMNS = [
("track", "tags"),
("album", "tags"),
("artist", "tags"),
("track", "genres"),
("track", "genres_all"),
]
for column in COLUMNS:
tracks[column] = tracks[column].map(ast.literal_eval)
COLUMNS = [
("track", "date_created"),
("track", "date_recorded"),
("album", "date_created"),
("album", "date_released"),
("artist", "date_created"),
("artist", "active_year_begin"),
("artist", "active_year_end"),
]
for column in COLUMNS:
tracks[column] = pd.to_datetime(tracks[column])
SUBSETS = ("small", "medium", "large")
tracks["set", "subset"] = tracks["set", "subset"].astype(
CategoricalDtype(categories=SUBSETS, ordered=True)
)
COLUMNS = [
("track", "genre_top"),
("track", "license"),
("album", "type"),
("album", "information"),
("artist", "bio"),
]
for column in COLUMNS:
tracks[column] = tracks[column].astype("category")
return tracks
def get_id_from_path(path):
base_name = os.path.basename(path)
return base_name.replace(".mp3", "").replace(".npy", "")
def labels_to_vector(labels, mapping):
vec = [0] * len(mapping)
for i in labels:
vec[mapping[i]] = 1
return vec
if __name__ == "__main__":
in_path = "/media/ml/data_ml/fma_metadata/tracks.csv"
genres_path = "/media/ml/data_ml/fma_metadata/genres.csv"
out_path = "/media/ml/data_ml/fma_metadata/tracks_genre.json"
mapping_path = "/media/ml/data_ml/fma_metadata/mapping.json"
df = load("/media/ml/data_ml/fma_metadata/tracks.csv")
df2 = pd.read_csv(genres_path)
id_to_title = {k: v for k, v in zip(df2.genre_id.tolist(), df2.title.tolist())}
df.reset_index(inplace=True)
print(df.head())
print(df.columns.values)
print(set(df[("set", "subset")].tolist()))
df = df[df[("set", "subset")].isin(["small", "medium", "large"])]
print(set(df[("track", "genre_top")].tolist()))
print(
df[
[
("track_id", ""),
("track", "genre_top"),
("track", "genres"),
("set", "subset"),
]
]
)
data = {
k: [id_to_title[a] for a in v]
for k, v in zip(df[("track_id", "")].tolist(), df[("track", "genres")].tolist())
}
json.dump(data, open(out_path, "w"), indent=4)
mapping = {k: i for i, k in enumerate(df2.title.tolist())}
json.dump(mapping, open(mapping_path, "w"), indent=4)