| @@ -1,31 +1,17 @@ |
| 1 | 1 | #! /usr/bin/python |
| 2 | 2 | |
| 3 | | −from aubio.bench.config import * |
| 4 | 3 | from aubio.bench.node import * |
| 5 | | − |
| 6 | | −class onset_parameters: |
| 7 | | − def __init__(self): |
| 8 | | − """ set default parameters """ |
| 9 | | − self.silence = -70 |
| 10 | | − self.derivate = False |
| 11 | | − self.localmin = False |
| 12 | | − self.bufsize = 512 |
| 13 | | − self.hopsize = 256 |
| 14 | | − self.samplerate = 44100 |
| 15 | | − self.tol = 0.05 |
| 16 | | − self.step = float(self.hopsize)/float(self.samplerate) |
| 17 | | − self.threshold = 0.1 |
| 18 | | − self.mode = 'dual' |
| 4 | +from aubio.tasks import * |
| 19 | 5 | |
| 20 | 6 | class benchonset(bench): |
| 21 | 7 | |
| 22 | | − def compute_results(self): |
| 8 | + def dir_eval(self): |
| 23 | 9 | self.P = 100*float(self.expc-self.missed-self.merged)/(self.expc-self.missed-self.merged + self.bad+self.doubled) |
| 24 | 10 | self.R = 100*float(self.expc-self.missed-self.merged)/(self.expc-self.missed-self.merged + self.missed+self.merged) |
| 25 | 11 | if self.R < 0: self.R = 0 |
| 26 | 12 | self.F = 2* self.P*self.R / (self.P+self.R) |
| 27 | 13 | |
| 28 | | − self.values = [self.params.mode, |
| 14 | + self.values = [self.params.onsetmode, |
| 29 | 15 | "%2.3f" % self.params.threshold, |
| 30 | 16 | self.orig, |
| 31 | 17 | self.expc, |
| @@ -42,51 +28,17 @@ class benchonset(bench): · 10 unmodified lines |
| 42 | 28 | "%2.3f" % self.R, |
| 43 | 29 | "%2.3f" % self.F ] |
| 44 | 30 | |
| 45 | | − def compute_onset(self,input,output): |
| 46 | | − from aubio.tasks import getonsets, get_onset_mode |
| 47 | | − from aubio.onsetcompare import onset_roc, onset_diffs |
| 48 | | − from aubio.txtfile import read_datafile |
| 49 | | − amode = 'roc' |
| 50 | | − vmode = 'verbose' |
| 51 | | − vmode = '' |
| 52 | | − lres, ofunc = getonsets(input, |
| 53 | | − self.params.threshold, |
| 54 | | − self.params.silence, |
| 55 | | − mode=get_onset_mode(self.params.mode), |
| 56 | | − localmin=self.params.localmin, |
| 57 | | − derivate=self.params.derivate, |
| 58 | | − bufsize=self.params.bufsize, |
| 59 | | − hopsize=self.params.hopsize, |
| 60 | | − storefunc=False) |
| 61 | | − |
| 62 | | − for i in range(len(lres)): lres[i] = lres[i]*self.params.step |
| 63 | | − ltru = read_datafile(input.replace('.wav','.txt'),depth=0) |
| 64 | | − if vmode=='verbose': |
| 65 | | − print "Running with mode %s" % self.params.mode, |
| 66 | | − print " and threshold %f" % self.params.threshold, |
| 67 | | − print " on file", input |
| 68 | | − #print ltru; print lres |
| 69 | | − if amode == 'localisation': |
| 70 | | − l = onset_diffs(ltru,lres,self.params.tol) |
| 71 | | − mean = 0 |
| 72 | | − for i in l: mean += i |
| 73 | | − if len(l): print "%.3f" % (mean/len(l)) |
| 74 | | − else: print "?0" |
| 75 | | − elif amode == 'roc': |
| 76 | | − orig, missed, merged, expc, bad, doubled = onset_roc(ltru,lres,self.params.tol) |
| 77 | | − self.orig += orig |
| 78 | | − self.missed += missed |
| 79 | | − self.merged += merged |
| 80 | | − self.expc += expc |
| 81 | | − self.bad += bad |
| 82 | | − self.doubled += doubled |
| 83 | | − self.compute_results() |
| 84 | | − |
| 85 | | − def compute_data(self): |
| 86 | | − self.orig, self.missed, self.merged, self.expc, \ |
| 87 | | − self.bad, self.doubled = 0, 0, 0, 0, 0, 0 |
| 88 | | − act_on_data(self.compute_onset,self.datadir,self.resdir, \ |
| 89 | | − suffix='',filter='f -name \'*.wav\'') |
| 31 | + def file_exec(self,input,output): |
| 32 | + filetask = self.task(input,params=self.params) |
| 33 | + computed_data = filetask.compute_all() |
| 34 | + results = filetask.eval(computed_data) |
| 35 | + self.orig += filetask.orig |
| 36 | + self.missed += filetask.missed |
| 37 | + self.merged += filetask.merged |
| 38 | + self.expc += filetask.expc |
| 39 | + self.bad += filetask.bad |
| 40 | + self.doubled += filetask.doubled |
| 41 | + |
| 90 | 42 | |
| 91 | 43 | def run_bench(self,modes=['dual'],thresholds=[0.5]): |
| 92 | 44 | self.modes = modes |
| @@ -94,11 +46,11 @@ class benchonset(bench): · 1 unmodified line |
| 94 | 46 | |
| 95 | 47 | self.pretty_print(self.titles) |
| 96 | 48 | for mode in self.modes: |
| 97 | | − self.params.mode = mode |
| 49 | + self.params.onsetmode = mode |
| 98 | 50 | for threshold in self.thresholds: |
| 99 | 51 | self.params.threshold = threshold |
| 100 | | − self.compute_data() |
| 101 | | − self.compute_results() |
| 52 | + self.dir_exec() |
| 53 | + self.dir_eval() |
| 102 | 54 | self.pretty_print(self.values) |
| 103 | 55 | |
| 104 | 56 | def auto_learn(self,modes=['dual'],thresholds=[0.1,1.5]): |
| @@ -109,21 +61,24 @@ class benchonset(bench): · 4 unmodified lines |
| 109 | 61 | steps = 10 |
| 110 | 62 | lesst = thresholds[0] |
| 111 | 63 | topt = thresholds[1] |
| 112 | | − self.params.mode = mode |
| 64 | + self.params.onsetmode = mode |
| 113 | 65 | |
| 114 | 66 | self.params.threshold = topt |
| 115 | | − self.compute_data() |
| 67 | + self.dir_exec() |
| 68 | + self.dir_eval() |
| 116 | 69 | self.pretty_print(self.values) |
| 117 | 70 | topF = self.F |
| 118 | 71 | |
| 119 | 72 | self.params.threshold = lesst |
| 120 | | − self.compute_data() |
| 73 | + self.dir_exec() |
| 74 | + self.dir_eval() |
| 121 | 75 | self.pretty_print(self.values) |
| 122 | 76 | lessF = self.F |
| 123 | 77 | |
| 124 | 78 | for i in range(steps): |
| 125 | 79 | self.params.threshold = ( lesst + topt ) * .5 |
| 126 | | − self.compute_data() |
| 80 | + self.dir_exec() |
| 81 | + self.dir_eval() |
| 127 | 82 | self.pretty_print(self.values) |
| 128 | 83 | if self.F == 100.0 or self.F == topF: |
| 129 | 84 | print "assuming we converged, stopping" |
| @@ -143,34 +98,31 @@ class benchonset(bench): · 13 unmodified lines |
| 143 | 98 | lesst /= 2. |
| 144 | 99 | |
| 145 | 100 | |
| 146 | | −#modes = [ 'complex' ] |
| 147 | | −modes = ['complex', 'energy', 'phase', 'specdiff', 'kl', 'mkl', 'dual'] |
| 148 | | −#thresholds = [1.5] |
| 149 | | −thresholds = [ 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5] |
| 150 | | − |
| 151 | | −#datapath = "%s%s" % (DATADIR,'/onset/DB/*/') |
| 152 | | −datapath = "%s%s" % (DATADIR,'/onset/DB/PercussivePhrases/RobertRich') |
| 153 | | −respath = '/var/tmp/DB-testings' |
| 154 | | − |
| 155 | | −benchonset = benchonset(datapath,respath,checkres=True,checkanno=True) |
| 156 | | − |
| 157 | | −benchonset.params = onset_parameters() |
| 158 | | − |
| 159 | | −benchonset.titles = [ 'mode', 'thres', 'orig', 'expc', 'missd', 'mergd', |
| 160 | | −'bad', 'doubl', 'corrt', 'GD', 'FP', 'GD-merged', 'FP-pruned', |
| 161 | | −'prec', 'recl', 'dist' ] |
| 162 | | −benchonset.formats = ["%12s" , "| %6s", "| %6s", "| %6s", "| %6s", "| %6s", |
| 163 | | −"| %6s", "| %6s", "| %6s", "| %8s", "| %8s", "| %8s", "| %8s", |
| 164 | | −"| %6s", "| %6s", "| %6s"] |
| 165 | | − |
| 166 | | −#benchonset.run_bench(modes=modes,thresholds=thresholds) |
| 167 | | −benchonset.auto_learn(modes=modes) |
| 168 | | − |
| 169 | | −# gatherdata |
| 170 | | −#act_on_data(my_print,datapath,respath,suffix='.txt',filter='f -name \'*.wav\'') |
| 171 | | −# gatherthreshold |
| 172 | | −# gathermodes |
| 173 | | −# comparediffs |
| 174 | | −# gatherdiffs |
| 175 | | − |
| 176 | | − |
| 101 | +if __name__ == "__main__": |
| 102 | + import sys |
| 103 | + if len(sys.argv) > 1: datapath = sys.argv[1] |
| 104 | + else: print "ERR: a path is required"; sys.exit(1) |
| 105 | + modes = ['complex', 'energy', 'phase', 'specdiff', 'kl', 'mkl', 'dual'] |
| 106 | + #modes = [ 'complex' ] |
| 107 | + thresholds = [ 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5] |
| 108 | + #thresholds = [1.5] |
| 109 | + |
| 110 | + #datapath = "%s%s" % (DATADIR,'/onset/DB/*/') |
| 111 | + respath = '/var/tmp/DB-testings' |
| 112 | + |
| 113 | + benchonset = benchonset(datapath,respath,checkres=True,checkanno=True) |
| 114 | + benchonset.params = taskparams() |
| 115 | + benchonset.task = taskonset |
| 116 | + |
| 117 | + benchonset.titles = [ 'mode', 'thres', 'orig', 'expc', 'missd', 'mergd', |
| 118 | + 'bad', 'doubl', 'corrt', 'GD', 'FP', 'GD-merged', 'FP-pruned', |
| 119 | + 'prec', 'recl', 'dist' ] |
| 120 | + benchonset.formats = ["%12s" , "| %6s", "| %6s", "| %6s", "| %6s", "| %6s", |
| 121 | + "| %6s", "| %6s", "| %6s", "| %8s", "| %8s", "| %8s", "| %8s", |
| 122 | + "| %6s", "| %6s", "| %6s"] |
| 123 | + |
| 124 | + try: |
| 125 | + benchonset.auto_learn(modes=modes) |
| 126 | + #benchonset.run_bench(modes=modes) |
| 127 | + except KeyboardInterrupt: |
| 128 | + sys.exit(1) |