/
BirdSongClassification.py
386 lines (293 loc) · 11.4 KB
/
BirdSongClassification.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
import pymongo
import sys
import argparse
from sklearn.mixture import GMM
#from features import mfcc
#import scipy.io.wavfile as wav
#from glob import glob
import os
import numpy as np
from scikits.audiolab import Sndfile
from MFCC import melScaling
import random
from sklearn.linear_model import LogisticRegression
from sklearn import cross_validation
from sklearn.metrics import accuracy_score, classification_report
framelen=1024
fs = 44100.0
'''
This class contains functions to do feature extraction, cross validation, and so on
'''
class BirdSong:
def __init__(self):
self.mfccMaker = melScaling(int(fs), framelen/2, 40)
self.mfccMaker.update()
'''
Convert a file to features. Take the file location as input and return a numpy array
'''
def file_to_features(self,wavpath):
sf = Sndfile(wavpath, "r")
window = np.hamming(framelen)
features = []
while(True):
try:
chunk = sf.read_frames(framelen, dtype=np.float32)
if len(chunk) != framelen:
print("Not read sufficient samples - returning")
break
if sf.channels != 1:
chunk = np.mean(chunk, 1) # mixdown
framespectrum = np.fft.fft(window * chunk)
magspec = abs(framespectrum[:framelen/2])
# do the frequency warping and MFCC computation
melSpectrum = self.mfccMaker.warpSpectrum(magspec)
melCepstrum = self.mfccMaker.getMFCCs(melSpectrum,cn=True)
melCepstrum = melCepstrum[1:] # exclude zeroth coefficient
melCepstrum = melCepstrum[:13] # limit to lower MFCCs
framefeatures = melCepstrum
features.append(framefeatures)
except RuntimeError:
break
sf.close()
return np.array(features)
'''
Do 10 folds cross validation, print classifier, accuracy, and classification report
'''
def do_multiple_10foldcrossvalidation(self,clf,data,classifications):
predictions = cross_validation.cross_val_predict(clf, data,classifications, cv=10)
print clf
print "accuracy"
print accuracy_score(classifications,predictions)
print classification_report(classifications,predictions)
'''
Fetch features from database, and store the features into a dictionary
'''
def get_feature_dic_mongodb(self,mongo_path):
connection = pymongo.MongoClient(mongo_path)
# get a handle to the bird database
db = connection.bird
birdFeature = db.birdFeature
dic={}
try:
cursor = birdFeature.find({})
except Exception as e:
print "Unexpected error:", type(e), e
count = 0
for doc in cursor:
count += 1
dic[doc['_id']]=doc['feature']
#print count
return dic
'''
Get a dictionary of features for all the files in the files_list.
files_list is a list of file locations.
A dictionary of features is returned
'''
def get_feature_dic(self,files_list):
dic={}
for a_file in sorted(files_list):
mfcc_feat=self.file_to_features(a_file)
dic[a_file]=mfcc_feat
return dic
'''
normed_features returns the normalized features
dic: a dictionary of features to be normalized
means: a vector of means
invstds: a vector of inversed standard deviations
return: a dictionary of normalized features
'''
def normed_features(self, dic,means,invstds):
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('-c', '--charsplit', default='_', help="Character used to split filenames")
args = vars(parser.parse_args())
normedFeatures = {}
for aLabel, feature in dic.items():
label = os.path.basename(aLabel).split(args['charsplit'])[0]
#print label
if label not in normedFeatures:
normedFeatures[label] = (feature-means)*invstds#.tolist()
else:
normedFeatures[label] = np.vstack((normedFeatures[label], (feature-means)*invstds)) #.tolist()
return normedFeatures
'''
Train a gmm for each key in normedData dictionary
'''
def train_the_model(self, normedData):
gmm = {}
for aLabel in normedData.keys():
gmm[aLabel] = GMM(n_components=10)
gmm[aLabel].fit(normedData[aLabel])
return gmm
'''
Test the models using a dictionary of features.
dic2: a dictionary of features to be tested. Keys are the file names.
gmm: a dictionary of gmm models
return: the number of correct predictions and total number of files, the predicted labels and actual
labels
'''
def test(self,dic2,gmm):
actual_labels=[]
predicted_labels=[]
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group()
group.add_argument('-c', '--charsplit', default='_', help="Character used to split filenames")
args = vars(parser.parse_args())
i=0
n=len(dic2)
for a_file in dic2:
best_label = ''
best_likelihood = -9e99
likelihood_list=[]
for label, agmm in gmm.items():
likelihood = agmm.score_samples(dic2[a_file])[0]
likelihood = np.sum(likelihood)
likelihood_list.append(
likelihood)
#print(ll,'ll')
if likelihood > best_likelihood:
best_likelihood = likelihood
best_label = label
likelihood_list=sorted(likelihood_list)
#confidence
#print(likelihood_list[len(likelihood_list)-2]/(likelihood_list[len(likelihood_list)-2]+likelihood_list[len(likelihood_list)-1]),'confidence')
predicted_labels.append(best_label)
actual_labels.append(os.path.basename(a_file).split(args['charsplit'])[0])
if best_label == (os.path.basename(a_file).split(args['charsplit'])[0]):
#print('prediction correct',best_label)
i = i + 1
'''
else:
print 'predicion wrong predicted',best_label,(os.path.basename(a_file).split(args['charsplit'])[0])
'''
print(i,float(i)/n,'accuracy')
return i, n,predicted_labels,actual_labels
'''
predict the bird species for one file.
'''
def predict_one_bird(self,mfcc_feature,gmm):
best_label = ''
best_likelihood = -9e99
likelihood_list=[]
for label, agmm in gmm.items():
likelihood = agmm.score_samples(mfcc_feature)[0]
likelihood = np.sum(likelihood)
likelihood_list.append(
likelihood)
if likelihood > best_likelihood:
best_likelihood = likelihood
best_label = label
likelihood_list=sorted(likelihood_list)
confidence=likelihood_list[len(likelihood_list)-2]/(likelihood_list[len(likelihood_list)-2]+likelihood_list[len(likelihood_list)-1])
print likelihood_list[len(likelihood_list)-2],likelihood_list[len(likelihood_list)-1]
print best_label,' for one file test'
return best_label,confidence
'''
Calculate n folds cross validation
'''
def n_fold_cross_validation(self, num_fold,dic,means,invstds):
num_files=len(dic)
file_list=dic.keys()
test_dic=[]
train_dic=[]
correct=0
total=0
actual_labels=[]
predicted_labels=[]
for fold in range(num_fold):
test_dic.append({})
train_dic.append({})
test_file=[]
for i in random.sample(range(0,num_files), num_files/num_fold):
test_dic[fold][file_list[i]]=dic[file_list[i]]
test_file.append(file_list[i])
test_file_set=set(test_file)
for file in file_list:
if file not in test_file_set:
train_dic[fold][file]=dic[file]
for fold in range(num_fold):
print fold
normed_train=self.normed_features(train_dic[fold],means,invstds)
gmm =self.train_the_model(normed_train)
for test in test_dic[fold]:
test_dic[fold][test]=(test_dic[fold][test]-means)*invstds
num_correct,num_total,alabels,plabels=self.test(test_dic[fold],gmm)
correct+=num_correct
total+=num_total
actual_labels=actual_labels+alabels
predicted_labels=predicted_labels+plabels
print classification_report(actual_labels,predicted_labels)
return float(correct)/total
def logistic_regression_test_one(self,clf, test_data,label):
prob_list=np.asarray(clf.predict_proba(test_data))
prob_list=prob_list.sum(axis=0)
print prob_list
best_label=''
best_prob=-9e99
for index,prob in enumerate(prob_list.tolist()):
if prob > best_prob:
best_prob=prob
best_label=clf.classes_[index]
if best_label==label:
return 1
else:
return 0
if __name__ == '__main__':
birdsong=BirdSong()
dic=birdsong.get_feature_dic_mongodb("mongodb://localhost")
allconcat = np.vstack((dic.values()))
means = np.mean(allconcat, 0)
invstds = np.std(allconcat, 0)
for i, val in enumerate(invstds):
if val == 0.0:
invstds[i] = 1.0
else:
invstds[i] = 1.0 / val
print birdsong.n_fold_cross_validation(7,dic,means,invstds)
'''
#using test files from another folder
normedTrain=birdsong.normed_features(dic,means,invstds)
gmm =birdsong.train_the_model(normedTrain)
print(len(gmm) ,'len gmm')
files_list = glob(os.path.join('wavs2', '*.wav'))
dic2={}
for a_file in sorted(files_list):
#print a_file
mfcc_feat=birdsong.file_to_features(a_file)
mfcc_feat=(mfcc_feat-means)*invstds
dic2[a_file]=mfcc_feat
birdsong.test(dic2,gmm)
#test for one file
mfcc_feat=birdsong.file_to_features('wavs2/BrushCuckoo_34.wav')
mfcc_feat=(mfcc_feat-means)*invstds
birdsong.predict_one_bird(mfcc_feat,gmm)
'''
#logistic regression ----------------
'''
trn_data=[]
trn_data_labels=[]
for bird in normedTrain:
for feature in normedTrain[bird]:
trn_data_labels.append(bird)
trn_data.append(feature)
print len(trn_data)
print len(trn_data_labels)
trn_data_labels=np.asarray(trn_data_labels)
trn_data=np.asarray(trn_data)
clf=LogisticRegression()
#birdsong.do_multiple_10foldcrossvalidation(clf,trn_data,trn_data_labels)
clf.fit(trn_data,trn_data_labels)
files_list = glob(os.path.join('wavs2', '*.wav'))
dic2={}
for a_file in sorted(files_list):
#print a_file
mfcc_feat=birdsong.file_to_features(a_file)
mfcc_feat=(mfcc_feat-means)*invstds
dic2[a_file]=mfcc_feat
n_correct=0
n_total=len(dic2)
for a_file in dic2:
label=os.path.basename(a_file).split('_')[0]
n_correct+= birdsong.logistic_regression_test_one(clf,dic2[a_file],label)
print 'logistic regression accuracy',float(n_correct)/n_total
'''