def pfaget(f): if not 'sig' in f: raise ValueError("feaget without sig for: " + f['fn']) fea = ifea.fft(f['sig'], crate=icfg.get('pfa.crate'), wlen=icfg.get('pfa.wlen')).db() ifea.cavg(fea, rat=4, inplace=True) fea.sel(axis=len(fea.shape) - 1, len=icfg.get('pfa.dim'), inplace=True) return fea
def hmmtrn(ftrn,ftst,fea,s,kwargs={}): print('hmm start '+fea+'_'+s) chmm=ihmm.trn(flst=ftrn,fea=fea,its=[0],states=3,**kwargs) nldtrn=ihmm.evlp(chmm,flst=ftrn,fea=fea) nldtst=ihmm.evlp(chmm,flst=ftst,fea=fea) if icfg.get('exp')=='triclass' or icfg.get('trn.regression')==True: print('hmm finish '+fea+'_'+s) else: res=np.array(chmm['cls'])[nldtst.argmin(axis=1)] icls.cmp(ftst,res,'hmm finish '+fea+'_'+s) return (chmm,nldtrn,nldtst)
def dnntrn(ftrn,ftst,fea,s,kwargs={}): print('dnn start '+fea+'_'+s) cdnn=idnn.trn(ftrn,ftst,fea=fea,dmod=dmod,**kwargs) if cdnn is None: print('dnn failed '+fea+'_'+s) return (None,None,[]) else: restrn=idnn.evlp(cdnn,ftrn,fea=fea) restst=idnn.evlp(cdnn,ftst,fea=fea) if icfg.get('exp')=='triclass' or icfg.get('trn.regression')==True: print('dnn finish '+fea+'_'+s) else: res=np.array(cdnn['cls'])[prob.argmax(axis=1)] icls.cmp(ftst,res,'dnn finish '+fea+'_'+s) return (cdnn,restrn,restst)
def run_sen(s): print("flst [%s]"%(s)) fdb=ifdb.Fdb(s) ftrns=ftrn.expandsensor(s,fdb).maplab(labmap) ftsts=ftst.expandsensor(s,fdb).maplab(labmap) for typ in ['sig','pfa']: fdb.analyse(typ,eval(typ+'get')) sfaget(ftrns,ftsts,fdb) fdb.save() if not regression: ftrns=ftrns.equalcls() if '-n' in sys.argv: return for cls in clsuse: for fea in feause: if cls=='hmm' and ftrns[0][fea].shape[-1]>40: continue resfn=os.path.join(dlog,'res_'+cls+'_'+fea+'_'+s+'.npy') if os.path.exists(resfn): continue print('####################',fea,cls,s,'####################') kwargs=icfg.get('trnargs.%s.%s'%(cls,fea)) if kwargs is None: if cls=='dnn' or cls[:3]=='cnn': continue kwargs={} else: print('trnargs = '+kwargs) kwargs=eval(kwargs) kwargs['regression']=regression fnctrn=eval(cls[:3]+'trn') for i in range(3): (mod,restrn,restst)=fnctrn(ftrns,ftsts,fea,s,kwargs) if len(restst)>0: np.save(resfn,restst) np.save(resfn[:-4]+'_trn.npy',restrn) eval('i'+cls[:3]+'.save')(mod,resfn[:-4]+'.model') break
def dnntest(s,fea='sfa',**kwargs): fdb=ifdb.load(s) ftrns=ftrn.expandsensor(s,fdb).maplab(labmap) ftsts=ftst.expandsensor(s,fdb).maplab(labmap) if icfg.get('trn.regression')!=True: ftrns=ftrns.equalcls() cdnn=idnn.trn(ftrns,ftsts,fea=fea,dmod=dmod,verbose=True,**kwargs) if not cdnn is None: cdnn['idim']=ftrns[0][fea].shape return cdnn
def plot_roc(ftst, nld): okpat = 'Z00' if icfg.get('db') == 'izfp/cfk': okpat = 'Z0[0-2]' detect = ( nld[:, np.array([not re.match(okpat, f['lab']) is None for f in ftst])].mean(axis=0), nld[:, np.array([re.match(okpat, f['lab']) is None for f in ftst])].mean(axis=0), ) dres = [] for v in sorted([*detect[0], *detect[1]]): dres.append({ 'x': v, 'tp': np.sum(detect[0] > v), 'fp': np.sum(detect[1] > v), 'fn': np.sum(detect[0] <= v), 'tn': np.sum(detect[1] <= v), }) for r in dres: r['facc'] = r['fp'] / (r['fp'] + r['tn']) r['frej'] = r['fn'] / (r['fn'] + r['tp']) dpl = sorted([[r['facc'], r['frej']] for r in dres], key=lambda a: a[0] * 100 + (1 - a[1]))
def sigget(f): dsig = icfg.getdir('sig') sigext = '.' + icfg.get('sig.ext', 'wav') sig = isig.load(os.path.join(dsig, f['fn'] + sigext)).rmaxis() sig.inc = [1 / icfg.get('sig.srate')] return sig
if len(sys.argv) < 2: raise ValueError("Usage: " + sys.argv[0] + " (CFG prob_*.npy | csv)") if sys.argv[1] == 'csv': for cfgi in range(1, 4): cfg = '../cfk/X%i/info/default.cfg' % cfgi os.system(str.join(' ', ['python3', sys.argv[0], cfg, 'csv'])) raise SystemExit() icfg.Cfg(sys.argv[1]) ocsv = len(sys.argv) > 2 and sys.argv[2] == 'csv' if ocsv: sys.argv.remove('csv') ftst = icfg.readflst('test') ftrn = icfg.readflst('train') dlog = icfg.getdir('log') sen = getsensors() regression = icfg.get('trn.regression') == True labmap = {} if icfg.get('db') == 'izfp/cfk': labmap = {'Z0[0-2]': 'Z00'} ftst.maplab(labmap) lcls = np.array(icls.getcls(ftst)) if regression: icls.labf(ftst) lab = np.array([f['labf'] for f in ftst]) lfcls = [i[-1] for i in rle(sorted(lab))] resh = {} for fn in argv2resfns('res_', sys.argv[2:]): if fn.find('_trn.npy') >= 0 or fn.find('.model') >= 0: continue cls, fea, s = os.path.basename(fn)[4:-4].split('_')
np.save(dlog+'/dnnrnd/dnnrnd_'+str(int(time.time()))+'_par.npy',xpar) if len(stat)==3: np.save(dlog+'/dnnrnd/dnnrnd_'+str(int(time.time()))+'_stat.npy',stat) def dnnrndloop(): while not os.path.exists('stop'): dnnrnd() if len(sys.argv)<2: raise ValueError("Usage: "+sys.argv[0]+" CFG [-n]") icfg.Cfg(*sys.argv[1:]) ftrn=icfg.readflst('train') ftst=icfg.readflst('test') dmod=icfg.getdir('model') dlog=icfg.getdir('log') sen=getsensors() regression=icfg.get('trn.regression')==True senuse=sen feause=['pfa','sfa','sig'] clsuse=['hmm','svm','cnnb','cnn','dnn'] if not icfg.get('senuse') is None: senuse=icfg.get('senuse').split(',') if not icfg.get('feause') is None: feause=icfg.get('feause').split(',') if not icfg.get('clsuse') is None: clsuse=icfg.get('clsuse').split(',') labmap={} if icfg.get('db')=='izfp/cfk': labmap={'Z0[0-2]':'Z00'} maxjob=18 if '-nn' in sys.argv: raise SystemExit()
def prtres(prob, flst): eer, cm = icls.eer( prob, flst=flst, okpat='Z0[0-2]' if icfg.get('db') == 'izfp/cfk' else 'Z00') return 'EER: %6.2f%% CM: %6.2f%%' % (eer * 100, cm * 100)
if len(sys.argv) < 2: raise ValueError("Usage: " + sys.argv[0] + " CFG [-n]") icfg.Cfg(*sys.argv[1:]) if '-nn' in sys.argv: raise SystemExit() print("flst") ftrn = icfg.readflst('train') ftst = icfg.readflst('test') #fdev=[] if icfg.get('flist.dev') is None else icfg.readflst('dev') dmod = icfg.getdir('model') dlog = icfg.getdir('log') dsig = icfg.getdir('sig') sigext = '.' + icfg.get('sig.ext', 'wav') sen = getsensors() senuse = sen feause = ['pfa', 'sfa', 'sig'] clsuse = ['hmm', 'svm'] if not icfg.get('senuse') is None: senuse = icfg.get('senuse').split(',') if not icfg.get('feause') is None: feause = icfg.get('feause').split(',') if not icfg.get('clsuse') is None: clsuse = icfg.get('clsuse').split(',') maxjobs = 16 # Generate ftrns & ftsts for universal background model ftrns = {} for strn in senuse: ftrns[strn] = []
dlog = icfg.getdir('log') fns = argv2resfns('prob_', fns) out = [] for fn in sorted(fns): prob = np.load(fn) msg = '' if fn[-8:] == '_old.npy': continue if fn.find('_hmm_') >= 0 and prob.shape[-1] == 3: if np.sum(prob.take(2, -1) != prob.take(2, -1)[0]) == 0: msg = ' ERR in hmm[1]' prob = hmmprob(prob.take([1, 2], -1)) #prob=prob.take(0,-1) okpat = 'Z0[0-2]' if icfg.get('db') == 'izfp/cfk' else 'Z00' #okpat='Z0[01]' eer, cm = icls.eer(prob, flst=ftst, okpat=okpat) #ref=[not re.match(okpat,f['lab']) is None for f in ftst] #msg+=' [%.2f<=>%.2f]'%(np.mean(prob.mean(axis=0)[ref]),np.mean(prob.mean(axis=0)[np.array(ref)==False])) if np.max(prob.mean(0)) < 0.4: msg += ' ERR low max %.2f' % (np.max(prob.mean(0))) if np.min(prob.mean(0)) > 0.6: msg += ' ERR high min %.2f' % (np.min(prob.mean(0))) if ocsv: (cls, fea) = os.path.basename(fn)[5:-4].split('_') out.append( '"%s" "%s" "%s" %s %s "%s"' % (icfg.get('db').split('/')[-1] + '/' + icfg.get('exp'), fea, cls,