History

update to new bench onset update to new bench onset

Paul Brossier committed 20 years ago

Commit e9689391 parent 677b2671 file · +157 −67Browse files

Showing the changes to python (1 of 1 file). Show all

Unified · Split

python/test/bench/onset/bench-onset+157−67
@@ -3,60 +3,150 @@ · 2 unmodified lines
3from aubio.bench.node import *3from aubio.bench.node import *
4from aubio.tasks import *4from aubio.tasks import *
5 5
6
7
8
9def mmean(l):
10 return sum(l)/float(len(l))
11
12def stdev(l):
13 smean = 0
14 lmean = mmean(l)
15 for i in l:
16 smean += (i-lmean)**2
17 smean *= 1. / len(l)
18 return smean**.5
19
6class benchonset(bench):20class benchonset(bench):
21
22 valuenames = ['orig','missed','Tm','expc','bad','Td']
23 valuelists = ['l','labs']
24 printnames = [ 'mode', 'thres', 'dist', 'prec', 'recl', 'Ttrue', 'Tfp', 'Tfn', 'Tm', 'Td',
25 'aTtrue', 'aTfp', 'aTfn', 'aTm', 'aTd', 'mean', 'smean', 'amean', 'samean']
26
27 formats = {'mode': "%12s" ,
28 'thres': "%5.4s",
29 'dist': "%5.4s",
30 'prec': "%5.4s",
31 'recl': "%5.4s",
32
33 'Ttrue': "%5.4s",
34 'Tfp': "%5.4s",
35 'Tfn': "%5.4s",
36 'Tm': "%5.4s",
37 'Td': "%5.4s",
38
39 'aTtrue':"%5.4s",
40 'aTfp': "%5.4s",
41 'aTfn': "%5.4s",
42 'aTm': "%5.4s",
43 'aTd': "%5.4s",
44
45 'mean': "%5.40s",
46 'smean': "%5.40s",
47 'amean': "%5.40s",
48 'samean': "%5.40s"}
7 49
8 def dir_eval(self):50 def file_gettruth(self,input):
9 self.P = 100*float(self.expc-self.missed-self.merged)/(self.expc-self.missed-self.merged + self.bad+self.doubled)51 from os.path import isfile
10 self.R = 100*float(self.expc-self.missed-self.merged)/(self.expc-self.missed-self.merged + self.missed+self.merged)52 ftrulist = []
11 if self.R < 0: self.R = 053 # search for match as filetask.input,".txt"
12 self.F = 2* self.P*self.R / (self.P+self.R)54 ftru = '.'.join(input.split('.')[:-1])
13 55 ftru = '.'.join((ftru,'txt'))
14 self.values = [self.params.onsetmode, 56 if isfile(ftru):
15 "%2.3f" % self.params.threshold,57 ftrulist.append(ftru)
16 self.orig,58 else:
17 self.expc,59 # search for matches for filetask.input in the list of results
18 self.missed,60 for i in range(len(self.reslist)):
19 self.merged,61 check = '.'.join(self.reslist[i].split('.')[:-1])
20 self.bad,62 check = '_'.join(check.split('_')[:-1])
21 self.doubled,63 if check == '.'.join(input.split('.')[:-1]):
22 (self.orig-self.missed-self.merged),64 ftrulist.append(self.reslist[i])
23 "%2.3f" % (100*float(self.orig-self.missed-self.merged)/(self.orig)),65 return ftrulist
24 "%2.3f" % (100*float(self.bad+self.doubled)/(self.orig)),
25 "%2.3f" % (100*float(self.orig-self.missed)/(self.orig)),
26 "%2.3f" % (100*float(self.bad)/(self.orig)),
27 "%2.3f" % self.P,
28 "%2.3f" % self.R,
29 "%2.3f" % self.F ]
30 66
31 def file_exec(self,input,output):67 def file_exec(self,input,output):
32 filetask = self.task(input,params=self.params)68 filetask = self.task(input,params=self.params)
33 computed_data = filetask.compute_all()69 computed_data = filetask.compute_all()
34 results = filetask.eval(computed_data)70 ftrulist = self.file_gettruth(filetask.input)
35 self.orig += filetask.orig71 for i in ftrulist:
36 self.missed += filetask.missed72 #print i
37 self.merged += filetask.merged73 filetask.eval(computed_data,i,mode='rocloc',vmode='')
38 self.expc += filetask.expc74 for i in self.valuenames:
39 self.bad += filetask.bad75 self.v[i] += filetask.v[i]
40 self.doubled += filetask.doubled76 for i in filetask.v['l']:
77 self.v['l'].append(i)
78 for i in filetask.v['labs']:
79 self.v['labs'].append(i)
80
81 def dir_exec(self):
82 """ run file_exec on every input file """
83 self.l , self.labs = [], []
84 self.v = {}
85 for i in self.valuenames:
86 self.v[i] = 0.
87 for i in self.valuelists:
88 self.v[i] = []
89 self.v['thres'] = self.params.threshold
90 act_on_files(self.file_exec,self.sndlist,self.reslist, \
91 suffix='',filter=sndfile_filter)
41 92
93 def dir_eval(self):
94 totaltrue = self.v['expc']-self.v['bad']-self.v['Td']
95 totalfp = self.v['bad']+self.v['Td']
96 totalfn = self.v['missed']+self.v['Tm']
97 self.P = 100*float(totaltrue)/max(totaltrue + totalfp,1)
98 self.R = 100*float(totaltrue)/max(totaltrue + totalfn,1)
99 if self.R < 0: self.R = 0
100 self.F = 2.* self.P*self.R / max(float(self.P+self.R),1)
101
102 N = float(len(self.reslist))
103
104 self.v['mode'] = self.params.onsetmode
105 self.v['thres'] = "%2.3f" % self.params.threshold
106 self.v['dist'] = "%2.3f" % self.F
107 self.v['prec'] = "%2.3f" % self.P
108 self.v['recl'] = "%2.3f" % self.R
109 self.v['Ttrue'] = totaltrue
110 self.v['Tfp'] = totalfp
111 self.v['Tfn'] = totalfn
112 self.v['aTtrue'] = totaltrue/N
113 self.v['aTfp'] = totalfp/N
114 self.v['aTfn'] = totalfn/N
115 self.v['aTm'] = self.v['Tm']/N
116 self.v['aTd'] = self.v['Td']/N
117 self.v['mean'] = mmean(self.v['l'])
118 self.v['smean'] = stdev(self.v['l'])
119 self.v['amean'] = mmean(self.v['labs'])
120 self.v['samean'] = stdev(self.v['labs'])
42 121
43 def run_bench(self,modes=['dual'],thresholds=[0.5]):122 def run_bench(self,modes=['dual'],thresholds=[0.5]):
44 self.modes = modes123 self.modes = modes
45 self.thresholds = thresholds124 self.thresholds = thresholds
46 125
47 self.pretty_print(self.titles)126 self.pretty_titles()
48 for mode in self.modes:127 for mode in self.modes:
49 self.params.onsetmode = mode128 self.params.onsetmode = mode
50 for threshold in self.thresholds:129 for threshold in self.thresholds:
51 self.params.threshold = threshold130 self.params.threshold = threshold
52 self.dir_exec()131 self.dir_exec()
53 self.dir_eval()132 self.dir_eval()
54 self.pretty_print(self.values)133 self.pretty_print()
134 #print self.v
135
136 def pretty_print(self,sep='|'):
137 for i in self.printnames:
138 print self.formats[i] % self.v[i], sep,
139 print
140
141 def pretty_titles(self,sep='|'):
142 for i in self.printnames:
143 print self.formats[i] % i, sep,
144 print
55 145
56 def auto_learn(self,modes=['dual'],thresholds=[0.1,1.5]):146 def auto_learn(self,modes=['dual'],thresholds=[0.1,1.5]):
57 """ simple dichotomia like algorithm to optimise threshold """147 """ simple dichotomia like algorithm to optimise threshold """
58 self.modes = modes148 self.modes = modes
59 self.pretty_print(self.titles)149 self.pretty_titles()
60 for mode in self.modes:150 for mode in self.modes:
61 steps = 10 151 steps = 10
62 lesst = thresholds[0] 152 lesst = thresholds[0]
@@ -66,20 +156,20 @@ class benchonset(bench): · 3 unmodified lines
66 self.params.threshold = topt 156 self.params.threshold = topt
67 self.dir_exec()157 self.dir_exec()
68 self.dir_eval()158 self.dir_eval()
69 self.pretty_print(self.values)159 self.pretty_print()
70 topF = self.F 160 topF = self.F
71 161
72 self.params.threshold = lesst 162 self.params.threshold = lesst
73 self.dir_exec()163 self.dir_exec()
74 self.dir_eval()164 self.dir_eval()
75 self.pretty_print(self.values)165 self.pretty_print()
76 lessF = self.F 166 lessF = self.F
77 167
78 for i in range(steps):168 for i in range(steps):
79 self.params.threshold = ( lesst + topt ) * .5 169 self.params.threshold = ( lesst + topt ) * .5
80 self.dir_exec()170 self.dir_exec()
81 self.dir_eval()171 self.dir_eval()
82 self.pretty_print(self.values)172 self.pretty_print()
83 if self.F == 100.0 or self.F == topF: 173 if self.F == 100.0 or self.F == topF:
84 print "assuming we converged, stopping" 174 print "assuming we converged, stopping"
85 break175 break
@@ -97,46 +187,51 @@ class benchonset(bench): · 11 unmodified lines
97 if topt == lesst:187 if topt == lesst:
98 lesst /= 2.188 lesst /= 2.
99 189
100 def auto_learn2(self,modes=['dual'],thresholds=[0.1,1.0]):190 def auto_learn2(self,modes=['dual'],thresholds=[0.00001,1.0]):
101 """ simple dichotomia like algorithm to optimise threshold """191 """ simple dichotomia like algorithm to optimise threshold """
102 self.modes = modes192 self.modes = modes
103 self.pretty_print(self.titles)193 self.pretty_titles([])
104 for mode in self.modes:194 for mode in self.modes:
105 steps = 10 195 steps = 10
106 step = thresholds[1]196 step = 0.4
107 curt = thresholds[0]
108 self.params.onsetmode = mode197 self.params.onsetmode = mode
109 198 self.params.threshold = thresholds[0]
110 self.params.threshold = curt 199 cur = 0
111 self.dir_exec()
112 self.dir_eval()
113 self.pretty_print(self.values)
114 curexp = self.expc
115 200
116 for i in range(steps):201 for i in range(steps):
117 if curexp < self.orig:
118 #print "we found at most less onsets than annotated"
119 self.params.threshold -= step
120 step /= 2
121 elif curexp > self.orig:
122 #print "we found more onsets than annotated"
123 self.params.threshold += step
124 step /= 2
125 self.dir_exec()202 self.dir_exec()
126 self.dir_eval()203 self.dir_eval()
127 curexp = self.expc204 self.pretty_print()
128 self.pretty_print(self.values)205 new = self.P
129 if self.orig == 100.0 or self.orig == self.expc: 206 if self.R == 0.0:
130 print "assuming we converged, stopping" 207 #print "Found maximum, highering"
208 step /= 2.
209 self.params.threshold -= step
210 elif new == 100.0:
211 #print "Found maximum, highering"
212 step *= .99
213 self.params.threshold += step
214 elif cur > new:
215 #print "lower"
216 step /= 2.
217 self.params.threshold -= step
218 elif cur < new:
219 #print "higher"
220 step *= .99
221 self.params.threshold += step
222 else:
223 print "Assuming we converged"
131 break224 break
225 cur = new
226
132 227
133if __name__ == "__main__":228if __name__ == "__main__":
134 import sys229 import sys
135 if len(sys.argv) > 1: datapath = sys.argv[1]230 if len(sys.argv) > 1: datapath = sys.argv[1]
136 else: print "ERR: a path is required"; sys.exit(1)231 else: print "ERR: a path is required"; sys.exit(1)
137 modes = ['complex', 'energy', 'phase', 'specdiff', 'kl', 'mkl', 'dual']232 modes = ['complex', 'energy', 'phase', 'specdiff', 'kl', 'mkl', 'dual']
138 #modes = [ 'complex' ]233 #modes = [ 'phase' ]
139 thresholds = [ 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5]234 thresholds = [ 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2]
140 #thresholds = [1.5]235 #thresholds = [1.5]
141 236
142 #datapath = "%s%s" % (DATADIR,'/onset/DB/*/')237 #datapath = "%s%s" % (DATADIR,'/onset/DB/*/')
@@ -145,16 +240,11 @@ if __name__ == "__main__": · 2 unmodified lines
145 benchonset = benchonset(datapath,respath,checkres=True,checkanno=True)240 benchonset = benchonset(datapath,respath,checkres=True,checkanno=True)
146 benchonset.params = taskparams()241 benchonset.params = taskparams()
147 benchonset.task = taskonset242 benchonset.task = taskonset
243 benchonset.valuesdict = {}
148 244
149 benchonset.titles = [ 'mode', 'thres', 'orig', 'expc', 'missd', 'mergd',
150 'bad', 'doubl', 'corrt', 'GD', 'FP', 'GD-merged', 'FP-pruned',
151 'prec', 'recl', 'dist' ]
152 benchonset.formats = ["%12s" , "| %6s", "| %6s", "| %6s", "| %6s", "| %6s",
153 "| %6s", "| %6s", "| %6s", "| %8s", "| %8s", "| %8s", "| %8s",
154 "| %6s", "| %6s", "| %6s"]
155 245
156 try:246 try:
157 benchonset.auto_learn2(modes=modes)247 #benchonset.auto_learn2(modes=modes)
158 #benchonset.run_bench(modes=modes)248 benchonset.run_bench(modes=modes,thresholds=thresholds)
159 except KeyboardInterrupt:249 except KeyboardInterrupt:
160 sys.exit(1)250 sys.exit(1)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t