CovIntra.cpp

Go to the documentation of this file.
00001 /*
00002 This file is part of LIA_RAL which is a set of software based on ALIZE
00003 toolkit for speaker recognition. ALIZE toolkit is required to use LIA_RAL.
00004 
00005 LIA_RAL project is a development project was initiated by the computer
00006 science laboratory of Avignon / France (Laboratoire Informatique d'Avignon -
00007 LIA) [http://lia.univ-avignon.fr <http://lia.univ-avignon.fr/>]. Then it
00008 was supported by two national projects of the French Research Ministry:
00009         - TECHNOLANGUE program [http://www.technolangue.net]
00010         - MISTRAL program [http://mistral.univ-avignon.fr]
00011 
00012 LIA_RAL is free software: you can redistribute it and/or modify
00013 it under the terms of the GNU Lesser General Public License as
00014 published by the Free Software Foundation, either version 3 of
00015 the License, or any later version.
00016 
00017 LIA_RAL is distributed in the hope that it will be useful,
00018 but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00020 GNU Lesser General Public License for more details.
00021 
00022 You should have received a copy of the GNU Lesser General Public
00023 License along with LIA_RAL.
00024 If not, see [http://www.gnu.org/licenses/].
00025 
00026 The LIA team as well as the LIA_RAL project team wants to highlight the
00027 limits of voice authentication in a forensic context.
00028 The "Person Authentification by Voice: A Need of Caution" paper
00029 proposes a good overview of this point (cf. "Person
00030 Authentification by Voice: A Need of Caution", Bonastre J.F.,
00031 Bimbot F., Boe L.J., Campbell J.P., Douglas D.A., Magrin-
00032 chagnolleau I., Eurospeech 2003, Genova].
00033 The conclusion of the paper of the paper is proposed bellow:
00034 [Currently, it is not possible to completely determine whether the
00035 similarity between two recordings is due to the speaker or to other
00036 factors, especially when: (a) the speaker does not cooperate, (b) there
00037 is no control over recording equipment, (c) recording conditions are not
00038 known, (d) one does not know whether the voice was disguised and, to a
00039 lesser extent, (e) the linguistic content of the message is not
00040 controlled. Caution and judgment must be exercised when applying speaker
00041 recognition techniques, whether human or automatic, to account for these
00042 uncontrolled factors. Under more constrained or calibrated situations,
00043 or as an aid for investigative purposes, judicious application of these
00044 techniques may be suitable, provided they are not considered as infallible.
00045 At the present time, there is no scientific process that enables one to
00046 uniquely characterize a persones voice or to identify with absolute
00047 certainty an individual from his or her voice.]
00048 
00049 Copyright (C) 2004-2010
00050 Laboratoire d'informatique d'Avignon [http://lia.univ-avignon.fr]
00051 LIA_RAL admin [alize@univ-avignon.fr]
00052 Jean-Francois Bonastre [jean-francois.bonastre@univ-avignon.fr]
00053 */
00054 
00055 #if !defined(ALIZE_CovIntra_cpp)
00056 #define ALIZE_CovIntra_cpp
00057 
00058 #include <iostream>
00059 #include <fstream>  // pour outFile
00060 #include <cstdio>   // pour printf()
00061 #include <cassert> // pour le debug pratique
00062 #include <cmath>
00063 #include <liatools.h>
00064 #include "CovIntra.h"
00065 extern "C" {
00066 #include "svdlib.h" 
00067 }
00068 
00069 #if defined(_WIN32)
00070   #include <cfloat> // for _isnan()
00071   #define ISNAN(x) _isnan(x)
00072   #define ISINF(x) (!_finite(x))
00073 #elif defined(linux) || defined(__linux) || defined(__CYGWIN__) || defined(__APPLE__)
00074   #define ISNAN(x) isnan(x)
00075   #define ISINF(x) isinf(x)
00076 #else
00077   #error "Unsupported OS\n"
00078 #endif
00079 
00080 
00081 #define myTINY 1e-11
00082 using namespace std;
00083 using namespace alize;
00084 
00085 // Divides a Vector by an Integer
00086 void dividesSvByInteger(RealVector <double> & v, unsigned long nb) {
00087         if (nb==0) throw Exception("Divides by 0!",__FILE__,__LINE__);  
00088         for (unsigned long j=0;j<v.size();j++) 
00089                 v[j]/=nb;
00090 }
00091 
00092 // Divides a Vector by an Integer
00093 void dividesSvByInteger(Matrix <double> & v, unsigned long nb) {
00094         if (nb==0) throw Exception("Divides by 0!",__FILE__,__LINE__);
00095         for (unsigned long j=0;j<v.cols();j++) 
00096                 v(0,j)/=nb;
00097 }
00098 
00099 bool badSv(Matrix <double> & v) {
00100         for (unsigned long j=0;j<v.cols();j++) {        
00101                 if (abs(v(0,j))>myTINY) return false;
00102                 if (ISNAN(v(0,j)) || ISINF(v(0,j))) return true;        
00103         }
00104 return true;
00105 }
00106 
00107 void loadMeanSv(String id,Matrix <double> & v,Config & config) {
00108         MixtureServer ms(config);
00109         MixtureGD & curr=ms.loadMixtureGD(id);
00110         RealVector <double> currSv;
00111         if (config.getParam("vsize").toLong()!= (long)(ms.getDistribCount()*ms.getVectSize())) throw Exception("VectSize mismatch, check vsize parameter",__FILE__,__LINE__);
00112         currSv.setSize(config.getParam("vsize").toLong());
00113         if (config.existsParam("klgmm")) getKLVector(curr,currSv,config); 
00114         else modelToSv(curr,currSv);
00115         v.setDimensions(1,currSv.size());
00116         for (unsigned long j=0;j<currSv.size();j++)     
00117                 v(0,j)=currSv[j];
00118 }
00119 
00120 template <class T> class RealVectorStat {
00121         private:                
00122                 unsigned long _size;
00123                 unsigned long _nb;
00124                 RealVector <T> _xAcc;
00125                 RealVector <T> _xxAcc;
00126                 RealVector <double> _mean;
00127                 RealVector <double> _covInv;    
00128         public:
00129                 explicit RealVectorStat(unsigned long size) {
00130                         _size=size;
00131                         _nb=0;
00132                         _xAcc.setSize(_size);_xxAcc.setSize(_size);_mean.setSize(_size);_covInv.setSize(_size);
00133                         _xAcc.setAllValues(0);_xxAcc.setAllValues(0);_mean.setAllValues(0.0);_covInv.setAllValues(0.0);
00134                 }
00135                 ~ RealVectorStat() {};
00136                 void acc(RealVector <T> &v) {
00137                         if (_size!=v.size()) throw Exception("(RealVectorStat) size does not match!",__FILE__,__LINE__);
00138                         for (unsigned long i=0;i<v.size();i++) {
00139                                 _xAcc[i]+=v[i];
00140                                 _xxAcc[i]+=v[i]*v[i];
00141                         }
00142                         _nb++;
00143                 };
00144                 RealVector <double> & covInv() {
00145                         if (_nb==0)  throw Exception("(RealVectorStat) Nothing accumulated!",__FILE__,__LINE__);
00146                         for (unsigned long i=0;i<_size;i++) {
00147                                 _mean[i]=((double)_xAcc[i])/_nb;
00148                                 _covInv[i]=1/((((double)_xxAcc[i])/_nb)-(_mean[i]*_mean[i]));
00149                         }                       
00150                 return _covInv;
00151                 };
00152                 RealVector <double> & mean() {
00153                         if (_nb==0)  throw Exception("(RealVectorStat) Nothing accumulated!",__FILE__,__LINE__);                        
00154                         for (unsigned long i=0;i<_size;i++)                             
00155                                 _mean[i]=((double)_xAcc[i])/_nb;        
00156                 return _mean;
00157                 };
00158 };
00159 
00160 int CovIntra(Config & config) {
00161   try {
00162         bool gmm=false;
00163         if (config.existsParam("gmm")) {if(verbose) cout << "(CovIntra) Gmm mode: reading SV from gmm models"<<endl;gmm=true;}
00164         bool svd=false;
00165         if (config.existsParam("svd")) {if(verbose) cout << "(CovIntra) Computing the SVD"<<endl;svd=true;}     
00166         if (config.existsParam("klgmm")) {if(verbose) cout << "(CovIntra) Gmm KL mode: computing KL distance from gmm models"<<endl;gmm=true;}
00167         SVDVerbosity=verboseLevel;
00168         XList fileList(config.getParam("ndx"),config);
00169         XLine *pline;
00170         String *pModel;
00171         unsigned long svSize=config.getParam("vsize").toLong();
00172         unsigned long nbSpeakers=fileList.getLineCount();
00173         if (config.existsParam("nbSpeakers") && config.getParam("nbSpeakers").toLong() <= (long) nbSpeakers) nbSpeakers=config.getParam("nbSpeakers").toLong();
00174         
00175         // Compute desired Nb examples
00176         unsigned long nbEx=0;
00177         for (unsigned long s=0;s<nbSpeakers;s++)
00178                 nbEx+=fileList.getLine(s).getElementCount();
00179         fileList.rewind();
00180         Matrix <double> UBMsv;
00181         loadMeanSv(config.getParam("inputWorldFilename"),UBMsv,config);
00182         DMat CCt=svdNewDMat(svSize,nbEx); //svdlib format       
00183         RealVectorStat <double> acc(svSize);
00184         if (verbose) cout<<"Channel Matrix dimension: "<<svSize<<","<<nbEx <<endl << "Speaker Matrix dimension: "<<svSize<<","<<nbSpeakers<<endl;
00185         unsigned long idx=0;
00186         for (unsigned long r=0;r<nbSpeakers;r++) {
00187                 pline=fileList.getLine();
00188                 if (verbose) cout << "Sp ["<<r<<"] " <<pline->getElementCount() << " sessions, " <<endl;
00189                 // Compute True Mean Statistic for Speaker in different sessions
00190                 Matrix <double> meanSv; 
00191                 meanSv.setDimensions(1,svSize);
00192                 while((pModel=pline->getElement())!=NULL) {
00193                         if (verboseLevel > 1) cout << "Load " <<*pModel<<endl;
00194                         String filename;
00195                         Matrix <double> currSv;
00196                         if (!gmm) currSv.load(config.getParam("vectorFilesPath")+*pModel+config.getParam("vectorFilesExtension"),config);
00197                         else loadMeanSv(*pModel,currSv,config);
00198                         meanSv+=currSv;
00199                 }
00200                 
00201                 /************************ This compute InterSpeaker variability in a R vector **************************************************/
00202                 RealVector <double> vm;
00203                 for (unsigned long i=0;i<meanSv.cols();i++) {
00204                         double val=meanSv(0,i)/(pline->getElementCount());
00205                         val-=UBMsv(0,i);
00206                         vm.addValue(val);
00207                 }
00208                 acc.acc(vm);
00209                 
00210                 /************************* 2nd pass: Remove mean sv to get only channel (drop one) *******************************************/
00211                 for (unsigned long c=0;c<pline->getElementCount();c++) {
00212                         Matrix <double> currSv;                                 
00213                         if (!gmm) currSv.load(config.getParam("vectorFilesPath")+pline->getElement(c)+config.getParam("vectorFilesExtension"),config);
00214                         else loadMeanSv(pline->getElement(c),currSv,config);            
00215                         Matrix <double> meanMinusCurrent(meanSv); // if drop one to compute mean on the line
00216                         meanMinusCurrent-=currSv;
00217                         dividesSvByInteger(meanMinusCurrent,(pline->getElementCount()-1));      
00218                         currSv-=meanMinusCurrent;
00219                         if (debug && badSv(currSv)) cout <<pline->getElement(c)<<" gives a bad channel supervector (zero|inf|nan value)"<<endl; // is faster as badSv is not evaluated if debug=0, at least that's what I think
00220 
00221                         // Copy SV into matrix
00222                         for(unsigned long val=0;val<currSv.cols(); val++) {
00223                                 CCt->value[val][idx]=currSv(0,val);
00224                                 if(abs(CCt->value[val][idx])<myTINY) CCt->value[val][idx]=0.0; // sparsify matrix
00225                         }
00226                         idx++;
00227                 }
00228         }
00229         // Save interSp vector
00230         RealVector <double>& interSp=acc.mean();
00231         ((Matrix <double>)interSp).saveDT((String)"mean.inter",config);
00232 
00233         String filename=config.getParam("channelMatrix");
00234         char *out=strdup(filename.c_str());
00235         if (verbose && !svd) cout << "(CovIntra) Covariance matrix saved in " << config.getParam("channelMatrix")<< ", need to perform eigenvalue analysis yourself or add --svd flag to the config file" <<endl;
00236         svdWriteDenseMatrix(CCt,out, SVD_F_DB);
00237         free(out);
00238         if (!svd) exit(1);
00239                 
00240         /****** SVDLIBC part: EigenValue decomposition with SVD ********************/
00241         unsigned long nbIt=0;
00242         double kappa=1e-6;
00243         double las2end[2] = {-1.0e-30, 1.0e-30};        
00244         if (config.existsParam("nbIt")) nbIt=config.getParam("nbIt").toLong();
00245         if (config.existsParam("kappa")) kappa=config.getParam("kappa").toDouble();             
00246         if (config.existsParam("bound")) {
00247                 las2end[1] = config.getParam("bound").toDouble();
00248                 las2end[0] = -las2end[1];
00249                 }
00250                 
00251         SMat SCCt=svdConvertDtoS(CCt);  
00252         svdFreeDMat(CCt);               
00253         for(int i=0;i<SCCt->vals;i++)
00254                 if (ISNAN(SCCt->value[i]) || ISINF(SCCt->value[i])) throw Exception("Error:nan or inf in sparse matrix file",__FILE__,__LINE__);
00255         unsigned long nbEv=config.getParam("nbEigenVectors").toLong();
00256         if (verbose) cout << "(CovIntra) Begin eigenValue problem resolution (using Doug Rohde's SVD C Library)" << endl;
00257         SVDRec res=svdLAS2(SCCt,nbEv,nbIt,las2end,kappa);
00258         svdFreeSMat(SCCt);
00259         /*Save EigValues*/
00260         Matrix <double> ev(1,res->d);
00261         for (int i=0;i<res->d;i++) ev(0,i)=res->S[i];
00262         String evFile=config.getParam("channelMatrix")+".S";
00263         if (verbose) cout << "(CovIntra) Singluar Values (square root) in " <<evFile<< endl;    
00264         ev.saveDT(evFile,config);
00265         /*Save EigVectors*/
00266         Matrix <double> NAP(res->Ut->rows,res->Ut->cols);
00267         if (verbose) cout<< "(CovIntra) Dimensions of U: ("<<NAP.rows()<<","<<NAP.cols()<<")"<< endl;
00268         for (int i=0;i<res->Ut->rows;i++)
00269                 for (int j=0;j<res->Ut->cols;j++)
00270                         NAP(i,j)=res->Ut->value[i][j];  
00271         svdFreeSVDRec(res);                     
00272         if (verbose) cout << "(CovIntra) U saved in " << config.getParam("channelMatrix")<<endl;                
00273         NAP.save(config.getParam("channelMatrix"),config);
00274 }
00275   catch (Exception& e) {cout << e.toString() << endl;}
00276 return 0;
00277 }
00278 
00279 
00280 
00281 #endif