-
Notifications
You must be signed in to change notification settings - Fork 9
/
audioTrainTest.py.bak
1147 lines (975 loc) · 45.5 KB
/
audioTrainTest.py.bak
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import sys
import numpy
import time
import os
import glob
import cPickle
import shutil
import audioop
import signal
import csv
import ntpath
import audioFeatureExtraction as aF
import audioBasicIO
from matplotlib.mlab import find
import matplotlib.pyplot as plt
import scipy.io as sIO
from scipy import linalg as la
from scipy.spatial import distance
import sklearn.svm
import sklearn.decomposition
import sklearn.ensemble
def signal_handler(signal, frame):
print 'You pressed Ctrl+C! - EXIT'
os.system("stty -cbreak echo")
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
shortTermWindow = 0.050
shortTermStep = 0.050
eps = 0.00000001
class kNN:
def __init__(self, X, Y, k):
self.X = X
self.Y = Y
self.k = k
def classify(self, testSample):
nClasses = numpy.unique(self.Y).shape[0]
YDist = (distance.cdist(self.X, testSample.reshape(1, testSample.shape[0]), 'euclidean')).T
iSort = numpy.argsort(YDist)
P = numpy.zeros((nClasses,))
for i in range(nClasses):
P[i] = numpy.nonzero(self.Y[iSort[0][0:self.k]] == i)[0].shape[0] / float(self.k)
return (numpy.argmax(P), P)
def classifierWrapper(classifier, classifierType, testSample):
'''
This function is used as a wrapper to pattern classification.
ARGUMENTS:
- classifier: a classifier object of type sklearn.svm.SVC or kNN (defined in this library) or sklearn.ensemble.RandomForestClassifier or sklearn.ensemble.GradientBoostingClassifier or sklearn.ensemble.ExtraTreesClassifier
- classifierType: "svm" or "knn" or "randomforests" or "gradientboosting" or "extratrees"
- testSample: a feature vector (numpy array)
RETURNS:
- R: class ID
- P: probability estimate
EXAMPLE (for some audio signal stored in array x):
import audioFeatureExtraction as aF
import audioTrainTest as aT
# load the classifier (here SVM, for kNN use loadKNNModel instead):
[Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep] = aT.loadSVModel(modelName)
# mid-term feature extraction:
[MidTermFeatures, _] = aF.mtFeatureExtraction(x, Fs, mtWin * Fs, mtStep * Fs, round(Fs*stWin), round(Fs*stStep));
# feature normalization:
curFV = (MidTermFeatures[:, i] - MEAN) / STD;
# classification
[Result, P] = classifierWrapper(Classifier, modelType, curFV)
'''
R = -1
P = -1
if classifierType == "knn":
[R, P] = classifier.classify(testSample)
elif classifierType == "svm" or classifierType == "randomforest" or classifierType == "gradientboosting" or "extratrees":
R = classifier.predict(testSample.reshape(1,-1))[0]
P = classifier.predict_proba(testSample.reshape(1,-1))[0]
return [R, P]
def regressionWrapper(model, modelType, testSample):
'''
This function is used as a wrapper to pattern classification.
ARGUMENTS:
- model: regression model
- modelType: "svm" or "knn" (TODO)
- testSample: a feature vector (numpy array)
RETURNS:
- R: regression result (estimated value)
EXAMPLE (for some audio signal stored in array x):
TODO
'''
if modelType == "svm" or modelType == "randomforest" or modelType == "svm_rbf":
return (model.predict(testSample.reshape(1,-1))[0])
# elif classifierType == "knn":
# TODO
return None
def randSplitFeatures(features, partTrain):
'''
def randSplitFeatures(features):
This function splits a feature set for training and testing.
ARGUMENTS:
- features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features.
each matrix features[i] of class i is [numOfSamples x numOfDimensions]
- partTrain: percentage
RETURNS:
- featuresTrains: a list of training data for each class
- featuresTest: a list of testing data for each class
'''
featuresTrain = []
featuresTest = []
for i, f in enumerate(features):
[numOfSamples, numOfDims] = f.shape
randperm = numpy.random.permutation(range(numOfSamples))
nTrainSamples = int(round(partTrain * numOfSamples))
featuresTrain.append(f[randperm[0:nTrainSamples]])
featuresTest.append(f[randperm[nTrainSamples::]])
return (featuresTrain, featuresTest)
def trainKNN(features, K):
'''
Train a kNN classifier.
ARGUMENTS:
- features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features.
each matrix features[i] of class i is [numOfSamples x numOfDimensions]
- K: parameter K
RETURNS:
- kNN: the trained kNN variable
'''
[Xt, Yt] = listOfFeatures2Matrix(features)
knn = kNN(Xt, Yt, K)
return knn
def trainSVM(features, Cparam):
'''
Train a multi-class probabilitistic SVM classifier.
Note: This function is simply a wrapper to the sklearn functionality for SVM training
See function trainSVM_feature() to use a wrapper on both the feature extraction and the SVM training (and parameter tuning) processes.
ARGUMENTS:
- features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features
each matrix features[i] of class i is [numOfSamples x numOfDimensions]
- Cparam: SVM parameter C (cost of constraints violation)
RETURNS:
- svm: the trained SVM variable
NOTE:
This function trains a linear-kernel SVM for a given C value. For a different kernel, other types of parameters should be provided.
'''
[X, Y] = listOfFeatures2Matrix(features)
svm = sklearn.svm.SVC(C = Cparam, kernel = 'linear', probability = True)
svm.fit(X,Y)
return svm
def trainSVM_RBF(features, Cparam):
'''
Train a multi-class probabilitistic SVM classifier.
Note: This function is simply a wrapper to the sklearn functionality for SVM training
See function trainSVM_feature() to use a wrapper on both the feature extraction and the SVM training (and parameter tuning) processes.
ARGUMENTS:
- features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features
each matrix features[i] of class i is [numOfSamples x numOfDimensions]
- Cparam: SVM parameter C (cost of constraints violation)
RETURNS:
- svm: the trained SVM variable
NOTE:
This function trains a linear-kernel SVM for a given C value. For a different kernel, other types of parameters should be provided.
'''
[X, Y] = listOfFeatures2Matrix(features)
svm = sklearn.svm.SVC(C = Cparam, kernel = 'rbf', probability = True)
svm.fit(X,Y)
return svm
def trainRandomForest(features, n_estimators):
'''
Train a multi-class decision tree classifier.
Note: This function is simply a wrapper to the sklearn functionality for SVM training
See function trainSVM_feature() to use a wrapper on both the feature extraction and the SVM training (and parameter tuning) processes.
ARGUMENTS:
- features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features
each matrix features[i] of class i is [numOfSamples x numOfDimensions]
- n_estimators: number of trees in the forest
RETURNS:
- svm: the trained SVM variable
NOTE:
This function trains a linear-kernel SVM for a given C value. For a different kernel, other types of parameters should be provided.
'''
[X, Y] = listOfFeatures2Matrix(features)
rf = sklearn.ensemble.RandomForestClassifier(n_estimators = n_estimators)
rf.fit(X,Y)
return rf
def trainGradientBoosting(features, n_estimators):
'''
Train a gradient boosting classifier
Note: This function is simply a wrapper to the sklearn functionality for SVM training
See function trainSVM_feature() to use a wrapper on both the feature extraction and the SVM training (and parameter tuning) processes.
ARGUMENTS:
- features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features
each matrix features[i] of class i is [numOfSamples x numOfDimensions]
- n_estimators: number of trees in the forest
RETURNS:
- svm: the trained SVM variable
NOTE:
This function trains a linear-kernel SVM for a given C value. For a different kernel, other types of parameters should be provided.
'''
[X, Y] = listOfFeatures2Matrix(features)
rf = sklearn.ensemble.GradientBoostingClassifier(n_estimators = n_estimators)
rf.fit(X,Y)
return rf
def trainExtraTrees(features, n_estimators):
'''
Train a gradient boosting classifier
Note: This function is simply a wrapper to the sklearn functionality for extra tree classifiers
See function trainSVM_feature() to use a wrapper on both the feature extraction and the SVM training (and parameter tuning) processes.
ARGUMENTS:
- features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features
each matrix features[i] of class i is [numOfSamples x numOfDimensions]
- n_estimators: number of trees in the forest
RETURNS:
- svm: the trained SVM variable
NOTE:
This function trains a linear-kernel SVM for a given C value. For a different kernel, other types of parameters should be provided.
'''
[X, Y] = listOfFeatures2Matrix(features)
et = sklearn.ensemble.ExtraTreesClassifier(n_estimators = n_estimators)
et.fit(X,Y)
return et
def trainSVMregression(Features, Y, Cparam):
svm = sklearn.svm.SVR(C = Cparam, kernel = 'linear')
svm.fit(Features,Y)
trainError = numpy.mean(numpy.abs(svm.predict(Features) - Y))
return svm, trainError
def trainSVMregression_rbf(Features, Y, Cparam):
svm = sklearn.svm.SVR(C = Cparam, kernel = 'rbf')
svm.fit(Features,Y)
trainError = numpy.mean(numpy.abs(svm.predict(Features) - Y))
return svm, trainError
def trainRandomForestRegression(Features, Y, n_estimators):
rf = sklearn.ensemble.RandomForestRegressor(n_estimators = n_estimators)
rf.fit(Features,Y)
trainError = numpy.mean(numpy.abs(rf.predict(Features) - Y))
return rf, trainError
def featureAndTrain(listOfDirs, mtWin, mtStep, stWin, stStep, classifierType, modelName, computeBEAT=False, perTrain=0.90):
'''
This function is used as a wrapper to segment-based audio feature extraction and classifier training.
ARGUMENTS:
listOfDirs: list of paths of directories. Each directory contains a signle audio class whose samples are stored in seperate WAV files.
mtWin, mtStep: mid-term window length and step
stWin, stStep: short-term window and step
classifierType: "svm" or "knn" or "randomforest" or "gradientboosting" or "extratrees"
modelName: name of the model to be saved
RETURNS:
None. Resulting classifier along with the respective model parameters are saved on files.
'''
# STEP A: Feature Extraction:
[features, classNames, _] = aF.dirsWavFeatureExtraction(listOfDirs, mtWin, mtStep, stWin, stStep, computeBEAT=computeBEAT)
if len(features) == 0:
print "trainSVM_feature ERROR: No data found in any input folder!"
return
numOfFeatures = features[0].shape[1]
featureNames = ["features" + str(d + 1) for d in range(numOfFeatures)]
writeTrainDataToARFF(modelName, features, classNames, featureNames)
for i, f in enumerate(features):
if len(f) == 0:
print "trainSVM_feature ERROR: " + listOfDirs[i] + " folder is empty or non-existing!"
return
# STEP B: Classifier Evaluation and Parameter Selection:
if classifierType == "svm" or classifierType == "svm_rbf":
classifierParams = numpy.array([0.001, 0.01, 0.5, 1.0, 5.0, 10.0, 20.0])
elif classifierType == "randomforest":
classifierParams = numpy.array([10, 25, 50, 100,200,500])
elif classifierType == "knn":
classifierParams = numpy.array([1, 3, 5, 7, 9, 11, 13, 15])
elif classifierType == "gradientboosting":
classifierParams = numpy.array([10, 25, 50, 100,200,500])
elif classifierType == "extratrees":
classifierParams = numpy.array([10, 25, 50, 100,200,500])
# get optimal classifeir parameter:
features2 = []
for f in features:
fTemp = []
for i in range(f.shape[0]):
temp = f[i,:]
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"
features2.append(numpy.array(fTemp))
features = features2
bestParam = evaluateClassifier(features, classNames, 100, classifierType, classifierParams, 0, perTrain)
print "Selected params: {0:.5f}".format(bestParam)
C = len(classNames)
[featuresNorm, MEAN, STD] = normalizeFeatures(features) # normalize features
MEAN = MEAN.tolist()
STD = STD.tolist()
featuresNew = featuresNorm
# STEP C: Save the classifier to file
if classifierType == "svm":
Classifier = trainSVM(featuresNew, bestParam)
elif classifierType == "svm_rbf":
Classifier = trainSVM_RBF(featuresNew, bestParam)
elif classifierType == "randomforest":
Classifier = trainRandomForest(featuresNew, bestParam)
elif classifierType == "gradientboosting":
Classifier = trainGradientBoosting(featuresNew, bestParam)
elif classifierType == "extratrees":
Classifier = trainExtraTrees(featuresNew, bestParam)
if classifierType == "knn":
[X, Y] = listOfFeatures2Matrix(featuresNew)
X = X.tolist()
Y = Y.tolist()
fo = open(modelName, "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(mtWin, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(mtStep, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(stWin, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(stStep, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(computeBEAT, fo, protocol=cPickle.HIGHEST_PROTOCOL)
fo.close()
elif classifierType == "svm" or classifierType == "svm_rbf" or classifierType == "randomforest" or classifierType == "gradientboosting" or classifierType == "extratrees":
with open(modelName, 'wb') as fid: # save to file
cPickle.dump(Classifier, fid)
fo = open(modelName + "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(mtWin, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(mtStep, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(stWin, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(stStep, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(computeBEAT, fo, protocol=cPickle.HIGHEST_PROTOCOL)
fo.close()
def featureAndTrainRegression(dirName, mtWin, mtStep, stWin, stStep, modelType, modelName, computeBEAT=False):
'''
This function is used as a wrapper to segment-based audio feature extraction and classifier training.
ARGUMENTS:
dirName: path of directory containing the WAV files and Regression CSVs
mtWin, mtStep: mid-term window length and step
stWin, stStep: short-term window and step
modelType: "svm" or "knn" or "randomforest"
modelName: name of the model to be saved
RETURNS:
None. Resulting regression model along with the respective model parameters are saved on files.
'''
# STEP A: Feature Extraction:
[features, _, fileNames] = aF.dirsWavFeatureExtraction([dirName], mtWin, mtStep, stWin, stStep, computeBEAT=computeBEAT)
features = features[0]
fileNames = [ntpath.basename(f) for f in fileNames[0]]
featuresFinal = []
# Read CSVs:
CSVs = glob.glob(dirName + os.sep + "*.csv")
regressionLabels = []
regressionNames = []
featuresFinal = []
for c in CSVs: # for each CSV
#curRegressionLabels = numpy.zeros((len(fileNames, ))) # read filenames, map to "fileNames" and append respective values in the regressionLabels
curRegressionLabels = []
featuresTemp = []
with open(c, 'rb') as csvfile: # open the csv file that contains the current target value's annotations
CSVreader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in CSVreader:
if len(row) == 2: # if the current row contains two fields (filename, target value)
if row[0] in fileNames: # ... and if the current filename exists in the list of filenames
index = fileNames.index(row[0])
#curRegressionLabels[index] = float(row[1])
curRegressionLabels.append(float(row[1]))
featuresTemp.append(features[index,:])
featuresFinal.append(numpy.array(featuresTemp))
regressionLabels.append(numpy.array(curRegressionLabels)) # curRegressionLabels is the list of values for the current regression problem
regressionNames.append(ntpath.basename(c).replace(".csv", "")) # regression task name
if len(features) == 0:
print "ERROR: No data found in any input folder!"
return
numOfFeatures = featuresFinal[0].shape[1]
# TODO: ARRF WRITE????
# STEP B: Classifier Evaluation and Parameter Selection:
if modelType == "svm" or modelType == "svm_rbf":
modelParams = numpy.array([0.001, 0.005, 0.01, 0.05, 0.1, 0.25, 0.5, 1.0, 5.0, 10.0])
elif modelType == "randomforest":
modelParams = numpy.array([5, 10, 25, 50, 100])
# elif modelType == "knn":
# modelParams = numpy.array([1, 3, 5, 7, 9, 11, 13, 15]);
errors = []
errorsBase = []
bestParams = []
for iRegression, r in enumerate(regressionNames):
# get optimal classifeir parameter:
print "Regression task " + r
bestParam, error, berror = evaluateRegression(featuresFinal[iRegression], regressionLabels[iRegression], 100, modelType, modelParams)
errors.append(error)
errorsBase.append(berror)
bestParams.append(bestParam)
print "Selected params: {0:.5f}".format(bestParam)
[featuresNorm, MEAN, STD] = normalizeFeatures([featuresFinal[iRegression]]) # normalize features
# STEP C: Save the model to file
if modelType == "svm":
Classifier, _ = trainSVMregression(featuresNorm[0], regressionLabels[iRegression], bestParam)
if modelType == "svm_rbf":
Classifier, _ = trainSVMregression_rbf(featuresNorm[0], regressionLabels[iRegression], bestParam)
if modelType == "randomforest":
Classifier, _ = trainRandomForestRegression(featuresNorm[0], regressionLabels[iRegression], bestParam)
if modelType == "svm" or modelType == "svm_rbf" or modelType == "randomforest":
with open(modelName + "_" + r, 'wb') as fid: # save to file
cPickle.dump(Classifier, fid)
fo = open(modelName + "_" + r + "MEANS", "wb")
cPickle.dump(MEAN, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(STD, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(mtWin, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(mtStep, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(stWin, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(stStep, fo, protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(computeBEAT, fo, protocol=cPickle.HIGHEST_PROTOCOL)
fo.close()
return errors, errorsBase, bestParams
def loadKNNModel(kNNModelName, isRegression=False):
try:
fo = open(kNNModelName, "rb")
except IOError:
print "didn't find file"
return
try:
X = cPickle.load(fo)
Y = cPickle.load(fo)
MEAN = cPickle.load(fo)
STD = cPickle.load(fo)
if not isRegression:
classNames = cPickle.load(fo)
K = cPickle.load(fo)
mtWin = cPickle.load(fo)
mtStep = cPickle.load(fo)
stWin = cPickle.load(fo)
stStep = cPickle.load(fo)
computeBEAT = cPickle.load(fo)
except:
fo.close()
fo.close()
X = numpy.array(X)
Y = numpy.array(Y)
MEAN = numpy.array(MEAN)
STD = numpy.array(STD)
Classifier = kNN(X, Y, K) # Note: a direct call to the kNN constructor is used here
if isRegression:
return(Classifier, MEAN, STD, mtWin, mtStep, stWin, stStep, computeBEAT)
else:
return(Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT)
def loadSVModel(SVMmodelName, isRegression=False):
'''
This function loads an SVM model either for classification or training.
ARGMUMENTS:
- SVMmodelName: the path of the model to be loaded
- isRegression: a flag indigating whereas this model is regression or not
'''
try:
fo = open(SVMmodelName+"MEANS", "rb")
except IOError:
print "Load SVM Model: Didn't find file"
return
try:
MEAN = cPickle.load(fo)
STD = cPickle.load(fo)
if not isRegression:
classNames = cPickle.load(fo)
mtWin = cPickle.load(fo)
mtStep = cPickle.load(fo)
stWin = cPickle.load(fo)
stStep = cPickle.load(fo)
computeBEAT = cPickle.load(fo)
except:
fo.close()
fo.close()
MEAN = numpy.array(MEAN)
STD = numpy.array(STD)
COEFF = []
with open(SVMmodelName, 'rb') as fid:
SVM = cPickle.load(fid)
if isRegression:
return(SVM, MEAN, STD, mtWin, mtStep, stWin, stStep, computeBEAT)
else:
return(SVM, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT)
def loadRandomForestModel(RFmodelName, isRegression=False):
'''
This function loads an SVM model either for classification or training.
ARGMUMENTS:
- SVMmodelName: the path of the model to be loaded
- isRegression: a flag indigating whereas this model is regression or not
'''
try:
fo = open(RFmodelName+"MEANS", "rb")
except IOError:
print "Load Random Forest Model: Didn't find file"
return
try:
MEAN = cPickle.load(fo)
STD = cPickle.load(fo)
if not isRegression:
classNames = cPickle.load(fo)
mtWin = cPickle.load(fo)
mtStep = cPickle.load(fo)
stWin = cPickle.load(fo)
stStep = cPickle.load(fo)
computeBEAT = cPickle.load(fo)
except:
fo.close()
fo.close()
MEAN = numpy.array(MEAN)
STD = numpy.array(STD)
COEFF = []
with open(RFmodelName, 'rb') as fid:
RF = cPickle.load(fid)
if isRegression:
return(RF, MEAN, STD, mtWin, mtStep, stWin, stStep, computeBEAT)
else:
return(RF, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT)
def loadGradientBoostingModel(GBModelName, isRegression=False):
'''
This function loads gradient boosting either for classification or training.
ARGMUMENTS:
- SVMmodelName: the path of the model to be loaded
- isRegression: a flag indigating whereas this model is regression or not
'''
try:
fo = open(GBModelName+"MEANS", "rb")
except IOError:
print "Load Random Forest Model: Didn't find file"
return
try:
MEAN = cPickle.load(fo)
STD = cPickle.load(fo)
if not isRegression:
classNames = cPickle.load(fo)
mtWin = cPickle.load(fo)
mtStep = cPickle.load(fo)
stWin = cPickle.load(fo)
stStep = cPickle.load(fo)
computeBEAT = cPickle.load(fo)
except:
fo.close()
fo.close()
MEAN = numpy.array(MEAN)
STD = numpy.array(STD)
COEFF = []
with open(GBModelName, 'rb') as fid:
GB = cPickle.load(fid)
if isRegression:
return(GB, MEAN, STD, mtWin, mtStep, stWin, stStep, computeBEAT)
else:
return(GB, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT)
def loadExtraTreesModel(ETmodelName, isRegression=False):
'''
This function loads extra trees either for classification or training.
ARGMUMENTS:
- SVMmodelName: the path of the model to be loaded
- isRegression: a flag indigating whereas this model is regression or not
'''
try:
fo = open(ETmodelName+"MEANS", "rb")
except IOError:
print "Load Random Forest Model: Didn't find file"
return
try:
MEAN = cPickle.load(fo)
STD = cPickle.load(fo)
if not isRegression:
classNames = cPickle.load(fo)
mtWin = cPickle.load(fo)
mtStep = cPickle.load(fo)
stWin = cPickle.load(fo)
stStep = cPickle.load(fo)
computeBEAT = cPickle.load(fo)
except:
fo.close()
fo.close()
MEAN = numpy.array(MEAN)
STD = numpy.array(STD)
COEFF = []
with open(ETmodelName, 'rb') as fid:
GB = cPickle.load(fid)
if isRegression:
return(GB, MEAN, STD, mtWin, mtStep, stWin, stStep, computeBEAT)
else:
return(GB, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT)
def evaluateClassifier(features, ClassNames, nExp, ClassifierName, Params, parameterMode, perTrain=0.90):
'''
ARGUMENTS:
features: a list ([numOfClasses x 1]) whose elements containt numpy matrices of features.
each matrix features[i] of class i is [numOfSamples x numOfDimensions]
ClassNames: list of class names (strings)
nExp: number of cross-validation experiments
ClassifierName: 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
'''
# feature normalization:
(featuresNorm, MEAN, STD) = normalizeFeatures(features)
#featuresNorm = features;
nClasses = len(features)
CAll = []
acAll = []
F1All = []
PrecisionClassesAll = []
RecallClassesAll = []
ClassesAll = []
F1ClassesAll = []
CMsAll = []
# compute total number of samples:
nSamplesTotal = 0
for f in features:
nSamplesTotal += f.shape[0]
if nSamplesTotal > 1000 and nExp > 50:
nExp = 50
print "Number of training experiments changed to 50 due to high number of samples"
if nSamplesTotal > 2000 and nExp > 10:
nExp = 10
print "Number of training experiments changed to 10 due to high number of samples"
for Ci, C in enumerate(Params): # for each param value
CM = numpy.zeros((nClasses, nClasses))
for e in range(nExp): # for each cross-validation iteration:
print "Param = {0:.5f} - Classifier Evaluation Experiment {1:d} of {2:d}".format(C, e+1, nExp)
# split features:
featuresTrain, featuresTest = randSplitFeatures(featuresNorm, perTrain)
# train multi-class svms:
if ClassifierName == "svm":
Classifier = trainSVM(featuresTrain, C)
elif ClassifierName == "svm_rbf":
Classifier = trainSVM_RBF(featuresTrain, C)
elif ClassifierName == "knn":
Classifier = trainKNN(featuresTrain, C)
elif ClassifierName == "randomforest":
Classifier = trainRandomForest(featuresTrain, C)
elif ClassifierName == "gradientboosting":
Classifier = trainGradientBoosting(featuresTrain, C)
elif ClassifierName == "extratrees":
Classifier = trainExtraTrees(featuresTrain, C)
CMt = numpy.zeros((nClasses, nClasses))
for c1 in range(nClasses):
#Results = Classifier.pred(featuresTest[c1])
nTestSamples = len(featuresTest[c1])
Results = numpy.zeros((nTestSamples, 1))
for ss in range(nTestSamples):
[Results[ss], _] = classifierWrapper(Classifier, ClassifierName, featuresTest[c1][ss])
for c2 in range(nClasses):
CMt[c1][c2] = float(len(numpy.nonzero(Results == c2)[0]))
CM = CM + CMt
CM = CM + 0.0000000010
Rec = numpy.zeros((CM.shape[0], ))
Pre = numpy.zeros((CM.shape[0], ))
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])
PrecisionClassesAll.append(Pre)
RecallClassesAll.append(Rec)
F1 = 2 * Rec * Pre / (Rec + Pre)
F1ClassesAll.append(F1)
acAll.append(numpy.sum(numpy.diagonal(CM)) / numpy.sum(CM))
CMsAll.append(CM)
F1All.append(numpy.mean(F1))
# print "{0:6.4f}{1:6.4f}{2:6.1f}{3:6.1f}".format(nu, g, 100.0*acAll[-1], 100.0*F1All[-1])
print ("\t\t"),
for i, c in enumerate(ClassNames):
if i == len(ClassNames)-1:
print "{0:s}\t\t".format(c),
else:
print "{0:s}\t\t\t".format(c),
print ("OVERALL")
print ("\tC"),
for c in ClassNames:
print "\tPRE\tREC\tF1",
print "\t{0:s}\t{1:s}".format("ACC", "F1")
bestAcInd = numpy.argmax(acAll)
bestF1Ind = numpy.argmax(F1All)
for i in range(len(PrecisionClassesAll)):
print "\t{0:.3f}".format(Params[i]),
for c in range(len(PrecisionClassesAll[i])):
print "\t{0:.1f}\t{1:.1f}\t{2:.1f}".format(100.0 * PrecisionClassesAll[i][c], 100.0 * RecallClassesAll[i][c], 100.0 * F1ClassesAll[i][c]),
print "\t{0:.1f}\t{1:.1f}".format(100.0 * acAll[i], 100.0 * F1All[i]),
if i == bestF1Ind:
print "\t best F1",
if i == bestAcInd:
print "\t best Acc",
print
if parameterMode == 0: # keep parameters that maximize overall classification accuracy:
print "Confusion Matrix:"
printConfusionMatrix(CMsAll[bestAcInd], ClassNames)
return Params[bestAcInd]
elif parameterMode == 1: # keep parameters that maximize overall F1 measure:
print "Confusion Matrix:"
printConfusionMatrix(CMsAll[bestF1Ind], ClassNames)
return Params[bestF1Ind]
def evaluateRegression(features, labels, nExp, MethodName, Params):
'''
ARGUMENTS:
features: numpy matrices of features [numOfSamples x numOfDimensions]
labels: list of sample labels
nExp: number of cross-validation experiments
MethodName: "svm" or "randomforest"
Params: list of classifier params to be evaluated
RETURNS:
bestParam: the value of the input parameter that optimizes the selected performance measure
'''
# feature normalization:
(featuresNorm, MEAN, STD) = normalizeFeatures([features])
featuresNorm = featuresNorm[0]
nSamples = labels.shape[0]
partTrain = 0.9
ErrorsAll = []
ErrorsTrainAll = []
ErrorsBaselineAll = []
for Ci, C in enumerate(Params): # for each param value
Errors = []
ErrorsTrain = []
ErrorsBaseline = []
for e in range(nExp): # for each cross-validation iteration:
# split features:
randperm = numpy.random.permutation(range(nSamples))
nTrain = int(round(partTrain * nSamples))
featuresTrain = [featuresNorm[randperm[i]] for i in range(nTrain)]
featuresTest = [featuresNorm[randperm[i+nTrain]] for i in range(nSamples - nTrain)]
labelsTrain = [labels[randperm[i]] for i in range(nTrain)]
labelsTest = [labels[randperm[i + nTrain]] for i in range(nSamples - nTrain)]
# train multi-class svms:
featuresTrain = numpy.matrix(featuresTrain)
if MethodName == "svm":
[Classifier, trainError] = trainSVMregression(featuresTrain, labelsTrain, C)
elif MethodName == "svm_rbf":
[Classifier, trainError] = trainSVMregression_rbf(featuresTrain, labelsTrain, C)
elif MethodName == "randomforest":
[Classifier, trainError] = trainRandomForestRegression(featuresTrain, labelsTrain, C)
ErrorTest = []
ErrorTestBaseline = []
for itest, fTest in enumerate(featuresTest):
R = regressionWrapper(Classifier, MethodName, fTest)
Rbaseline = numpy.mean(labelsTrain)
ErrorTest.append((R - labelsTest[itest]) * (R - labelsTest[itest]))
ErrorTestBaseline.append((Rbaseline - labelsTest[itest]) * (Rbaseline - labelsTest[itest]))
Error = numpy.array(ErrorTest).mean()
ErrorBaseline = numpy.array(ErrorTestBaseline).mean()
Errors.append(Error)
ErrorsTrain.append(trainError)
ErrorsBaseline.append(ErrorBaseline)
ErrorsAll.append(numpy.array(Errors).mean())
ErrorsTrainAll.append(numpy.array(ErrorsTrain).mean())
ErrorsBaselineAll.append(numpy.array(ErrorsBaseline).mean())
bestInd = numpy.argmin(ErrorsAll)
print "{0:s}\t\t{1:s}\t\t{2:s}\t\t{3:s}".format("Param", "MSE", "T-MSE", "R-MSE")
for i in range(len(ErrorsAll)):
print "{0:.4f}\t\t{1:.2f}\t\t{2:.2f}\t\t{3:.2f}".format(Params[i], ErrorsAll[i], ErrorsTrainAll[i], ErrorsBaselineAll[i]),
if i == bestInd:
print "\t\t best",
print
return Params[bestInd], ErrorsAll[bestInd], ErrorsBaselineAll[bestInd]
def printConfusionMatrix(CM, ClassNames):
'''
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)
ClassNames: a list that contains the names of the classes
'''
if CM.shape[0] != len(ClassNames):
print "printConfusionMatrix: Wrong argument sizes\n"
return
for c in ClassNames:
if len(c) > 4:
c = c[0:3]
print "\t{0:s}".format(c),
print
for i, c in enumerate(ClassNames):
if len(c) > 4:
c = c[0:3]
print "{0:s}".format(c),
for j in range(len(ClassNames)):
print "\t{0:.2f}".format(100.0 * CM[i][j] / numpy.sum(CM)),
print
def normalizeFeatures(features):
'''
This function normalizes a feature set to 0-mean and 1-std.
Used in most classifier trainning cases.
ARGUMENTS:
- features: list of feature matrices (each one of them is a numpy matrix)
RETURNS:
- featuresNorm: list of NORMALIZED feature matrices
- MEAN: mean vector
- STD: std vector
'''
X = numpy.array([])
for count, f in enumerate(features):
if f.shape[0] > 0:
if count == 0:
X = f
else:
X = numpy.vstack((X, f))
count += 1
MEAN = numpy.mean(X, axis=0) + 0.00000000000001;
STD = numpy.std(X, axis=0) + 0.00000000000001;
featuresNorm = []
for f in features:
ft = f.copy()
for nSamples in range(f.shape[0]):
ft[nSamples, :] = (ft[nSamples, :] - MEAN) / STD
featuresNorm.append(ft)
return (featuresNorm, MEAN, STD)
def listOfFeatures2Matrix(features):
'''
listOfFeatures2Matrix(features)
This function takes a list of feature matrices as argument and returns a single concatenated feature matrix and the respective class labels.
ARGUMENTS:
- features: a list of feature matrices
RETURNS:
- X: a concatenated matrix of features
- Y: a vector of class indeces
'''
X = numpy.array([])
Y = numpy.array([])
for i, f in enumerate(features):
if i == 0:
X = f
Y = i * numpy.ones((len(f), 1))
else:
X = numpy.vstack((X, f))
Y = numpy.append(Y, i * numpy.ones((len(f), 1)))
return (X, Y)
def pcaDimRed(features, nDims):
[X, Y] = listOfFeatures2Matrix(features)
pca = sklearn.decomposition.PCA(n_components = nDims)
pca.fit(X)
coeff = pca.components_
coeff = coeff[:, 0:nDims]
featuresNew = []
for f in features:
ft = f.copy()
# ft = pca.transform(ft, k=nDims)
ft = numpy.dot(f, coeff)
featuresNew.append(ft)
return (featuresNew, coeff)
def fileClassification(inputFile, modelName, modelType):
# Load classifier:
if not os.path.isfile(modelName):
print "fileClassification: input modelName not found!"
return (-1, -1, -1)
if not os.path.isfile(inputFile):
print "fileClassification: wav file not found!"
return (-1, -1, -1)
if (modelType) == 'svm' or (modelType == 'svm_rbf'):
[Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT] = loadSVModel(modelName)
elif modelType == 'knn':
[Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT] = loadKNNModel(modelName)
elif modelType == 'randomforest':
[Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT] = loadRandomForestModel(modelName)
elif modelType == 'gradientboosting':
[Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT] = loadGradientBoostingModel(modelName)
elif modelType == 'extratrees':
[Classifier, MEAN, STD, classNames, mtWin, mtStep, stWin, stStep, computeBEAT] = loadExtraTreesModel(modelName)
[Fs, x] = audioBasicIO.readAudioFile(inputFile) # read audio file and convert to mono
x = audioBasicIO.stereo2mono(x)
if isinstance(x, int): # audio file IO problem
return (-1, -1, -1)
if x.shape[0] / float(Fs) <= mtWin:
return (-1, -1, -1)
# feature extraction: