-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseline_classification.py
360 lines (291 loc) · 12 KB
/
baseline_classification.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# ------------------------------------
# OrchideaSOL classification baselines
# ------------------------------------
#
# Written by Carmine E. Cella, 2020
#
# This code is distributed with the OrchideaSOL dataset
# of extended instrumental techniques.
#
# For more information, please see:
# C. E. Cella, D. Ghisi, V. Lostanlen, F. Lévy, J. Fineberg and Y. Maresz,
# OrchideaSOL: a dataset of extended instrumental techniques for computer-assisted orchestration,
# ICMC 2020, Santiago, Chile.
#
import numpy as np
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder, StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import Pipeline
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.decomposition import PCA
from sklearn.model_selection import cross_val_score
from sklearn.svm import SVC
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import KFold
from sklearn.ensemble import RandomForestClassifier
from sklearn.multioutput import MultiOutputClassifier
from collections import Counter
from matplotlib import pyplot as plt
import librosa
import librosa.display
import json
import os
import random
import sys
from time import process_time
# raw dataset
# file hierarchy: (note that folders Brass, Winds, Strings are not present)
# ----TinySOL
# ----Bn
# ----Cb
# ----Va
# etc...
path = './TinySOL_0.6/TinySOL'
# time duration
time = 4
# number of samples between successive frames in librosa.melspectrogram
mel_hop_length = 44100
pitch_classes = ['A', 'A#', 'B', 'C', 'C#', 'D', 'D#', 'E', 'F', 'F#', 'G', 'G#']
def mix(fa, fb):
diff = len(fa) - len(fb)
if diff > 0:
add = np.zeros((1, diff), dtype=np.float32)
fb = np.append(fb, add)
elif diff < 0:
add = np.zeros((1, -diff), dtype=np.float32)
fa = np.append(fa, add)
return fa+fb
def combine_sounds(soundlist):
mixed_file = np.zeros((1, 1))
sr = 0
for sound in soundlist:
sound_path = os
sfile, sr = librosa.load(sound, sr=None)
# mfcc = librosa.feature.mfcc(y=sfile, sr=sr, hop_length=mel_hop_length)
# librosa.display.specshow(mfcc)
# plt.title(sound)
# plt.show()
if len(sfile) > time*sr:
# randomly select one part of the raw audio
n = np.random.randint(0, len(sfile)-time*sr)
sfile = sfile[n:n+time*sr]
# add augment
# sfile = wav_augment(sfile, sr)
mixed_file = mix(mixed_file, sfile)
mixed_file = mixed_file/len(soundlist)
# mfcc = librosa.feature.mfcc(y=sfile, sr=sr, hop_length=mel_hop_length)
# librosa.display.specshow(mfcc)
# plt.title('mix')
# plt.show()
return [mixed_file, sr]
def calculate_features(sample, sr):
feature = librosa.feature.mfcc(y=sample, sr=sr,
hop_length=mel_hop_length)
# zero padding
expected_length = sr*time // mel_hop_length + 1
diff = expected_length - feature.shape[1]
if diff > 0:
padding = np.zeros((feature.shape[0], diff), dtype=np.float32)
feature = np.append(feature, padding)
return feature
def extract_label(sample):
'''
return the instrument name and pitch class given a file path
expects sample to be similar to "./TinySOL_0.6/TinySOL/BTb/BTb-ord-F2-ff.wav"
'''
sample = sample.split('/')[-1]
sample = sample.split('.')[0]
sample = sample.split('-')
instrument = sample[0]
playing_style = sample[1]
pitch = sample[2]
dynamic = sample[3]
pitch_class = pitch[:-1]
return [instrument, pitch_class]
label_mapping = {}
def create_label_mapping(orchestra, pitch_instruments):
'''
creates a dictionary that maps an instrument and pitch class to its index in the label
for instruments in 'pitch_instruments' a key looks like: Fl-C or Vn-G
for all other instruments, a key is just the instrument name: Fl or Vn or Cb
and each of these instruments maps to the same index, because they all fall under the same class
'''
assert len(label_mapping) == 0
i = 0
for instrument in pitch_instruments:
for pitch in pitch_classes:
key = instrument + '-' + pitch
label_mapping[key] = i
i += 1
for instrument in orchestra:
if instrument not in pitch_instruments:
label_mapping[instrument] = i
def create_binary_label(samples, pitch_instruments, orchestra):
'''
given a list of samples, return a binary vector
for N pitch_instruments, the first N * 12 indices correspond to each pitch_instrument and one of 12
pitches associated with it.
The N * 12 + 1 index corresponds to the class that represents "noise" i.e. the fact that some other
instrument that is not a pitch_instrument is present
'''
label_length = (len(pitch_instruments) * 12) + 1
assert len(set(label_mapping.values())) == label_length
label = np.zeros(label_length, dtype=np.float32)
for sample in samples:
instrument, pitch_class = extract_label(sample)
key = instrument
if instrument in pitch_instruments:
key = key + '-' + pitch_class
index = label_mapping[key]
label[index] = 1.0
return label
def generate_data(orchestra, pitch_instruments, n, num_samples):
'''
create combinations of n instruments using only the instruments defined in 'orchestra'
the instruments in 'pitch_instruments' will have their pitch class indentified as well
'''
create_label_mapping(orchestra, pitch_instruments)
# dictionary where key is instrument name
# and value is a list of all the samples in the dataset for that instrument
samples = {}
for instrument in orchestra:
samples[instrument] = []
instrument_path = os.path.join(path, instrument)
for sample in os.listdir(instrument_path):
sample_path = os.path.join(instrument_path, sample)
samples[instrument].append(sample_path)
X = [] # data
y = [] # labels
i = 0
while i < num_samples:
instruments = []
# select one of 'pitch_instruments'
instruments.append(random.choice(pitch_instruments))
# select n - 1 instruments
instruments.extend(random.sample(orchestra, n - 1))
samples_to_combine = []
# for each of n chosen instruments, randomly select one sample
for instrument in instruments:
sample = random.choice(samples[instrument])
samples_to_combine.append(sample)
# combine sounds, storing combined sound in `mixture`
mixture, sr = combine_sounds(samples_to_combine)
# calculate feature of combined sound
features = calculate_features(mixture, sr)
# create label from the samples that were chosen
label = create_binary_label(samples_to_combine, pitch_instruments, orchestra)
# need to have more than 1 label
if (np.sum(label) == 1):
continue
# since you cant know the dimensions until the features have been computed,
# you can't make the ndarray until now
if i == 0:
num_features = features.flatten().shape[0]
X = np.zeros((num_samples, num_features))
y = np.zeros((num_samples, label.shape[0]))
X[i] = features.flatten()
y[i] = label
if i % 100 == 0:
print("{} / {} have finished".format(i, num_samples))
i += 1
return X, y
def train_and_test(X, y):
X_train, X_test, y_train, y_test = train_test_split(X,
y,
test_size = 0.4,
random_state = 42,
shuffle=True)
# print('X train: ', X_train.shape)
# print('X test: ', X_test.shape)
# print('y train: ', y_train.shape)
# print('y test: ', y_test.shape)
clfs = []
clf = SVC(kernel='rbf')
clf = MultiOutputClassifier(clf)
clfs.append(clf)
# clf = RandomForestClassifier(max_depth=15)
# clf = MultiOutputClassifier(clf)
# clfs.append(clf)
test_scores = []
print("\nRunning classifications...")
for classifier in clfs:
start_time = process_time()
pipeline = Pipeline([
('normalizer', StandardScaler()),
('clf', classifier)
])
print('---------------------------------')
print(str(classifier))
print('---------------------------------')
shuffle = KFold(n_splits=5, random_state=5, shuffle=True)
scores = cross_val_score(pipeline, X, y, cv=shuffle)
print("model scores: ", scores)
print("average training score: ", scores.mean())
pipeline.fit(X_train, y_train)
ncvscore = pipeline.score(X_test, y_test)
print("test accuracy: ", ncvscore)
print("time: ", process_time() - start_time)
test_scores.append(ncvscore)
return test_scores
def make_plot(x, y):
plt.plot(x, y, marker='o')
for a, b in zip(x, y):
if a < 3:
placement = (20, -10)
else:
placement = (0, 10)
plt.annotate(str(b), # this is the text
(a, b), # this is the point to label
textcoords="offset points", # how to position the text
xytext=placement, # distance from text to points (x,y)
ha='center') # horizontal alignment can be left, right or center
plt.ylim((0, 1))
plt.xticks(range(1, max(x) + 1))
plt.xlabel("Number of instruments combined")
plt.ylabel("Accuracy")
# plt.title("""Accuracy of non-linear SVM classifying various numbers of instrument combinations \n from an orchestra of 12 instruments using 50,000 samples per combination""")
plt.title("classifier: non-linear SVM, classifying: instrument only, \n orchestra size: 12, number of samples: 50,000")
plt.show()
def make_plot_multiple_lines(all_scores):
'''
scores is a nested dictionary that maps a value of n to a dictionary of samples and scores
'''
for n in all_scores.keys():
scores = all_scores[n]
num_samples = list(scores.keys())
accuracies = list(scores.values())
plt.plot(num_samples, accuracies,
label="combinations of {} instruments".format(n), marker='o')
for x, y in scores.items():
plt.annotate(str(y), # this is the text
(x, y), # this is the point to label
textcoords="offset points", # how to position the text
xytext=(0, 10), # distance from text to points (x,y)
ha='center') # horizontal alignment can be left, right or center
plt.ylim((0, 0.6))
plt.legend()
plt.xlabel("Number of samples used to train and test")
plt.ylabel("Accuracy")
plt.title("Accuracy of SVM classifying combinations of instruments".format(n))
plt.show()
orchestra = ['Vc', 'Fl', 'Va', 'Vn', 'Ob', 'BTb',
'Cb', 'ClBb', 'Hn', 'TpC', 'Bn', 'Tbn']
num_samples = 3
scores = []
n = 2
pitch_instruments = ['Vn', 'Fl']
X, y = generate_data(orchestra, pitch_instruments, n, num_samples)
print("\nNumber of classes: ", y[0].shape)
score = train_and_test(X, y)
scores.append(score)
print('---------------------------------')
print("orchestra size: ", len(orchestra))
print("n: ", n)
print("number of samples: ", num_samples)
print("scores from this run: ", scores)
print('---------------------------------')
#eof