int main(int argc, char const *argv[]) { int n; while (scanf("%d", &n) && n) { for (int i = 0; i < n; i++) { scanf("%lf%lf%lf%lf", &s[i].st.x, &s[i].st.y, &s[i].ed.x, &s[i].ed.y); s[i].calc(); } sort(s, s + n); int ret = 1; Seg cur = s[0]; for (int i = 1; i < n; i++) { if (cur.same(s[i])) { cur.st = min(cur.st, s[i].st); cur.ed = max(cur.ed, s[i].ed); } else { cur = s[i]; ret++; } } printf("%d\n", ret); } return 0; }
//------------------------------------------------------------------------- void computeLabelNGram(NGram & NG,SegCluster &cluster,SegCluster &clusterOut,ULongVector &tabS,unsigned long nbSym){ unsigned long begin[100]; // max order 100... short int sym[100]; unsigned long end[100]; SegServer & segServerOut=clusterOut.getServer(); // Get the clusterserver reelated to the output cluster.rewind(); Seg* seg; // Reset the reader at the begin of the input stream while((seg=cluster.getSeg())!=NULL){ // For each of the selected segments unsigned long idxFrame=seg->begin(); unsigned long endS=endSeg(seg); if (endS>=nbSym) endS=nbSym; // Just if there is less symbol in the file than in the label if (idxFrame>endS) idxFrame=endS; unsigned long beginOOV=idxFrame; bool oov=true; if (debug) cout <<"begin Seg["<<idxFrame<<"]"<<endl; for (unsigned long n=0;(idxFrame<endS) &&(n<NG.getOrder()-1);n++){ // Recognize the (n-1) first symbols begin[n]=idxFrame; sym[n]=recognizeSymbol(idxFrame,endS,tabS); end[n]=idxFrame-1; if (debug) cout <<"sym ["<<sym[n]<<"] begin["<<begin[n]<<"] end["<<end[n]<<"] idxframe["<<idxFrame<<"]"<<endl; } while(idxFrame<endS){ begin[NG.getOrder()-1]=idxFrame; sym[NG.getOrder()-1]=recognizeSymbol(idxFrame,endS,tabS); end[NG.getOrder()-1]=idxFrame-1; if (debug) cout <<"sym ["<<sym[NG.getOrder()-1]<<"] begin["<<begin[NG.getOrder()-1] <<"] end["<<end[NG.getOrder()-1]<<"] idxframe["<<idxFrame<<"]"<<endl; unsigned long tag; if (isNGram(sym,NG,tag)){ if ((oov)&&(beginOOV<begin[0])){ if (debug) cout <<"OOV1 begin["<<beginOOV <<"] end["<<begin[0]-1<<"]"<<endl; Seg &segTmp=segServerOut.createSeg(beginOOV,begin[0]-beginOOV,0,"oov",seg->sourceName()); clusterOut.add(segTmp); } if (debug) cout <<"NGRAM ["<<tag<<"] begin["<<begin[0] <<"] end["<<end[NG.getOrder()-1]<<"]"<<endl; Seg &segTmp=segServerOut.createSeg(begin[0],end[NG.getOrder()-1]-begin[0]+1,0,String::valueOf(tag),seg->sourceName()); clusterOut.add(segTmp); beginOOV=idxFrame; oov=false; } else oov=true; moveTab(begin,sym,end,NG.getOrder()); } if (oov){ Seg &segTmp=segServerOut.createSeg(beginOOV,idxFrame-beginOOV,0,"oov",seg->sourceName()); clusterOut.add(segTmp); if (debug) cout <<"OOV2 begin["<<beginOOV <<"] end["<<idxFrame-1<<"]"<<endl; } } }
// Information on the quantity of data available for each client // Outputs a list with the selected files for a defined quantity of data int ExtractTargetDataInfo(Config& config) { String inputClientListFileName = config.getParam("targetIdList"); bool fixedLabelSelectedFrame; String labelSelectedFrames; if (config.existsParam("useIdForSelectedFrame")) // the ID of each speaker is used as labelSelectedFrame fixedLabelSelectedFrame=false; else{ // the label is decided by the command line and is unique for the run labelSelectedFrames=config.getParam("labelSelectedFrames"); if (verbose) cout << "Computing on" << labelSelectedFrames << " label" << endl; fixedLabelSelectedFrame=true; } unsigned long maxFrame=config.getParam("maxFrame").toLong(); String outputFilename=config.getParam("outputFilename"); ofstream outputFile(outputFilename.c_str(),ios::out| ios::trunc); try{ XList inputClientList(inputClientListFileName,config); // read the Id + filenames for each client XLine * linep; if (verbose) cout << "InfoTarget" << endl; // *********** Target loop ***************** while ((linep=inputClientList.getLine()) != NULL){ // linep gives the XLine with the Id of a given client and the list of files String *id=linep->getElement(); // Get the Client ID (id) outputFile<<*id; String currentFile=""; XLine featureFileListp=linep->getElements(); // Get the list of feature file for the client (end of the line) if (verbose) cout << "Info model ["<<*id<<"]"<<endl; if (!fixedLabelSelectedFrame){ // the ID is used as label for selecting the frame labelSelectedFrames=*id; if (debug) cout <<*id<<" is used for label selected frames"<<endl; } // label files reading - It creates, for each file and each label, a cluster of segments - will be integrated witth the featre s - asap SegServer segmentsServer; // Reading the segmentation files for each feature input file LabelServer labelServer; initializeClusters(featureFileListp,segmentsServer,labelServer,config); // Reading the segmentation files for each feature input file unsigned long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames); // Get the index of the cluster with in interest audio segments SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame); // Gives the cluster of the selected/used segments Seg *seg; // Will give the current segment unsigned long frameCount=0; selectedSegments.rewind(); // at the begin of the selected segments list while(((seg=selectedSegments.getSeg())!=NULL) && (frameCount<maxFrame)){ // For each of the selected segments until the amount of data is get frameCount+=seg->length(); cout << seg->sourceName()<<" "<<seg->begin()<<" "<<seg->length()<<" Total time="<<frameCount<<endl; if (seg->sourceName()!=currentFile){ outputFile<<" "<<seg->sourceName(); currentFile=seg->sourceName(); } } // end of the initial Train Iteration loop outputFile<<endl; if (verbose) cout << "Save info client ["<<*id<<"]" << endl; } // end of the the target loop } // fin try catch (Exception& e) { cout << e.toString().c_str() << endl; } return 0; }
// Can use this function to get likelihood with a topgauss double TopGauss::get(MixtureGD & UBM,FeatureServer &fs,String & featureFilename,Config & config){ StatServer ss(config); String labelSelectedFrames =config.getParam("labelSelectedFrames"); unsigned long begin=fs.getFirstFeatureIndexOfASource(featureFilename); fs.seekFeature(begin); SegServer segmentsServer; LabelServer labelServer; initializeClusters(featureFilename,segmentsServer,labelServer,config); // __android_log_print(ANDROID_LOG_DEBUG, "TopGauss::get", " Feature file %s \n", featureFilename.c_str()); verifyClusterFile(segmentsServer,fs,config); unsigned long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames); SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame); MixtureGDStat &acc=ss.createAndStoreMixtureStat(UBM); Seg *seg; // current selected segment selectedSegments.rewind(); unsigned long t=0; //cnt frames acc.resetLLK(); unsigned long idxBegin=0; while((seg=selectedSegments.getSeg())!=NULL){ unsigned long begin=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName()); fs.seekFeature(begin); Feature f; idxBegin=this->frameToIdx(t); for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){ fs.readFeature(f); //unsigned long idx=this->frameToIdx(t); unsigned long nbg=_nbg[t]; ULongVector index; double sumNonSelectedWeights=_snsw[t]; double sumNonSelectedLLK=_snsl[t]; for (unsigned long i=0;i<nbg;i++) { index.addValue(_idx[idxBegin+i]); } char c[100]; sprintf(c,"%d",(int)index.size()); config.setParam("topDistribsCount",c); // this should be high enough if (t==0) {acc.computeAndAccumulateLLK(f,1.0,DETERMINE_TOP_DISTRIBS);acc.resetLLK();} // to remove in ALIZE, this is to init the LKvector ss.setTopDistribIndexVector(index, sumNonSelectedWeights, sumNonSelectedLLK); acc.computeAndAccumulateLLK(f,1.0,USE_TOP_DISTRIBS); idxBegin+=nbg; t++; } } //ss.deleteMixtureStat(acc); if (t!=_nt || idxBegin !=_nbgcnt) cout << "W: t("<<t<<") != _nt(" <<_nt<<")"<<"W: idxBegin("<<idxBegin<<") != _nbgcnt(" <<_nbgcnt<<")"<<endl; return acc.getMeanLLK(); }
static int mput(Map *map, uvlong addr, void *buf, int size) { vlong off; Seg *s; s = reloc(map, addr, &off); if (!s) return -1; if (s->rw == nil) { werrstr("unwritable map"); return -1; } return s->rw(map, s, off, buf, size, 0); }
static int mget(Map *map, uvlong addr, void *buf, int size) { uvlong off; Seg *s; s = reloc(map, addr, (vlong*)&off); if (!s) return -1; if (s->rw == nil) { werrstr("unreadable map"); return -1; } return s->rw(map, s, off, buf, size, 1); }
/* void baggedSegments(SegCluster &selectedSegments,RefVector<SegCluster> &baggedA,double & baggedProbability, unsigned long minimumLength,unsigned long maximumLength){ Seg* seg; // reset the reader at the begin of the input stream selectedSegments.rewind(); seg=selectedSegments.getSeg(); bool end=(seg==NULL); unsigned long beginSeg=0,lengthSeg=0; if (!end){ beginSeg=seg->begin(); lengthSeg=seg->length(); } while(!end){ if (debug) cout << "bagged, current input seg ["<<beginSeg<<","<<lengthSeg<<"]"<<endl; unsigned long verifyLength=correctedLength(lengthSeg,minimumLength,maximumLength); bool moveSeg=true; unsigned long length=0; if (lengthSeg<=verifyLength){ moveSeg=true; length=lengthSeg; if (debug) cout <<"change seg"<<endl; } else{ moveSeg=false; length=verifyLength; } // for all cluster in baggedA if (length>0) for (unsigned long idx=0;idx<baggedA.size();idx++) // For each component if(baggedFrame(baggedProbability)){ SegServer &segServerOutput=baggedA[idx].getServer(); Seg &newSeg=segServerOutput.createSeg(beginSeg,length,0,seg->string(),seg->sourceName()); baggedA[idx].add(newSeg); if (debug) cout << "bagged - Adding in bagged["<<idx<<"] the seg ["<<seg->sourceName()<<"]"<<newSeg.begin()<<" "<<newSeg.length()<<endl; } if (moveSeg){ seg=selectedSegments.getSeg(); end=(seg==NULL); if (!end){ beginSeg=seg->begin(); lengthSeg=seg->length(); } } else{ lengthSeg-=length; beginSeg+=length; } } if ((debug) || (verboseLevel>3)){ cout <<"Bagged segments"<<endl; for (unsigned long idx=0;idx<baggedA.size();idx++){ cout << "Bagged cluster["<<idx<<"]"<<endl; showCluster(baggedA[idx]); } } if (verbose){ unsigned long total=totalFrame(selectedSegments); for (unsigned long idx=0;idx<baggedA.size();idx++){ unsigned long selected=totalFrame(baggedA[idx]); double percent=(double)selected*100/(double) total; cout <<"Bagged segments["<<idx<<"] Initial frames["<<total<<"] Selected frames["<<selected<<"] % selected["<<percent<<"]"<<endl; } } }*/ void baggedSegments(SegCluster &selectedSegments,SegCluster &baggedFrameSegment,double baggedProbability, unsigned long minimumLength,unsigned long maximumLength){ SegServer &segServerOutput=baggedFrameSegment.getServer(); Seg* seg; // reset the reader at the begin of the input stream selectedSegments.rewind(); seg=selectedSegments.getSeg(); bool end=(seg==NULL); unsigned long beginSeg=0,lengthSeg=0; if (!end){ beginSeg=seg->begin(); lengthSeg=seg->length(); } while(!end){ if (debug) cout << "bagged, current input seg ["<<beginSeg<<","<<lengthSeg<<"]"<<endl; unsigned long verifyLength=correctedLength(lengthSeg,minimumLength,maximumLength); double segBaggedProbability=baggedProbability; bool moveSeg=true; unsigned long length=0; if (lengthSeg<=verifyLength){ moveSeg=true; length=lengthSeg; if (debug) cout <<"change seg"<<endl; } else{ moveSeg=false; length=verifyLength; } if ((length>0) &&(baggedFrame(segBaggedProbability))){ Seg &newSeg=segServerOutput.createSeg(beginSeg,length,0,seg->string(),seg->sourceName()); baggedFrameSegment.add(newSeg); if (debug) cout << "bagged - Adding the seg ["<<seg->sourceName()<<"]"<<newSeg.begin()<<" "<<newSeg.length()<<endl; } if (moveSeg){ seg=selectedSegments.getSeg(); end=(seg==NULL); if (!end){ beginSeg=seg->begin(); lengthSeg=seg->length(); } } else{ lengthSeg-=length; beginSeg+=length; } } if ((debug) || (verboseLevel>3)){ cout <<"Bagged segments"<<endl; showCluster(baggedFrameSegment); } if (verbose){ unsigned long total=totalFrame(selectedSegments); unsigned long selected=totalFrame(baggedFrameSegment); double percent=(double)selected*100/(double) total; cout <<"Bagged segments, Initial frames["<<total<<"] Selected frames["<<selected<<"] % selected["<<percent<<"]"<<endl; } }
double dist(Seg o) const { double ret = 1e+60; ret = min(ret, this->dist(o.s)); ret = min(ret, this->dist(o.e)); ret = min(ret, o.dist(s)); ret = min(ret, o.dist(e)); return ret; }
/// Compute Log Likelihood of the Factor Analysis model double FactorAnalysisStat::getLLK(SegCluster &selectedSegments,MixtureGD &model,FeatureServer&fs,Config & config){ if (verbose) cout << "(FactorAnalysisStat) Compute Likelihood" << endl; double llk=0.0; MixtureGDStat &acc=_ss.createAndStoreMixtureStat(model); Seg *seg; selectedSegments.rewind(); while((seg=selectedSegments.getSeg())!=NULL){ unsigned long begin=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName()); fs.seekFeature(begin); Feature f; for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){ fs.readFeature(f); acc.computeAndAccumulateLLK(f,1.0,TOP_DISTRIBS_NO_ACTION); } } llk= acc.getMeanLLK(); _ss.deleteMixtureStat(acc); return llk; };
// Build the segments with the energized frames unsigned long selectFrames(FeatureServer &fs,SegServer & segServer,double threshold,SegCluster &selectedSeg,SegCluster &outputSeg,String labelOutput,String fileName) { unsigned long countFrames=0; fs.reset(); // feature server reset unsigned long ind=0; unsigned long begin=0; bool in=false; Seg *seg; // current selectd segment selectedSeg.rewind(); // reset the reader at the begin of the input stream while((seg=selectedSeg.getSeg())!=NULL){ // For each input segments for (unsigned long idx=seg->begin();idx<seg->begin()+seg->length();idx++){ // for each frame Feature f; fs.seekFeature(idx); fs.readFeature(f); if (f[0]>threshold){ // the frame is selected countFrames++; if (in==false){ // Begin of a new segment in=true; begin=ind; } } else if (in){ // End of a segment in=false; Seg & segFake=segServer.createSeg(begin,ind-begin,0, // Create a segment - Take care : length=end-begin+1 but ind =end+1 !! labelOutput,fileName); outputSeg.add(segFake); // Add a segment } ind++; // Increment the frame index } // end of one input segment if (in){ // deal with the last energized segmeent inside the current input segment in=false; Seg & segFake=segServer.createSeg(begin,ind-begin+1,0, // Create a segment labelOutput,fileName); outputSeg.add(segFake); // Add a segment - Take care : length=end-begin+1 and ind=end in this case !! } } // end feature loop return countFrames; }
void BChain::ouchify(double amount, double direction, double whip_speed, int segs) { STACKTRACE; Seg* cur; int seg_count = segs; cur=this; while(cur) { cur->hurty_time = amount; /* if (cur->Next_Seg == NULL && direction != 0) { //if (direction != 0) cur->accelerate_gravwhip(ship, cur->angle+(PI/2)*direction, whip_speed, MAX_SPEED); } */ cur->accelerate_gravwhip(ship, cur->angle+((PI/2)*direction), whip_speed, MAX_SPEED); whip_speed *= 0.75; seg_count--; cur = cur->Next_Seg; } };
void FactorAnalysisStat::computeAndAccumulateGeneralFAStats(SegCluster &selectedSegments,FeatureServer &fs,Config & config){ if (verbose) cout <<"(FactorAnalysisStat) Compute General FA Stats (Complete)" << endl; double *N_h, *N, *S_X_h, *S_X,*ff; _matN_h.setAllValues(0.0); _matN.setAllValues(0.0); _matS_X_h.setAllValues(0.0); _matS_X.setAllValues(0.0); N_h=_matN_h.getArray(); N=_matN.getArray(); S_X_h=_matS_X_h.getArray();S_X=_matS_X.getArray(); MixtureGD & UBM=_ms.getMixtureGD((unsigned long) 1); MixtureGDStat &acc=_ss.createAndStoreMixtureStat(UBM); // Compute Occupations and Statistics acc.resetOcc(); Seg *seg; selectedSegments.rewind(); String currentSource="";unsigned long loc=0;unsigned long sent=0; while((seg=selectedSegments.getSeg())!=NULL){ unsigned long begin=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName()); // Idx of the first frame of the current file in the feature server if (currentSource!=seg->sourceName()) { currentSource=seg->sourceName(); loc=_ndxTable.locNb(currentSource); sent=_ndxTable.sessionNb(currentSource); if (verbose)cout << "Processing speaker["<<currentSource<<"]"<< endl; } fs.seekFeature(begin); Feature f; if (!_topGauss) { for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){ fs.readFeature(f); acc.computeAndAccumulateOcc(f); RealVector <double> aPost=acc.getOccVect(); ff=f.getDataVector(); for(unsigned long k=0;k<_mixsize;k++) { N_h[sent*_mixsize+k]+=aPost[k]; N[loc*_mixsize+k] +=aPost[k]; for (unsigned long i=0;i<_vsize;i++) { S_X_h[sent*_supervsize+(k*_vsize+i)]+=aPost[k]*ff[i]; S_X[loc*_supervsize+(k*_vsize+i)] +=aPost[k]*ff[i]; } } } } else throw Exception("ComputeGeneralStats TopGauss not done at this level",__FILE__,__LINE__); } };
/// Normalize features with a smooth mixture transformation o't=ot-sum(P(c|ot)Uc.x) void FactorAnalysisStat::normalizeFeatures(SegCluster &selectedSegments,FeatureServer &fs,Config & config){ if (verbose) cout << "(FactorAnalysisStat) Normalize Features" << endl; MixtureGD & clientMixture=_ms.getMixtureGD(1); // copy the UBM mixture unsigned long nt=0; RealVector <double> m_xh_1; m_xh_1.setSize(_supervsize); double *_m_xh_1=m_xh_1.getArray(); Seg *seg; // current selectd segment selectedSegments.rewind(); String currentSource=""; while((seg=selectedSegments.getSeg())!=NULL){ unsigned long begin=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName()); if (currentSource!=seg->sourceName()) { currentSource=seg->sourceName(); this->getUX(m_xh_1,currentSource); this->getSpeakerModel(clientMixture,currentSource); if (verbose)cout << "Processing speaker["<<currentSource<<"]"<< endl; } fs.seekFeature(begin); Feature f; if (!_topGauss) { for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){ fs.readFeature(f,0); double *ff=f.getDataVector(); double sum=0.0; RealVector <double> P; P.setSize(_mixsize); double *Prob=P.getArray(); for(unsigned long k=0;k<_mixsize;k++) { Prob[k]=clientMixture.weight(k)*clientMixture.getDistrib(k).computeLK(f); sum+=Prob[k]; } for(unsigned long k=0;k<_mixsize;k++) Prob[k]/=sum; for(unsigned long k=0;k<_mixsize;k++) { for (unsigned long i=0;i<_vsize;i++) ff[i]-= Prob[k]*_m_xh_1[k*_vsize+i]; } fs.writeFeature(f); nt++; } } else { throw Exception("no topgauss yet",__FILE__,__LINE__); } } };
void audioCB(AudioIOData& io){ while(io()){ using namespace gam::rnd; if(tmr()){ env0 = uni(0.4, 0.39); if(prob(0.8)){ float r = uni(1.); tmr.period(r * 4); env0.period(r * 4); } int a = pick(8,6, 0.7); if(prob(0.2)) osc0.freq(quanOct(a, 440.)); if(prob(0.1)) osc1.freq(quanOct(a, 220.)); if(prob(0.1)) osc2.freq(quanOct(a, 110.)); if(prob(0.1)) osc3.freq(quanOct(a, 55.)); if(prob(0.2)) frq0 = lin(8000, 400); if(prob(0.2)) frq1 = lin(8000, 400);//printf("d"); } float e = lag(env0()); del0.delay(e); del1.delay(e * 0.9); float s = (osc0.up() * mod0() + osc1.up() * mod1() + osc2.up() * mod2() + osc3.up() * mod3()) * 0.05; res0.freq(frq0()); res1.freq(frq1()); s = res0(s) + res1(s); float sl = ech0(del0(s), ap0(ech0())); float sr = ech1(del1(s), ap1(ech1())); io.out(0) = sl; io.out(1) = sr; } }
void go() { String fname=Sconc(ImageDir,iname,".bmp"); String ename=Sconc(ImageDir,"exp/",iname,".bmp"); printf("read %s\n",fname); orig=RGBmp::readfile(fname); // image=orig->scaledby(scale)->getv3ds(); image=orig->scaletowidth(processwidth)->getv3ds(); printf("Scanning image %i x %i\n",image->width,image->height); printf("done\n"); if (train || test) expected=Map2d<float>::readfile(ename)->threshold(0.5)->binscaleto(image->width,image->height); greyscale=image->applyfn(&V3dtofloat); starttimer(); greyscale->edgedetection(Map2d<float>::sobel(),&edgemag,&edgeang); ghistscale=1.0/(float)windres; ghs=new Map2d<GHist *>(image->width*ghistscale,image->height*ghistscale,(GHist *)NULL); if (train) data=List<IOData>(2000); printf("Starting segmentation...\n"); Seg seg; if (train) seg=Seg(&classifytrain); else seg=Seg(&classifynn); if (show) for (int i=1;i<=nummeasures;i++) measmaps.add(new Map2d<float>(image->width,image->height,(float)0)); if (twopass) { pass=1; printf("First scan, reading measures...\n"); Map2d<bool> *b=seg.classifywindows(); destroy(b); normalisedata(); pausetimer(); printf("Asking neural network...\n"); printf("A\n"); for (int i=1;i<=data.len;i++) { writetraindata(data.num(i).input,123.456); data.p2num(i)->freedom(); } printf("Freeing data\n"); data.freedom(); printf("B\n"); addheaderto(&traindata,numtrainexs); writelinestofile(&traindata,"question.pat"); printf("Not freeing traindata strings\n"); // traindata.freeall(); printf("Freeing traindata\n"); traindata.freedom(); system("./asknn"); traindata=readlinesfromfile("answer.res"); unpausetimer(); currentline=14; pass=2; printf("Second scan...\n"); } Map2d<bool> *b=seg.classifywindows(); printf("Time taken: %f seconds.\n",gettimer()); if (show) for (int i=1;i<=nummeasures;i++) measmaps.num(i)->writefile(getnextfilename("meas","bmp")); // Seg seg=Seg(&measureglvariance); // Map2d<bool> *b=seg.classifytopdown(); // Map2d<bool> *b=seg.classifyneighbours(); // Map2d<bool> *b=seg.classifywindows(); b->scaleto(image->width,image->height)->writefile("initseg.bmp"); // Reject small regions List< Region * > *l=b->getrealregions(); b=new Map2d<bool>(b->width,b->height,false); for (int i=1;i<=l->len;i++) { List<Pixel> *ps=l->num(i)->getlist(); if (ps->len>=minarea) for (int j=1;j<=ps->len;j++) b->setpos(ps->num(j),true); } b->writefile("initb4join.bmp"); // Perform morphological joining (dilation and erosion) b=b->binscaleto(b->width/windres,b->height/windres); b=b->expand(morphrad)->contract(2*morphrad)->expand(morphrad); // b=b->contract(morphrad)->expand(2*morphrad)->contract(morphrad); // b=b->expand(morphrad)->inverse()->expand(morphrad)->inverse(); b=b->binscaleto(image->width,image->height); // b->writefile("initjoined.bmp"); b->writefile("textseg.bmp"); if (test) { int correct=0; int wrong=0; int ctextcorrect=0; int cnottextcorrect=0; int ctextincorrect=0; int cnottextincorrect=0; int total=0; for (int i=0;i<b->width;i++) for (int j=0;j<b->height;j++) { total++; bool exp=expected->getpos(i*expected->width/b->width,j*expected->height/b->height); bool cla=b->getpos(i,j); if (exp==cla) correct++; else wrong++; if (exp) if (cla) ctextcorrect++; else cnottextincorrect++; else if (cla) ctextincorrect++; else cnottextcorrect++; } if (fileexists("test.dat")) { List<String> ls=readlinesfromfile("test.dat"); correct+=tofloat(ls.num(1)); wrong+=tofloat(ls.num(2)); ctextcorrect+=tofloat(ls.num(3)); cnottextcorrect+=tofloat(ls.num(4)); ctextincorrect+=tofloat(ls.num(5)); cnottextincorrect+=tofloat(ls.num(6)); total+=tofloat(ls.num(7)); } List<String> ls; ls.add(Sformat("%i correctly classified",correct)); ls.add(Sformat("%i incorrectly classified",wrong)); ls.add(Sformat("%i correctly classified as text",ctextcorrect)); ls.add(Sformat("%i correct classified as non-text",cnottextcorrect)); ls.add(Sformat("%i classified as text when not",ctextincorrect)); ls.add(Sformat("%i classified as non-text when was text",cnottextincorrect)); ls.add(Sformat("%i total",total)); ls.add(""); int totaltext=ctextcorrect+cnottextincorrect; int totalnottext=ctextincorrect+cnottextcorrect; ls.add(Sformat("That's %i text in total",totaltext)); ls.add(Sformat("and %i non-text in total",totalnottext)); ls.add(""); ls.add("And in percent:"); ls.add(Sformat("%f percent correctly classified",100.0*(float)correct/(float)total)); ls.add(Sformat("%f percent incorrectly classified",100.0*(float)wrong/(float)total)); ls.add(Sformat("%f percent of text correctly classified as text",100.0*(float)ctextcorrect/(float)totaltext)); ls.add(Sformat("%f percent of non-text correctly classified as non-text",100.0*(float)cnottextcorrect/(float)totalnottext)); writelinestofile(ls,"test.dat"); } }
/* 1.29 Check tests, if the cycle of this face is valid. */ bool Face::Check() { bool ret = true; if (v.size() == 0) // Accept an empty face return true; if (v.size() < 3) { DEBUG(3, "too few segments: "); ret = false; } for (unsigned int i = 0; i < v.size(); i++) { if (v[i].s == v[i].e) { DEBUG(3, "degenerated segment"); ret = false; } for (unsigned int j = 0; j < v.size(); j++) { if (i == j) continue; if (v[i].intersects(v[j])) { DEBUG(3, "ERROR: Segments intersect"); ret = false; } if (v[i].s == v[j].s) { DEBUG(3, "ERROR: Same startpoint"); ret = false; } if (v[i].e == v[j].e) { DEBUG(3, "ERROR: Same endpoint"); ret = false; } } } int nr = (int) v.size(); #ifdef DO_ANGLECHECK for (unsigned int i = 0; i < nr; i++) { Seg a = v[i]; Seg b = v[(i + 1) % nr]; long double aa = a.angle(); long double ab = b.angle(); long double ab2 = b.angle() + 180; if (ab2 > 360) ab2 -= 180; if ((aa == ab) || (aa == ab2)) { DEBUG(3, "Angle-Check failed!"); ret = false; } } #endif for (int i = 0; i < (nr - 1); i++) { if (!(v[i].e == v[i + 1].s)) { DEBUG(3, "ERROR: Region not contiguous"); ret = false; } } if (nr > 0) { if (!(v[nr - 1].e == v[0].s)) { DEBUG(3, "ERROR: Region not closed\n"); ret = false; } if (v[0].angle() > v[nr - 1].angle()) { DEBUG(3, "ERROR: Region not in counter-clockwise order"); ret = false; } } if (!ret) { DEBUG(2, "Invalid Region:" << endl << this->ToString()); } if (STRICT) assert(ret); else if (!ret) { v.clear(); convexhull.clear(); holes.clear(); } return ret; }
// Main init function double TopGauss::compute(MixtureGD & UBM,FeatureServer &fs,String & featureFilename,Config & config){ StatServer ss(config); MixtureGDStat &acc=ss.createAndStoreMixtureStat(UBM); unsigned long _mixsize=UBM.getDistribCount(); String labelSelectedFrames =config.getParam("labelSelectedFrames"); unsigned long begin=fs.getFirstFeatureIndexOfASource(featureFilename); fs.seekFeature(begin); SegServer segmentsServer; LabelServer labelServer; initializeClusters(featureFilename,segmentsServer,labelServer,config); // __android_log_print(ANDROID_LOG_DEBUG, "TopGauss::compute", " Feature file %s \n", featureFilename.c_str()); verifyClusterFile(segmentsServer,fs,config); unsigned long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames); SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame); acc.resetLLK(); double topD=config.getParam("topGauss").toDouble(); if (verbose) {if(topD<1.0) cout << "LLK %="<< topD << "% ";else cout << "Top-"<<topD<<" ";} // Class values _nt=totalFrame(selectedSegments); _nbg.setSize(_nt); _idx.setSize(0);_snsw.setSize(0); _snsl.setSize(0); _nbg.setAllValues(0); _idx.setAllValues(0);_snsw.setAllValues(0.0);_snsl.setAllValues(0.0); _nbgcnt=0; Seg *seg; // current selected segment selectedSegments.rewind(); unsigned long t=0; //cnt frames while((seg=selectedSegments.getSeg())!=NULL){ unsigned long begin=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName()); fs.seekFeature(begin); Feature f; for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){ fs.readFeature(f); double llk=acc.computeAndAccumulateLLK(f,1.0,DETERMINE_TOP_DISTRIBS); const LKVector &topV=ss.getTopDistribIndexVector(); double lk_tot=exp(llk); double val=0.0; if (topD<1.0) { for(unsigned long j=0;j<_mixsize;j++){ if (val > topD*lk_tot) break; val+=(topV[j].lk); _nbg[t]++; } } else _nbg[t]=(unsigned long)topD; _nbgcnt+=_nbg[t]; double snsw=1.0; double snsl=lk_tot; for(unsigned long j=0;j<_nbg[t];j++) { _idx.addValue(topV[j].idx); snsw -=UBM.weight(topV[j].idx); snsl -=topV[j].lk; } _snsw.addValue(snsw); if (snsl < EPS_LK) _snsl.addValue(EPS_LK); else _snsl.addValue(snsl); t++; } } if (t!=_nt) cout << "W: t("<<t<<") != _nt(" <<_nt<<")"<<endl; return acc.getMeanLLK(); }
//------------------------------------------------------------------------- int trainWorld(Config& config){ if (verbose) cout << "Begin world model training"<<endl; try{ // Reading the data, one or multiple separate streams unsigned long nbStream=0; // Number of Streams FeatureServer **fsTab=NULL; // Array of FeatureServer (address) - one by input stream SegServer **segServTab=NULL; // Array of segment server (address)- one by input stream SegCluster **segTab=NULL; // Array of selected segments cluster(address) - one by stream double *weightTab=NULL; // Array of weight of each stream. i.e influence of a stream on the final model String outputWorldFilename = config.getParam("outputWorldFilename"); // output worldmodel file filename bool fileInit=config.existsParam("inputWorldFilename"); // if a inputWorlFilename is given, init by file, else from scratch bool saveInitModel=true; if (config.existsParam("saveInitModel")) saveInitModel=config.getParam("saveInitModel").toBool(); String inputWorldFilename=""; if (fileInit) inputWorldFilename=config.getParam("inputWorldFilename"); // if file init, the initial model filename String labelSelectedFrames =config.getParam("labelSelectedFrames"); // label for selected frames TrainCfg trainCfg(config); // Get the training algo params // Reading the data if(config.existsParam("inputStreamList")){// We want to work on separated list XList tmp(config.getParam("inputStreamList"),config); // Each data set influence will be balanced during training XLine & listInputFilename=tmp.getAllElements(); // Read the list of (list) filenames in tmp -> listInputFilename nbStream=listInputFilename.getElementCount(); if (nbStream==0) throw Exception("TrainWorld error:no input stream" , __FILE__, __LINE__); reserveMem(fsTab,segServTab,segTab,weightTab,nbStream); for (unsigned i=0;i<nbStream;i++) featureStream(config,listInputFilename.getElement(i),fsTab[i],segServTab[i],segTab[i],labelSelectedFrames); if (config.existsParam("weightStreamList")){ // Read the weight of each stream, text file XList tmpW(config.getParam("weightStreamList"),config); XLine & listW=tmpW.getAllElements(); // Read the list of (list) filenames in tmp -> listInputFilename if (listW.getElementCount()!=nbStream) throw Exception("TrainWorld error: number of weigths differs than number of input streams" , __FILE__, __LINE__); for (unsigned i=0;i<nbStream;i++) weightTab[i]=listW.getElement(i).toDouble(); } } else{ // Only one input stream, no stream list nbStream=1; reserveMem(fsTab,segServTab,segTab,weightTab,nbStream); featureStream(config,config.getParam("inputFeatureFilename"),fsTab[0],segServTab[0],segTab[0],labelSelectedFrames); } unsigned long vectSize=fsTab[0]->getVectSize(); // size of the input vectors // Create stat server and mixture server MixtureServer ms(config); StatServer ss(config, ms); if (debug || verbose) cout << "Stream mode, nb Stream="<<nbStream<<endl; if (debug|| (verboseLevel>2)){ for (unsigned long i=0;i<nbStream;i++){ cout <<"Stream["<<i<<"]"<<endl; segTab[i]->rewind(); Seg *seg; // Reset to the first segment while((seg=segTab[i]->getSeg())!=NULL) // For each of the selected segments cout << "File["<<seg->sourceName()<<"] Segment begin["<< seg->begin()<<"] length["<<seg->length()<<"] index in the feature server["<<fsTab[i]->getFirstFeatureIndexOfASource(seg->sourceName())<<"]"<<endl; } } // Global mean and variance matrices initialisation (computed from dataa or set to 0,1) bool use01=false; if (config.existsParam("use01")) use01=config.getParam("use01").toBool(); if (verbose){ if (use01) cout<<"Use 0 mean, 1 cov "<<endl; else cout << "Compute global mean and cov"<<endl;} DoubleVector globalMean; DoubleVector globalCov; if (!use01){ FrameAccGD globalFrameAcc; unsigned long nbFrame=computeMeanCov(config,fsTab,segTab,nbStream,globalMean,globalCov); // Compute the global mean and covariance if (verboseLevel>1){ cout <<"global mean and cov of training data, number of frame= ["<<nbFrame<<"]"<<endl; for (unsigned i=0; i < vectSize; i++)cout << "mean[" << i << "=" << globalMean[i] << "]\tcov[" << globalCov[i] << "]" << endl; } } else initialize01(vectSize,globalMean,globalCov); MixtureGD &world=ms.createMixtureGD(); if (fileInit){ // Load or initialize the initial model if (verbose) cout << "Load initial world model ["<<inputWorldFilename<<"]" << endl; world=ms.loadMixtureGD(inputWorldFilename); // Load } else{ if (verbose) cout <<"World model init from scratch"<<endl; mixtureInit(ms,fsTab,segTab,weightTab,nbStream,world,globalCov,config,trainCfg); // Initialize if (saveInitModel) world.save(outputWorldFilename+"init", config); } MixtureGD *newWorld=&world; // TODO Verify and suppress... trainModelStream(config,ms,ss,fsTab,segTab,weightTab,nbStream,globalMean,globalCov,newWorld,trainCfg); if (verbose) cout << "Save world model ["<<outputWorldFilename<<"]" << endl; newWorld->save(outputWorldFilename, config); // Cleaning the memory freeMem(fsTab,segServTab,segTab,weightTab,nbStream); } catch (Exception& e){ cout << e.toString() << endl; } return 0; }
int saveApost(Config &config) { bool writeAllFeature=true; // Output a vector for all input vectors (selected and not selected vectors) - DEFAULT=on if (config.existsParam("writeAllFeatures")) writeAllFeature=config.getParam("writeAllFeatures").toBool(); // Define if all the feature (selected or not) should be written String modelname = config.getParam("inputModelFilename"); String inputFeatureFileName =config.getParam("inputFeatureFilename"); // input feature - could be a simple feature file or a list of filenames XLine inputFeatureFileNameList; // The (feature) input filename list if (inputFeatureFileName.endsWith(".lst")){ // If the file parameter is the name of a XList file XList inputFileNameXList(inputFeatureFileName,config); // Read the filename list file inputFeatureFileNameList=inputFileNameXList.getAllElements(); // And put the filename in a list if the file is a list of feature filenames } else { // It was a simple feature file and not a filename list inputFeatureFileNameList.addElement(inputFeatureFileName); // add the filename in the list } try{ // read UBM MixtureServer _ms(config); StatServer _ss(config); _ms.loadMixtureGD(config.getParam("inputWorldFilename")); MixtureGD & UBM=_ms.getMixtureGD((unsigned long) 0); MixtureGDStat &acc=_ss.createAndStoreMixtureStat(UBM); unsigned long _vsize=UBM.getVectSize(); unsigned long _mixsize=UBM.getDistribCount(); // Loop over the list of feature files String *file; String labelSelectedFrames; unsigned long codeSelectedFrame; while ((file=inputFeatureFileNameList.getElement())!= NULL){ String & featureFilename=(*file); FeatureServer fs(config,featureFilename); FeatureServer fs_out(config,featureFilename); SegServer segmentsServer; LabelServer labelServer; initializeClusters(featureFilename,segmentsServer,labelServer,config); verifyClusterFile(segmentsServer,fs,config); labelSelectedFrames=config.getParam("labelSelectedFrames"); codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames); SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame); // Compute Occupations and Statistics acc.resetOcc(); Seg *seg; selectedSegments.rewind(); String currentSource=""; while((seg=selectedSegments.getSeg())!=NULL){ unsigned long begin=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName()); // Idx of the first frame of the current file in the feature server if (currentSource!=seg->sourceName()) { currentSource=seg->sourceName(); if (verbose)cout << "Processing speaker["<<currentSource<<"]"<< endl; } fs.seekFeature(begin); Feature f; for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){ fs.readFeature(f); acc.computeAndAccumulateOcc(f); RealVector <double> aPost=acc.getOccVect(); Feature tmpF; for(unsigned long k=0;k<_mixsize;k++) { tmpF[k]=aPost[k]; } fs_out.addFeature(f); } } // Writing apost probabilities to file cout << "Writing to: " << featureFilename << endl; FeatureFileWriter w(featureFilename, config); // build a featurefile writer to output the features (real features) SegServer fakeSegServer; if (writeAllFeature) { // Output all the features- feature count id the same SegServer fakeSegServer; // Create a new fake segment server fakeSegServer.createCluster(0); // Create a new cluster SegCluster& fakeSeg=fakeSegServer.getCluster(0); // Get the cluster fakeSeg.add(fakeSegServer.createSeg(0,fs_out.getFeatureCount(),codeSelectedFrame, labelSelectedFrames,featureFilename)); // Add a segment with all the features outputFeatureFile(config,fs_out,fakeSeg,w); // output all the features - giving the same file length } else outputFeatureFile(config,fs_out,selectedSegments, w); // Output only the selected features - giving a shorter output } } catch (Exception& e){cout << e.toString().c_str() << endl;} return 0; }
/* 1.27 IntegrateHoles is used to integrate all holes of a face into the main cycle by creating "bridges". This usually yields an invalid face, it is exclusively used to triangulate a face. */ void Face::IntegrateHoles() { vector<Seg> allsegs = v; // First, get a list of all segments (inclusively all holes' segments) for (unsigned int i = 0; i < holes.size(); i++) { allsegs.insert(allsegs.end(), holes[i].v.begin(), holes[i].v.end()); } for (unsigned int h = 0; h < holes.size(); h++) { Face hole = holes[h]; // Integrate one hole after another if (hole.isEmpty()) continue; unsigned int i = 0, j; bool found = false; Pt s, e; Seg se; // Try to find a suitable bridge-segment by creating all segments // which connect the hole to the face and testing them for intersection // with any other segment while (!found && i < v.size()) { s = v[i].e; j = 0; while (!found && j < hole.v.size()) { e = hole.v[j].s; se = Seg(s, e); // A segment connecting the face to the hole bool intersects = false; for (unsigned int k = 0; k < allsegs.size(); k++) { if (se.intersects(allsegs[k])) { // We have found an intersection, so this segment is // not our bridge intersects = true; break; } } Seg tmp = v[(i+1)%v.size()]; if (leftOf(tmp.s, tmp.e, e) && !intersects) { // No intersection was found, so this is the bridge we use found = true; break; } j = j + 1; } if (!found) { i = i + 1; } } assert(found); // We should always be able to find a bridge // Now insert the bridge and the hole into the face-cycle vector<Seg> newsegs; // First copy the cycle from start to the begin of the bridge newsegs.insert(newsegs.end(), v.begin(), v.begin()+i+1); newsegs.push_back(Seg(s, e)); // Then add the bridge segment unsigned int n = hole.v.size(); for (unsigned int k = 0; k < n; k++) { // Now add the hole-segments clockwise Seg ns = hole.v[(n + j - k - 1) % n]; ns.ChangeDir(); // and change the orientation of each segment newsegs.push_back(ns); } newsegs.push_back(Seg(e, s)); // Bridge back to the original face // and add the rest of the original cycle newsegs.insert(newsegs.end(), v.begin() + i + 1, v.end()); v = newsegs; // For the next holes we have to test intersection with the newly // created bridge, too. allsegs.push_back(Seg(s, e)); } // All holes were integrated, so clear the list. holes.clear(); }