Go to the documentation of this file.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 #include <iostream>
00056 #include "liatools.h"
00057
00058 void getMeanNorm(RealVector <double>& norm,MixtureGD & UBM) {
00059 unsigned long mSize=UBM.getDistribCount();
00060 unsigned long vSize=UBM.getVectSize();
00061 norm.setSize(mSize*vSize);
00062 for (unsigned long i=0;i<mSize;i++) {
00063 DistribGD & d=UBM.getDistrib(i);
00064 double w=UBM.weight(i);
00065 for (unsigned long j=0;j<vSize;j++)
00066 norm[i*vSize+j]=sqrt(w*d.getCovInv(j));
00067 }
00068 }
00069
00070 void getWeightNorm(RealVector <double>& norm,MixtureGD & UBM) {
00071 unsigned long mSize=UBM.getDistribCount();
00072 norm.setSize(mSize);
00073 for (unsigned long i=0;i<mSize;i++)
00074 norm[i]=1/sqrt(UBM.weight(i));
00075 }
00076
00077 int main(int argc, char* argv[]){
00078
00079 ConfigChecker cc;
00080 cc.addStringParam("config", false, true, "default config filename");
00081 cc.addBooleanParam("meanSv", true, true, "output Mean vector");
00082 cc.addBooleanParam("weightSv", true, true, "output Weight vector");
00083 cc.addBooleanParam("normSv", true, true, "normalize vector (need UBM)");
00084 cc.addBooleanParam("vectors", false, true, "read from vectors");
00085 try{
00086 CmdLine cmdLine(argc, argv);
00087 Config tmp;
00088 cmdLine.copyIntoConfig(tmp);
00089 Config config;
00090 if (tmp.existsParam("config")) config.load(tmp.getParam("config"));
00091 cmdLine.copyIntoConfig(config);
00092 cc.check(config);
00093 debug=config.getParam_debug();
00094 if (config.existsParam("verbose"))verbose=config.getParam("verbose").toBool();else verbose=false;
00095 if (verbose) verboseLevel=1;else verboseLevel=0;
00096 if (config.existsParam("verboseLevel"))verboseLevel=config.getParam("verboseLevel").toLong();
00097 if (verboseLevel>0) verbose=true;
00098
00099
00100 bool mean=config.getParam("meanSv").toBool();
00101 bool weight=config.getParam("weightSv").toBool();
00102 if (!(weight^mean)) throw Exception("weightSv and meanSv have same value !",__FILE__,__LINE__);
00103 bool norm=config.getParam("normSv").toBool();
00104 bool vectors=false;
00105 if (config.existsParam("vectors")) vectors=true;
00106 bool gmm=!vectors;
00107 if (verbose) {
00108 if (mean) cout << "(modelToSv) MeanSv"<<endl;
00109 if (weight) cout << "(modelToSv) WeightSv"<<endl;
00110 if (norm) cout << "(modelToSv) NormSv: Normalizing vector with UBM model "<<endl;
00111 if (norm && !(weight||mean)) cout << "(modelToSv) NormSv: only normalize vectors" << endl;
00112 }
00113 XList inputList(config.getParam("inputFilename"));
00114 String vPath=config.getParam("vectorFilesPath");
00115 String vExt=config.getParam("vectorFilesExtension");
00116 MixtureServer ms(config);
00117 MixtureGD UBM=ms.loadMixtureGD(config.getParam("inputWorldFilename"));
00118 RealVector <double> normVector;
00119 if (norm) {
00120 if (verbose) cout << "(modelToSv) Compute normalisation vector"<<endl;
00121 if(mean) getMeanNorm(normVector,UBM);
00122 if (weight) getWeightNorm(normVector,UBM);
00123 String normFile=vPath+"norm"+vExt;
00124 if (debug ) {
00125 ((Matrix<double>)normVector).save(normFile,config);
00126 cout<<"(modelToSv) Saving normalisation vector to ["<<normFile<<"]"<<endl;
00127 }
00128 }
00129
00130 for (unsigned long r=0;r<inputList.getLineCount();r++) {
00131 String out=vPath+inputList.getLine(r).getElement(0)+vExt;
00132 if (verbose) cout << "(modelToSv) Processing ["<<inputList.getLine(r).getElement(0)<<"] to ["<<out<<"]"<<endl;
00133 RealVector <double> sv;
00134 if (gmm) {
00135 if (verbose) cout << "(modelToSv) Read from GMM models"<<endl;
00136 MixtureGD &curr=ms.loadMixtureGD(inputList.getLine(r).getElement(0));
00137 if (mean) modelToSv(curr,sv);
00138 if (weight) {
00139 unsigned long mSize=curr.getDistribCount();
00140 sv.setSize(mSize);
00141 for (unsigned long i=0;i<mSize;i++)
00142 sv[i]=curr.weight(i);
00143 }
00144 ms.deleteMixture(curr);
00145 ms.deleteUnusedDistribs();
00146 }
00147 else if (vectors) {
00148 if (verbose) cout << "(modelToSv) Read from SV from vectors"<<endl;
00149 String vIExt=config.getParam("inputVectorFilesExtension");
00150 String in=vPath+inputList.getLine(r).getElement(0)+vIExt;
00151 Matrix <double> a;
00152 a.load(in,config);
00153 sv.setSize(a.cols());
00154 for (unsigned long i=0;i<sv.size();i++)
00155 sv[i]=a(0,i);
00156 }
00157 if(norm) {
00158 for (unsigned long i=0;i<sv.size();i++)
00159 sv[i]*=normVector[i];
00160 }
00161 ((Matrix<double>)sv).save(out,config);
00162 }
00163 return 0;
00164 }
00165 catch(alize::Exception & e){ cout <<"modelToSv"<< e.toString() << endl << cc.getParamList()<<endl;}
00166 }