This repository has been archived by the owner on Oct 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mielke_replication.py
386 lines (328 loc) · 14.5 KB
/
mielke_replication.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# ------------------------------------
# file: mielke_replication.py
# date: Mon July 21 18:13 2014
# author:
# Maarten Versteegh
# github.com/mwv
# maartenversteegh AT gmail DOT com
#
# Licensed under GPLv3
# ------------------------------------
"""mielke_replication: replication of some of the results described in
Mielke, Zuberbuehler (2013) A method for automated individual, species and
call type recognition in free-ranging animals, Animal Behaviour 86, pp 475--482
"""
from __future__ import division
import os
import os.path as path
import cPickle as pickle
from pprint import pformat
import warnings
warnings.filterwarnings('ignore')
import numpy as np
from scipy.special import expit
from sklearn.cross_validation import train_test_split
from sklearn.svm import SVC
from sklearn.grid_search import GridSearchCV
import sklearn.metrics as metrics
from data import BASEDIR, load_data_stacked
MONKEYS = ['Titi_monkeys', 'Blue_monkeys', 'colobus', 'Blue_monkeys_Fuller']
def load_all_monkeys():
"""Load stacked data for all monkeys.
:return
X: dict from monkeyname to ndarray containing the stacked audio
y: dict from monkeyname to ndarray containing the labels (as ints)
labelset: dict from monkeyname to sorted list of call names
"""
_memo_fname = path.join(BASEDIR, 'monkey_calls_stacked.pkl')
if path.exists(_memo_fname):
print 'loading data from:', _memo_fname
with open(_memo_fname, 'rb') as fid:
X, y, labelset = pickle.load(fid)
else:
print 'loading data from individual files...'
labelset = {}
X = {}
y = {}
for monkey in MONKEYS:
print ' ', monkey
X_, y_, labelset_ = load_data_stacked(monkey)
# remap James' labels
labelset[monkey] = labelset_
X[monkey] = X_
y[monkey] = y_
with open(_memo_fname, 'wb') as fid:
pickle.dump((X, y, labelset), fid, -1)
return X, y, labelset
def merge_blue(X, y, labelset):
X_merged = {k: v for k, v in X.iteritems() if not k.startswith('Blue')}
y_merged = {k: v for k, v in y.iteritems() if not k.startswith('Blue')}
labelset_merged = {k: v for k, v in labelset.iteritems()
if not k.startswith('Blue')}
X_murphy, y_murphy, labelset_murphy = \
X['Blue_monkeys'], y['Blue_monkeys'], labelset['Blue_monkeys']
X_fuller, y_fuller, labelset_fuller = \
X['Blue_monkeys_Fuller'], y['Blue_monkeys_Fuller'], \
labelset['Blue_monkeys_Fuller']
remap = {'A': 'p', 'KA': 'h', 'KATR':'h', 'PY':'p'}
remap_inds = {label:None for label in set(remap.values())}
for idx, label in enumerate(labelset_fuller):
newlabel = remap[label]
if remap_inds[newlabel] is None:
remap_inds[newlabel] = np.nonzero(y_fuller==idx)[0]
else:
remap_inds[newlabel] = np.hstack((remap_inds[newlabel], np.nonzero(y_fuller==idx)[0]))
remap_inds = {k: np.sort(v) for k, v in remap_inds.iteritems()}
y_new = np.zeros(y_fuller.shape, dtype=np.uint8)
y_new[remap_inds[labelset_murphy[1]]] = 1
X_merged['Blue'] = np.vstack((X_murphy, X_fuller))
y_merged['Blue'] = np.hstack((y_murphy, y_new))
labelset_merged['Blue'] = labelset_murphy
return X_merged, y_merged, labelset_merged
def combine_labels(X, y, labelset):
combined_labels = []
for monkey in labelset:
for label in labelset[monkey]:
combined_labels.append(monkey + '-' + label)
combined_labels = sorted(combined_labels)
comblabel2idx = dict(zip(combined_labels, range(len(combined_labels))))
X_comb = None
y_comb = None
for comb_label in combined_labels:
monkey, label = comb_label.split('-')
idx2label = dict(zip(range(len(labelset[monkey])), labelset[monkey]))
X_ = X[monkey]
y_ = np.array([comblabel2idx[monkey + '-' + idx2label[x]]
for x in y[monkey]])
if X_comb is None:
X_comb = X_
y_comb = y_
else:
X_comb = np.vstack((X_comb, X_))
y_comb = np.hstack((y_comb, y_))
return X_comb, y_comb, combined_labels
def make_monkey_set(X, y, labelset):
monkeys = sorted(labelset.keys())
label2idx = dict(zip(monkeys, range(len(monkeys))))
X_comb = None
y_comb = None
for monkey in monkeys:
X_ = X[monkey]
y_ = np.ones(X_.shape[0]) * label2idx[monkey]
if X_comb is None:
X_comb = X_
y_comb = y_
else:
X_comb = np.vstack((X_comb, X_))
y_comb = np.hstack((y_comb, y_))
return X_comb, y_comb, monkeys
def print_cm(stream, cm, target_names=None, vert_labels=False):
"""pretty print the confusion matrix to stream"""
def print_vert(stream, strings, spacing=1, offset=0):
lsize = max(len(x) for x in strings)
strings = ['{0:s}'.format(l.rjust(lsize)) for l in strings]
for line in range(lsize):
print >>stream, ' ' * offset + (' ' * spacing).join(s[line] for s in strings)
nlabels = cm.shape[0]
esize = int(np.maximum(np.max(np.floor(np.log10(cm))+1), 0))
if target_names is None:
target_names = map(str, range(nlabels))
lsize = max(len(x) for x in target_names)
names = ['{0:s}'.format(n.rjust(lsize)) for n in target_names]
if vert_labels:
print_vert(stream, names, spacing=esize, offset=lsize+esize)
print >>stream, ''
for y_idx in range(cm.shape[0]):
name = '{0:s}'.format(names[y_idx].rjust(lsize))
vline = ' '.join(str(cm[y_idx, x_idx]).rjust(esize)
for x_idx in range(cm.shape[1]))
print >>stream, name + ' ' + vline
def classification_by_monkey(X, y, labelset, param_grid, stream,
n_folds_test=10, n_folds_gridsearch=5,
verbose=True):
for monkey in X.keys():
if verbose:
print '-' * len(monkey)
print monkey
print '-' * len(monkey)
print >>stream, '***', monkey
y_true = None
y_pred = None
pvals = None
print >>stream, '\n**** Cross-validation scores\n'
for fold in range(n_folds_test):
if verbose:
print ' FOLD:', fold
X_train, X_test, y_train, y_test = train_test_split(X[monkey],
y[monkey],
test_size=0.1)
if verbose:
print 'training classifier...'
clf = GridSearchCV(SVC(),
param_grid,
cv=n_folds_gridsearch,
scoring='f1',
verbose=1 if verbose else 0, n_jobs=-1)
clf.fit(X_train, y_train)
print >>stream, 'FOLD:', fold, clf.best_score_
print >>stream, pformat(clf.best_params_)
if verbose:
print 'predicting class labels...'
if y_true is None:
y_true = y_test
y_pred = clf.predict(X_test)
pvals = expit(clf.decision_function(X_test))
else:
y_true = np.hstack((y_true, y_test))
y_pred = np.hstack((y_pred, clf.predict(X_test)))
pvals = np.hstack((pvals, expit(clf.decision_function(X_test))))
print >>stream, '\n**** Classification report\n'
print >>stream, metrics.classification_report(y_true, y_pred,
target_names=labelset[monkey])
print >>stream, '\n**** Confusion matrix\n'
print_cm(stream, metrics.confusion_matrix(y_true, y_pred),
labelset[monkey])
print >>stream, ''
stream.flush()
with open('results/clf_by_monkey_{0}_blue_merged.pkl'.format(monkey), 'wb') as fid:
pickle.dump((y_true, y_pred, pvals, labelset[monkey]), fid, -1)
def classification_across_monkey(X, y, labelset, param_grid, stream,
n_folds_test=5, n_folds_gridsearch=5,
verbose=True):
X_comb, y_comb, labelset_comb = combine_labels(X, y, labelset)
print >>stream, '*** Cross-validation scores\n'
y_true = None
y_pred = None
pvals = None
for fold in range(n_folds_test):
X_train, X_test, y_train, y_test = train_test_split(X_comb, y_comb,
test_size=0.1)
if verbose:
print 'training classifier...'
clf = GridSearchCV(SVC(),
param_grid,
cv=n_folds_gridsearch,
scoring='f1',
verbose=1 if verbose else 0, n_jobs=-1)
clf.fit(X_train, y_train)
print >>stream, 'FOLD:', fold, clf.best_score_
print >>stream, pformat(clf.best_params_)
if y_true is None:
y_true = y_test
y_pred = clf.predict(X_test)
pvals = expit(clf.decision_function(X_test))
else:
y_true = np.hstack((y_true, y_test))
y_pred = np.hstack((y_pred, clf.predict(X_test)))
pvals = np.hstack((pvals, expit(clf.decision_function(X_test))))
print >>stream, '\n*** Classification report\n'
print >>stream, metrics.classification_report(y_true, y_pred,
target_names=labelset_comb)
print >>stream, '\n*** Confusion matrix\n'
print_cm(stream, metrics.confusion_matrix(y_true, y_pred),
labelset_comb)
print >>stream, ''
stream.flush()
with open('results/clf_across_monkey_blue_merged.pkl', 'wb') as fid:
pickle.dump((y_true, y_pred, pvals, labelset_comb), fid, -1)
def classification_by_species(X, y, labelset, param_grid, stream,
n_folds_test=10, n_folds_gridsearch=5,
verbose=True):
X_comb, y_comb, labelset_comb = make_monkey_set(X, y, labelset)
y_true = None
y_pred = None
pvals = None
print >>stream, '*** Cross-validation scores\n'
for fold in range(n_folds_test):
if verbose:
print ' FOLD:', fold
X_train, X_test, y_train, y_test = train_test_split(X_comb, y_comb,
test_size=0.1)
clf = GridSearchCV(SVC(),
param_grid,
cv=n_folds_gridsearch,
scoring='f1',
verbose=1 if verbose else 0, n_jobs=-1)
clf.fit(X_train, y_train)
print >>stream, 'FOLD:', fold, clf.best_score_
print >>stream, pformat(clf.best_params_)
if y_true is None:
y_true = y_test
y_pred = clf.predict(X_test)
pvals = expit(clf.decision_function(X_test))
else:
y_true = np.hstack((y_true, y_test))
y_pred = np.hstack((y_pred, clf.predict(X_test)))
pvals = np.hstack((pvals, expit(clf.decision_function(X_test))))
print >>stream, '\n*** Classification report\n'
print >>stream, metrics.classification_report(y_true, y_pred,
target_names=labelset_comb)
print >>stream, '\n*** Confusion matrix\n'
print_cm(stream, metrics.confusion_matrix(y_true, y_pred),
labelset_comb)
print >>stream, ''
stream.flush()
with open('results/clf_species_blue_merged.pkl', 'wb') as fid:
pickle.dump((y_true, y_pred, pvals, labelset_comb), fid, -1)
def replicate(resultfile, n_folds_test=10, n_folds_gridsearch=5, verbose=True):
X, y, labelset = merge_blue(*load_all_monkeys())
from svc_param_grid import param_grid
with open(resultfile, 'w') as stream:
print >>stream, '* Replication of Mielke & Zuberbuehler'
print >>stream, """
Replication of some of the results described in Mielke, Zuberbuehler (2013),
"A method for automated individual, species and call type recognition in
free-ranging animals", Animal Behaviour 86, pp 475--482
The document lists the results of multiclass classification only on
pre-determined intervals of audio. Manually labeled intervals were extracted
from the audio recordings of monkey calls. Labels with less than 50 instances
were discarded. Classification was performed with a Support Vector Classifier
with a radial basis function kernel.
A grid search with 3-fold crossvalidation on the training set was performed to
tune the hyperparameters $C$ and $\gamma$. Scores are reported on the average
of 5 independent splits of the data into training and test sets.
This file was automatically generated by running `mielke_replication.py`.
"""
stream.flush()
# 1. classify calls per monkey
if verbose:
print '---------------------------'
print '1. CLASSIFICATION BY MONKEY'
print '---------------------------'
print >>stream, '** CLASSIFICATION BY MONKEY'
classification_by_monkey(X, y, labelset, param_grid, stream,
n_folds_test=n_folds_test,
n_folds_gridsearch=n_folds_gridsearch,
verbose=verbose)
# 2. classify over all monkeys and calls
if verbose:
print '-------------------------------'
print '2. CLASSIFICATION ACROSS MONKEY'
print '-------------------------------'
print >>stream, '** CLASSIFICATION ACROSS MONKEYS'
classification_across_monkey(X, y, labelset, param_grid, stream,
n_folds_test=n_folds_test,
n_folds_gridsearch=n_folds_gridsearch,
verbose=verbose)
# 3. classify the monkeys
if verbose:
print '----------------------------'
print '3. CLASSIFICATION BY SPECIES'
print '----------------------------'
print >>stream, '** CLASSIFICATION BY SPECIES'
classification_by_species(X, y, labelset, param_grid, stream,
n_folds_test=n_folds_test,
n_folds_gridsearch=n_folds_gridsearch,
verbose=verbose)
if __name__ == '__main__':
n_folds_test = 10
n_folds_gridsearch = 5
try:
os.makedirs('results')
except OSError:
pass
replicate('results/mielke_results_blue_merged.org',
n_folds_gridsearch=n_folds_gridsearch,
n_folds_test=n_folds_test)