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_TrainTools_cpp)
00056 #define ALIZE_TrainTools_cpp
00057
00058 #include <iostream>
00059 #include <fstream>
00060 #include <cstdio>
00061 #include <cassert>
00062 #include <cmath>
00063 #include <liatools.h>
00064 #include <DoubleSquareMatrix.h>
00065
00066
00067 TrainCfg::TrainCfg(Config &config){
00068 _initVarianceFlooring = config.getParam("initVarianceFlooring").toDouble();
00069 _initVarianceCeiling = config.getParam("initVarianceCeiling").toDouble();
00070 _finalVarianceFlooring = config.getParam("finalVarianceFlooring").toDouble();
00071 _finalVarianceCeiling = config.getParam("finalVarianceCeiling").toDouble();
00072 _nbTrainIt = config.getParam("nbTrainIt").toLong();
00073 _baggedFrameProbability= config.getParam("baggedFrameProbability").toDouble();
00074 if (config.existsParam("baggedFrameProbabilityInit")) _baggedFrameProbabilityInit =config.getParam("baggedFrameProbabilityInit").toDouble();
00075 else _baggedFrameProbabilityInit=0;
00076 if (config.existsParam("normalizeModel")) _normalizeModel=config.getParam("normalizeModel").toBool();
00077 else _normalizeModel=false;
00078 if (_normalizeModel){
00079 _normalizeModelNbIt=1;
00080 if (config.existsParam("normalizeModelMeanOnly"))
00081 _normalizeModelMeanOnly=config.getParam("normalizeModelMeanOnly").toBool();
00082 else _normalizeModelMeanOnly=false;
00083 if (_normalizeModelMeanOnly)
00084 _normalizeModelNbIt=config.getParam("normalizeModelNbIt").toLong();
00085 }
00086
00087 _componentReduction=false;
00088 if (config.existsParam("componentReduction")) _componentReduction=config.getParam("componentReduction").toBool();
00089 _targetDistribCount=0;
00090 if (_componentReduction){
00091 _targetDistribCount=config.getParam("targetMixtureDistribCount").toLong();
00092 }
00093 }
00094 void TrainCfg::showConfig(ostream & st=cout){
00095 st << "Training parameters "<<endl;
00096 st <<"nb training iterations:"<<_nbTrainIt<<endl;
00097 st <<"init variance floor:"<<_initVarianceFlooring<<" final floor:"<<_finalVarianceFlooring<<endl;
00098 st <<"init variance ceil:"<<_initVarianceCeiling<<" final ceil:"<<_finalVarianceCeiling<<endl;
00099 if (_normalizeModel){
00100 st <<"normaliseModel on";
00101 if (_normalizeModelMeanOnly){
00102 st <<" Mean Only mode, nbIt:"<<_normalizeModelNbIt<<endl;
00103 }
00104 else st<<endl;
00105 }
00106 else st <<"normalise model off"<<endl;
00107 if (_componentReduction)
00108 st <<"Component number reduction, final["<<_targetDistribCount<<"]"<<endl;
00109 }
00110 MAPCfg::MAPCfg(Config &config){
00111
00112 _mean=false;_var=false;_weight=false;
00113 if (config.existsParam("meanAdapt")) _mean=config.getParam("meanAdapt").toBool();
00114 if (config.existsParam("varAdapt")) _var=config.getParam("varAdapt").toBool();
00115 if (config.existsParam("weightAdapt")) _weight=config.getParam("weightAdapt").toBool();
00116 if (!_var && !_mean && !_weight) cerr <<"MAP, no adaptation selected !"<<endl;
00117 _method=config.getParam("MAPAlgo");
00118 if ((_method=="MAPConst")||(_method=="MAPConst2")){
00119 if (_mean)_r[0]=config.getParam("MAPAlphaMean").toDouble();
00120 if (_var)_r[1]=config.getParam("MAPAlphaVar").toDouble();
00121 if (_weight)_r[2]=config.getParam("MAPAlphaWeight").toDouble();
00122 }
00123 else
00124 if ((_method=="MAPOccDep")||(_method=="MAPModelBased")|| (_method=="MLLR")){
00125 if (_mean) _r[0]= config.getParam("MAPRegFactorMean").toDouble();
00126 if (_var) _r[1]= config.getParam("MAPRegFactorVar").toDouble();
00127 if (_weight)_r[2]= config.getParam("MAPRegFactorWeight").toDouble();
00128 } else cerr <<"mapAlgo["<<_method<<"] unknown"<<endl;
00129 _normalizeModel=false;
00130 if (config.existsParam("normalizeModel")) _normalizeModel=config.getParam("normalizeModel").toBool();
00131 if (_normalizeModel){
00132 _normalizeModelNbIt=1;
00133 if (config.existsParam("normalizeModelMeanOnly"))
00134 _normalizeModelMeanOnly=config.getParam("normalizeModelMeanOnly").toBool();
00135 else _normalizeModelMeanOnly=false;
00136 if (_normalizeModelMeanOnly)
00137 _normalizeModelNbIt=config.getParam("normalizeModelNbIt").toLong();
00138 }
00139 else _normalizeModel=false;
00140 _nbTrainIt = 1;
00141 if (config.existsParam("nbTrainIt")) _nbTrainIt=config.getParam("nbTrainIt").toLong();
00142 _nbEmIt = 1;
00143 if (config.existsParam("nbEmIt")) _nbEmIt=config.getParam("nbEmIt").toLong();
00144 if (config.existsParam("baggedFrameProbability"))
00145 _baggedFrameProbability= config.getParam("baggedFrameProbability").toDouble();
00146 else _baggedFrameProbability=1.0;
00147 }
00148 void MAPCfg::showConfig(ostream & st=cout){
00149 st << "MAP Algo "<<_method<<endl;
00150 if (_mean) st<<"Mean adaption, param["<<_r[0]<<"]"<<endl;
00151 if (_var) st<<"Variance adaption, param["<<_r[1]<<"]"<<endl;
00152 if (_weight) st<<"Weight adaptation, param["<<_r[2]<<"]"<<endl;
00153 st <<"Nb training iterations["<<_nbTrainIt<<"]"<<endl;
00154 if (_normalizeModel){
00155 st <<"NormaliseModel on";
00156 if (_normalizeModelMeanOnly){
00157 st <<" Mean Only mode, nbIt["<<_normalizeModelNbIt<<"]"<<endl;
00158 }
00159 else st<<endl;
00160 }
00161 else st <<"Normalise model off"<<endl;
00162 }
00163
00164
00165
00166 void copyMixture(DistribGD &s,DistribGD &d){
00167 unsigned long vectSize=s.getVectSize();
00168 if (vectSize!=d.getVectSize()) throw Exception("vectSize != for copyMixture" , __FILE__, __LINE__);
00169 for (unsigned long c=0;c<vectSize;c++){
00170 d.setMean(s.getMean(c),c);
00171 d.setCov(s.getCov(c),c);
00172 }
00173 d.computeAll();
00174 }
00175 unsigned long selectComponent(bool selectCompA[],XList & distribL, MixtureGD &inputM){
00176 unsigned long nbInputDistrib=inputM.getDistribCount();
00177 for (unsigned long idx=0;idx<nbInputDistrib;idx++) selectCompA[idx]=true;
00178 for (unsigned long idx=0;idx<nbInputDistrib;idx++) selectCompA[idx]=true;
00179 unsigned long nbOutputDistrib=nbInputDistrib;
00180 for (unsigned long idxS=0;idxS<distribL.getLineCount();idxS++){
00181 selectCompA[distribL.getLine(idxS).getElement(0).toLong()]=false;
00182 nbOutputDistrib--;
00183 }
00184 return nbOutputDistrib;
00185 }
00186 unsigned long selectComponent(bool selectCompA[],double wFactor,MixtureGD &inputM){
00187 unsigned long nbInputDistrib=inputM.getDistribCount();
00188 for (unsigned long idx=0;idx<nbInputDistrib;idx++) selectCompA[idx]=true;
00189 unsigned long nbOutputDistrib=nbInputDistrib;
00190 for (unsigned long idx=0;idx<nbInputDistrib;idx++)
00191 if ((selectCompA[idx])&&(inputM.weight(idx)<wFactor)){
00192 selectCompA[idx]=false;
00193 nbOutputDistrib--;
00194 }
00195 return nbOutputDistrib;
00196 }
00197 unsigned long selectComponent(bool selectCompA[],unsigned long nbTop,MixtureGD &inputM){
00198 unsigned long nbInputDistrib=inputM.getDistribCount();
00199 for (unsigned long idx=0;idx<nbInputDistrib;idx++) selectCompA[idx]=false;
00200 TabWeight tabW(inputM);
00201 for (unsigned long i=0; i<nbTop;i++){
00202 selectCompA[tabW.getDistrib(i)]=true;
00203 }
00204 return nbTop;
00205 }
00206 double reduceModel(bool selectCompA[],MixtureGD &inputM,MixtureGD &outputM){
00207 unsigned long nbInputDistrib=inputM.getDistribCount();
00208 unsigned long outputIdx=0;
00209 double totW=0;
00210 for (unsigned idx=0;idx<nbInputDistrib;idx++){
00211 if (selectCompA[idx]){
00212 DistribGD &s=inputM.getDistrib(idx);
00213 DistribGD &d=outputM.getDistrib(outputIdx);
00214 copyMixture(s,d);
00215 outputM.weight(outputIdx)=inputM.weight(idx);
00216 totW+=outputM.weight(outputIdx);
00217 outputIdx++;
00218 }
00219 }
00220 return totW;
00221 }
00222
00223 void normalizeWeights(MixtureGD &outputM){
00224 double totW=0;
00225 for (unsigned long idx=0;idx<outputM.getDistribCount();idx++)
00226 totW+=outputM.weight(idx);
00227 for (unsigned long idx=0;idx<outputM.getDistribCount();idx++)
00228 outputM.weight(idx)/=totW;
00229 }
00230
00231
00232
00233
00234
00235
00236 void fuseModels(const MixtureGD & model1,unsigned long nbFrameM1,const MixtureGD & model2,unsigned long nbFrameM2,MixtureGD &result){
00237 unsigned long vectSize=model1.getVectSize();
00238 if (vectSize!=model2.getVectSize()) throw Exception("Feature vector size should be the same" , __FILE__, __LINE__);
00239 unsigned long nbDistrib=model1.getDistribCount();
00240 if (nbDistrib!=model2.getDistribCount()) throw Exception("Number of components should be the same" , __FILE__, __LINE__);
00241 for (unsigned long idx=0;idx<nbDistrib;idx++){
00242 DistribGD & d1=model1.getDistrib(idx);
00243 DistribGD & d2=model2.getDistrib(idx);
00244 DistribGD & dr=result.getDistrib(idx);
00245 double totOcc1=nbFrameM1*model1.weight(idx);
00246 double totOcc2=nbFrameM2*model2.weight(idx);
00247 for (unsigned long c=0;c<vectSize;c++){
00248 dr.setMean(((d1.getMean(c)*totOcc1)+(d2.getMean(c)*totOcc2))/(totOcc1+totOcc2),c);
00249 dr.setCov(((d1.getCov(c)*totOcc1)+(d2.getCov(c)*totOcc2))/(totOcc1+totOcc2),c);
00250 }
00251 dr.computeAll();
00252 result.weight(idx)=((model1.weight(idx)*nbFrameM1)+(model2.weight(idx)*nbFrameM2))/(nbFrameM1+nbFrameM2);
00253 }
00254 }
00255
00256
00257 void gaussianFusion(const DistribGD &g1,double w1,const DistribGD & g2,double w2, DistribGD &res,double &w)
00258 {
00259 unsigned long vectSize = g1.getVectSize();
00260 double a1=w1/(w1+w2);
00261 double a2=1.0-a1;
00262 for (unsigned long k=0;k<vectSize;k++)
00263 {
00264 double d=g1.getMean(k)-g2.getMean(k);
00265 res.setCov(a1*g1.getCov(k)+a2*g2.getCov(k)+a1*a2*d*d,k);
00266 res.setMean((a1*g1.getMean(k))+(a2*g2.getMean(k)),k);
00267 }
00268 res.computeAll();
00269 w=w1+w2;
00270 }
00271 void mixtureFusion(const MixtureGD &mixt,DistribGD &res,double &wres){
00272 unsigned long distribCount = mixt.getDistribCount();
00273
00274 DistribGD& tmp=mixt.getDistrib(0);
00275 double wtmp=mixt.weight(0);
00276
00277 res=tmp;
00278 wres=wtmp;
00279 for (unsigned long i=1;i<distribCount;i++){
00280 gaussianFusion(mixt.getDistrib(i),mixt.weight(i),res,wtmp,res,wres);
00281 wtmp=wres;
00282 }
00283 }
00284
00285
00286
00287 void normalizeMixture(MixtureGD &mixt,const DoubleVector &meanSignal,const DoubleVector &covSignal,bool zeroOne,
00288 unsigned long nbIt, bool meanOnly,Config &config){
00289 unsigned long vectSize = mixt.getVectSize();
00290 unsigned long distribCount = mixt.getDistribCount();
00291 double wtmp;
00292 MixtureServer ms(config);
00293 DistribGD &tmp=ms.createDistribGD();
00294 for (unsigned long it=0;it<nbIt;it++){
00295 mixtureFusion(mixt,tmp,wtmp);
00296 for (unsigned long c=0;c<distribCount;c++){
00297 DistribGD &d=mixt.getDistrib(c);
00298 for (unsigned long i=0;i<vectSize;i++){
00299 double newMean=d.getMean(i)-tmp.getMean(i);
00300 newMean/=sqrt(tmp.getCov(i));
00301 if (!zeroOne) {
00302 newMean*=covSignal[i];
00303 newMean+=meanSignal[i];
00304 }
00305 d.setMean(newMean,i);
00306 if (!meanOnly){
00307 double newCov=d.getCov(i)/tmp.getCov(i);
00308 if (zeroOne==false) newCov*=covSignal[i];
00309 d.setCov(newCov,i);
00310 }
00311 }
00312 d.computeAll();
00313 }
00314 }
00315 }
00316 void normalizeMixture(MixtureGD &mixt,const DoubleVector &meanSignal,const DoubleVector &covSignal,bool zeroOne,TrainCfg &trainCfg,Config & config){
00317 normalizeMixture(mixt,meanSignal,covSignal,zeroOne,trainCfg.getNormalizeModelNbIt(),trainCfg.getNormalizeModelMeanOnly(),config);
00318 }
00319 void normalizeMixture(MixtureGD &mixt,const DoubleVector &meanSignal,const DoubleVector covSignal,bool zeroOne,Config &config){
00320 TrainCfg trainCfg(config);
00321 normalizeMixture(mixt,meanSignal,covSignal,zeroOne,trainCfg,config);
00322 }
00323
00324
00325 void normalizeMixture(MixtureGD &mixt,TrainCfg &trainCfg,Config &config){
00326 DoubleVector fake1,fake2;
00327 normalizeMixture(mixt,fake1,fake2,true,trainCfg,config);
00328 }
00329 void normalizeMixture(MixtureGD &mixt,Config &config){
00330 TrainCfg trainCfg(config);
00331 DoubleVector fake1,fake2;
00332 normalizeMixture(mixt,fake1,fake2,true,trainCfg,config);
00333 }
00334 void normalizeMixture(MixtureGD &mixt,MAPCfg &mapCfg,Config &config){
00335 DoubleVector fake1,fake2;
00336 normalizeMixture(mixt,fake1,fake2,true,mapCfg.getNormalizeModelNbIt(),mapCfg.getNormalizeModelMeanOnly(),config);
00337 }
00338 double likelihoodLoss(const DistribGD &g1,double w1,const DistribGD & g2,double w2)
00339 {
00340 unsigned long vectSize = g1.getVectSize();
00341 double a1=w1/(w1+w2);
00342 double a2=1.0-a1;
00343 double d1=1.0;
00344 double d2=1.0;
00345 for (unsigned long k=0;k<vectSize;k++){
00346 double d=g1.getMean(k)-g2.getMean(k);
00347 double var=a1*g1.getCov(k)+a2*g2.getCov(k)+a1*a2*d*d;
00348 d1*=var/g1.getCov(k);
00349 d2*=var/g2.getCov(k);
00350 }
00351 return (0.5*(w1*log(d1)+w2*log(d2)));
00352 }
00353
00354
00355
00356 void computeMAPConst(const MixtureGD& initModel, MixtureGD &client, MAPCfg &cfg)
00357 {
00358 unsigned long vectSize = initModel.getVectSize();
00359 unsigned long distribCount = initModel.getDistribCount();
00360 String Id = "tmp";
00361 Config config;
00362 MixtureServer ms(config);
00363 MixtureGD & tmp=ms.duplicateMixture(initModel,DUPL_DISTRIB);
00364 if (cfg.getMeanAdapt()){
00365 double alpha=cfg.getMeanAlpha();
00366 for ( unsigned long indC=0; indC < distribCount; indC++) {
00367 DistribGD& t = tmp.getDistrib(indC);
00368 DistribGD& c = client.getDistrib(indC);
00369 for (unsigned long coef=0;coef<vectSize;coef++){
00370 double res=(alpha*t.getMean(coef)) +((1-alpha)*c.getMean(coef));
00371 t.setMean(res, coef);
00372 }
00373 }
00374 }
00375 if (cfg.getVarAdapt()){
00376
00377
00378 }
00379 if (cfg.getWeightAdapt()){
00380
00381
00382 }
00383 client=tmp;
00384 }
00385
00386
00387
00388
00389
00390 void computeMAPConst2(const MixtureGD& initModel, MixtureGD &client,MAPCfg &cfg )
00391 {
00392 unsigned long vectSize = initModel.getVectSize();
00393 unsigned long distribCount = initModel.getDistribCount();
00394 String Id = "tmp";
00395 Config config;
00396 MixtureServer ms(config);
00397 MixtureGD & tmp=ms.duplicateMixture(initModel,DUPL_DISTRIB);
00398
00399
00400 if (cfg.getMeanAdapt()){
00401 double alpha=cfg.getMeanAlpha();
00402 for ( unsigned long indC=0; indC < distribCount; indC++) {
00403 DistribGD& t = tmp.getDistrib(indC);
00404 DistribGD& c = client.getDistrib(indC);
00405 for (unsigned long coef=0;coef<vectSize;coef++){
00406 double res=((alpha*tmp.weight(indC)*t.getMean(coef)) +
00407 ((1-alpha)*client.weight(indC)*c.getMean(coef)))/(tmp.weight(indC)*alpha+client.weight(indC)*(1-alpha));
00408 t.setMean(res, coef);
00409 }
00410 }
00411 }
00412 if (cfg.getVarAdapt()){
00413
00414
00415 }
00416 if (cfg.getWeightAdapt()){
00417
00418
00419 }
00420 client=tmp;
00421 }
00422
00423 void copyMean(MixtureGD & mixtS, MixtureGD & mixtD){
00424 for ( unsigned long indC=0; indC <mixtS.getDistribCount(); indC++) {
00425 DistribGD& s = mixtS.getDistrib(indC);
00426 DistribGD& d = mixtD.getDistrib(indC);
00427 for (unsigned long coef=0;coef<mixtS.getVectSize();coef++)
00428 d.setMean(s.getMean(coef), coef);;
00429 }
00430 mixtD.computeAll();
00431 }
00432 void copyVar(MixtureGD & mixtS, MixtureGD & mixtD){
00433 for ( unsigned long indC=0; indC <mixtS.getDistribCount(); indC++) {
00434 DistribGD& s = mixtS.getDistrib(indC);
00435 DistribGD& d = mixtD.getDistrib(indC);
00436 for (unsigned long coef=0;coef<mixtS.getVectSize();coef++)
00437 d.setCov(s.getCov(coef), coef);;
00438 }
00439 mixtD.computeAll();
00440 }
00441 void copyWeight(MixtureGD & mixtS, MixtureGD & mixtD){
00442 for ( unsigned long indC=0; indC <mixtS.getDistribCount(); indC++)
00443 mixtD.weight(indC)=mixtS.weight(indC);
00444 }
00445
00446
00447 void computeMAPOccDep(const MixtureGD& initModel, MixtureGD &client, MAPCfg &cfg,double frameCount)
00448 {
00449 unsigned long vectSize = initModel.getVectSize();
00450 unsigned long distribCount = initModel.getDistribCount();
00451 if (verbose) cfg.showConfig();
00452
00453 Config config;
00454 MixtureServer ms(config);
00455 MixtureGD & tmp=ms.duplicateMixture(initModel,DUPL_DISTRIB);
00456
00457 if ((cfg.getMeanAdapt()) || (cfg.getVarAdapt()))
00458 for ( unsigned long indC=0; indC < distribCount; indC++) {
00459 DistribGD& t = tmp.getDistrib(indC);
00460 DistribGD& c = client.getDistrib(indC);
00461 DistribGD& w = initModel.getDistrib(indC);
00462 double alpha= (client.weight(indC)*frameCount);
00463 if (cfg.getMeanAdapt()){
00464 double alphaMean=alpha/(alpha+cfg.getMeanReg());
00465 for (unsigned long coef=0;coef<vectSize;coef++){
00466 double res=((1-alphaMean)*w.getMean(coef)) +(alphaMean*c.getMean(coef));
00467 t.setMean(res, coef);
00468 }
00469 }
00470 if (cfg.getVarAdapt()){
00471 double alphaVar=alpha/(alpha+cfg.getVarReg());
00472 for (unsigned long coef=0;coef<vectSize;coef++){
00473 double res=((1-alphaVar)*w.getCov(coef)) +(alphaVar*c.getCov(coef))
00474 +(((1-alphaVar)*alphaVar)*pow(w.getMean(coef)-c.getMean(coef),2));
00475 t.setCov(res, coef);
00476 }
00477 }
00478 }
00479 if (cfg.getWeightAdapt()){
00480 double sumWeight=0.0;
00481 for ( unsigned long indC=0; indC < distribCount; indC++) {
00482 double alpha= (client.weight(indC)*frameCount);
00483 double alphaWeight=alpha/(alpha+cfg.getWeightReg());
00484 tmp.weight(indC)=alphaWeight*client.weight(indC)+((1-alphaWeight)*initModel.weight(indC));
00485 sumWeight+=tmp.weight(indC);
00486 }
00487 for ( unsigned long indC=0; indC < distribCount; indC++) tmp.weight(indC)/=sumWeight;
00488 }
00489 tmp.computeAll();
00490 client=tmp;
00491 }
00492
00493 void computeModelBasedMAPOccDep(const MixtureGD& initModel, MixtureGD &client, MAPCfg &cfg,double frameCount)
00494 {
00495 unsigned long vectSize = initModel.getVectSize();
00496 unsigned long distribCount = initModel.getDistribCount();
00497 if (verbose) {
00498 cout << "modelBased MAP Adaptation"<<endl;
00499 cfg.showConfig();
00500 }
00501
00502 Config config;
00503 MixtureServer ms(config);
00504 MixtureGD & tmp=ms.duplicateMixture(initModel,DUPL_DISTRIB);
00505
00506
00507 if ((cfg.getMeanAdapt()) || (cfg.getVarAdapt()))
00508 for ( unsigned long indC=0; indC < distribCount; indC++) {
00509 DistribGD& t = tmp.getDistrib(indC);
00510 DistribGD& c = client.getDistrib(indC);
00511 DistribGD& w = initModel.getDistrib(indC);
00512 double alpha= (client.weight(indC)*frameCount);
00513 if (cfg.getMeanAdapt()){
00514 double alphaMean=alpha/(alpha+cfg.getMeanReg());
00515 for (unsigned long coef=0;coef<vectSize;coef++){
00516 double res=((1-alphaMean)*w.getMean(coef)) +(alphaMean*c.getMean(coef));
00517 t.setMean(res, coef);
00518 }
00519 }
00520 if (cfg.getVarAdapt()){
00521 double alphaVar=alpha/(alpha+cfg.getVarReg());
00522 for (unsigned long coef=0;coef<vectSize;coef++){
00523 double res=((1-alphaVar)*w.getCov(coef)) +(alphaVar*c.getCov(coef))
00524 +(((1-alphaVar)*alphaVar)*pow(w.getMean(coef)-c.getMean(coef),2));
00525 t.setCov(res, coef);
00526 }
00527 }
00528 }
00529 if (cfg.getWeightAdapt()){
00530 double sumWeight=0.0;
00531 for ( unsigned long indC=0; indC < distribCount; indC++) {
00532 double alpha= (client.weight(indC)*frameCount);
00533 double alphaWeight=alpha/(alpha+cfg.getWeightReg());
00534 tmp.weight(indC)=alphaWeight*client.weight(indC)+((1-alphaWeight)*initModel.weight(indC));
00535 sumWeight+=tmp.weight(indC);
00536 }
00537 for ( unsigned long indC=0; indC < distribCount; indC++) tmp.weight(indC)/=sumWeight;
00538 }
00539 tmp.computeAll();
00540 client=tmp;
00541 }
00542
00543 void computeMAP(MixtureServer &ms,const MixtureGD& initModel,MixtureGD &client,unsigned long frameCount,Config &config)
00544 {
00545 MAPCfg cfg(config);
00546 computeMAP(ms,initModel,client,frameCount,cfg);
00547 }
00548 void computeMAP(MixtureServer &ms,const MixtureGD& initModel,MixtureGD &client,unsigned long frameCount,MAPCfg &cfg)
00549 {
00550 if (debug) cfg.showConfig(cout);
00551 if (cfg.getMethod()=="MAPConst")
00552 computeMAPConst(initModel,client,cfg);
00553 else if (cfg.getMethod()=="MAPOccDep")
00554 computeMAPOccDep(initModel,client,cfg,frameCount);
00555 else if (cfg.getMethod()=="MAPConst2")
00556 computeMAPConst2(initModel,client,cfg);
00557 else if (cfg.getMethod()=="MAPModelBased")
00558 computeModelBasedMAPOccDep(initModel,client,cfg,frameCount);
00559 else cerr <<"mapAlgo["<<cfg.getMethod()<<"] unknown, No adaptation will be perform"<<endl;
00560 }
00561
00562 double setItParameter(double begin, double end, int nbIt, int it){
00563 if (nbIt<2) return begin;
00564 double itVal=((begin-end)/((double)nbIt-1));
00565 return begin-(itVal*it);
00566 }
00567
00568
00569 void varianceControl(MixtureGD& model,double flooring,double ceiling,const DoubleVector &covSignal)
00570 {
00571 unsigned long vectSize = model.getVectSize();
00572 unsigned long distribCount = model.getDistribCount();
00573 unsigned long cptFlooring = 0;
00574 unsigned long cptCeiling = 0;
00575
00576 for (unsigned long c=0; c<distribCount; c++){
00577 DistribGD& d = model.getDistrib(c);
00578 for (unsigned long v=0; v<vectSize; v++){
00579 double cov = d.getCov(v);
00580 if (cov <= (flooring*covSignal[v])) { cov = flooring*covSignal[v]; cptFlooring++; }
00581 if (cov >= (ceiling*covSignal[v])) { cov = ceiling*covSignal[v]; cptCeiling++; }
00582 d.setCov(cov, v);
00583 }
00584 d.computeAll();
00585 }
00586 if (verbose) cout << " total variance flooring = " << cptFlooring
00587 << " ceiling = " << cptCeiling << endl;
00588
00589 }
00590
00591
00592
00593
00594
00595 unsigned long computeMeanCov(Config &config,FeatureServer **fsTab,SegCluster ** segTab,unsigned long nbStream,DoubleVector &mean,DoubleVector &cov){
00596 initialize01(fsTab[0]->getVectSize(),mean,cov);
00597 FrameAccGD frameAcc;
00598 for (unsigned long stream=0;stream<nbStream;stream++)
00599 accumulateStatFrame(frameAcc,*fsTab[stream],*segTab[stream],config);
00600 mean=frameAcc.getMeanVect();
00601 cov=frameAcc.getCovVect();
00602 unsigned long nbFrame= frameAcc.getCount();
00603 return nbFrame;
00604 }
00605 unsigned long computeMeanCov(Config &config,FeatureServer &fs,SegCluster &seg,DoubleVector &mean,DoubleVector &cov){
00606 initialize01(fs.getVectSize(),mean,cov);
00607 FrameAccGD frameAcc;
00608 accumulateStatFrame(frameAcc,fs,seg,config);
00609 mean=frameAcc.getMeanVect();
00610 cov=frameAcc.getCovVect();
00611 unsigned long nbFrame= frameAcc.getCount();
00612 return nbFrame;
00613 }
00614 void initialize01(unsigned long vectSize,DoubleVector &mean,DoubleVector &cov){
00615 for(unsigned long i=0;i<vectSize;i++){
00616 mean.addValue(0);
00617 cov.addValue(1);
00618 }
00619 }
00620
00621 MixtureGD &mixtureInit(MixtureServer &ms,FeatureServer &fs,MixtureGD &world,
00622 SegCluster &selectedSegments,const DoubleVector &globalCov, Config& config,TrainCfg & trainCfg)
00623 {
00624 unsigned long vectSize = world.getVectSize();
00625 unsigned long minimumLength=3;
00626 unsigned long maximumLength=7;
00627 if (config.existsParam("baggedMinimalLength")) minimumLength=config.getParam("baggedMinimalLength").toLong();
00628 if (config.existsParam("baggedMaximalLength")) maximumLength=config.getParam("baggedMaximalLength").toLong();
00629 if (verbose) cout <<"baggedMinimalLength["<< minimumLength<<"] MaximalLength["<<maximumLength<<"]"<<endl;
00630 unsigned long distribCount = world.getDistribCount();
00631 FrameAcc** frameAcc=new FrameAcc* [distribCount];
00632 for (unsigned long indg=0;indg<distribCount;indg++) {
00633 frameAcc[indg]= new FrameAccGD();
00634 frameAcc[indg]->reset();
00635 } ;
00636 double baggedProba=trainCfg.getBaggedFrameProbabilityInit()/distribCount;
00637 unsigned long nbBaggedIt=1;
00638 if (baggedProba>1){
00639 nbBaggedIt=(unsigned long) baggedProba+1;
00640 baggedProba/=nbBaggedIt;
00641 }
00642 for (unsigned long baggedIt=0;baggedIt<nbBaggedIt;baggedIt++){
00643 for(unsigned long indg=0;indg<distribCount;indg++){
00644 SegServer segServer;
00645 SegCluster & baggedFramesCluster=segServer.createCluster(1,"","");
00646 srand((indg+1)*(baggedIt+1));
00647 baggedSegments(selectedSegments,baggedFramesCluster,
00648 baggedProba,minimumLength,maximumLength);
00649 accumulateStatFrame(*frameAcc[indg],fs,baggedFramesCluster,config);
00650 }
00651 }
00652 for (unsigned long indg=0;indg<distribCount;indg++) {
00653 DistribGD& d = world.getDistrib(indg);
00654 DoubleVector mean=frameAcc[indg]->getMeanVect();
00655 for (unsigned long c=0;c<vectSize;c++){
00656 d.setCov(globalCov[c], c);
00657 d.setMean(mean[c], c);
00658 }
00659 d.computeAll();
00660 }
00661 world.equalizeWeights();
00662 if (verbose){
00663 cout << "Initialize model"<<endl;
00664 for (unsigned long indg=0;indg<distribCount;indg++)
00665 cout <<"Nb Frame for mean["<<indg<<"] init =["<<frameAcc[indg]->getCount()<<"]"<<endl;
00666 }
00667 for (unsigned long indg=0;indg<distribCount;indg++)
00668 delete frameAcc[indg];
00669 delete [] frameAcc;
00670 return world;
00671 }
00672
00673
00674
00675
00676 MixtureGD &mixtureInit(MixtureServer &ms,FeatureServer **fsTab,SegCluster **segTab,double *weightTab,unsigned long nbStream,MixtureGD &world,
00677 const DoubleVector &globalCov, Config& config,TrainCfg & trainCfg)
00678 {
00679 if (debug|| verbose) cout << "begin of mixtureInit"<<endl;
00680 unsigned long minimumLength=3;
00681 unsigned long maximumLength=7;
00682 if (config.existsParam("baggedMinimalLength")) minimumLength=config.getParam("baggedMinimalLength").toLong();
00683 if (config.existsParam("baggedMaximalLength")) maximumLength=config.getParam("baggedMaximalLength").toLong();
00684 if (verbose) cout <<"baggedMinimalLength["<< minimumLength<<"] MaximalLength["<<maximumLength<<"]"<<endl;
00685 unsigned long vectSize = world.getVectSize();
00686 unsigned long distribCount = world.getDistribCount();
00687 FrameAcc** frameAcc=new FrameAcc* [distribCount];
00688 for (unsigned long indg=0;indg<distribCount;indg++) {
00689 frameAcc[indg]= new FrameAccGD();
00690 frameAcc[indg]->reset();
00691 }
00692 unsigned long nbTotalFrame=0;
00693 for (unsigned long stream=0;stream<nbStream;stream++)
00694 nbTotalFrame+=(unsigned long) ((double)totalFrame(*segTab[stream])*weightTab[stream]);
00695 double nbFrameToSelect=50;
00696 if (config.existsParam("nbFrameToSelect")) nbFrameToSelect=config.getParam("nbFrameToSelect").toLong();
00697
00698 if (verbose) cout <<"mixtureInit, total frames with stream weighting["<<nbTotalFrame<<"] total frame to select by component["<<nbFrameToSelect<<"]"<<endl;
00699
00700 DoubleVector baggedProbaA(nbStream,nbStream);
00701 ULongVector baggedItA(nbStream,nbStream);
00702 for (unsigned long stream=0;stream<nbStream;stream++){
00703 baggedProbaA[stream]=(nbFrameToSelect*weightTab[stream])/(double)totalFrame(*segTab[stream]);
00704 baggedItA[stream]=1;
00705 double baggedTmp=baggedProbaA[stream];
00706 while (baggedTmp>1){
00707 baggedItA[stream]++;
00708 baggedTmp/=baggedProbaA[stream]/baggedItA[stream];
00709 }
00710 baggedProbaA[stream]=baggedTmp;
00711 }
00712 if (debug || verbose) cout << "bagged proba init ok"<<endl;
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732 for (unsigned long stream=0;stream<nbStream;stream++){
00733 for (unsigned long baggedIt=0;baggedIt<baggedItA[stream];baggedIt++){
00734 if (debug || verbose) cout <<"Init Stream["<<stream<<"] Bagged by comp="<<baggedProbaA[stream]<<" It["<<baggedIt<<"]"<<endl;
00735
00736 SegServer segServer;
00737 SegCluster & baggedSeg=segServer.createCluster(1,"","");
00738
00739 srand(((stream+1)*100)+(baggedIt+1));
00740 baggedSegments(*segTab[stream],baggedSeg,distribCount,baggedProbaA[stream],minimumLength,maximumLength);
00741
00742 Seg* seg;
00743 baggedSeg.rewind();
00744 while((seg=baggedSeg.getSeg())!=NULL)
00745 accumulateStatFrame(*frameAcc[seg->labelCode()],*fsTab[stream],seg,config);
00746 }
00747 }
00748 for (unsigned long indg=0;indg<distribCount;indg++) {
00749 DistribGD& d = world.getDistrib(indg);
00750 DoubleVector mean=frameAcc[indg]->getMeanVect();
00751 for (unsigned long c=0;c<vectSize;c++){
00752 d.setCov(globalCov[c], c);
00753 d.setMean(mean[c], c);
00754 }
00755 d.computeAll();
00756 }
00757
00758 world.equalizeWeights();
00759 if (verbose){
00760 cout << "Initialize model"<<endl;
00761 for (unsigned long indg=0;indg<distribCount;indg++)
00762 cout <<"Nb Frame for mean["<<indg<<"] init =["<<frameAcc[indg]->getCount()<<"]"<<endl;
00763 }
00764 for (unsigned long indg=0;indg<distribCount;indg++)
00765 delete frameAcc[indg];
00766 delete [] frameAcc;
00767 return world;
00768 }
00769 MixtureGD &mixtureInit(MixtureServer &ms,FeatureServer &fs,MixtureGD &world,
00770 SegCluster &selectedSegments,const DoubleVector &globalCov, Config& config){
00771 TrainCfg trainCfg(config);
00772 return mixtureInit(ms,fs,world,selectedSegments,globalCov,config,trainCfg);
00773 }
00774 MixtureGD &mixtureInit(MixtureServer &ms,FeatureServer **fsTab, SegCluster **segTab,double *weightTab,unsigned long nbStream,MixtureGD &world,
00775 const DoubleVector &globalCov, Config& config){
00776 TrainCfg trainCfg(config);
00777 return mixtureInit(ms,fsTab,segTab,weightTab,nbStream,world,globalCov,config,trainCfg);
00778 }
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790 Matrix <double> computeMLLR (MixtureGD &inM,MixtureGD& outM,unsigned long frameCount, Config &config){
00791 StatServer ss(config);
00792
00793
00794 if(verbose)cout<<"MLLR begins"<<endl;
00795 unsigned long distribCount=inM.getDistribCount();
00796 unsigned long vectSize= inM.getVectSize();
00797 Matrix <double> W (vectSize, vectSize+1);
00798 Matrix <double> Z(vectSize, vectSize+1);
00799 W.setAllValues(0);
00800 Z.setAllValues(0);
00801
00802
00803 DoubleSquareMatrix **G;
00804 G=new DoubleSquareMatrix*[vectSize];
00805 for (unsigned i=0;i<vectSize;i++) {
00806 G[i]= new DoubleSquareMatrix();
00807 G[i]->setSize(vectSize+1);
00808 G[i]->setAllValues(0);
00809 }
00810 if(verboseLevel>=1 )cout<<"Initialization complete"<<endl<<"Begining the Statistic accumulation"<<endl;
00811
00812 for (unsigned long j=0; j<distribCount;j++){
00813 DistribGD & distri = inM.getDistrib(j);
00814 DistribGD & distriMl = outM.getDistrib(j);
00815 double occ=outM.weight(j)*frameCount;
00816 DoubleVector mean(vectSize+1,1);
00817 DoubleVector &cov=distri.getCovVect();
00818
00819
00820 mean[0]=1;
00821 mean.addValue(distri.getMeanVect());
00822
00823
00824 for (unsigned long p=0; p<vectSize;p++){
00825 double occFrame=distriMl.getMean(p)*occ;
00826 for (unsigned long q=0; q<vectSize+1;q++)
00827 Z(p,q)+=occFrame*mean[q]/cov[p];
00828 }
00829
00830 for (unsigned long p=0; p<vectSize;p++){
00831 for (unsigned long q=0; q<vectSize+1; q++){
00832 for (unsigned long r=0; r<vectSize+1; r++) (*G[p])(q,r) += occ*mean[q]*mean[r]/cov[p];
00833 }
00834 }
00835 }
00836
00837 if(verboseLevel >=1)cout<<"G and Z matrix calculated"<<endl;
00838
00839
00840 for (unsigned long l=0; l<vectSize;l++) {
00841 DoubleSquareMatrix G_inv(vectSize+1);
00842 (*G[l]).invert(G_inv);
00843 for (unsigned long c=0; c<vectSize+1;c++)
00844 for( unsigned long k=0;k<vectSize+1;k++)
00845 W(l,c) += G_inv(c,k)*Z(l,k);
00846
00847 }
00848 if(verboseLevel >= 1)cout<<"W complete"<<endl<<"Computing new means"<<endl;
00849
00850
00851 for (unsigned long j=0; j<distribCount;j++){
00852 DoubleVector meanOut(vectSize, vectSize);
00853 DistribGD & distri = inM.getDistrib(j);
00854 DoubleVector &mean= distri.getMeanVect();
00855 for (unsigned long i=0;i<vectSize;i++) meanOut[i]=W(i,0);
00856 for (unsigned long i=0;i<vectSize;i++) {
00857 for (unsigned long k=0;k<vectSize;k++) meanOut[i]+=W(i,k+1)*mean[k];
00858 }
00859 outM.getDistrib(j).setMeanVect(meanOut);
00860 }
00861
00862 if(verbose) cout<<"MLLR finished, New Means computed"<<endl;
00863
00864 copyVar(inM, outM);
00865 copyWeight(inM,outM);
00866
00867 return W;
00868 }
00869
00870
00871
00872
00873 void adaptModel(Config& config,StatServer &ss,MixtureServer &ms,FeatureServer &fs,SegCluster& selectedSegments,
00874 MixtureGD &aprioriModel,MixtureGD &clientMixture,MAPCfg &mapCfg){
00875 if (verbose) mapCfg.showConfig();
00876
00877 for (unsigned long trainIt=0; trainIt<mapCfg.getNbTrainIt(); trainIt++){
00878 MixtureStat &emAcc=ss.createAndStoreMixtureStat(clientMixture);
00879 SegServer segServer;
00880 SegCluster & baggedFramesCluster=segServer.createCluster(1,"","");
00881 baggedSegments(selectedSegments,baggedFramesCluster,mapCfg.getBaggedFrameProbability());
00882 emAcc.resetEM();
00883 srand(trainIt);
00884 double llkPreviousIt=accumulateStatEM(ss,fs,emAcc,baggedFramesCluster,config);
00885 clientMixture = emAcc.getEM();
00886 unsigned long frameCount=(unsigned long) emAcc.getEMFeatureCount();
00887 llkPreviousIt=llkPreviousIt/(double) frameCount;
00888 if (verbose)cout <<"ML (partial) estimate it["<<trainIt<<"] (take care, it corresponds to the previous it,0 means init likelihood) = "
00889 <<llkPreviousIt<<endl;
00890 if(mapCfg.getMethod()=="MLLR") {
00891
00892 Matrix <double> W=computeMLLR(aprioriModel,clientMixture,frameCount,config);
00893
00894 String MLLR_matrix = "MLLR_matrix.mat";
00895 W.save(MLLR_matrix, config);
00896 }
00897 else {
00898 computeMAP(ms,aprioriModel,clientMixture,frameCount,config);
00899 }
00900 if (mapCfg.getNormalizeModel()) normalizeMixture(clientMixture,mapCfg,config);
00901 ss.deleteMixtureStat(emAcc);
00902 if (verboseLevel>2) cout << "Likelihood on all frames= "<< meanLikelihood(ss,fs,clientMixture, selectedSegments,config) << endl;
00903 }
00904 if (verboseLevel>1) cout << "Final likelihood on all frames= "<< meanLikelihood(ss,fs,clientMixture, selectedSegments,config) << endl;
00905 if (debug) cout <<"adaptModel nb distrib:"<<ms.getDistribCount() <<"nb mixt:"<<ms.getMixtureCount()<<endl;
00906 }
00907
00908 void adaptModel(Config& config,StatServer &ss,MixtureServer &ms,FeatureServer &fs,SegCluster& selectedSegments,
00909 MixtureGD &aprioriModel,MixtureGD &clientMixture){
00910 MAPCfg mapCfg(config);
00911 adaptModel(config,ss,ms,fs,selectedSegments,aprioriModel,clientMixture,mapCfg);
00912 }
00913
00914
00915
00916
00917 void modelBasedadaptModel(Config& config,StatServer &ss,MixtureServer &ms,FeatureServer &fs,SegCluster& selectedSegments,
00918 MixtureGD &aprioriModel,MixtureGD &clientMixture, MixtureGD &initModel){
00919 MAPCfg mapCfg(config);
00920 if (verbose){
00921 cout <<"Model adaptation based on true EM/ML estimate of training data"<<endl;
00922 mapCfg.showConfig();
00923 }
00924 MixtureServer msTmp(config);
00925 MixtureGD & data=msTmp.duplicateMixture(initModel,DUPL_DISTRIB);
00926
00927 unsigned long totalFrameCount=totalFrame(selectedSegments);
00928
00929 for (unsigned long emIt=0; emIt<mapCfg.getNbEmIt(); emIt++){
00930 MixtureStat &emAcc=ss.createAndStoreMixtureStat(data);
00931 SegServer segServer;
00932 SegCluster & baggedFramesCluster=segServer.createCluster(1,"","");
00933 baggedSegments(selectedSegments,baggedFramesCluster,mapCfg.getBaggedFrameProbability());
00934 emAcc.resetEM();
00935 double llkPreviousIt=accumulateStatEM(ss,fs,emAcc,baggedFramesCluster,config);
00936 data = emAcc.getEM();
00937 unsigned long frameCount=(unsigned long) emAcc.getEMFeatureCount();
00938 llkPreviousIt=llkPreviousIt/(double) frameCount;
00939 if (verbose)cout <<"ML (partial) estimate it["<<emIt<<"] (take care, it corresponds to the previous it,0 means init likelihood) = "
00940 <<llkPreviousIt<<endl;
00941 ss.deleteMixtureStat(emAcc);
00942 }
00943 unsigned long modelNbComp =aprioriModel.getDistribCount();
00944
00945 unsigned long vectSize=fs.getVectSize();
00946 for (unsigned long idxModel=0;idxModel<modelNbComp;idxModel++){
00947 DistribGD &c=clientMixture.getDistrib(idxModel);
00948 for (unsigned long idxC=0;idxC<vectSize;idxC++){
00949 c.setMean(0.00,idxC);
00950 c.setCov(0.00,idxC);
00951 }
00952 }
00953 DoubleVector apProbaTot(modelNbComp,modelNbComp);
00954 apProbaTot.setAllValues(0.0);
00955 for (unsigned long idxData=0;idxData<data.getDistribCount();idxData++){
00956 if (debug) cout <<"Distrib Data["<<idxData<<"]"<<endl;
00957 DistribGD &d=data.getDistrib(idxData);
00958 double totLk=0.0;
00959 DoubleVector apProba(modelNbComp,modelNbComp);
00960 apProba.setAllValues(0.0);
00961 for (unsigned long idxModel=0;idxModel<modelNbComp;idxModel++){
00962 if (debug) cout <<"Distrib A Priori model["<<idxModel<<"]"<<endl;
00963 DistribGD &m=aprioriModel.getDistrib(idxModel);
00964 apProba[idxModel]=aprioriModel.weight(idxModel)*likelihoodGD(d,m);
00965 totLk+=apProba[idxModel];
00966 }
00967 for (unsigned long idxModel=0;idxModel<modelNbComp;idxModel++){
00968 DistribGD &c=clientMixture.getDistrib(idxModel);
00969 apProba[idxModel]/=totLk;
00970 for (unsigned long idxC=0;idxC<vectSize;idxC++){
00971 c.setMean(c.getMean(idxC)+(d.getMean(idxC)*apProba[idxModel]*data.weight(idxData)),idxC);
00972 c.setCov(c.getCov(idxC)+((d.getMean(idxC)*d.getMean(idxC))*apProba[idxModel]*data.weight(idxData)),idxC);
00973 }
00974 apProbaTot[idxModel]+=apProba[idxModel]*data.weight(idxData);
00975 }
00976 }
00977 for (unsigned long idxModel=0;idxModel<modelNbComp;idxModel++){
00978 DistribGD &c=clientMixture.getDistrib(idxModel);
00979 for (unsigned long idxC=0;idxC<vectSize;idxC++){
00980 c.setMean(c.getMean(idxC)/apProbaTot[idxModel],idxC);
00981
00982 }
00983 }
00984 for (unsigned long idxModel=0;idxModel<modelNbComp;idxModel++){
00985 DistribGD &c=clientMixture.getDistrib(idxModel);
00986 c.computeAll();
00987 }
00988
00989 computeMAP(ms,aprioriModel,clientMixture,totalFrameCount,config);
00990 if (mapCfg.getNormalizeModel()) normalizeMixture(clientMixture,mapCfg,config);
00991 if (verboseLevel>2) cout << "Likelihood on all frames= "<< meanLikelihood(ss,fs,clientMixture, selectedSegments,config) << endl;
00992 if (verboseLevel>1) cout << "Final likelihood on all frames= "<< meanLikelihood(ss,fs,clientMixture, selectedSegments,config) << endl; if (debug) cout <<"adaptModel nb distrib:"<<ms.getDistribCount() <<"nb mixt:"<<ms.getMixtureCount()<<endl;
00993 }
00994
00995 void trainModel(Config& config,StatServer &ss,FeatureServer &fs,SegCluster& selectedSegments,
00996 DoubleVector & globalMean,DoubleVector &globalCov,MixtureGD &world,TrainCfg &trainCfg){
00997 float varianceFlooring = (float)trainCfg.getInitVarFloor();
00998 float varianceCeiling = (float)trainCfg.getInitVarCeil();
00999 if (verbose) trainCfg.showConfig();
01000 if (verboseLevel>1) cout << "Train It : initial model: ll world = " << meanLikelihood(ss,fs,world,selectedSegments,config)<< endl;
01001 try{
01002 for (unsigned long trainIt=0; trainIt<trainCfg.getNbTrainIt(); trainIt++){
01003 MixtureStat& emAcc=ss.createAndStoreMixtureStat(world);
01004
01005 varianceFlooring = (float)setItParameter(trainCfg.getInitVarFloor(),trainCfg.getFinalVarFloor(),trainCfg.getNbTrainIt(),trainIt);
01006 varianceCeiling = (float)setItParameter(trainCfg.getInitVarCeil(),trainCfg.getFinalVarCeil(), trainCfg.getNbTrainIt(), trainIt);
01007 if (verbose) cout << "Train it["<<trainIt<<"], Variance floor["<<varianceFlooring
01008 <<"] ceiling["<<varianceCeiling<<"]"<<endl;
01009 SegServer segServer;
01010 SegCluster & baggedFramesCluster=segServer.createCluster(1,"","");
01011 srand(trainIt);
01012 baggedSegments(selectedSegments,baggedFramesCluster,trainCfg.getBaggedFrameProbability());
01013 emAcc.resetEM();
01014 double llkPreviousIt=accumulateStatEM(ss,fs,emAcc,baggedFramesCluster,config);
01015 llkPreviousIt=llkPreviousIt/(double) emAcc.getEMFeatureCount();
01016 world = emAcc.getEM();
01017 varianceControl(world,varianceFlooring,varianceCeiling,globalCov);
01018 if (trainCfg.getNormalizeModel()) normalizeMixture(world,config);
01019 if (verbose) cout << "Partial Train it["<<trainIt<<"] (take care, it corresponds to the previous it,0 means init likelihood) ="<<llkPreviousIt<<" Nb Frames="<<emAcc.getEMFeatureCount()<<endl;
01020 if (verboseLevel>2) cout << "Train it["<<trainIt <<"] Complete LLK=[" << meanLikelihood(ss,fs,world,selectedSegments,config)<< "]"<<endl;
01021 ss.deleteMixtureStat(emAcc);
01022 }
01023 if (verboseLevel>1) cout << " Final ll world = " << meanLikelihood(ss,fs,world,selectedSegments,config)<< endl;
01024 }
01025 catch (Exception& e){
01026 cout << e.toString() << endl;
01027 }
01028 }
01029
01030
01031
01032 void trainModelStream(Config& config,MixtureServer &ms,StatServer &ss,FeatureServer **fsTab,SegCluster** segTab,
01033 double *weightTab,unsigned long nbStream,
01034 DoubleVector & globalMean,DoubleVector &globalCov,MixtureGD* &world,TrainCfg &trainCfg){
01035 unsigned long initialDistribCount=world->getDistribCount();
01036 double varianceFlooring = trainCfg.getInitVarFloor();
01037 double varianceCeiling = trainCfg.getInitVarCeil();
01038 unsigned long initRand=0;
01039 if (config.existsParam("initRand")) initRand=config.getParam("initRand").toLong();
01040 unsigned long minimumLength=3;
01041 unsigned long maximumLength=7;
01042 if (config.existsParam("baggedMinimalLength")) minimumLength=config.getParam("baggedMinimalLength").toLong();
01043 if (config.existsParam("baggedMaximalLength")) maximumLength=config.getParam("baggedMaximalLength").toLong();
01044 if (verbose) trainCfg.showConfig();
01045 if (verbose) cout <<"baggedMinimalLength["<< minimumLength<<"] MaximalLength["<<maximumLength<<"]"<<endl;
01046 if (verboseLevel>1) cout <<"LLK Initial model= " << meanLikelihood(ss,fsTab,segTab,nbStream,*world,config)<< endl;
01047 try{
01048 for (unsigned long trainIt=0; trainIt<trainCfg.getNbTrainIt(); trainIt++){
01049 MixtureStat& emAcc=ss.createAndStoreMixtureStat(*world);
01050
01051 varianceFlooring = setItParameter(trainCfg.getInitVarFloor(),trainCfg.getFinalVarFloor(),trainCfg.getNbTrainIt(),trainIt);
01052 varianceCeiling = setItParameter(trainCfg.getInitVarCeil(),trainCfg.getFinalVarCeil(), trainCfg.getNbTrainIt(), trainIt);
01053 if (verbose) cout << "Train it["<<trainIt<<"], Variance floor["<<varianceFlooring
01054 <<"] ceiling["<<varianceCeiling<<"]"<<endl;
01055 unsigned long nbTotalFrame=0;
01056 for (unsigned long stream=0;stream<nbStream;stream++) nbTotalFrame+=(unsigned long) ((double)totalFrame(*segTab[stream])*weightTab[stream]);
01057 double nbFrameToSelect=trainCfg.getBaggedFrameProbability()*nbTotalFrame;
01058 if (verbose) cout <<"total frames with stream weighting["<<nbTotalFrame<<"] total frame to select ["<<(unsigned long) nbFrameToSelect<<"]"<<endl;
01059 emAcc.resetEM();
01060 double llkPreviousIt=0;
01061 for(unsigned long stream=0;stream<nbStream;stream++){
01062 unsigned long nbBaggedIt=1;
01063 double baggedProba=(nbFrameToSelect*weightTab[stream])/(double)totalFrame(*segTab[stream]);
01064 if (baggedProba>1){
01065 nbBaggedIt=(unsigned long) baggedProba+1;
01066 baggedProba/=nbBaggedIt;
01067 }
01068 for (unsigned long baggedIt=0;baggedIt<nbBaggedIt;baggedIt++){
01069 if (debug || verboseLevel) cout <<"Stream["<<stream<<"] Bagged="<<baggedProba<<endl;
01070 SegServer segServer;
01071 SegCluster & baggedFramesCluster=segServer.createCluster(1,"","");
01072 srand(((trainIt+1+initRand)*200)+(((stream+1)*20)+(baggedIt+1)));
01073 baggedSegments(*segTab[stream],baggedFramesCluster,baggedProba,minimumLength,maximumLength);
01074 llkPreviousIt+=accumulateStatEM(ss,*fsTab[stream],emAcc,baggedFramesCluster,config);
01075 }
01076 }
01077 llkPreviousIt=llkPreviousIt/(double) emAcc.getEMFeatureCount();
01078 (*world) = emAcc.getEM();
01079 varianceControl(*world,varianceFlooring,varianceCeiling,globalCov);
01080 if (trainCfg.getComponentReduction()){
01081
01082
01083 bool * selectCompA = new bool[world->getDistribCount()];
01084
01085 double diff=(initialDistribCount-trainCfg.getTargetDistribCount())/(double) trainCfg.getNbTrainIt();
01086 unsigned long nbTop=initialDistribCount-(unsigned long)((double)(trainIt+1)*diff);
01087 if (trainIt== (trainCfg.getNbTrainIt()-1)) nbTop=trainCfg.getTargetDistribCount();
01088 if (nbTop<world->getDistribCount())
01089 {
01090 if (verbose) cout <<"target number of d:"<<nbTop<<endl;
01091 unsigned long nbOutputDistrib=selectComponent(selectCompA,nbTop,*world);
01092 if (verbose) cout <<" suppress distrib nb initial:"<<world->getDistribCount()<<" nb final :"<<nbOutputDistrib;
01093 MixtureGD &outputM=ms.createMixtureGD(nbOutputDistrib);
01094 double totWeights=reduceModel(selectCompA,*world,outputM);
01095 if (verbose) cout <<" Total weights["<<totWeights<<"] normalized to 1"<<endl;
01096 normalizeWeights(outputM);
01097 world=&outputM;
01098 }
01099 }
01100 if (trainCfg.getNormalizeModel()) normalizeMixture(*world,trainCfg,config);
01101
01102 if (verbose) cout << "Partial Train it["<<trainIt<<"] (take care, it corresponds to the previous it,0 means init likelihood) ="<<llkPreviousIt<<" Nb Frames="<<(unsigned long) emAcc.getEMFeatureCount()<<endl;
01103 if (verboseLevel>2) cout << "Train it["<<trainIt <<"] Complete LLK=[" << meanLikelihood(ss,fsTab,segTab,nbStream,*world,config)<< "]"<<endl;
01104 ss.deleteMixtureStat(emAcc);
01105
01106 }
01107 if (verboseLevel>1) cout << " Final ll world = " << meanLikelihood(ss,fsTab,segTab,nbStream,*world,config)<< endl;
01108 }
01109 catch (Exception& e){
01110 cout << e.toString() << endl;
01111 }
01112 }
01113 void trainModel(Config& config,StatServer &ss,FeatureServer &fs,SegCluster& selectedSegments,
01114 DoubleVector & globalMean,DoubleVector &globalCov,MixtureGD &world){
01115 TrainCfg trainCfg(config);
01116 trainModel(config,ss,fs,selectedSegments,globalMean,globalCov,world,trainCfg) ;
01117 }
01118 void trainModel(Config& config,MixtureServer &ms,StatServer &ss,FeatureServer **fsTab,SegCluster** segTab,double *weightTab,unsigned long nbStream,
01119 DoubleVector & globalMean,DoubleVector &globalCov,MixtureGD* &world){
01120 TrainCfg trainCfg(config);
01121 if (verbose) trainCfg.showConfig();
01122 trainModelStream(config,ms,ss,fsTab,segTab,weightTab,nbStream,globalMean,globalCov,world,trainCfg);
01123 }
01124 #endif //!defined(ALIZE_TrainTools_cpp)