-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathml_main.py
213 lines (164 loc) · 7.16 KB
/
ml_main.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
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 14 19:20:54 2018
@author: kathi
"""
import numpy as np
from sklearn.cross_validation import train_test_split
from sklearn import svm
from sklearn.metrics import accuracy_score
from sklearn import neighbors, datasets
from sklearn import preprocessing
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
import pickle
import collections
import librosa
import AudioSetLoad
from letRoboySpeak import roboy_talk
import scipy.io.wavfile as sciwav
import sounddevice as sd
import os, sys
def svm(X_train, X_test, y_train, y_test):
clf = svm.LinearSVC()
clf.fit(X_train, y_train)
y_svm = clf.predict(X_test)
accuracy = accuracy_score(y_test,y_svm)
#print ("Accuracy is ",accuracy)
filename = 'svm.sav'
pickle.dump(clf, open(filename, 'wb'))
return accuracy
def knn(X_train, X_test, y_train, y_test):
h=.02
n_neighbors = 15
X_scaled = preprocessing.scale(X)
Z=[]
for weights in ['uniform', 'distance']:
# we create an instance of Neighbours Classifier and fit the data.
clf = neighbors.KNeighborsClassifier(n_neighbors, weights=weights)
clf.fit(X_train, y_train)
Z_new=clf.predict(X_test)
Z.append(Z_new)
print(accuracy_score(y_test, Z_new))
Z_max=np.amax(Z)
filename = 'knn'+weights+'.sav'
print filename
pickle.dump(clf, open(filename, 'wb'))
return Z_max
def decTree(X_train, X_test, y_train, y_test):
clf_entropy = DecisionTreeClassifier(criterion = "entropy", random_state = 100, max_depth=15)
clf_entropy.fit(X_train, y_train)
#test
y_pred_en = clf_entropy.predict(X_test)
y_pred_en
#accuracy
#print ("Accuracy is ", accuracy_score(y_test,y_pred_en)*100)
accuracy=accuracy_score(y_test,y_pred_en)
filename = 'decTree.sav'
pickle.dump(clf_entropy, open(filename, 'wb'))
return accuracy
def randForest(X_train, X_test, y_train, y_test):
ran_forest = RandomForestClassifier(n_estimators=10, criterion = "gini")
ran_forest.fit(X_train, y_train)
y_pred_ran = ran_forest.predict(X_test)
accuracy = accuracy_score(y_test, y_pred_ran)
filename = 'randForest.sav'
pickle.dump(ran_forest, open(filename, 'wb'))
#print("Accuracy is ", accuracy_score(y_test, y_pred_ran)*100)
return accuracy
def extract_feature(file_name):
X, sample_rate = librosa.load(file_name)
stft = np.abs(librosa.stft(X))
mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T,axis=0)
chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T,axis=0)
mel = np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T,axis=0)
contrast = np.mean(librosa.feature.spectral_contrast(S=stft, sr=sample_rate).T,axis=0)
tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(X), sr=sample_rate).T,axis=0)
return mfccs,chroma,mel,contrast,tonnetz
def load_model_and_predict_class(model, test_input):
loaded_model = pickle.load(open(model, 'rb'))
y = loaded_model.predict([test_input])
return y
def test_audiofile(model, filename):
mfccs, chroma, mel, contrast,tonnetz = extract_feature(filename)
ext_features = np.hstack([mfccs,chroma,mel,contrast,tonnetz])
features = ext_features
return load_model_and_predict_class(model, features)
if __name__ == "__main__":
trained = True
if trained == False:
features = np.load("feature_all.npy")
labels = np.load("hot_labels_all.npy")
print(features.shape)
print(labels.shape)
labels_not_hot = np.load("labels_all.npy")
"""for element in labels:
for i in range(0, len(element)):
if element[i] == 1:
labels_not_hot.append(i)"""
labels_array = labels_not_hot
print(labels_array)
print(labels_array.shape)
X = features
y = labels_array
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size = 0.3, random_state = 100)
#acc_svm=svm(X_train, X_test, y_train, y_test)
#print (acc_svm)
acc_knn=knn(X_train, X_test, y_train, y_test)
print (acc_knn)
acc_decTree=decTree(X_train, X_test, y_train, y_test)
print (acc_decTree)
acc_randForest=randForest(X_train, X_test, y_train, y_test)
votes=[]
#model_files=['svm.sav','knnuniform.sav', 'knndistance.sav', 'decTree.sav', 'randForest.sav']
model_files = ['knnuniform.sav', 'knndistance.sav', 'decTree.sav', 'randForest.sav']
#fn, ytid, classes = AudioSetLoad.dl_random_file()
#print AudioSetLoad.file_labelling
class_list = ["air_conditioner", "car_horn", "children_playing", "dog_bark", "drilling", "engine_idling",
"gun_shot", "jackhammer", "siren", "street_music"]
fn = "recorded.wav"
classes = "recorded stuff"
file_list = os.listdir("./data/")
if sys.argv[1] == "google":
for file in file_list:
if ".wav" in file:
votes = []
for model in model_files:
new_vote = test_audiofile(model, "./data/" + file)
votes.append(new_vote)
votes_array = np.array(votes)
counts = np.bincount(votes_array.flatten()) # in counts steht anzahl wie oft diese zahl vorkommt(=index )
max_class = np.argmax(counts)
print max_class, " -> ", class_list[max_class]
try:
classes = AudioSetLoad.file_labelling[file.split('.')[0]]
print classes, "(", file, ")"
except KeyError as e:
print "no class for ", file.split(".")
elif sys.argv[1] == "recorded":
for model in model_files:
new_vote=test_audiofile(model,fn)
votes.append(new_vote)
votes_array=np.array(votes)
counts= np.bincount(votes_array.flatten()) # in counts steht anzahl wie oft diese zahl vorkommt(=index )
max_class= np.argmax(counts)
print max_class, " -> ", class_list[max_class]
print classes, "(", fn, ")"
rate, data = sciwav.read(fn)
sd.play(data, rate, blocking=True)
#roboy_talk(max_class)
elif sys.argv[1] == "demo":
demo_list = ["./data/1cS0oGvV5PY.wav", "recorded_child.wav", "./data/YJG1Zz097M4.wav", "./data/-x2aAKUtNRw.wav", "recorded.wav"]
for fn in demo_list:
votes = []
for model in model_files:
new_vote=test_audiofile(model,fn)
votes.append(new_vote)
votes_array=np.array(votes)
counts= np.bincount(votes_array.flatten()) # in counts steht anzahl wie oft diese zahl vorkommt(=index )
max_class= np.argmax(counts)
print max_class, " -> ", class_list[max_class]
print classes, "(", fn, ")"
rate, data = sciwav.read(fn)
sd.play(data, rate, blocking=True)
roboy_talk(max_class)