-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioClassifierManager.py
401 lines (347 loc) · 17.3 KB
/
AudioClassifierManager.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
'''
AudioClassifierManager is used to manage classification
'''
from pyAudioAnalysis import audioBasicIO as aBIO
from pyAudioAnalysis import audioFeatureExtraction as aF
from pyAudioAnalysis import audioTrainTest as aT
import numpy
import utils
import pickle as cPickle
class AudioClassifierManager:
# experiment executed per parameter in classification
__num_experiment = 100
__compute_beat = True
__svmModelName = "svm"
__svmRbfModelName = "svm_rbf"
__randomforestModelName = "randomforest"
__knnModelName = "knn"
__gradientboostingModelName = "gradientboosting"
__extratreesModelName = "extratrees"
__models = ["knn", "svm","svm_rbf", "randomforest","gradientboosting"]
__perTrain = [0.9, 0.7]
BEST_ACCURACY = 0
BEST_F1 = 1
@staticmethod
def getAllModels():
return AudioClassifierManager.__models
@staticmethod
def getModelNameForTypeAndPt(model_type,pT):
return "{0}_pt{1}".format(model_type, str(int(pT * 100)))
@staticmethod
def getPerTrainProportions():
return AudioClassifierManager.__perTrain
@staticmethod
def getMtWin():
return aT.shortTermWindow * 2
@staticmethod
def getMtStep():
return aT.shortTermStep * 2
@staticmethod
def getStWin():
return aT.shortTermWindow
@staticmethod
def getStStep():
return aT.shortTermStep
@staticmethod
def getFeaturesAndClasses(dirs):
return aF.dirsWavFeatureExtraction(dirs,
AudioClassifierManager.getMtWin(),
AudioClassifierManager.getMtStep(),
AudioClassifierManager.getStWin(),
AudioClassifierManager.getStStep(),
compute_beat=AudioClassifierManager.__compute_beat)
@staticmethod
def getFeaturesOptimized(features):
'''
:param features: global matrix
:return: matrix optimized without feature vectors with NaN or Inf
'''
featuresOpt = []
for matFeatClass in features:
fTemp = []
for i in range(matFeatClass.shape[0]):
temp = matFeatClass[i, :]
# Verify if current vector of features of track has Nan or inf
if (not numpy.isnan(temp).any()) and (not numpy.isinf(temp).any()):
fTemp.append(temp.tolist())
else:
print("NaN Found! Feature vector not used for training")
featuresOpt.append(numpy.array(fTemp))
return featuresOpt
@staticmethod
def getCountClasses(features):
'''
:param features: matrix of features
:return: number of classes (genres)
'''
return len(features)
@staticmethod
def getCountTracksInClass(matrix_feature_class):
'''
:param matrix_feature_class: matrix of features of a class
:return: number of tracks for genre
'''
return len(matrix_feature_class)
@staticmethod
def getCountFeatures(features):
'''
:param features: global matrix with classes and vector of features inside
:return: number of features ('zcr', 'energy', 'energy_entropy', 'spectral_centroid' , e.g)
'''
# get columns of first genre of matrix
return features[0].shape[1]
@staticmethod
def writeTrainDataToARFF(model_name, features, classNames):
n_feats = AudioClassifierManager.getCountFeatures(features)
feature_names = ["features" + str(d + 1) for d in range(n_feats)]
aT.writeTrainDataToARFF(model_name, features, classNames, feature_names)
@staticmethod
def getListParamsForClassifierType(classifier_type):
if classifier_type == AudioClassifierManager.__svmModelName or classifier_type == AudioClassifierManager.__svmRbfModelName:
classifier_par = numpy.array([0.001, 0.01, 0.5, 1.0, 5.0, 10.0, 20.0])
elif classifier_type == AudioClassifierManager.__randomforestModelName:
classifier_par = numpy.array([10, 25, 50, 100, 200, 500])
elif classifier_type == AudioClassifierManager.__knnModelName:
classifier_par = numpy.array([1, 3, 5, 7, 9, 11, 13, 15])
elif classifier_type == AudioClassifierManager.__gradientboostingModelName:
classifier_par = numpy.array([10, 25, 50, 100, 200, 500])
elif classifier_type == AudioClassifierManager.__extratreesModelName:
classifier_par = numpy.array([10, 25, 50, 100, 200, 500])
else:
classifier_par = numpy.array([])
return classifier_par
@staticmethod
def getOptimalNumberExperiment(features,n_exp):
'''
:param features: global matrix
:param n_exp: number of experiment from user
:return:
'''
n_samples_total = 0
for f in features:
n_samples_total += f.shape[0]
print("Number of total samples: {0}".format(n_samples_total))
if n_samples_total > 1000 and n_exp > 50:
n_exp = 50
print("Number of training experiments changed to 50 due to high number of samples")
if n_samples_total > 2000 and n_exp > 10:
n_exp = 10
print("Number of training experiments changed to 10 due to high number of samples")
return n_exp
@staticmethod
def getTrainClassifier(f_train,classifier_name,param):
if classifier_name == AudioClassifierManager.__svmModelName:
classifier = aT.trainSVM(f_train, param)
elif classifier_name == AudioClassifierManager.__svmRbfModelName:
classifier = aT.trainSVM_RBF(f_train, param)
elif classifier_name == AudioClassifierManager.__knnModelName:
classifier = aT.trainKNN(f_train, param)
elif classifier_name == AudioClassifierManager.__randomforestModelName:
classifier = aT.trainRandomForest(f_train, param)
elif classifier_name == AudioClassifierManager.__gradientboostingModelName:
classifier = aT.trainGradientBoosting(f_train, param)
elif classifier_name == AudioClassifierManager.__extratreesModelName:
classifier = aT.trainExtraTrees(f_train, param)
else:
classifier = None
return classifier
@staticmethod
def saveClassifierModel(features,model_name,classifier_type,classifier,MEAN,STD,classNames,bestParam):
if classifier_type == "knn":
[X, Y] = aT.listOfFeatures2Matrix(features)
X = X.tolist()
Y = Y.tolist()
fo = open(model_name, "wb")
cPickle.dump(X, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(Y, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(MEAN, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(STD, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(classNames, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(bestParam, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(AudioClassifierManager.getMtWin(), fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(AudioClassifierManager.getMtStep(), fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(AudioClassifierManager.getStWin(), fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(AudioClassifierManager.getStStep(), fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(AudioClassifierManager.__compute_beat, fo, protocol=cPickle.HIGHEST_PROTOCOL)
fo.close()
elif classifier_type == AudioClassifierManager.__svmModelName or classifier_type == AudioClassifierManager.__svmRbfModelName or \
classifier_type == AudioClassifierManager.__randomforestModelName or \
classifier_type == AudioClassifierManager.__gradientboostingModelName or \
classifier_type == AudioClassifierManager.__extratreesModelName:
with open(model_name, 'wb') as fid:
cPickle.dump(classifier, fid)
fo = open(model_name + "MEANS", "wb")
cPickle.dump(MEAN, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(STD, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(classNames, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(AudioClassifierManager.getMtWin(), fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(AudioClassifierManager.getMtStep(), fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(AudioClassifierManager.getStWin(), fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(AudioClassifierManager.getStStep(), fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(AudioClassifierManager.__compute_beat, fo, protocol=cPickle.HIGHEST_PROTOCOL)
fo.close()
@staticmethod
def getResultMatrixAndBestParam(features, class_names, classifier_name, parameterMode, perTrain=0.90, model_name='',Params=[]):
'''
ARGUMENTS:
features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features.
each matrix features[i] of class i is [n_samples x numOfDimensions]
class_names: list of class names (strings)
n_exp: number of cross-validation experiments
classifier_name: svm or knn or randomforest
Params: list of classifier parameters (for parameter tuning during cross-validation)
parameterMode: 0: choose parameters that lead to maximum overall classification ACCURACY
1: choose parameters that lead to maximum overall f1 MEASURE
RETURNS:
bestParam: the value of the input parameter that optimizes the selected performance measure
confufionMatrix
'''
# feature normalization:
(features_norm, MEAN, STD) = aT.normalizeFeatures(features)
n_classes = len(features)
ac_all = []
f1_all = []
precision_classes_all = []
recall_classes_all = []
f1_classes_all = []
cms_all = []
smooth = 0.0000000010
# Optimize number of experiment
n_exp = AudioClassifierManager.getOptimalNumberExperiment(features,AudioClassifierManager.__num_experiment)
Params = AudioClassifierManager.getListParamsForClassifierType(classifier_name) if len(Params)==0 else Params
# For each param value
for Ci, C in enumerate(Params):
# Init confusion matrix
cm = numpy.zeros((n_classes, n_classes))
for e in range(n_exp):
# Split features in Train and Test:
f_train, f_test = aT.randSplitFeatures(features_norm, perTrain)
countFTrain = 0
countFTest = 0
for g in f_train:
for track in g:
countFTrain += 1
for g in f_test:
for track in g:
countFTest += 1
if(countFTest == 0):
print("WARNING: {0} has no test values".format(class_names[Ci]))
# for each cross-validation iteration:
print("Param = {0:.5f} - classifier Evaluation "
"Experiment {1:d} of {2:d} - lenTrainingSet {3} lenTestSet {4}".format(C, e + 1, n_exp,
countFTrain,
countFTest))
# Get Classifier for train
classifier = AudioClassifierManager.getTrainClassifier(f_train,classifier_name,C)
cmt = numpy.zeros((n_classes, n_classes))
for c1 in range(n_classes):
#print("==> Class {1}: {0} for exp {2}".format(class_names[c1],c1,e))
n_test_samples = len(f_test[c1])
res = numpy.zeros((n_test_samples, 1))
for ss in range(n_test_samples):
[res[ss], _] = aT.classifierWrapper(classifier,
classifier_name,
f_test[c1][ss])
for c2 in range(n_classes):
nnzero = numpy.nonzero(res == c2)[0]
rlen = len(nnzero)
cmt[c1][c2] = float(rlen)
#print("cmt[{0}][{1}] = {2}".format(c1,c2,float(rlen)))
cm = cm + cmt
cm = cm + smooth
rec = numpy.zeros((cm.shape[0],))
pre = numpy.zeros((cm.shape[0],))
# Calculate Precision, Recall and f1 Misure
for ci in range(cm.shape[0]):
rec[ci] = cm[ci, ci] / numpy.sum(cm[ci, :])
pre[ci] = cm[ci, ci] / numpy.sum(cm[:, ci])
precision_classes_all.append(pre)
recall_classes_all.append(rec)
f1 = 2 * rec * pre / (rec + pre)
f1_classes_all.append(f1)
ac_all.append(numpy.sum(numpy.diagonal(cm)) / numpy.sum(cm))
cms_all.append(cm)
f1_all.append(numpy.mean(f1))
best_ac_ind = numpy.argmax(ac_all)
best_f1_ind = numpy.argmax(f1_all)
bestParam = 0
resultConfusionMatrix = None
if parameterMode == AudioClassifierManager.BEST_ACCURACY:
bestParam = Params[best_ac_ind]
resultConfusionMatrix = cms_all[best_ac_ind]
elif parameterMode == AudioClassifierManager.BEST_F1:
bestParam = Params[best_f1_ind]
resultConfusionMatrix = cms_all[best_f1_ind]
return bestParam, resultConfusionMatrix, precision_classes_all, recall_classes_all, f1_classes_all, f1_all, ac_all
@staticmethod
def saveConfusionMatrix(cm, class_names, classifier_name='ns'):
'''
This function prints a confusion matrix for a particular classification task.
ARGUMENTS:
cm: a 2-D numpy array of the confusion matrix
(cm[i,j] is the number of times a sample from class i was classified in class j)
class_names: a list that contains the names of the classes
'''
cmCsv = numpy.empty([len(class_names), len(class_names)])
header = [] # header of matrix
header.append("/")
if cm.shape[0] != len(class_names):
print("printConfusionMatrix: Wrong argument sizes\n")
return
for i, c in enumerate(class_names):
header.append(c)
for i, c in enumerate(class_names):
for j in range(len(class_names)):
val = 100.0 * cm[i][j] / numpy.sum(cm)
cmCsv[i][j] = format(val, '.2f')
# Save as csv
out = numpy.column_stack([class_names, cmCsv])
out1 = numpy.row_stack([header, out])
numpy.savetxt('confusion_matrix_{0}.csv'.format(classifier_name), out1, delimiter=',', fmt="%s")
utils.save_plot(utils.get_confusion_matrix(cmCsv, class_names, class_names),
"confusion_matrix_{0}".format(classifier_name))
@staticmethod
def saveParamsFromClassification(class_names,Params,model_name,precision_classes_all,recall_classes_all,f1_classes_all,ac_all,f1_all):
'''
:param class_names: Name of classes
:param Params: Params for model type
:param model_name: Name of model of classification
:param precision_classes_all: list of precisions
:param recall_classes_all: list of recalls
:param f1_classes_all: list of f1
:param ac_all: accuracy for param
:param f1_all: mean f1 for param
'''
header = []
headerParams = []
matrixValues = numpy.zeros([len(Params), (len(class_names) * 3) + 5])
header.append("/")
for i, c in enumerate(class_names):
header.extend([c, "", ""])
header.extend(["OVERALL", "", "", ""])
headerParams.append("C")
for c in class_names:
headerParams.extend(["PRE", "REC", "f1"])
headerParams.extend(["ACC", "f1", "best ACC", "best f1"])
best_ac_ind = numpy.argmax(ac_all)
best_f1_ind = numpy.argmax(f1_all)
for i in range(len(precision_classes_all)):
matrixValues[i][0] = Params[i]
for c in range(len(precision_classes_all[i])):
curr_pr = 100.0 * precision_classes_all[i][c]
curr_rec = 100.0 * recall_classes_all[i][c]
curr_f1 = 100.0 * f1_classes_all[i][c]
matrixValues[i][(c * 3) + 1] = curr_pr
matrixValues[i][(c * 3) + 2] = curr_rec
matrixValues[i][(c * 3) + 3] = curr_f1
curr_acc_all = 100.0 * ac_all[i]
curr_f1_all = 100.0 * f1_all[i]
matrixValues[i][(len(class_names) * 3) + 1] = curr_acc_all
matrixValues[i][(len(class_names) * 3) + 2] = curr_f1_all
if i == best_f1_ind:
matrixValues[i][(len(class_names) * 3) + 4] = 1
if i == best_ac_ind:
matrixValues[i][(len(class_names) * 3) + 3] = 1
out = numpy.row_stack([headerParams, matrixValues])
out1 = numpy.row_stack([header, out])
numpy.savetxt('params_{0}.csv'.format(model_name), out1, delimiter=',', fmt="%s")