-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti-test
executable file
·358 lines (315 loc) · 13.4 KB
/
multi-test
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
#!/usr/bin/python
# Copyright 2014 Douglas Bagnall <douglas@halo.gen.nz> LGPL
import os, sys
import argparse
import numpy as np
from colour import COLOURS
from classify import gst_init, Gst, GObject, COLOURS
from classify import draw_roc_curve, draw_presence_roc, actually_show_roc
from classify import BaseClassifier, add_common_args, process_common_args
WINDOW_SIZE = 1024
BASENAME = 'kiwi'
class MultiClassifier(BaseClassifier):
data = []
verbosity = 1
ground_truth_file = None
classification_file = None
net_scales = None
def build_pipeline(self, channels, sinkname, samplerate, n_classifiers):
self.channels = channels
self.sink = self.make_add_link(sinkname, None)
print "making %d classifiers" % n_classifiers
c = self.make_add_link('classify', self.sink)
self.classifiers = [c]
for x in range(n_classifiers - 1):
c = self.make_add_link('classify', c)
self.classifiers.append(c)
self.capsfilter = self.make_add_link('capsfilter', c)
self.interleave = self.make_add_link('interleave', self.capsfilter)
self.sources = []
for i in range(channels):
ac = self.make_add_link('audioconvert', self.interleave)
ar = self.make_add_link('audioresample', ac)
wp = self.make_add_link('wavparse', ar)
fs = self.make_add_link('filesrc', wp)
self.sources.append(fs)
self.channels = channels
caps = Gst.caps_from_string("audio/x-raw, "
"layout=(string)interleaved, "
"channel-mask=(bitmask)0x0, "
"rate=%d, channels=%d"
% (samplerate, channels))
self.capsfilter.set_property("caps", caps)
def __init__(self, channels=1, mainloop=None, sinkname='fakesink',
samplerate=8000, n_classifiers=1):
if mainloop is None:
mainloop = GObject.MainLoop()
self.mainloop = mainloop
self.build_pipeline(channels, sinkname, samplerate, n_classifiers)
def setp_all(self, prop, value):
for c in self.classifiers:
c.set_property(prop, value)
def set_filenames(self, *filenames):
print self.classifiers
for c, fn in zip(self.classifiers, filenames):
c.set_property('net-filename', fn)
classes = c.get_property('classes')
for c in self.classifiers[:-1]:
classes2 = c.get_property('classes')
if classes2 != classes:
raise RuntimeError("classes mismatch %r != %r" % (classes2, classes))
self.classes = classes.split(',')
def classify(self, data,
ground_truth_file=None,
classification_file=None, show_roc=False,
show_presence_roc=False,
target_index=None):
self.target_index = target_index
if ground_truth_file:
self.ground_truth_file = open(ground_truth_file, 'w')
if classification_file:
self.classification_file = open(classification_file, 'w')
self.show_roc = show_roc
self.show_presence_roc = show_presence_roc
self.data = list(reversed(data))
self.setp_all('training', False)
self.scores = self.get_results_counter(0)
self.minute_results = {x:[] for x in self.classes[0]}
self.load_next_file()
self.mainloop.run()
def load_next_file(self):
self.pipeline.set_state(Gst.State.READY)
f = self.data.pop()
targets = ' '.join(x % 0 for x in f.targets)
self.current_file = f
self.sources[0].set_property('location', f.fullname)
self.setp_all('target', targets)
self.file_results = [[] for x in self.classes]
self.file_scores = self.get_results_counter(0)
keys = ''.join(self.classes)
self.timed_scores = {x: {} for x in keys}
self.pipeline.set_state(Gst.State.PLAYING)
def on_element(self, bus, msg):
s = msg.get_structure()
if s.get_name() != "classify":
return
if self.net_scales is not None:
cname = msg.src.get_property('basename')
scale = self.net_scales[cname]
else:
scale = 1.0
v = s.get_value
no_targets = not self.current_file.targets
timestamp = msg.timestamp
for i, group in enumerate(self.classes):
scores = self.file_scores[i]
key = 'channel 0, group %d ' % i
correct = v(key + 'correct')
target = v(key + 'target')
if no_targets:
for k in group:
results = self.timed_scores[k].setdefault(timestamp, [])
res = (v(key + k) * scale, None)
results.append(res)
scores[k].append(res)
elif target is None:
continue
else:
for k in group:
results = self.timed_scores[k].setdefault(timestamp, [])
res = (v(key + k) * scale, k == target)
results.append(res)
scores[k].append(res)
self.file_results[i].append((target, correct))
def report(self):
self.pipeline.set_state(Gst.State.READY)
out = []
colours = [COLOURS[x] for x in 'PPrrRRYYGgCC']
for groupno, file_results in enumerate(self.file_results):
classes = self.classes[groupno]
step = len(file_results) / 100.0
next_stop = 0
#print file_results
for i, result in enumerate(file_results):
if i >= next_stop:
if i:
s = sum(current_targets)
m = max(current_targets)
if m > s * 0.9:
c = classes[current_targets.index(m)]
else:
c = '~'
colour = colours[int(current_correct * 10.01 / s)]
out.append('%s%s' % (colour, c))
next_stop += step
current_correct = 0
current_targets = [0] * len(classes)
target, correct = result
t_index = classes.index(target)
current_correct += correct
current_targets[t_index] += 1
out.extend((COLOURS['Z'], str(len(file_results)), '\n'))
if self.target_index:
i, k = self.target_index
else:
i, k = 0, self.classes[0][-1]
scores = self.file_scores[i][k]
r_sum = 0
w_sum = 0
r_sum2, w_sum2 = 0, 0
r_count = 0
w_count = 0
for s, t in scores:
if t:
r_sum += s
r_sum2 += s * s
r_count += 1
else:
w_sum += s
w_sum2 += s * s
w_count += 1
if r_count:
r_mean = r_sum / r_count
r_stddev = (r_sum2 / r_count - r_mean * r_mean) ** 0.5
else:
r_mean = r_stddev = float('nan')
if w_count:
w_mean = w_sum / w_count
w_stddev = (w_sum2 / w_count - w_mean * w_mean) ** 0.5
else:
w_mean = w_stddev = float('nan')
if r_count:
diff = r_mean - w_mean
sd = r_stddev + w_stddev
c = colours[(diff > 0) + (diff > r_stddev) + (diff > w_stddev) +
(diff > sd) + (diff > sd * 2) + (diff > sd * 3) +
(diff > w_mean) + (diff > w_mean * 2) +
(diff > 0.1) + (diff > 0.5)]
else:
c = COLOURS['Z']
sigma = unichr(0x03c3).encode('utf-8')
out.append("%s scores. %s%s %.2f (%s %.2f) not-%s %.2f (%s %.2f)%s %s\n" %
(k, c, k, r_mean, sigma, r_stddev, k, w_mean, sigma,
w_stddev, COLOURS['Z'], self.current_file.basename))
print ''.join(out)
def on_eos(self, bus, msg):
if self.verbosity > 0:
self.report()
fn = self.current_file.basename
#scores = self.file_scores
#print self.timed_scores
scores = self.get_results_counter(0)
for k, v in self.timed_scores.items():
sk = scores[0][k]
for ts, vals in v.iteritems():
#print ts, len(vals)
#s = sum(x[0] * x[0] for x in vals)
s = sorted(x[0] for x in vals)[-1]
#for x in vals:
# s *= x[0]
sk.append((s, any(x[1] for x in vals)))
if self.target_index and (self.classification_file
or self.ground_truth_file):
i, k = self.target_index
ground_truth = [fn]
classifications = [fn]
for s, t in scores[i][k]:
ground_truth.append('%d' % t)
classifications.append('%.5g' % s)
if self.ground_truth_file:
print >>self.ground_truth_file, ','.join(ground_truth)
if self.classification_file:
print >>self.classification_file, ','.join(classifications)
if self.show_presence_roc:
r = {}
window = np.kaiser(15, 6)
for k, v in scores[0].items():
gt = any([x[1] for x in v[10:]])
s = np.array([x[0] for x in v])
s = np.convolve(s, window)
ss = np.sort(s[10:])
r[k] = (ss[-70:][::-1], gt)
#print r[k]
for k in r:
self.minute_results[k].append(r[k])
for scores, fscores in zip(self.scores, scores):
for k in scores:
scores[k].extend(fscores[k])
if 0:
i, k = self.target_index
draw_roc_curve(scores[i][k], k)
actually_show_roc()
if not self.data:
if self.show_roc:
if self.target_index:
i, k = self.target_index
draw_roc_curve(self.scores[i][k], k)
else:
for i, group in enumerate(self.classes):
for k in group[len(group) == 2:]:
draw_roc_curve(self.scores[i][k], k)
if self.show_presence_roc:
for n in (0, 4, 9, 16, 25, 36, 49, 64):
draw_presence_roc(self.minute_results[k], n,
'%s-nth %s' % (k, n))
actually_show_roc()
self.stop()
else:
self.load_next_file()
def on_error(self, bus, msg):
pass
def main():
gst_init()
parser = argparse.ArgumentParser()
prop_names = add_common_args(parser)
group = parser.add_argument_group('multi-test specific arguments')
group.add_argument('-C', '--first-n', type=int, default=0,
help="classify this many files")
group.add_argument('--ground-truth-file',
help="write ground truth to this file (CSV)")
group.add_argument('--classification-file',
help="write classifications to this file")
group.add_argument('--roc', action='store_true',
help="show ROC curves")
group.add_argument('--target-class',
help="use this class in reports and ROC")
group.add_argument('--target-group', type=int, default=0,
help="use this class group in reports and ROC")
group.add_argument('--min-changes', type=int, default=0,
help="only test files with at least this many class switches")
group.add_argument('--no-timings', action='store_true',
help="Don't compare with canonical timings")
group.add_argument('--presence-roc', action='store_true',
help="plot a ROC curve of presence (implies --roc)")
group.add_argument('--net-scales',
help="colon separated list of scales for each net, in order")
group.add_argument('nets', nargs='+',
help="use these nets")
args = parser.parse_args()
timed = not args.no_timings
c = MultiClassifier(n_classifiers=len(args.nets))
#c.verbosity = 2
c.set_filenames(*args.nets)
timed_files = process_common_args(c, args, prop_names, timed=timed,
load_net=False)
if args.min_changes:
timed_files = [x for x in timed_files
if len(x.timings) >= args.min_changes]
if args.first_n:
timed_files = timed_files[:args.first_n]
target = None
if args.target_class:
target=(args.target_group, args.target_class)
if args.net_scales:
c.net_scales = {}
scales = [float(x) for x in args.net_scales.split(':')]
norm = sum(scales) / len(scales)
scales = [x / norm for x in scales]
for cf, scale in zip(c.classifiers, scales):
basename = cf.get_property('basename')
c.net_scales[basename] = scale
show_roc = args.roc or args.show_presence_roc
c.classify(timed_files, ground_truth_file=args.ground_truth_file,
classification_file=args.classification_file, show_roc=show_roc,
show_presence_roc=args.presence_roc, target_index=target)
main()