00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #if !defined(ALIZE_GeneralTools_cpp)
00056 #define ALIZE_GeneralTools_cpp
00057
00058 #include <iostream>
00059 #include <fstream>
00060 #include <cstdio>
00061 #include <cassert>
00062 #include <cmath>
00063 #include <liatools.h>
00064 #if defined(THREAD)
00065 #include <pthread.h>
00066 #endif
00067
00068
00069
00070 void accumulateStatLLK(StatServer &ss,FeatureServer &fs,MixtureStat &llkAcc, unsigned long idxBeginFrame,unsigned long nbFrames,
00071 Config &config)
00072 {
00073 fs.seekFeature(idxBeginFrame);
00074 for (unsigned long n=0;n<nbFrames;n++){
00075 Feature f;
00076 if(fs.readFeature(f)==false) cout<<"No more features"<<endl;
00077
00078 double toto=llkAcc.computeAndAccumulateLLK(f);
00079 if (debug) cout << "likelihood Frame["<<idxBeginFrame+n<<"]="<<toto<<endl;
00080 }
00081 }
00082
00083 void accumulateStatLLK(StatServer &ss,FeatureServer &fs,MixtureStat &llkAcc,Seg* seg,Config &config)
00084 {
00085 unsigned long begin=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName());
00086 accumulateStatLLK(ss,fs,llkAcc,begin,seg->length(),config);
00087 }
00088
00089 void accumulateStatLLK(StatServer &ss,FeatureServer &fs,MixtureStat &llkAcc,SegCluster &selectedSegments,Config &config)
00090 {
00091 Seg* seg;
00092 selectedSegments.rewind();
00093 while((seg=selectedSegments.getSeg())!=NULL)
00094 accumulateStatLLK(ss,fs,llkAcc,seg,config);
00095 }
00096
00097
00098
00099
00100
00101
00102
00103
00104 double accumulateStatEM(StatServer &ss,FeatureServer &fs,MixtureStat &emAcc,unsigned long idxBeginFrame,unsigned long nbFrames,Config &config){
00105 double llkAcc=0.0;
00106 fs.seekFeature(idxBeginFrame);
00107 for (unsigned long n=0;n<nbFrames;n++){
00108 Feature f;
00109 fs.readFeature(f);
00110 llkAcc+=log(emAcc.computeAndAccumulateEM(f));
00111
00112
00113 }
00114 return llkAcc;
00115 }
00116
00117 double accumulateStatEM(StatServer &ss,FeatureServer &fs,MixtureStat &emAcc,Seg* seg,Config &config){
00118 unsigned long begin=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName());
00119 return accumulateStatEM(ss,fs,emAcc,begin,seg->length(),config);
00120 }
00121
00122 double accumulateStatEMUnThreaded(StatServer &ss,FeatureServer &fs,MixtureStat &emAcc,SegCluster &selectedSegments,Config &config){
00123 double llkAcc=0.0;
00124 Seg* seg;
00125 selectedSegments.rewind();
00126 while((seg=selectedSegments.getSeg())!=NULL)
00127 llkAcc+=accumulateStatEM(ss,fs,emAcc,seg,config);
00128 return llkAcc;
00129 }
00130
00131
00132 double accumulateStatEM(StatServer &ss,FeatureServer &fs,MixtureStat &emAcc,SegCluster &selectedSegments,Config &config){
00133 double llkAcc=0.0;
00134 #if defined(THREAD)
00135 if (config.existsParam("numThread") && config.getParam("numThread").toLong() >0) llkAcc=accumulateStatEMThreaded(ss,fs,emAcc,selectedSegments,config);
00136 else llkAcc=accumulateStatEMUnThreaded(ss,fs,emAcc,selectedSegments,config);
00137 #else
00138 llkAcc= accumulateStatEMUnThreaded(ss,fs,emAcc,selectedSegments,config);
00139 #endif
00140 return llkAcc;
00141 }
00142
00143
00144 double accumulateStatEM(StatServer &ss,FeatureServer &fs,MixtureStat &emAcc,SegCluster &selectedSegments,double & weight, Config &config){
00145 double llkAcc=0.0;
00146 char cW[200];
00147 sprintf(cW,"%f",weight);
00148 String sW(cW);
00149 config.setParam("weightedEM","weight");
00150 if (verbose) cout << "Weighted EM with weight" << sW << endl;
00151 llkAcc= accumulateStatEM(ss,fs,emAcc,selectedSegments,config);
00152 return (weight*llkAcc);
00153 }
00154
00155
00156
00157 #if defined(THREAD)
00158 pthread_mutex_t mutexsum;
00159 bool stop=false;
00160
00161 struct EMthread_data{
00162 SegCluster *selectedSegments;
00163 MixtureStat *emAcc;
00164 FeatureServer *fs;
00165 Config *config;
00166 double *llkAcc;
00167 RefVector <Feature> *featThreadBuff;
00168 unsigned long nThread;
00169 };
00170
00171 static void *EMthread(void *threadarg) {
00172 struct EMthread_data *my_data;
00173 my_data = (struct EMthread_data *) threadarg;
00174 SegCluster &selectedSegments=*(my_data->selectedSegments);
00175 MixtureStat &emAcc=*(my_data->emAcc);
00176 FeatureServer &fs=*(my_data->fs);
00177 Config &config=*(my_data->config);
00178 unsigned long nT=my_data->nThread;
00179 double weight=1.0;
00180 if (config.existsParam("weightedEM")) weight=config.getParam("weightedEM").toDouble();
00181
00182 Seg* seg;
00183 unsigned long cnt=0;
00184 while(!stop) {
00185
00186 RefVector <Feature> featThreadBuff;
00187 pthread_mutex_lock (&mutexsum);
00188 if (stop) {pthread_mutex_unlock (&mutexsum);break;}
00189 cnt++;
00190 if ((seg=selectedSegments.getSeg())==NULL) {
00191 pthread_mutex_unlock (&mutexsum);
00192 if (verboseLevel >1) cout<<"Thread:"<<nT << " broke" << endl;
00193 stop=true;
00194 break;
00195 }
00196 unsigned long begin=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName());
00197 fs.seekFeature(begin);
00198 for (unsigned long j=0;j<seg->length();j++){
00199 featThreadBuff.addObject(*(new Feature),j);
00200 fs.readFeature(featThreadBuff[j]);
00201 }
00202 pthread_mutex_unlock (&mutexsum);
00203
00204
00205
00206 for (unsigned long j=0;j<seg->length();j++)
00207 (*(my_data->llkAcc))+=log(emAcc.computeAndAccumulateEM(featThreadBuff[j],weight));
00208 featThreadBuff.deleteAllObjects();
00209 }
00210 if (verboseLevel > 2) cout << "(AccumulateStatEM) Number of segments treated by thread ["<<nT<<"]="<<cnt<<endl;
00211 pthread_exit((void*) 0);
00212 }
00213
00214
00215 void splitSegCluster(SegCluster & selectedSegments,unsigned long nSplit,RefVector <SegCluster> &vSelected) {
00216 Seg* seg;
00217 unsigned long t=0;
00218 unsigned long offset=(totalFrame(selectedSegments))/nSplit;
00219 if (verbose) cout << "(AccumulateStatEM) Splitting cluster: Total["<<totalFrame(selectedSegments)<<"] "<<offset<<" frames/cluster"<<endl;
00220 unsigned long limit=offset-1;
00221 selectedSegments.rewind();
00222 unsigned long cnt=0;
00223 while((seg=selectedSegments.getSeg())!=NULL) {
00224 cnt+=seg->length();
00225 if (cnt>=limit) {
00226 if (verbose) cout<<"SegCluster["<<t<<"] full with "<<totalFrame(vSelected[t])<<" frames"<<endl;
00227 if (t!=nSplit-1) t++;cnt=0;
00228 }
00229 vSelected[t].addCopy(*seg);
00230 }
00231 }
00232
00233
00234 double accumulateStatEMThreaded(StatServer &ss,FeatureServer &fs,MixtureStat &emAcc,SegCluster &selectedSegments,Config &config){
00235 unsigned long NUM_THREADS=config.getParam("numThread").toLong();
00236 stop=false;
00237 if (verbose) cout << "(AccumulateStatEM) Threaded version of EM with "<<NUM_THREADS<<" threads"<<endl;
00238 double llkAcc=0.0;
00239 SegServer segServer;
00240
00241 RefVector <MixtureStat> vEmAcc;
00242 RealVector <double> vLlkAcc;
00243 vLlkAcc.setSize(NUM_THREADS);
00244 vLlkAcc.setAllValues(0.0);
00245 selectedSegments.rewind();
00246 for(unsigned long t=0; t<NUM_THREADS; t++){
00247
00248
00249 vEmAcc.addObject(ss.createAndStoreMixtureStat(emAcc.getMixture()),t);
00250 vEmAcc[t].resetEM();
00251 }
00252
00253 struct EMthread_data thread_data_array[NUM_THREADS];
00254 pthread_t threads[NUM_THREADS];
00255 pthread_attr_t attr;
00256 pthread_attr_init(&attr);
00257 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
00258 pthread_mutex_init(&mutexsum, NULL);
00259 int rc,status;
00260 for(unsigned long t=0; t<NUM_THREADS; t++){
00261 thread_data_array[t].selectedSegments=&selectedSegments;
00262 thread_data_array[t].emAcc=&vEmAcc[t];
00263 thread_data_array[t].fs=&fs;
00264 thread_data_array[t].config=&config;
00265 thread_data_array[t].llkAcc=&vLlkAcc[t];
00266 thread_data_array[t].nThread=t;
00267 if (verbose) cout<<"(AccumulateStatEM) Creating thread n["<< t<< "]"<<endl;
00268 rc = pthread_create(&threads[t], &attr, EMthread, (void *)&thread_data_array[t]);
00269 if (rc) throw Exception("ERROR; return code from pthread_create() is ",__FILE__,rc);
00270 }
00271 if (verbose) cout<<"(AccumulateStatEM) Computing on thread"<<endl;
00272 pthread_attr_destroy(&attr);
00273 for(unsigned long t=0; t<NUM_THREADS; t++) {
00274 rc = pthread_join(threads[t], (void **)&status);
00275 if (rc) throw Exception("ERROR; return code from pthread_join() is ",__FILE__,rc);
00276 if (verbose) cout <<"(AccumulateStatEM) Completed join with thread ["<<t<<"] status["<<status<<"]"<<endl;
00277 }
00278 if (verbose) cout <<"(AccumulateStatEM) Fuse EM Accs "<<endl;
00279 unsigned long total=0;
00280 for(unsigned long t=0; t<NUM_THREADS; t++) {
00281 if (verbose) cout << "Number of frames treated by thread ["<<t<<"]="<<vEmAcc[t].getEMFeatureCount()<<endl;
00282 total+=vEmAcc[t].getEMFeatureCount();
00283 emAcc.addAccEM(vEmAcc[t]);
00284 ss.deleteMixtureStat(vEmAcc[t]);
00285 llkAcc+=vLlkAcc[t];
00286 }
00287 cout << "Total Number of frames in threads: "<<total<<endl;
00288 pthread_mutex_destroy(&mutexsum);
00289 return llkAcc;
00290 }
00291 #endif
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335 void accumulateStatLLK(StatServer & ss, FeatureServer & fs,
00336 MixtureStat & llkAcc, unsigned long idxBeginFrame, unsigned long nbFrames,
00337 double &weight, Config & config)
00338 {
00339 fs.seekFeature(idxBeginFrame);
00340 for (unsigned long n = 0; n < nbFrames; n++)
00341 {
00342 Feature f;
00343 if (fs.readFeature(f) == false)
00344 cout << "No more features" << endl;
00345
00346 double toto = llkAcc.computeAndAccumulateLLK(f, weight);
00347 if (debug)
00348 cout << "likelihood Frame[" << idxBeginFrame +
00349 n << "]=" << toto << endl;
00350 }
00351 }
00352
00353
00354 void accumulateStatLLK(StatServer & ss, FeatureServer & fs,
00355 MixtureStat & llkAcc, Seg * seg, double &weight, Config & config)
00356 {
00357 unsigned long begin = seg->begin() + fs.getFirstFeatureIndexOfASource(seg->sourceName());
00358 accumulateStatLLK(ss, fs, llkAcc, begin, seg->length(), weight, config);
00359 }
00360
00361
00362 void accumulateStatLLK(StatServer & ss, FeatureServer & fs,
00363 MixtureStat & llkAcc, SegCluster & selectedSegments, double &weight,
00364 Config & config)
00365 {
00366 Seg *seg;
00367 selectedSegments.rewind();
00368 while ((seg = selectedSegments.getSeg()) != NULL)
00369 accumulateStatLLK(ss, fs, llkAcc, seg, weight, config);
00370 }
00371
00372
00373
00374
00375
00376
00377
00378 void accumulateStatFrame(FrameAcc & frameAcc,FeatureServer &fs,
00379 unsigned long idxBeginFrame,unsigned long nbFrames,Config &config)
00380 {
00381 fs.seekFeature(idxBeginFrame);
00382 Feature f;
00383 for (unsigned long n=0;n<nbFrames;n++){
00384 fs.readFeature(f);
00385 frameAcc.accumulate(f);
00386 }
00387 }
00388
00389 void accumulateStatFrame(FrameAcc & frameAcc,FeatureServer &fs,Seg* seg,Config &config)
00390 {
00391 unsigned long begin=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName());
00392 accumulateStatFrame(frameAcc,fs,begin,seg->length(),config);
00393 }
00394
00395 void accumulateStatFrame(FrameAcc &frameAcc,FeatureServer &fs,SegCluster &selectedSegments,Config &config)
00396 {
00397 Seg* seg;
00398 selectedSegments.rewind();
00399 while((seg=selectedSegments.getSeg())!=NULL)
00400 accumulateStatFrame(frameAcc,fs,seg,config);
00401 }
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415 double areaHisto(const Histo & histo,unsigned long bin)
00416 {
00417 return histo.count(bin)*(histo.higherBound(bin)-histo.lowerBound(bin));
00418 }
00419 double areaHisto(const Histo & histo,unsigned long bin, double nonObserved)
00420 {
00421 return areaHisto(histo,bin)*(1-nonObserved) ;
00422 }
00423 double linearInterpolation(double val,double lower,double higher){
00424 const double EPS=0.000000000000000000001;
00425 double interval=higher-lower;
00426 if (interval<EPS) return 1;
00427 return (val-lower)/interval;
00428 }
00429 void freezeHistoTab(Histo* &histoT)
00430 {
00431 delete []histoT;
00432 }
00433 void initHistoTab(Histo* &histoT,unsigned long size, unsigned long nbBins)
00434 {
00435 Histo tmp(nbBins);
00436 histoT=new Histo[size];
00437 for (unsigned long i=0;i<size;i++) histoT[i]=tmp;
00438 }
00439 void computeHistoTab(Histo* histoT,unsigned long size)
00440 {
00441 for (unsigned long i=0;i<size;i++) histoT[i].computeHisto();
00442 }
00443
00444 void accumulateHistoFrame(Histo *histoT,FeatureServer &fs,
00445 unsigned long idxBeginFrame,unsigned long nbFrames,Config &config)
00446 {
00447 fs.seekFeature(idxBeginFrame);
00448 Feature f;
00449 for (unsigned long n=0;n<nbFrames;n++){
00450 fs.readFeature(f);
00451 for (unsigned long c=0;c<fs.getVectSize();c++)
00452 histoT[c].accumulateValue(f[c]);
00453 }
00454 }
00455
00456 void accumulateHistoFrame(Histo *histoT,FeatureServer &fs,Seg* seg,Config &config)
00457 {
00458 unsigned long begin=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName());
00459 accumulateHistoFrame(histoT,fs,begin,seg->length(),config);
00460 }
00461
00462 void accumulateHistoFrame(Histo *histoT,FeatureServer &fs,SegCluster &selectedSegments,Config &config)
00463 {
00464 Seg* seg;
00465 selectedSegments.rewind();
00466 while((seg=selectedSegments.getSeg())!=NULL)
00467 accumulateHistoFrame(histoT,fs,seg,config);
00468 }
00469
00470
00471 #endif //!defined(ALIZE_AccumulateStat_cpp)
00472
00473