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_ComputeTest_cpp)
00056 #define ALIZE_ComputeTest_cpp
00057
00058 #include <iostream>
00059 #include <fstream>
00060 #include <cassert>
00061 #include <cmath>
00062 #include <liatools.h>
00063 #include "ComputeTest.h"
00064 #include "AccumulateJFAStat.h"
00065 #include <sys/stat.h>
00066
00067 using namespace alize;
00068 using namespace std;
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 int fexist(char *filename ) {
00080 struct stat buffer;
00081 if ( stat(filename, &buffer ) ) return 1 ;
00082 return 0 ;
00083 }
00084
00085 class WindowLLR{
00086 bool _set;
00087 unsigned long _size;
00088 unsigned long _dec;
00089 unsigned long _nClient;
00090 Matrix <double> *_llrM;
00091 DoubleVector *_accLlrA;
00092 ULongVector *_idxA;
00093 unsigned long _bIdx;
00094 unsigned long _count;
00095 void _initMem();
00096 void _freeMem();
00097
00098 public:
00099 WindowLLR(Config &config);
00100 ~WindowLLR();
00101 bool isSet(){return _set;}
00102 void setNbClient(unsigned long nClient){_nClient=nClient;_initMem();}
00103 unsigned long getIdxBegin(){return (*_idxA)[_bIdx];}
00104 unsigned long getIdxEnd(){return (*_idxA)[(_bIdx+_count-1)%_size];}
00105 void showConfig();
00106 void accLLR(unsigned long clientIdx,double llr);
00107 double getLLR(unsigned long clientIdx);
00108 bool isEnd();
00109 unsigned long wCount();
00110 void dec(unsigned long idxFrame);
00111 };
00112
00113 void WindowLLR::_initMem(){
00114 _freeMem();
00115 _idxA= new ULongVector(_size,_size);
00116 _accLlrA = new DoubleVector(_nClient,_nClient);
00117 _llrM= new Matrix <double>(_size,_nClient);
00118 for (unsigned long idxC=0;idxC<_nClient;idxC++){
00119 for (unsigned long idxF=0;idxF<_size;idxF++)
00120 (*_llrM)(idxF,idxC)=0;
00121 (*_accLlrA)[idxC]=0;
00122 }
00123
00124 _bIdx=0;
00125 _count=0;
00126 }
00127 void WindowLLR::_freeMem(){
00128 if (_llrM) {
00129 delete _llrM;
00130 delete _accLlrA;
00131 delete _idxA;
00132 }
00133 _llrM=NULL;
00134 }
00135 WindowLLR::WindowLLR(Config &config){
00136 _set=false;
00137 _size=0;
00138 _dec=0;
00139 _bIdx=0;
00140 _count=0;
00141 _nClient=0;
00142 _llrM=NULL;
00143 if (config.existsParam("windowLLR")) _set=config.getParam("windowLLR").toBool();
00144 if (_set){
00145 if (config.existsParam("windowLLRSize")) _size=config.getParam("windowLLRSize").toLong();
00146 else _size=30;
00147 if (config.existsParam("windowLLRDec")) _dec=config.getParam("windowLLRDec").toLong();
00148 else _dec=_size;
00149 _nClient=1;
00150 _initMem();
00151 }
00152 }
00153 WindowLLR::~WindowLLR(){
00154 _freeMem();}
00155 void WindowLLR::showConfig(){
00156 if (_set) cout<<"windowLLR mode size["<<_size<<"] dec["<<_dec<<"]"<<endl;
00157 }
00158 unsigned long WindowLLR::wCount(){
00159 return (_count);
00160 }
00161 void WindowLLR::dec(unsigned long idxFrame){
00162 if (_count<_size){
00163 _count++;
00164 unsigned long eIdx=(_bIdx+_count-1)%_size;
00165 (*_idxA)[eIdx]=idxFrame;
00166 }
00167 else{
00168 for (unsigned long wIdx=0;wIdx<_dec;wIdx++){
00169
00170 for (unsigned long cIdx=0;cIdx<_nClient;cIdx++)
00171 (*_accLlrA)[cIdx]-=(*_llrM)(_bIdx,cIdx);
00172 _bIdx=(_bIdx+1)%_size;
00173 } _count-=(_dec-1);
00174 (*_idxA)[(_bIdx+_count-1)%_size]=idxFrame;
00175 }
00176 }
00177 void WindowLLR::accLLR(unsigned long clientIdx,double llr){
00178 (*_llrM)((_bIdx+_count-1)%_size,clientIdx)=llr;
00179 (*_accLlrA)[clientIdx]+=llr;
00180 }
00181 double WindowLLR::getLLR(unsigned long clientIdx){
00182 return (*_accLlrA)[clientIdx]/(double)_size;
00183 }
00184 bool WindowLLR::isEnd(){
00185 return (wCount()==_size);
00186 }
00187
00188
00189
00190 int ComputeTest(Config& config)
00191 {
00192 String inputNDXFileName = config.getParam("ndxFilename");
00193 String inputWorldFilename = config.getParam("inputWorldFilename");
00194 String outputNISTFileName = config.getParam("outputFilename");
00195 String labelSelectedFrames =config.getParam("labelSelectedFrames");
00196
00197 bool segmentalMode=false;
00198 if (config.existsParam("segmentLLR")) segmentalMode=config.getParam("segmentLLR").toBool();
00199 String gender=config.getParam("gender");
00200 WindowLLR windowLLR(config);
00201 windowLLR.showConfig();
00202 real_t decisionThreshold;
00203 if (config.existsParam("decisionThreshold"))
00204 decisionThreshold=config.getParam("decisionthreshold").toDouble();
00205 else decisionThreshold=0;
00206 unsigned long maxClientLine=CST_MAX_CLIENT_LINE;
00207 if (config.existsParam("maxTargetLine")) maxClientLine=config.getParam("maxTargetLine").toLong();
00208 double frameLength = config.getParam("frameLength").toDouble();
00209 unsigned long nbMaxMixtureInMemory=0;
00210 if (config.existsParam("nbMaxMixtureInMemory")) nbMaxMixtureInMemory=config.getParam("nbMaxMixtureInMemory").toLong();
00211 unsigned long worldDecime=1;
00212 if (config.existsParam("worldDecime"))
00213 worldDecime=config.getParam("worldDecime").toLong();
00214 if (verbose){
00215 cout << "Compute Test, Decime mode["<<worldDecime<<"] ";
00216 if (segmentalMode) cout << " Segmental Mode - 1 LLr by segment"<<endl;
00217 else cout << "File Mode- 1 LLR by file" <<endl;
00218 }
00219 try{
00220 XList ndx(inputNDXFileName,config);
00221 XLine *linep;
00222 ndx.getLine(0);
00223 MixtureServer ms(config);
00224 StatServer ss(config, ms);
00225 MixtureGD& world = ms.loadMixtureGD(inputWorldFilename);
00226 TabClientLine tabClientLine(ms,config,maxClientLine);
00227 ofstream outNist(outputNISTFileName.c_str(),ios::out | ios::trunc);
00228 while ((linep=ndx.getLine()) != NULL){
00229 String &featureFileName=linep->getElement(0);
00230 FeatureServer fs(config,featureFileName);
00231 SegServer segmentsServer;
00232 LabelServer labelServer;
00233 initializeClusters(featureFileName,segmentsServer,labelServer,config);
00234 verifyClusterFile(segmentsServer,fs,config);
00235 long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);
00236 if (codeSelectedFrame==-1)
00237 cout << "ATTENTION, TEST FILE ["<<featureFileName<<"] is empty"<<endl;
00238 else{
00239 SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame);
00240 TabClientLine tabClientLine(ms,config);
00241 tabClientLine.loadLine(linep);
00242 if (windowLLR.isSet()) windowLLR.setNbClient(tabClientLine.nbClientLine());
00243 if (verbose)cout << "test seg["<<featureFileName<<"]"<< endl;
00244 MixtureGDStat &worldAcc=ss.createAndStoreMixtureGDStat(world);
00245 worldAcc.resetLLK();
00246 RefVector <MixtureGDStat> clientAcc(tabClientLine.nbClientLine());
00247 for (unsigned int i=0; i<tabClientLine.nbClientLine();i++) {
00248 clientAcc.addObject(ss.createAndStoreMixtureGDStat(tabClientLine.getClientModel(i)));
00249 clientAcc[i].resetLLK();
00250 }
00251 Seg* seg;
00252 selectedSegments.rewind();
00253 while((seg=selectedSegments.getSeg())!=NULL){
00254 unsigned long idxBeginFrame=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName());
00255 fs.seekFeature(idxBeginFrame);
00256 Feature f;
00257 for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){
00258 double llkw=0;
00259 double llkc=0;
00260 fs.readFeature(f);
00261 if ((idxFrame%worldDecime)==0)
00262 llkw=worldAcc.computeAndAccumulateLLK(f,1.0,DETERMINE_TOP_DISTRIBS);
00263 else worldAcc.computeAndAccumulateLLK(f,1.0,USE_TOP_DISTRIBS);
00264 if (windowLLR.isSet()) windowLLR.dec(idxBeginFrame+idxFrame);
00265 for (unsigned long i=0;i<tabClientLine.nbClientLine();i++){
00266 llkc=clientAcc[i].computeAndAccumulateLLK(f,1.0,USE_TOP_DISTRIBS);
00267 if (windowLLR.isSet()) windowLLR.accLLR(i,llkc-llkw);
00268 }
00269 if (windowLLR.isSet() && windowLLR.isEnd()){
00270 for (unsigned long i=0;i < tabClientLine.nbClientLine();i++){
00271 double LLRClient=windowLLR.getLLR(i);
00272 char decision=setDecision(LLRClient,decisionThreshold);
00273 outputResultLine(LLRClient, tabClientLine.getClientName(i),
00274 featureFileName, frameIdxToTime(windowLLR.getIdxBegin(),frameLength),frameIdxToTime(windowLLR.getIdxEnd(),frameLength),
00275 gender ,decision,outNist);
00276 if (verboseLevel>1) outputResultLine(LLRClient, tabClientLine.getClientName(i),
00277 featureFileName, frameIdxToTime(windowLLR.getIdxBegin(),frameLength),frameIdxToTime(windowLLR.getIdxEnd(),frameLength),
00278 gender ,decision,cout);
00279 }
00280 }
00281 }
00282 if (segmentalMode){
00283 double LLKWorld=worldAcc.getMeanLLK();
00284 for (unsigned int i=0;i < tabClientLine.nbClientLine();i++){
00285 double LLKClient=clientAcc[i].getMeanLLK();
00286 double LLRClient=LLKClient-LLKWorld;
00287 char decision=setDecision(LLRClient,decisionThreshold);
00288 outputResultLine(LLRClient, tabClientLine.getClientName(i),
00289 featureFileName, frameIdxToTime(idxBeginFrame,frameLength),frameIdxToTime(idxBeginFrame+seg->length(),frameLength),
00290 gender ,decision,outNist);
00291 if (verboseLevel>1) outputResultLine(LLRClient, tabClientLine.getClientName(i),
00292 featureFileName, frameIdxToTime(idxBeginFrame,frameLength),frameIdxToTime(idxBeginFrame+seg->length(),frameLength),
00293 gender,decision ,cout);
00294 clientAcc[i].resetLLK();
00295 }
00296 worldAcc.resetLLK();
00297 }
00298 }
00299 if (segmentalMode==false){
00300 double LLKWorld=worldAcc.getMeanLLK();
00301 for (unsigned int i=0;i < tabClientLine.nbClientLine();i++){
00302 double LLKClient=clientAcc[i].getMeanLLK();
00303 double LLRClient=LLKClient-LLKWorld;
00304 char decision=setDecision(LLRClient,decisionThreshold);
00305 outputResultLine(LLRClient, tabClientLine.getClientName(i),featureFileName ,gender ,decision,outNist);
00306 if (verboseLevel>1) {
00307 outputResultLine(LLRClient, tabClientLine.getClientName(i),featureFileName ,gender ,decision,cout);
00308 if (verboseLevel>2) cout<<"** Client LLK:"<<LLKClient<<" UBM LLk:"<<LLKWorld<<endl;
00309 }
00310 }
00311 }
00312 clientAcc.deleteAllObjects();
00313 }
00314 if ((nbMaxMixtureInMemory>0)&&(ms.getMixtureCount()>nbMaxMixtureInMemory)){
00315 if (verbose) cout <<"Cleaning the mixture server from 1 to "<<ms.getMixtureCount()<<endl;
00316 ms.deleteMixtures(1,ms.getMixtureCount());
00317 ms.deleteUnusedDistribs();
00318 }
00319 }
00320 outNist.close();
00321 }
00322 catch (Exception& e){
00323 cout << e.toString().c_str() << endl;
00324 }
00325 return 0;
00326 }
00327
00328
00329
00330
00331 int ComputeTestDotProduct(Config& config){
00332
00333 String inputNDXFileName = config.getParam("ndxFilename");
00334 String inputWorldFilename = config.getParam("inputWorldFilename");
00335 String outputNISTFileName = config.getParam("outputFilename");
00336 String labelSelectedFrames =config.getParam("labelSelectedFrames");
00337
00338
00339 String gender=config.getParam("gender");
00340 real_t decisionThreshold;
00341 if (config.existsParam("decisionThreshold"))
00342 decisionThreshold=config.getParam("decisionthreshold").toDouble();
00343 else decisionThreshold=0;
00344 unsigned long maxClientLine=CST_MAX_CLIENT_LINE;
00345 if (config.existsParam("maxTargetLine")) maxClientLine=config.getParam("maxTargetLine").toLong();
00346
00347 if (config.getParam_debug())debug=true; else debug=false;
00348
00349 try{
00350 XList ndx(inputNDXFileName,config);
00351 XLine *linep;
00352 ndx.getLine(0);
00353 MixtureServer ms(config);
00354 StatServer ss(config, ms);
00355 MixtureGD& world = ms.loadMixtureGD(inputWorldFilename);
00356 ofstream outNist(outputNISTFileName.c_str(),ios::out | ios::trunc);
00357
00358
00359 Matrix<double> U, V;
00360 DoubleVector D;
00361
00362
00363 if(config.existsParam("eigenChannelMatrix")){
00364 String uName = config.getParam("matrixFilesPath") + config.getParam("eigenChannelMatrix") + config.getParam("loadMatrixFilesExtension");
00365 U.load (uName, config);
00366 }
00367 else{
00368 unsigned long sS = world.getVectSize() * world.getDistribCount();
00369 U.setDimensions(1,sS);
00370 U.setAllValues(0.0);
00371 }
00372
00373
00374 if(config.existsParam("eigenVoiceMatrix")){
00375 String vName = config.getParam("matrixFilesPath") + config.getParam("eigenVoiceMatrix") + config.getParam("loadMatrixFilesExtension");
00376 V.load (vName, config);
00377 }
00378 else{
00379 unsigned long sS = world.getVectSize() * world.getDistribCount();
00380 V.setDimensions(1,sS);
00381 V.setAllValues(0.0);
00382 }
00383
00384
00385 if(config.existsParam("DMatrix")){
00386 String dName = config.getParam("matrixFilesPath") + config.getParam("DMatrix") + config.getParam("loadMatrixFilesExtension");
00387 Matrix<double> tmpD(dName, config);
00388
00389 if( (tmpD.rows() != 1) || ( tmpD.cols() != world.getVectSize()*world.getDistribCount() ) ){
00390 throw Exception("Incorrect dimension of D Matrix",__FILE__,__LINE__);
00391 }
00392 else{
00393 D.setSize(world.getVectSize()*world.getDistribCount());
00394 D.setAllValues(0.0);
00395 for(unsigned long i=0; i<world.getVectSize()*world.getDistribCount(); i++){
00396 D[i] = tmpD(0,i);
00397 }
00398 }
00399 }
00400 else{
00401 unsigned long sS = world.getVectSize() * world.getDistribCount();
00402 D.setSize(sS,sS);
00403 D.setAllValues(0.0);
00404 }
00405
00406
00407 while ((linep=ndx.getLine()) != NULL){
00408
00409 String &featureFileName=linep->getElement(0);
00410 XLine featureFileListp;
00411 featureFileListp.addElement(linep->getElement(0));
00412 XList ndx; ndx.addLine() = featureFileListp;
00413 JFAAcc jfaAcc(ndx,config);
00414
00415
00416 jfaAcc.loadEV(V, config); jfaAcc.loadEC(U, config); jfaAcc.loadD(D);
00417
00418
00419 jfaAcc.computeAndAccumulateJFAStat(config);
00420
00421
00422 jfaAcc.estimateUEUT(config);
00423
00424
00425 jfaAcc.estimateAndInverseL_EC(config);
00426
00427
00428 jfaAcc.substractMplusVYplusDZ(config);
00429
00430
00431 jfaAcc.estimateX(config);
00432
00433
00434 jfaAcc.substractMplusUX();
00435
00436 double sumN = 0.0;
00437 double *n = jfaAcc.getN().getArray();
00438 for(unsigned long i=0; i<jfaAcc.getNDistrib(); i++){
00439 sumN += n[i];
00440 }
00441
00442 double *f_x = jfaAcc.getF_X().getArray();
00443 for(unsigned long i=0; i<jfaAcc.getSvSize();i++){
00444 f_x[i] /= sumN;
00445 }
00446
00447
00448 for(unsigned long i = 1; i< linep->getElementCount(); i++){
00449 String &fileName=linep->getElement(i);
00450
00451
00452 String clientSvFile = config.getParam("vectorFilesPath") + "/" + fileName + config.getParam("vectorFilesExtension");
00453 Matrix<double> clientSV( clientSvFile , config);
00454
00455
00456 double score = 0.0;
00457 double *clientsv = clientSV.getArray();
00458 for(unsigned long j=0; j<jfaAcc.getSvSize(); j++){
00459 score += clientsv[j] * f_x[j];
00460 }
00461
00462
00463 char decision=setDecision(score,decisionThreshold);
00464 outputResultLine(score, fileName,featureFileName ,gender ,decision,outNist);
00465 }
00466 }
00467 outNist.close();
00468 }
00469 catch (Exception& e){
00470 cout << e.toString().c_str() << endl;
00471 }
00472 return 0;
00473 }
00474
00475
00476
00477
00478
00479 int ComputeTestJFA(Config& config){
00480
00481 String inputNDXFileName = config.getParam("ndxFilename");
00482 String inputWorldFilename = config.getParam("inputWorldFilename");
00483 String outputNISTFileName = config.getParam("outputFilename");
00484 String labelSelectedFrames =config.getParam("labelSelectedFrames");
00485
00486
00487 String gender=config.getParam("gender");
00488 real_t decisionThreshold;
00489 if (config.existsParam("decisionThreshold"))
00490 decisionThreshold=config.getParam("decisionthreshold").toDouble();
00491 else decisionThreshold=0;
00492 unsigned long maxClientLine=CST_MAX_CLIENT_LINE;
00493 if (config.existsParam("maxTargetLine")) maxClientLine=config.getParam("maxTargetLine").toLong();
00494 unsigned long nbMaxMixtureInMemory=1;
00495 if (config.existsParam("nbMaxMixtureInMemory")) nbMaxMixtureInMemory=config.getParam("nbMaxMixtureInMemory").toLong();
00496 unsigned long worldDecime=1;
00497 if (config.existsParam("worldDecime"))
00498 worldDecime=config.getParam("worldDecime").toLong();
00499 if (config.getParam_debug())debug=true; else debug=false;
00500 if (verbose){
00501 cout << "Compute Test, Decime mode["<<worldDecime<<"] ";
00502 cout << "File Mode- 1 LLR by file" <<endl;
00503 }
00504
00505 try{
00506
00507 XList ndx(inputNDXFileName,config);
00508 XLine *linep;
00509 ndx.getLine(0);
00510 MixtureServer ms(config);
00511 StatServer ss(config, ms);
00512 MixtureGD& world = ms.loadMixtureGD(inputWorldFilename);
00513 ofstream outNist(outputNISTFileName.c_str(),ios::out | ios::trunc);
00514
00515
00516 Matrix<double> U, V;
00517 DoubleVector D;
00518
00519
00520 if(config.existsParam("eigenChannelMatrix")){
00521 String uName = config.getParam("matrixFilesPath") + config.getParam("eigenChannelMatrix") + config.getParam("loadMatrixFilesExtension");
00522 U.load (uName, config);
00523 }
00524 else{
00525 U.setDimensions(1,world.getVectSize());
00526 U.setAllValues(0.0);
00527 }
00528
00529
00530 if(config.existsParam("eigenVoiceMatrix")){
00531 String vName = config.getParam("matrixFilesPath") + config.getParam("eigenVoiceMatrix") + config.getParam("loadMatrixFilesExtension");
00532 V.load (vName, config);
00533 }
00534 else{
00535 V.setDimensions(1,world.getVectSize());
00536 V.setAllValues(0.0);
00537 }
00538
00539
00540 if(config.existsParam("DMatrix")){
00541 String dName = config.getParam("matrixFilesPath") + config.getParam("DMatrix") + config.getParam("loadMatrixFilesExtension");
00542 Matrix<double> tmpD(dName, config);
00543
00544 if( (tmpD.rows() != 1) || ( tmpD.cols() != world.getVectSize()*world.getDistribCount() ) ){
00545 throw Exception("Incorrect dimension of D Matrix",__FILE__,__LINE__);
00546 }
00547 else{
00548 D.setSize(world.getVectSize()*world.getDistribCount());
00549 D.setAllValues(0.0);
00550 for(unsigned long i=0; i<world.getVectSize()*world.getDistribCount(); i++){
00551 D[i] = tmpD(0,i);
00552 }
00553 }
00554 }
00555 else{
00556 D.setSize(world.getVectSize(),world.getVectSize()*world.getDistribCount());
00557 D.setAllValues(0.0);
00558 }
00559
00560
00561 while ((linep=ndx.getLine()) != NULL){
00562
00563 String &featureFileName=linep->getElement(0);
00564 XLine featureFileListp;
00565 featureFileListp.addElement(linep->getElement(0));
00566 XList ndx; ndx.addLine() = featureFileListp;
00567 JFAAcc jfaAcc(ndx,config);
00568
00569
00570 MixtureGD & tmpWorld=ms.duplicateMixture (ms.getMixtureGD(0), DUPL_DISTRIB);
00571
00573 jfaAcc.loadEV(V, config); jfaAcc.loadEC(U, config); jfaAcc.loadD(D);
00574
00576 jfaAcc.computeAndAccumulateJFAStat(config);
00577
00578
00579
00580
00581
00583 jfaAcc.estimateUEUT(config);
00584
00586 jfaAcc.estimateAndInverseL_EC(config);
00587
00589 jfaAcc.substractMplusVYplusDZ(config);
00590
00592 jfaAcc.estimateX(config);
00593
00595 FeatureServer fs(config,featureFileName);
00596 jfaAcc.substractUXfromFeatures(fs,config);
00597
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00613
00614 if (verbose)cout << "---> LogLikelihood Ratio Computation of test segment ["<<featureFileName<<"]"<< endl;
00615
00616 TabClientLine tabClientLine(ms,config);
00617 tabClientLine.loadLine(linep);
00618
00619
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00633
00634 SegServer segmentsServer;
00635 LabelServer labelServer;
00636 initializeClusters(featureFileName,segmentsServer,labelServer,config);
00637 verifyClusterFile(segmentsServer,fs,config);
00638 long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);
00639 SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame);
00640
00641 MixtureGDStat &worldAcc=ss.createAndStoreMixtureGDStat(world);
00642
00643
00644
00645 worldAcc.resetLLK();
00646
00647
00648 RefVector <MixtureGDStat> clientAcc(tabClientLine.nbClientLine());
00649 for (unsigned int i=0; i<tabClientLine.nbClientLine();i++) {
00650 clientAcc.addObject(ss.createAndStoreMixtureGDStat(tabClientLine.getClientModel(i)));
00651 clientAcc[i].resetLLK();
00652 }
00653
00654 Seg* seg;
00655 selectedSegments.rewind();
00656 while((seg=selectedSegments.getSeg())!=NULL){
00657 unsigned long idxBeginFrame=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName());
00658 fs.seekFeature(idxBeginFrame);
00659 Feature f;
00660 for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){
00661 double llkw;
00662 double llkc;
00663 fs.readFeature(f);
00664 if ((idxFrame%worldDecime)==0)
00665 llkw=worldAcc.computeAndAccumulateLLK(f,1.0,DETERMINE_TOP_DISTRIBS);
00666
00667 else{
00668 worldAcc.computeAndAccumulateLLK(f,1.0,USE_TOP_DISTRIBS);
00669
00670 }
00671 for (unsigned long i=0;i<tabClientLine.nbClientLine();i++){
00672 llkc=clientAcc[i].computeAndAccumulateLLK(f,1.0,USE_TOP_DISTRIBS);
00673 }
00674 }
00675 }
00676
00677 double LLKWorld=worldAcc.getMeanLLK();
00678
00679 for (unsigned int i=0;i < tabClientLine.nbClientLine();i++){
00680 double LLKClient=clientAcc[i].getMeanLLK();
00681 double LLRClient=LLKClient-LLKWorld;
00682 char decision=setDecision(LLRClient,decisionThreshold);
00683 outputResultLine(LLRClient, tabClientLine.getClientName(i),featureFileName ,gender ,decision,outNist);
00684 if (verboseLevel>0) {
00685 outputResultLine(LLRClient, tabClientLine.getClientName(i),featureFileName ,gender ,decision,cout);
00686 if (verboseLevel>0) cout<<"** Client LLK:"<<LLKClient<<" UBM LLk:"<<LLKWorld<<endl;
00687 }
00688 }
00689 clientAcc.deleteAllObjects();
00690
00691 if (verboseLevel > 2) cout << "To be deleted" << ms.toString() << endl;
00692 if ((nbMaxMixtureInMemory>0)&&(ms.getMixtureCount()>nbMaxMixtureInMemory)){
00693 if (verboseLevel >1) cout <<"Cleaning the mixture server from 1 to "<<ms.getMixtureCount()<<endl;
00694 ms.deleteMixtures(1,ms.getMixtureCount());
00695 ms.deleteUnusedDistribs();
00696 if (verboseLevel > 2) cout << "deleted" << ms.toString() << endl;
00697 }
00698
00699
00700
00701
00702 }
00703 outNist.close();
00704
00705 }
00706 catch (Exception& e){
00707 cout << e.toString().c_str() << endl;
00708 }
00709 return 0;
00710 }
00711
00712
00713
00714
00715 int ComputeTestLFA(Config& config){
00716
00717 String inputNDXFileName = config.getParam("ndxFilename");
00718 String inputWorldFilename = config.getParam("inputWorldFilename");
00719 String outputNISTFileName = config.getParam("outputFilename");
00720 String labelSelectedFrames =config.getParam("labelSelectedFrames");
00721
00722
00723 String gender=config.getParam("gender");
00724 real_t decisionThreshold;
00725 if (config.existsParam("decisionThreshold"))
00726 decisionThreshold=config.getParam("decisionthreshold").toDouble();
00727 else decisionThreshold=0;
00728 unsigned long maxClientLine=CST_MAX_CLIENT_LINE;
00729 if (config.existsParam("maxTargetLine")) maxClientLine=config.getParam("maxTargetLine").toLong();
00730 unsigned long nbMaxMixtureInMemory=1;
00731 if (config.existsParam("nbMaxMixtureInMemory")) nbMaxMixtureInMemory=config.getParam("nbMaxMixtureInMemory").toLong();
00732 unsigned long worldDecime=1;
00733 if (config.existsParam("worldDecime"))
00734 worldDecime=config.getParam("worldDecime").toLong();
00735 if (config.getParam_debug())debug=true; else debug=false;
00736 if (verbose){
00737 cout << "Compute Test, Decime mode["<<worldDecime<<"] ";
00738 cout << "File Mode- 1 LLR by file" <<endl;
00739 }
00740
00741 try{
00742
00743 XList ndx(inputNDXFileName,config);
00744 XLine *linep;
00745 ndx.getLine(0);
00746 MixtureServer ms(config);
00747 StatServer ss(config, ms);
00748 MixtureGD& world = ms.loadMixtureGD(inputWorldFilename);
00749 ofstream outNist(outputNISTFileName.c_str(),ios::out | ios::trunc);
00750
00751
00752 unsigned long svsize=world.getVectSize()*world.getDistribCount();
00753 Matrix<double> U, V;
00754 DoubleVector D(svsize,svsize);
00755
00756
00757
00758 if(config.existsParam("eigenChannelMatrix")){
00759 String uName = config.getParam("matrixFilesPath") + config.getParam("eigenChannelMatrix") + config.getParam("loadMatrixFilesExtension");
00760 U.load (uName, config);
00761 }
00762 else{
00763 U.setDimensions(1,world.getVectSize());
00764 U.setAllValues(0.0);
00765 }
00766
00767
00768 if(config.existsParam("eigenVoiceMatrix")){
00769 String vName = config.getParam("matrixFilesPath") + config.getParam("eigenVoiceMatrix") + config.getParam("loadMatrixFilesExtension");
00770 V.load (vName, config);
00771 }
00772 else{
00773 V.setDimensions(1,world.getVectSize());
00774 V.setAllValues(0.0);
00775 }
00776
00777
00778 for(unsigned long i=0; i<world.getDistribCount(); i++){
00779 for(unsigned long j = 0; j<world.getVectSize(); j++){
00780 D[i*world.getVectSize()+j] = sqrt(1.0/(world.getDistrib(i).getCovInv(j)*config.getParam("regulationFactor").toDouble()));
00781 }
00782 }
00783
00784
00785 while ((linep=ndx.getLine()) != NULL){
00786
00787 String &featureFileName=linep->getElement(0);
00788 XLine featureFileListp;
00789
00790 featureFileListp.addElement(linep->getElement(0));
00791
00792 XList ndx; ndx.addLine() = featureFileListp;
00793
00794 JFAAcc jfaAcc(ndx,config);
00795
00796 if(verboseLevel >= 1) cout<<"Compute Likelihood ratio of test segment [ "<<featureFileName<<" ]"<<endl;
00797
00799 jfaAcc.loadEV(V, config); jfaAcc.loadEC(U, config); jfaAcc.loadD(D);
00800
00802 jfaAcc.computeAndAccumulateJFAStat(config);
00803
00804 jfaAcc.substractMplusDZByChannel();
00805 jfaAcc.substractMplusUX();
00806
00808 jfaAcc.estimateUEUT(config);
00809
00811 jfaAcc.estimateAndInverseL_EC(config);
00812
00814 jfaAcc.estimateX(config);
00815
00816 jfaAcc.estimateZMAP(config.getParam("regulationFactor").toLong());
00817
00818 FeatureServer fs(config,featureFileName);
00819
00821 jfaAcc.substractUXfromFeatures(fs,config);
00822
00823
00824
00825
00826 if(config.getParam("cms").toBool()){
00827 cms(featureFileName,fs,config);
00828 }
00829
00830
00831 TabClientLine tabClientLine(ms,config);
00832 tabClientLine.loadLine(linep);
00833
00834 SegServer segmentsServer;
00835 LabelServer labelServer;
00836 initializeClusters(featureFileName,segmentsServer,labelServer,config);
00837 verifyClusterFile(segmentsServer,fs,config);
00838 long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);
00839 SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame);
00840
00841 MixtureGDStat &worldAcc=ss.createAndStoreMixtureGDStat(world);
00842 worldAcc.resetLLK();
00843
00844
00845 RefVector <MixtureGDStat> clientAcc(tabClientLine.nbClientLine());
00846 for (unsigned int i=0; i<tabClientLine.nbClientLine();i++) {
00847 clientAcc.addObject(ss.createAndStoreMixtureGDStat(tabClientLine.getClientModel(i)));
00848 clientAcc[i].resetLLK();
00849 }
00850
00851 Seg* seg;
00852 selectedSegments.rewind();
00853 while((seg=selectedSegments.getSeg())!=NULL){
00854 unsigned long idxBeginFrame=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName());
00855 fs.seekFeature(idxBeginFrame);
00856 Feature f;
00857 for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){
00858 double llkw;
00859 double llkc;
00860 fs.readFeature(f);
00861 if ((idxFrame%worldDecime)==0)
00862 llkw=worldAcc.computeAndAccumulateLLK(f,1.0,DETERMINE_TOP_DISTRIBS);
00863
00864 else{
00865 worldAcc.computeAndAccumulateLLK(f,1.0,USE_TOP_DISTRIBS);
00866 }
00867 for (unsigned long i=0;i<tabClientLine.nbClientLine();i++){
00868 llkc=clientAcc[i].computeAndAccumulateLLK(f,1.0,USE_TOP_DISTRIBS);
00869 }
00870 }
00871 }
00872
00873 double LLKWorld=worldAcc.getMeanLLK();
00874
00875 for (unsigned int i=0;i < tabClientLine.nbClientLine();i++){
00876 double LLKClient=clientAcc[i].getMeanLLK();
00877 double LLRClient=LLKClient-LLKWorld;
00878 char decision=setDecision(LLRClient,decisionThreshold);
00879 outputResultLine(LLRClient, tabClientLine.getClientName(i),featureFileName ,gender ,decision,outNist);
00880 if (verboseLevel>0) {
00881 outputResultLine(LLRClient, tabClientLine.getClientName(i),featureFileName ,gender ,decision,cout);
00882 if (verboseLevel>0) cout<<"** Client LLK:"<<LLKClient<<" UBM LLk:"<<LLKWorld<<endl;
00883 }
00884 }
00885 clientAcc.deleteAllObjects();
00886
00887 if (verboseLevel > 2) cout << "To be deleted" << ms.toString() << endl;
00888 if ((nbMaxMixtureInMemory>0)&&(ms.getMixtureCount()>nbMaxMixtureInMemory)){
00889 if (verboseLevel >1) cout <<"Cleaning the mixture server from 1 to "<<ms.getMixtureCount()<<endl;
00890 ms.deleteMixtures(1,ms.getMixtureCount());
00891 ms.deleteUnusedDistribs();
00892 if (verboseLevel > 2) cout << "deleted" << ms.toString() << endl;
00893 }
00894 }
00895 outNist.close();
00896
00897 }
00898 catch (Exception& e){
00899 cout << e.toString().c_str() << endl;
00900 }
00901 return 0;
00902 }
00903
00904 int ComputeTestFA(Config& config)
00905 {
00906 String inputNDXFileName = config.getParam("ndxFilename");
00907 String inputWorldFilename = config.getParam("inputWorldFilename");
00908 String outputNISTFileName = config.getParam("outputFilename");
00909 String labelSelectedFrames =config.getParam("labelSelectedFrames");
00910
00911 bool segmentalMode=(config.getParam("segmentalMode")=="segmentLLR");
00912 String gender=config.getParam("gender");
00913 real_t decisionThreshold;
00914 if (config.existsParam("decisionThreshold"))
00915 decisionThreshold=config.getParam("decisionthreshold").toDouble();
00916 else decisionThreshold=0;
00917 unsigned long maxClientLine=CST_MAX_CLIENT_LINE;
00918 if (config.existsParam("maxTargetLine")) maxClientLine=config.getParam("maxTargetLine").toLong();
00919
00920 unsigned long nbMaxMixtureInMemory=1;
00921 if (config.existsParam("nbMaxMixtureInMemory")) nbMaxMixtureInMemory=config.getParam("nbMaxMixtureInMemory").toLong();
00922 unsigned long worldDecime=1;
00923 if (config.existsParam("worldDecime"))
00924 worldDecime=config.getParam("worldDecime").toLong();
00925 if (config.getParam_debug())debug=true; else debug=false;
00926 if (verbose){
00927 cout << "(Compute Test) Decime mode["<<worldDecime<<"] ";
00928 if (segmentalMode) cout << " Segmental Mode - 1 LLr by segment"<<endl;
00929 else cout << "File Mode- 1 LLR by file" <<endl;
00930 }
00931 if (verbose) cout << "(ComputeTest) Factor Analysis mode for channel effect" << endl;
00932
00933
00934 try{
00935 XList ndx(inputNDXFileName,config);
00936 XLine *linep;
00937 ndx.getLine(0);
00938 MixtureServer ms(config);
00939 StatServer ss(config, ms);
00940 MixtureGD& world = ms.loadMixtureGD(inputWorldFilename);
00941 TabClientLine tabClientLine(ms,config,maxClientLine);
00942 ofstream outNist(outputNISTFileName.c_str(),ios::out | ios::trunc);
00943
00944 while ((linep=ndx.getLine()) != NULL){
00945 String &featureFileName=linep->getElement(0);
00946
00947 FeatureServer fs(config,featureFileName);
00948
00949
00950
00951 FactorAnalysisStat FAAcc(featureFileName,fs,config);
00952 FAAcc.estimateXYAndNorm(fs,config);
00953 cms(featureFileName,fs,config);
00954
00955 if (verbose)cout << "---> LogLikelihood Ratio Computation of test segment ["<<featureFileName<<"]"<< endl;
00956 TabClientLine tabClientLine(ms,config);
00957 tabClientLine.loadLine(linep);
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978 SegServer segmentsServer;
00979 LabelServer labelServer;
00980 initializeClusters(featureFileName,segmentsServer,labelServer,config);
00981 verifyClusterFile(segmentsServer,fs,config);
00982 long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);
00983 SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame);
00984
00985 MixtureGDStat &worldAcc=ss.createAndStoreMixtureGDStat(world);
00986 worldAcc.resetLLK();
00987 RefVector <MixtureGDStat> clientAcc(tabClientLine.nbClientLine());
00988 for (unsigned int i=0; i<tabClientLine.nbClientLine();i++) {
00989 clientAcc.addObject(ss.createAndStoreMixtureGDStat(tabClientLine.getClientModel(i)));
00990 clientAcc[i].resetLLK();
00991 }
00992 Seg* seg;
00993 selectedSegments.rewind();
00994 while((seg=selectedSegments.getSeg())!=NULL){
00995 unsigned long idxBeginFrame=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName());
00996 fs.seekFeature(idxBeginFrame);
00997 Feature f;
00998 for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){
00999 double llkw;
01000 double llkc;
01001 fs.readFeature(f);
01002 if ((idxFrame%worldDecime)==0)
01003 llkw=worldAcc.computeAndAccumulateLLK(f,1.0,DETERMINE_TOP_DISTRIBS);
01004 else worldAcc.computeAndAccumulateLLK(f,1.0,USE_TOP_DISTRIBS);
01005 for (unsigned long i=0;i<tabClientLine.nbClientLine();i++){
01006 llkc=clientAcc[i].computeAndAccumulateLLK(f,1.0,USE_TOP_DISTRIBS);
01007 }
01008 }
01009 }
01010 if (segmentalMode==false){
01011 double LLKWorld=worldAcc.getMeanLLK();
01012 for (unsigned int i=0;i < tabClientLine.nbClientLine();i++){
01013 double LLKClient=clientAcc[i].getMeanLLK();
01014 double LLRClient=LLKClient-LLKWorld;
01015 char decision=setDecision(LLRClient,decisionThreshold);
01016 outputResultLine(LLRClient, tabClientLine.getClientName(i),featureFileName ,gender ,decision,outNist);
01017 if (verboseLevel>0) {
01018 outputResultLine(LLRClient, tabClientLine.getClientName(i),featureFileName ,gender ,decision,cout);
01019 if (verboseLevel>0) cout<<"** Client LLK:"<<LLKClient<<" UBM LLk:"<<LLKWorld<<endl;
01020 }
01021 }
01022 }
01023
01024 clientAcc.deleteAllObjects();
01025
01026 if (verboseLevel > 2) cout << "To be deleted" << ms.toString() << endl;
01027 if ((nbMaxMixtureInMemory>0)&&(ms.getMixtureCount()>nbMaxMixtureInMemory)){
01028 if (verboseLevel >1) cout <<"Cleaning the mixture server from 1 to "<<ms.getMixtureCount()<<endl;
01029 ms.deleteMixtures(1,ms.getMixtureCount());
01030 ms.deleteUnusedDistribs();
01031 if (verboseLevel > 2) cout << "deleted" << ms.toString() << endl;
01032 }
01033 }
01034 outNist.close();
01035 }
01036 catch (Exception& e){
01037 cout << e.toString().c_str() << endl;
01038 }
01039 return 0;
01040 }
01041
01042 int ComputeTestNAP(Config& config)
01043 {
01044 String inputNDXFileName = config.getParam("ndxFilename");
01045 String inputWorldFilename = config.getParam("inputWorldFilename");
01046 String outputNISTFileName = config.getParam("outputFilename");
01047 String labelSelectedFrames =config.getParam("labelSelectedFrames");
01048
01049 bool segmentalMode=(config.getParam("segmentalMode")=="segmentLLR");
01050 String gender=config.getParam("gender");
01051 real_t decisionThreshold;
01052 if (config.existsParam("decisionThreshold"))
01053 decisionThreshold=config.getParam("decisionthreshold").toDouble();
01054 else decisionThreshold=0;
01055 unsigned long maxClientLine=CST_MAX_CLIENT_LINE;
01056 if (config.existsParam("maxTargetLine")) maxClientLine=config.getParam("maxTargetLine").toLong();
01057 double frameLength = config.getParam("frameLength").toDouble();
01058 unsigned long nbMaxMixtureInMemory=1;
01059 if (config.existsParam("nbMaxMixtureInMemory")) nbMaxMixtureInMemory=config.getParam("nbMaxMixtureInMemory").toLong();
01060 unsigned long worldDecime=1;
01061 if (config.existsParam("worldDecime"))
01062 worldDecime=config.getParam("worldDecime").toLong();
01063 if (config.getParam_debug())debug=true; else debug=false;
01064 if (verbose){
01065 cout << "Compute Test, Decime mode["<<worldDecime<<"] ";
01066 if (segmentalMode) cout << " Segmental Mode - 1 LLr by segment"<<endl;
01067 else cout << "File Mode- 1 LLR by file" <<endl;
01068 }
01069 bool UBM=false;
01070 bool NAP=false;
01071 if (config.getParam("channelCompensation")=="NAP" && verbose) {cout << "NAP mode for channel effect" << endl; NAP=true;}
01072 else if (config.getParam("channelCompensation")=="UBM") {if (verbose) cout << "NAP mode for channel effect [W: add UBM channel effect]" << endl;UBM=true;}
01073 else throw Exception("channelCompensation parameter is wrong",__FILE__,__LINE__);
01074
01075 try{
01076 XList ndx(inputNDXFileName,config);
01077 XLine *linep;
01078 ndx.getLine(0);
01079 MixtureServer ms(config);
01080 StatServer ss(config, ms);
01081 MixtureGD& world = ms.loadMixtureGD(inputWorldFilename);
01082 TabClientLine tabClientLine(ms,config,1);
01083 ofstream outNist(outputNISTFileName.c_str(),ios::out | ios::trunc);
01084
01085 Matrix<double> ChannelMatrix;
01086 ChannelMatrix.load(config.getParam("initChannelMatrix"),config);
01087 if (verbose) cout<< "Removing channel effect from " << config.getParam("initChannelMatrix") << " of size: ["<< ChannelMatrix.rows() << "," <<ChannelMatrix.cols() << "]" << endl;
01088 unsigned long svSize=world.getDistribCount()*ms.getVectSize();
01089 RealVector <double> channelVector(svSize,svSize);
01090
01091 while ((linep=ndx.getLine()) != NULL){
01092 String &featureFileName=linep->getElement(0);
01093 MixtureGD &testModel=ms.createMixtureGD();
01094 if (NAP) ms.loadMixture(testModel,featureFileName);
01095 FeatureServer fs(config,featureFileName);
01096 SegServer segmentsServer;
01097 LabelServer labelServer;
01098 initializeClusters(featureFileName,segmentsServer,labelServer,config);
01099 verifyClusterFile(segmentsServer,fs,config);
01100 long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);
01101 if (codeSelectedFrame==-1)
01102 cout << "WARNING, TEST FILE ["<<featureFileName<<"] is empty"<<endl;
01103 else{
01104 SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame);
01105 TabClientLine tabClientLine(ms,config);
01106 tabClientLine.loadLine(linep);
01107 if (verboseLevel > 2) cout << "Load line : " << ms.toString() << endl;
01108 for (unsigned int i=0; i<tabClientLine.nbClientLine();i++) {
01109 if (verbose) cout << "---> Compute Test Channel effect for: [" << featureFileName <<"]["<<tabClientLine.getClientModel(i).getId()<<"]"<<endl;
01110 if (UBM) computeNAPChannelEffect(world,tabClientLine.getClientModel(i),ChannelMatrix);
01111 if (NAP) computeNAPChannelEffect(testModel,tabClientLine.getClientModel(i),ChannelMatrix);
01112 }
01113 if (verbose)cout << "---> LogLikelihood Ratio Computation of test segment ["<<featureFileName<<"]"<< endl;
01114 MixtureGDStat &worldAcc=ss.createAndStoreMixtureGDStat(world);
01115 worldAcc.resetLLK();
01116 RefVector <MixtureGDStat> clientAcc(tabClientLine.nbClientLine());
01117 for (unsigned int i=0; i<tabClientLine.nbClientLine();i++) {
01118 clientAcc.addObject(ss.createAndStoreMixtureGDStat(tabClientLine.getClientModel(i)));
01119 clientAcc[i].resetLLK();
01120 }
01121
01122 Seg* seg;
01123 selectedSegments.rewind();
01124
01125 while((seg=selectedSegments.getSeg())!=NULL){
01126 unsigned long idxBeginFrame=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName());
01127 fs.seekFeature(idxBeginFrame);
01128 Feature f;
01129 for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){
01130 fs.readFeature(f);
01131 if ((idxFrame%worldDecime)==0)
01132 worldAcc.computeAndAccumulateLLK(f,1.0,DETERMINE_TOP_DISTRIBS);
01133 else worldAcc.computeAndAccumulateLLK(f,1.0,USE_TOP_DISTRIBS);
01134 for (unsigned int i=0;i<tabClientLine.nbClientLine();i++)
01135 clientAcc[i].computeAndAccumulateLLK(f,1.0,USE_TOP_DISTRIBS);
01136 }
01137 if (segmentalMode){
01138 double LLKWorld=worldAcc.getMeanLLK();
01139 for (unsigned int i=0;i < tabClientLine.nbClientLine();i++){
01140 double LLKClient=clientAcc[i].getMeanLLK();
01141 double LLRClient=LLKClient-LLKWorld;
01142 char decision=setDecision(LLRClient,decisionThreshold);
01143 outputResultLine(LLRClient, tabClientLine.getClientName(i),
01144 featureFileName, frameIdxToTime(idxBeginFrame,frameLength),frameIdxToTime(idxBeginFrame+seg->length(),frameLength),
01145 gender ,decision,outNist);
01146 if (verboseLevel>1) outputResultLine(LLRClient, tabClientLine.getClientName(i),
01147 featureFileName, frameIdxToTime(idxBeginFrame,frameLength),frameIdxToTime(idxBeginFrame+seg->length(),frameLength),
01148 gender,decision ,cout);
01149 clientAcc[i].resetLLK();
01150 }
01151 worldAcc.resetLLK();
01152 }
01153 }
01154 if (segmentalMode==false){
01155 double LLKWorld=worldAcc.getMeanLLK();
01156 for (unsigned int i=0;i < tabClientLine.nbClientLine();i++){
01157 double LLKClient=clientAcc[i].getMeanLLK();
01158 double LLRClient=LLKClient-LLKWorld;
01159 char decision=setDecision(LLRClient,decisionThreshold);
01160 outputResultLine(LLRClient, tabClientLine.getClientName(i),featureFileName ,gender ,decision,outNist);
01161 if (verboseLevel>1) {
01162 outputResultLine(LLRClient, tabClientLine.getClientName(i),featureFileName ,gender ,decision,cout);
01163 if (verboseLevel>2) cout<<"** C:"<<LLKClient<<" W:"<<LLKWorld<<endl;
01164 }
01165 }
01166 }
01167 }
01168 if (verboseLevel > 2) cout << "To be deleted" << ms.toString() << endl;
01169 if ((nbMaxMixtureInMemory>0)&&(ms.getMixtureCount()>nbMaxMixtureInMemory)){
01170 if (verboseLevel >1) cout <<"Cleaning the mixture server from 1 to "<<ms.getMixtureCount()<<endl;
01171 ms.deleteMixtures(1,ms.getMixtureCount());
01172 ms.deleteUnusedDistribs();
01173 if (verboseLevel > 2) cout << "deleted" << ms.toString() << endl;
01174 }
01175 }
01176 outNist.close();
01177 }
01178 catch (Exception& e){
01179 cout << e.toString().c_str() << endl;
01180 }
01181 return 0;
01182 }
01183
01184
01185
01186 int ComputeTestByLabel(Config& config)
01187 {
01188 String inputNDXFileName = config.getParam("ndxFileName");
01189 String inputWorldFilename = config.getParam("inputWorldFilename");
01190 String outputNISTFileName = config.getParam("outputFile");
01191
01192 String gender=config.getParam("gender");
01193 real_t decisionThreshold;
01194 if (config.existsParam("decisionThreshold"))
01195 decisionThreshold=config.getParam("decisionthreshold").toDouble();
01196 else decisionThreshold=0;
01197 unsigned long worldDecime=1;
01198 if (config.existsParam("worldDecime"))
01199 worldDecime=config.getParam("worldDecime").toLong();
01200 unsigned long maxClientLine=CST_MAX_CLIENT_LINE;
01201 if (config.existsParam("maxTargetLine")) maxClientLine=config.getParam("maxTargetLine").toLong();
01202 bool useDefaultClientModel=true;
01203 if (config.existsParam("useDefaultClientModel")) useDefaultClientModel=(config.getParam("useDefaultClientModel")=="true");
01204 bool byUserRep=false;
01205 if (config.existsParam("byUserRep")) byUserRep=(config.getParam("byUserRep")=="true");
01206
01207 bool weigthedFusion=true;
01208 if (config.existsParam("weigthedFusion")) weigthedFusion=(config.getParam("weigthedFusion")=="true");
01209
01210
01211 unsigned long nbMaxMixtureInMemory=0;
01212 if (config.existsParam("nbMaxMixtureInMemory")) nbMaxMixtureInMemory=config.getParam("nbMaxMixtureInMemory").toLong();
01213 bool verboseAll=false;
01214 if (config.existsParam("verboseAll"))verboseAll=true; else verboseAll=false;
01215 if (verbose){
01216 cout << "Compute Test -by label model option, world decime["<<worldDecime<<"]";
01217 if (useDefaultClientModel) cout <<" Use default client model if a label model is missing";
01218 if (byUserRep) cout <<" the label models are in a by user rep user/user_label.gmm";
01219 cout <<endl;
01220 }
01221 try{
01222 XList ndx(inputNDXFileName,config);
01223 XLine *linep;
01224 ndx.getLine(0);
01225 MixtureServer ms(config);
01226 StatServer ss(config, ms);
01227 MixtureGD& world = ms.loadMixtureGD(inputWorldFilename);
01228 TabClientLine tabClientLine(ms,config,maxClientLine);
01229 ofstream outNist(outputNISTFileName.c_str(),ios::out | ios::trunc);
01230 while ((linep=ndx.getLine()) != NULL){
01231 String &featureFileName=linep->getElement(0);
01232 FeatureServer fs(config,featureFileName);
01233 SegServer segmentsServer;
01234 LabelServer labelServer;
01235 initializeClusters(featureFileName,segmentsServer,labelServer,config);
01236 verifyClusterFile(segmentsServer,fs,config);
01237 TabClientLine tabClientLine(ms,config);
01238 ScoreAccum accumScore;
01239 for (unsigned long codeSelectedFrame=0;codeSelectedFrame<segmentsServer.getClusterCount();codeSelectedFrame++){
01240 String label=labelServer.getLabel(codeSelectedFrame).getString();
01241 if (verbose) cout <<"test seg["<<featureFileName<<"] Label ["<<label<<"]"<<endl;
01242 if (tabClientLine.loadLine(linep,label,useDefaultClientModel,byUserRep)){
01243 SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame);
01244 ss.resetLLK(world);
01245 for (unsigned int i=0; i<tabClientLine.nbClientLine();i++)
01246 ss.resetLLK(tabClientLine.getClientModel(i));
01247 Seg* seg;
01248 selectedSegments.rewind();
01249 unsigned long nbFrame=0;
01250 while((seg=selectedSegments.getSeg())!=NULL){
01251 unsigned long idxBeginFrame=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName());
01252 fs.seekFeature(idxBeginFrame);
01253 Feature f;
01254 nbFrame++;
01255 for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){
01256 fs.readFeature(f);
01257 if ((idxFrame%worldDecime)==0)
01258 ss.computeAndAccumulateLLK(world, f,DETERMINE_TOP_DISTRIBS);
01259 else ss.computeAndAccumulateLLK(world, f,USE_TOP_DISTRIBS);
01260 for (unsigned int i=0;i<tabClientLine.nbClientLine();i++)
01261 ss.computeAndAccumulateLLK(tabClientLine.getClientModel(i),f,USE_TOP_DISTRIBS);
01262 }
01263 }
01264 double LLKWorld=ss.getMeanLLK(world);
01265 for (unsigned int i=0;i < tabClientLine.nbClientLine();i++){
01266 double LLKClient=ss.getMeanLLK(tabClientLine.getClientModel(i));
01267 double LLRClient=LLKClient-LLKWorld;
01268 char decision=setDecision(LLRClient,decisionThreshold);
01269 outputResultLine(LLRClient, tabClientLine.getClientName(i)+"_"+label,featureFileName ,gender ,decision,outNist);
01270 if (verboseAll) {
01271 outputResultLine(LLRClient, tabClientLine.getClientName(i)+"_"+label,featureFileName ,gender ,decision,cout);
01272 cout<<"** C:"<<LLKClient<<" W:"<<LLKWorld<<endl;
01273 }
01274 if (weigthedFusion) accumScore.addAndAccumulate(tabClientLine.getClientName(i),LLRClient,nbFrame);
01275 else accumScore.addAndAccumulate(tabClientLine.getClientName(i),LLRClient,1);
01276 }
01277 }
01278 }
01279 for (unsigned int i=0;i < accumScore.getSize();i++){
01280 char decision=setDecision(accumScore.getScore(i),decisionThreshold);
01281 outputResultLine(accumScore.getScore(i), accumScore.getId(i),featureFileName ,gender ,decision,outNist);
01282 if (verboseAll)outputResultLine(accumScore.getScore(i),accumScore.getId(i),featureFileName ,gender ,decision,cout);
01283 }
01284 if ((nbMaxMixtureInMemory>0)&&(ms.getMixtureCount()>nbMaxMixtureInMemory)){
01285 if (verbose) cout <<"Cleaning the mixture server from 1 to "<<ms.getMixtureCount()<<endl;
01286 ms.deleteMixtures(1,ms.getMixtureCount());
01287 ms.deleteUnusedDistribs();
01288 }
01289 }
01290 outNist.close();
01291 }
01292 catch (Exception& e){
01293 cout << e.toString().c_str() << endl;
01294 }
01295 return 0;
01296 }
01297
01298
01299
01300 int ComputeTestHisto(Config& config)
01301 {
01302 String inputNDXFileName = config.getParam("ndxFileName");
01303 String inputWorldFilename = config.getParam("inputWorldFilename");
01304 String outputNISTFileName = config.getParam("outputFile");
01305 String labelSelectedFrames =config.getParam("labelSelectedFrames");
01306
01307 bool segmentalMode=(config.getParam("segmentalMode")=="segmentLLR");
01308 String gender=config.getParam("gender");
01309 real_t decisionThreshold;
01310 if (config.existsParam("decisionThreshold"))
01311 decisionThreshold=config.getParam("decisionthreshold").toDouble();
01312 else decisionThreshold=0;
01313 unsigned long maxClientLine=CST_MAX_CLIENT_LINE;
01314 if (config.existsParam("maxTargetLine")) maxClientLine=config.getParam("maxTargetLine").toLong();
01315 unsigned long nbBins=config.getParam("nbBins").toLong();
01316
01317 unsigned long nbMaxMixtureInMemory=0;
01318 if (config.existsParam("nbMaxMixtureInMemory")) nbMaxMixtureInMemory=config.getParam("nbMaxMixtureInMemory").toLong();
01319 unsigned long worldDecime=1;
01320 if (config.existsParam("worldDecime"))
01321 worldDecime=config.getParam("worldDecime").toLong();
01322 if (config.existsParam("debug"))debug=true; else debug=false;
01323 if (config.existsParam("verbose"))verbose=true; else verbose=false;
01324 bool verboseAll=false;
01325 if (config.existsParam("verboseAll"))verboseAll=true; else verboseAll=false;
01326 String scoreType=config.getParam("scoreType");
01327 if (verbose){
01328 cout << "**** Likelihood Ratio will be computed with Histo ****" << endl;
01329 if (config.getParam("scoreType")=="entropy") {cout << "**** Scores are entropies ****" << endl;}
01330 if (config.getParam("scoreType")=="mean") {cout << "**** Robust Mean using Histo ****" << endl;}
01331 cout << "Compute Test, Decime mode["<<worldDecime<<"] ";
01332 if (segmentalMode) cout << " Segmental Mode - Confidences scores will be used to discard some segments"<<endl;
01333 else cout << "File Mode- 1 LLR by file" <<endl;
01334 }
01335 try{
01336 XList ndx(inputNDXFileName,config);
01337 XLine *linep;
01338 ndx.getLine(0);
01339 MixtureServer ms(config);
01340 StatServer ss(config, ms);
01341 MixtureGD& world = ms.loadMixtureGD(inputWorldFilename);
01342 TabClientLine tabClientLine(ms,config,maxClientLine);
01343 ofstream outNist(outputNISTFileName.c_str(),ios::out | ios::trunc);
01344 while ((linep=ndx.getLine()) != NULL){
01345 String &featureFileName=linep->getElement(0);
01346 FeatureServer fs(config,featureFileName);
01347 SegServer segmentsServer;
01348 LabelServer labelServer;
01349 initializeClusters(featureFileName,segmentsServer,labelServer,config);
01350 verifyClusterFile(segmentsServer,fs,config);
01351 long codeSelectedFrame=labelServer.getLabelIndexByString(labelSelectedFrames);
01352 if (codeSelectedFrame==-1)
01353 cout << "ATTENTION, TEST FILE ["<<featureFileName<<"] is empty"<<endl;
01354 else{
01355 SegCluster& selectedSegments=segmentsServer.getCluster(codeSelectedFrame);
01356 TabClientLine tabClientLine(ms,config);
01357 tabClientLine.loadLine(linep);
01358 TabHisto tabHisto(nbBins,maxClientLine);
01359 ScoreAccum accumModifiedLLR;
01360 double llk_w=0.0;
01361 double llk_c=0.0;
01362 if (verbose)cout << "test seg["<<featureFileName<<"]"<< endl;
01363 ss.resetLLK(world);
01364 for (unsigned int i=0; i<tabClientLine.nbClientLine();i++)
01365 ss.resetLLK(tabClientLine.getClientModel(i));
01366 Seg* seg;
01367 selectedSegments.rewind();
01368 while((seg=selectedSegments.getSeg())!=NULL){
01369 unsigned long idxBeginFrame=seg->begin()+fs.getFirstFeatureIndexOfASource(seg->sourceName());
01370 fs.seekFeature(idxBeginFrame);
01371 Feature f;
01372 for (unsigned long idxFrame=0;idxFrame<seg->length();idxFrame++){
01373 fs.readFeature(f);
01374 if ((idxFrame%worldDecime)==0)
01375 llk_w=ss.computeAndAccumulateLLK(world, f,DETERMINE_TOP_DISTRIBS);
01376 else ss.computeAndAccumulateLLK(world, f,USE_TOP_DISTRIBS);
01377 for (unsigned int i=0;i<tabClientLine.nbClientLine();i++) {
01378 llk_c=ss.computeAndAccumulateLLK(tabClientLine.getClientModel(i),f,USE_TOP_DISTRIBS);
01379
01380 tabHisto.accumulateValueInTab(tabClientLine.getClientName(i),llk_c-llk_w);}
01381 }
01382 if (segmentalMode){
01383 double LLKWorld=ss.getMeanLLK(world);
01384 for (unsigned int i=0;i < tabClientLine.nbClientLine();i++){
01385 double LLKClient=ss.getMeanLLK(tabClientLine.getClientModel(i));
01386 double LLRClient=LLKClient-LLKWorld;
01387
01388 Histo & currentHisto=tabHisto.getHisto(tabClientLine.getClientName(i));
01389 double entropy=computeEntropy(currentHisto);
01390 if (verboseAll) cout << "Segment: "<<tabClientLine.getClientName(i)<< " "<<seg->begin()<<":"<<seg->length()<<" LLR: "<< LLRClient << " Entropy: " << entropy <<endl;
01391
01392 if (0) {
01393 if (verboseAll) cerr << "Discard segment" << LLRClient << endl;
01394 LLRClient=0.0;}
01395 accumModifiedLLR.addAndAccumulate(tabClientLine.getClientName(i),LLRClient,1);
01396 currentHisto.computeHisto(0);
01397
01398 ss.resetLLK(tabClientLine.getClientModel(i));
01399 }
01400 ss.resetLLK(world);
01401 }
01402 }
01403
01404 if (segmentalMode) {
01405 for (unsigned int i=0;i < tabClientLine.nbClientLine();i++){
01406 double score=accumModifiedLLR.getScore(tabClientLine.getClientName(i));
01407 if (verboseAll) outputResultLine(score, tabClientLine.getClientName(i),featureFileName ,gender ,0,cout);
01408 outputResultLine(score, tabClientLine.getClientName(i),featureFileName ,gender ,0,outNist);
01409 }
01410 }
01411 if (segmentalMode==false){
01412 double LLKWorld=ss.getMeanLLK(world);
01413 for (unsigned int i=0;i < tabClientLine.nbClientLine();i++){
01414 tabHisto.computeHistoInTab(tabClientLine.getClientName(i));
01415 double LLKClient=ss.getMeanLLK(tabClientLine.getClientModel(i));
01416 double LLRClient=LLKClient-LLKWorld;
01417 char decision=setDecision(LLRClient,decisionThreshold);
01418 Histo & currentHisto=tabHisto.getHisto(tabClientLine.getClientName(i));
01419 if (debug) currentHisto.saveGnuplot(tabClientLine.getClientName(i)+".hist");
01420 double score=0.0;
01421 if(scoreType=="entropy") score=computeEntropy(currentHisto);
01422 else if(scoreType=="mean") score=computeMean(currentHisto);
01423 else {cerr << "Error: unknown score type!" << endl;}
01424 outputResultLine(score, tabClientLine.getClientName(i),featureFileName ,gender ,decision,outNist);
01425 if (verboseAll) {
01426 outputResultLine(score, tabClientLine.getClientName(i),featureFileName ,gender ,decision,cout);
01427 cout<<"** C:"<<LLKClient<<" W:"<<LLKWorld<<" LR:"<<LLRClient<<endl;
01428 }
01429 }
01430 }
01431 }
01432 if ((nbMaxMixtureInMemory>0)&&(ms.getMixtureCount()>nbMaxMixtureInMemory)){
01433 if (verbose) cout <<"Cleaning the mixture server from 1 to "<<ms.getMixtureCount()<<endl;
01434 ms.deleteMixtures(1,ms.getMixtureCount());
01435 ms.deleteUnusedDistribs();
01436 }
01437 }
01438 outNist.close();
01439 }
01440 catch (Exception& e){
01441 cout << e.toString().c_str() << endl;
01442 }
01443 return 0;
01444 }
01445
01446
01447 #endif //!defined(ALIZE_ComputeTest_cpp)