Update Guzheng_Tech99.py
Browse files- Guzheng_Tech99.py +97 -48
Guzheng_Tech99.py
CHANGED
@@ -1,8 +1,10 @@
|
|
1 |
import os
|
2 |
import csv
|
3 |
import random
|
|
|
4 |
import datasets
|
5 |
import numpy as np
|
|
|
6 |
from glob import glob
|
7 |
|
8 |
_NAMES = {
|
@@ -64,9 +66,15 @@ class Guzheng_Tech99(datasets.GeneratorBasedBuilder):
|
|
64 |
if self.config.name == "default"
|
65 |
else datasets.Features(
|
66 |
{
|
67 |
-
"
|
|
|
|
|
|
|
68 |
dtype="float32", shape=(88, 258, 1)
|
69 |
),
|
|
|
|
|
|
|
70 |
"label": datasets.features.Array2D(
|
71 |
dtype="float32", shape=(7, 258)
|
72 |
),
|
@@ -83,7 +91,7 @@ class Guzheng_Tech99(datasets.GeneratorBasedBuilder):
|
|
83 |
square_sum = 0
|
84 |
tfle = 0
|
85 |
for i in range(len(data)):
|
86 |
-
tfle += (data[i].sum(-1).sum(0) != 0).astype("
|
87 |
common_sum += data[i].sum(-1).sum(-1)
|
88 |
square_sum += (data[i] ** 2).sum(-1).sum(-1)
|
89 |
|
@@ -92,20 +100,14 @@ class Guzheng_Tech99(datasets.GeneratorBasedBuilder):
|
|
92 |
std = np.sqrt(square_avg - common_avg**2)
|
93 |
return common_avg, std
|
94 |
|
95 |
-
def _norm(self,
|
|
|
|
|
96 |
avg = np.tile(avg.reshape((1, -1, 1, 1)), (size[0], 1, size[2], size[3]))
|
97 |
std = np.tile(std.reshape((1, -1, 1, 1)), (size[0], 1, size[2], size[3]))
|
98 |
-
|
99 |
-
return data
|
100 |
-
|
101 |
-
def _load(self, wav_dir, csv_dir, groups, avg=None, std=None):
|
102 |
-
# Return all [(audio address, corresponding to csv file address), ( , ), ...] list
|
103 |
-
if std is None:
|
104 |
-
std = np.array([None])
|
105 |
-
|
106 |
-
if avg is None:
|
107 |
-
avg = np.array([None])
|
108 |
|
|
|
109 |
def files(wav_dir, csv_dir, group):
|
110 |
flacs = sorted(glob(os.path.join(wav_dir, group, "*.flac")))
|
111 |
if len(flacs) == 0:
|
@@ -122,12 +124,18 @@ class Guzheng_Tech99(datasets.GeneratorBasedBuilder):
|
|
122 |
|
123 |
return result
|
124 |
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
-
|
130 |
-
|
131 |
# 帧长为32ms (1000ms/(16000/512) = 32ms), D2的频率是73.418
|
132 |
cqt = librosa.cqt(
|
133 |
y,
|
@@ -141,10 +149,21 @@ class Guzheng_Tech99(datasets.GeneratorBasedBuilder):
|
|
141 |
(1.0 / 80.0) * librosa.core.amplitude_to_db(np.abs(cqt), ref=np.max)
|
142 |
) + 1.0
|
143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
def chunk_data(f):
|
145 |
-
s = int(_SAMPLE_RATE * _TIME_LENGTH / _HOP_LENGTH)
|
146 |
-
xdata = np.transpose(f)
|
147 |
x = []
|
|
|
|
|
148 |
length = int(np.ceil((int(len(xdata) / s) + 1) * s))
|
149 |
app = np.zeros((length - xdata.shape[0], xdata.shape[1]))
|
150 |
xdata = np.concatenate((xdata, app), 0)
|
@@ -154,16 +173,16 @@ class Guzheng_Tech99(datasets.GeneratorBasedBuilder):
|
|
154 |
|
155 |
return np.array(x)
|
156 |
|
157 |
-
def load_all(audio_path, csv_path):
|
158 |
# Load audio features: The shape of cqt (88, 8520), 8520 is the number of frames on the time axis
|
159 |
-
|
|
|
|
|
|
|
160 |
# Load the ground truth label
|
161 |
-
hop = _HOP_LENGTH
|
162 |
n_steps = cqt.shape[1]
|
163 |
-
n_IPTs = 7
|
164 |
-
technique = _NAMES
|
165 |
IPT_label = np.zeros([n_IPTs, n_steps], dtype=int)
|
166 |
-
with open(csv_path, "r") as f: # csv file for each audio
|
167 |
reader = csv.DictReader(f, delimiter=",")
|
168 |
for label in reader: # each note
|
169 |
onset = float(label["onset_time"])
|
@@ -175,7 +194,12 @@ class Guzheng_Tech99(datasets.GeneratorBasedBuilder):
|
|
175 |
IPT_label[IPT, left:frame_right] = 1
|
176 |
|
177 |
return dict(
|
178 |
-
|
|
|
|
|
|
|
|
|
|
|
179 |
)
|
180 |
|
181 |
data = []
|
@@ -184,29 +208,33 @@ class Guzheng_Tech99(datasets.GeneratorBasedBuilder):
|
|
184 |
for input_files in files(wav_dir, csv_dir, group):
|
185 |
data.append(load_all(*input_files))
|
186 |
|
187 |
-
i =
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
y_i = dic["IPT_label"]
|
192 |
y_i = chunk_data(y_i)
|
193 |
if i == 0:
|
194 |
-
|
|
|
|
|
195 |
Ytr_i = y_i
|
196 |
-
i += 1
|
197 |
|
198 |
else:
|
199 |
-
|
|
|
|
|
200 |
Ytr_i = np.concatenate([Ytr_i, y_i], axis=0)
|
201 |
|
202 |
# Transform the shape of the input
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
avg, std = self._RoW_norm(Xtr)
|
207 |
# Normalize
|
208 |
-
|
209 |
-
|
|
|
|
|
210 |
|
211 |
def _parse_csv_label(self, csv_file):
|
212 |
label = []
|
@@ -259,19 +287,40 @@ class Guzheng_Tech99(datasets.GeneratorBasedBuilder):
|
|
259 |
testset.append(item)
|
260 |
|
261 |
else:
|
262 |
-
audio_dir = audio_files
|
263 |
-
csv_dir = csv_files
|
264 |
X_train, Y_train = self._load(audio_dir, csv_dir, ["train"])
|
265 |
X_valid, Y_valid = self._load(audio_dir, csv_dir, ["validation"])
|
266 |
X_test, Y_test = self._load(audio_dir, csv_dir, ["test"])
|
267 |
-
for i in range(len(
|
268 |
-
trainset.append(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
269 |
|
270 |
-
for i in range(len(
|
271 |
-
validset.append(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
272 |
|
273 |
-
for i in range(len(
|
274 |
-
testset.append(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
275 |
|
276 |
random.shuffle(trainset)
|
277 |
random.shuffle(validset)
|
|
|
1 |
import os
|
2 |
import csv
|
3 |
import random
|
4 |
+
import librosa
|
5 |
import datasets
|
6 |
import numpy as np
|
7 |
+
from tqdm import tqdm
|
8 |
from glob import glob
|
9 |
|
10 |
_NAMES = {
|
|
|
66 |
if self.config.name == "default"
|
67 |
else datasets.Features(
|
68 |
{
|
69 |
+
"mel": datasets.features.Array3D(
|
70 |
+
dtype="float32", shape=(128, 258, 1)
|
71 |
+
),
|
72 |
+
"cqt": datasets.features.Array3D(
|
73 |
dtype="float32", shape=(88, 258, 1)
|
74 |
),
|
75 |
+
"chroma": datasets.features.Array3D(
|
76 |
+
dtype="float32", shape=(12, 258, 1)
|
77 |
+
),
|
78 |
"label": datasets.features.Array2D(
|
79 |
dtype="float32", shape=(7, 258)
|
80 |
),
|
|
|
91 |
square_sum = 0
|
92 |
tfle = 0
|
93 |
for i in range(len(data)):
|
94 |
+
tfle += (data[i].sum(-1).sum(0) != 0).astype("float").sum()
|
95 |
common_sum += data[i].sum(-1).sum(-1)
|
96 |
square_sum += (data[i] ** 2).sum(-1).sum(-1)
|
97 |
|
|
|
100 |
std = np.sqrt(square_avg - common_avg**2)
|
101 |
return common_avg, std
|
102 |
|
103 |
+
def _norm(self, data):
|
104 |
+
size = data.shape
|
105 |
+
avg, std = self._RoW_norm(data)
|
106 |
avg = np.tile(avg.reshape((1, -1, 1, 1)), (size[0], 1, size[2], size[3]))
|
107 |
std = np.tile(std.reshape((1, -1, 1, 1)), (size[0], 1, size[2], size[3]))
|
108 |
+
return (data - avg) / std
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
+
def _load(self, wav_dir, csv_dir, groups):
|
111 |
def files(wav_dir, csv_dir, group):
|
112 |
flacs = sorted(glob(os.path.join(wav_dir, group, "*.flac")))
|
113 |
if len(flacs) == 0:
|
|
|
124 |
|
125 |
return result
|
126 |
|
127 |
+
def logMel(y, sr=_SAMPLE_RATE):
|
128 |
+
# 帧长为32ms (1000ms/(16000/512) = 32ms), D2的频率是73.418
|
129 |
+
mel = librosa.feature.melspectrogram(
|
130 |
+
y=y,
|
131 |
+
sr=sr,
|
132 |
+
hop_length=_HOP_LENGTH,
|
133 |
+
fmin=27.5,
|
134 |
+
)
|
135 |
+
return librosa.power_to_db(mel, ref=np.max)
|
136 |
|
137 |
+
# Returns the CQT of the input audio
|
138 |
+
def logCQT(y, sr=_SAMPLE_RATE):
|
139 |
# 帧长为32ms (1000ms/(16000/512) = 32ms), D2的频率是73.418
|
140 |
cqt = librosa.cqt(
|
141 |
y,
|
|
|
149 |
(1.0 / 80.0) * librosa.core.amplitude_to_db(np.abs(cqt), ref=np.max)
|
150 |
) + 1.0
|
151 |
|
152 |
+
def logChroma(y, sr=_SAMPLE_RATE):
|
153 |
+
# 帧长为32ms (1000ms/(16000/512) = 32ms), D2的频率是73.418
|
154 |
+
chroma = librosa.feature.chroma_stft(
|
155 |
+
y=y,
|
156 |
+
sr=sr,
|
157 |
+
hop_length=_HOP_LENGTH,
|
158 |
+
)
|
159 |
+
return (
|
160 |
+
(1.0 / 80.0) * librosa.core.amplitude_to_db(np.abs(chroma), ref=np.max)
|
161 |
+
) + 1.0
|
162 |
+
|
163 |
def chunk_data(f):
|
|
|
|
|
164 |
x = []
|
165 |
+
xdata = np.transpose(f)
|
166 |
+
s = _SAMPLE_RATE * _TIME_LENGTH // _HOP_LENGTH
|
167 |
length = int(np.ceil((int(len(xdata) / s) + 1) * s))
|
168 |
app = np.zeros((length - xdata.shape[0], xdata.shape[1]))
|
169 |
xdata = np.concatenate((xdata, app), 0)
|
|
|
173 |
|
174 |
return np.array(x)
|
175 |
|
176 |
+
def load_all(audio_path, csv_path, hop=_HOP_LENGTH, n_IPTs=7, technique=_NAMES):
|
177 |
# Load audio features: The shape of cqt (88, 8520), 8520 is the number of frames on the time axis
|
178 |
+
y, sr = librosa.load(audio_path, sr=_SAMPLE_RATE)
|
179 |
+
mel = logMel(y, sr)
|
180 |
+
cqt = logCQT(y, sr)
|
181 |
+
chroma = logChroma(y, sr)
|
182 |
# Load the ground truth label
|
|
|
183 |
n_steps = cqt.shape[1]
|
|
|
|
|
184 |
IPT_label = np.zeros([n_IPTs, n_steps], dtype=int)
|
185 |
+
with open(csv_path, "r", encoding="utf-8") as f: # csv file for each audio
|
186 |
reader = csv.DictReader(f, delimiter=",")
|
187 |
for label in reader: # each note
|
188 |
onset = float(label["onset_time"])
|
|
|
194 |
IPT_label[IPT, left:frame_right] = 1
|
195 |
|
196 |
return dict(
|
197 |
+
audio_path=audio_path,
|
198 |
+
csv_path=csv_path,
|
199 |
+
mel=mel,
|
200 |
+
cqt=cqt,
|
201 |
+
chroma=chroma,
|
202 |
+
IPT_label=IPT_label,
|
203 |
)
|
204 |
|
205 |
data = []
|
|
|
208 |
for input_files in files(wav_dir, csv_dir, group):
|
209 |
data.append(load_all(*input_files))
|
210 |
|
211 |
+
for i, dic in tqdm(enumerate(data), total=len(data), desc="Feature extracting"):
|
212 |
+
x_mel = chunk_data(dic["mel"])
|
213 |
+
x_cqt = chunk_data(dic["cqt"])
|
214 |
+
x_chroma = chunk_data(dic["chroma"])
|
215 |
y_i = dic["IPT_label"]
|
216 |
y_i = chunk_data(y_i)
|
217 |
if i == 0:
|
218 |
+
Xtr_mel = x_mel
|
219 |
+
Xtr_cqt = x_cqt
|
220 |
+
Xtr_chroma = x_chroma
|
221 |
Ytr_i = y_i
|
|
|
222 |
|
223 |
else:
|
224 |
+
Xtr_mel = np.concatenate([Xtr_mel, x_mel], axis=0)
|
225 |
+
Xtr_cqt = np.concatenate([Xtr_cqt, x_cqt], axis=0)
|
226 |
+
Xtr_chroma = np.concatenate([Xtr_chroma, x_chroma], axis=0)
|
227 |
Ytr_i = np.concatenate([Ytr_i, y_i], axis=0)
|
228 |
|
229 |
# Transform the shape of the input
|
230 |
+
Xtr_mel = np.expand_dims(Xtr_mel, axis=3)
|
231 |
+
Xtr_cqt = np.expand_dims(Xtr_cqt, axis=3)
|
232 |
+
Xtr_chroma = np.expand_dims(Xtr_chroma, axis=3)
|
|
|
233 |
# Normalize
|
234 |
+
Xtr_mel = self._norm(Xtr_mel)
|
235 |
+
Xtr_cqt = self._norm(Xtr_cqt)
|
236 |
+
Xtr_chroma = self._norm(Xtr_chroma)
|
237 |
+
return [list(Xtr_mel), list(Xtr_cqt), list(Xtr_chroma)], list(Ytr_i)
|
238 |
|
239 |
def _parse_csv_label(self, csv_file):
|
240 |
label = []
|
|
|
287 |
testset.append(item)
|
288 |
|
289 |
else:
|
290 |
+
audio_dir = os.path.join(audio_files, "audio")
|
291 |
+
csv_dir = os.path.join(csv_files, "label")
|
292 |
X_train, Y_train = self._load(audio_dir, csv_dir, ["train"])
|
293 |
X_valid, Y_valid = self._load(audio_dir, csv_dir, ["validation"])
|
294 |
X_test, Y_test = self._load(audio_dir, csv_dir, ["test"])
|
295 |
+
for i in range(len(Y_train)):
|
296 |
+
trainset.append(
|
297 |
+
{
|
298 |
+
"mel": X_train[0][i],
|
299 |
+
"cqt": X_train[1][i],
|
300 |
+
"chroma": X_train[2][i],
|
301 |
+
"label": Y_train[i],
|
302 |
+
}
|
303 |
+
)
|
304 |
|
305 |
+
for i in range(len(Y_valid)):
|
306 |
+
validset.append(
|
307 |
+
{
|
308 |
+
"mel": X_valid[0][i],
|
309 |
+
"cqt": X_valid[1][i],
|
310 |
+
"chroma": X_valid[2][i],
|
311 |
+
"label": Y_valid[i],
|
312 |
+
}
|
313 |
+
)
|
314 |
|
315 |
+
for i in range(len(Y_test)):
|
316 |
+
testset.append(
|
317 |
+
{
|
318 |
+
"mel": X_test[0][i],
|
319 |
+
"cqt": X_test[1][i],
|
320 |
+
"chroma": X_test[2][i],
|
321 |
+
"label": Y_test[i],
|
322 |
+
}
|
323 |
+
)
|
324 |
|
325 |
random.shuffle(trainset)
|
326 |
random.shuffle(validset)
|