📄 hcompv.c
字号:
/* ----------------------------------------------------------- *//* *//* ___ *//* |_| | |_/ SPEECH *//* | | | | \ RECOGNITION *//* ========= SOFTWARE */ /* *//* *//* ----------------------------------------------------------- *//* developed at: *//* *//* Speech Vision and Robotics group *//* Cambridge University Engineering Department *//* http://svr-www.eng.cam.ac.uk/ *//* *//* Entropic Cambridge Research Laboratory *//* (now part of Microsoft) *//* *//* ----------------------------------------------------------- *//* Copyright: Microsoft Corporation *//* 1995-2000 Redmond, Washington USA *//* http://www.microsoft.com *//* *//* 2001 Cambridge University *//* Engineering Department *//* *//* Use of this software is governed by a License Agreement *//* ** See the file License for the Conditions of Use ** *//* ** This banner notice must not be removed ** *//* *//* ----------------------------------------------------------- *//* File: HCompV.c: HMM global mean/variance initialisation *//* ----------------------------------------------------------- */char *hcompv_version = "!HVER!HCompV: 3.3 [CUED 28/04/05]";char *hcompv_vc_id = "$Id: HCompV.c,v 1.1.1.1 2005/05/12 10:52:53 jal58 Exp $";/* This program calculates a single overall variance vector from a sequence of training files. This vector is then copied into all of the components of all the states of the hidden Markov model specified on the command line. Optionally, HCompV will also update the mean vector. HCompV.c can be used as the initial step in Fixed Variance and Grand Variance training schemes, and to initialise HMMs for "flat-start" training. The structure of the HMM to be initialised must be defined beforehand (eg using a prototype HMM def). It can also be used to generate variance floor vectors.*/ #include "HShell.h"#include "HMem.h"#include "HMath.h"#include "HSigP.h"#include "HAudio.h"#include "HWave.h"#include "HVQ.h"#include "HParm.h"#include "HLabel.h"#include "HModel.h"#include "HUtil.h"/* -------------------------- Trace Flags & Vars ------------------------ */#define T_TOP 0001 /* basic progress reporting */#define T_COVS 0002 /* show covariance matrices */#define T_LOAD 0004 /* trace data loading */#define T_SEGS 0010 /* list label segments */#define T_CMV 0012 /* doing CMV */static int trace = 0; /* trace level */static ConfParam *cParm[MAXGLOBS]; /* configuration parameters */static int nParm = 0; /* total num params *//* ---------------------- Global Variables ----------------------- */static char *segLab = NULL; /* segment label if any */static LabId segId = NULL; /* and its id */static LabId hmmId = NULL; /* id of model */static char *labDir = NULL; /* label file directory */static char *labExt = "lab"; /* label file extension */static float minVar = 0.0; /* minimum variance */static FileFormat dff=UNDEFF; /* data file format */static FileFormat lff=UNDEFF; /* label file format */static char *hmmfn=NULL; /* HMM definition file name */static char *outfn=NULL; /* output HMM file name (name only) */static char *outDir=NULL; /* HMM output directory */static long totalCount=0; /* total number of vector samples*/static Boolean meanUpdate = FALSE; /* update means */static Boolean saveBinary = FALSE; /* save output in binary */static float vFloorScale = 0.0; /* if >0.0 then vFloor scaling *//* Major Data Structures */static MLink macroLink; /* Link to specific HMM macro */static HLink hmmLink; /* Link to the physical HMM */static HMMSet hset; /* HMM to be initialised with */static MemHeap iStack;/* Storage for mean and covariance accumulators */typedef struct { Vector meanSum; /* acc for mean vector value */ Covariance squareSum; /* acc for sum of squares */ Covariance fixed; /* fixed (co)variance values */} CovAcc;static CovAcc accs[SMAX]; /* one CovAcc for each stream */static Boolean fullcNeeded[SMAX]; /* true for each stream that needs full covariance calculated */static Observation obs; /* storage for observations *//* ------------ structures for cmn ------------ */typedef struct{ char SpkrName[MAXSTRLEN]; /* speaker name */ int NumFrame; /* number of frames for speaker */ Vector meanSum; /* mean accumulate structure for speaker */ Vector squareSum; /* variance structure for speaker */}SpkrAcc; typedef struct SpkrAccListItem{ SpkrAcc *sa; /* speaker accumulate */ struct SpkrAccListItem *nextSpkr; /* next pointer */}SpkrAccListItem;static SpkrAccListItem *salist = NULL; /* global speaker accumulate list */static int vSize = 0; /* target observation vector size */static char spPattern[MAXSTRLEN]; /* speaker mask */static char pathPattern[MAXSTRLEN]; /* path mask */static char oflags[MAXSTRLEN] = "m"; /* export flags for CMV */ static char cmDir[MAXSTRLEN]; /* directory to export CMV */static char TargetPKStr[MAXSTRLEN]; /* target parm kind string */static Boolean DoCMV = FALSE; /* switch from old HCompV to CMV *//* ------------- Process Command Line and Check Data ------------ *//* SetConfParms: set conf parms relevant to HCompV */void SetConfParms(void){ Boolean b,c; int i; double d; nParm = GetConfig("HCOMPV", TRUE, cParm, MAXGLOBS); if (nParm>0) { if (GetConfInt(cParm,nParm,"TRACE",&i)) trace = i; if (GetConfBool(cParm,nParm,"UPDATEMEANS",&b)) meanUpdate = b; if (GetConfBool(cParm,nParm,"SAVEBINARY",&c)) saveBinary = c; if (GetConfFlt(cParm,nParm,"MINVARFLOOR",&d)) minVar = d; }}void ReportUsage(void){ printf("\nUSAGE: HCompV [options] [hmmFile] trainFiles...\n" ); printf(" Option Default\n\n"); printf(" -c dir Set output directiry for CMV none\n"); printf(" -f f Output vFloor as f * global var none\n"); printf(" -k s spkr pattern for CMV none\n"); printf(" -l s Set segment label to s none\n"); printf(" -m Update means off\n"); printf(" -o fn Store new hmm def in fn (name only) outDir/srcfn\n"); printf(" -p s path pattern for CMV none\n"); printf(" -q nmv output type flags for CMV m\n"); printf(" -v f Set minimum variance to f 0.0\n"); PrintStdOpts("BCFGHILMX"); printf("\n\n");}/* ------------------------ Initialisation ----------------------- *//* CheckVarianceKind: set fullcNeeded[s] for each non-diag stream s */void CheckVarianceKind(void){ int i,s,m; StateElem *se; StreamElem *ste; MixtureElem *me; for (s=1;s<=hset.swidth[0];s++) fullcNeeded[s]=FALSE; for (i=2,se=hmmLink->svec+2; i < hmmLink->numStates; i++,se++) for (s=1,ste=se->info->pdf+1; s <= hset.swidth[0]; s++,ste++) for (m=1,me = ste->spdf.cpdf+1; m<=ste->nMix; m++, me++) if (me->mpdf->ckind == FULLC) fullcNeeded[s] = TRUE;}/* Initialise: load HMMs and create accumulators */void Initialise(void){ int s,V; Boolean eSep; char base[MAXSTRLEN]; char path[MAXSTRLEN]; char ext[MAXSTRLEN]; /* Load HMM defs */ if(MakeOneHMM(&hset,BaseOf(hmmfn,base))<SUCCESS) HError(2028,"Initialise: MakeOneHMM failed"); if(LoadHMMSet(&hset,PathOf(hmmfn,path),ExtnOf(hmmfn,ext))<SUCCESS) HError(2028,"Initialise: LoadHMMSet failed"); SetParmHMMSet(&hset); if (hset.hsKind==DISCRETEHS || hset.hsKind==TIEDHS) HError(2030,"Initialise: HCompV only uses continuous models"); /* Create a heap to store the input data */ CreateHeap(&iStack,"InBuf", MSTAK, 1, 0.5, 100000, LONG_MAX); /* Get a pointer to the physical HMM */ hmmId = GetLabId(base,FALSE); macroLink = FindMacroName(&hset,'h',hmmId); if (macroLink==NULL) HError(2020,"Initialise: cannot find hmm %s in hset",hmmfn); hmmLink = (HLink)macroLink->structure; /* Find out for which streams full covariance is needed */ CheckVarianceKind( ); /* Create accumulators for the mean and variance */ for (s=1;s<=hset.swidth[0]; s++){ V = hset.swidth[s]; accs[s].meanSum=CreateVector(&gstack,V); ZeroVector(accs[s].meanSum); if (fullcNeeded[s]) { accs[s].squareSum.inv=CreateSTriMat(&gstack,V); accs[s].fixed.inv=CreateSTriMat(&gstack,V); ZeroTriMat(accs[s].squareSum.inv); } else { accs[s].squareSum.var=CreateSVector(&gstack,V); accs[s].fixed.var=CreateSVector(&gstack,V); ZeroVector(accs[s].squareSum.var); } } /* Create an object to hold the input parameters */ SetStreamWidths(hset.pkind,hset.vecSize,hset.swidth,&eSep); obs=MakeObservation(&gstack,hset.swidth,hset.pkind,FALSE,eSep); if(segLab != NULL) { segId = GetLabId(segLab,TRUE); } if (trace&T_TOP) { printf("Calculating Fixed Variance\n"); printf(" HMM Prototype: %s\n",hmmfn); printf(" Segment Label: %s\n",(segLab==NULL)?"None":segLab); printf(" Num Streams : %d\n",hset.swidth[0]); printf(" UpdatingMeans: %s\n",(meanUpdate)?"Yes":"No"); printf(" Target Direct: %s\n",(outDir==NULL)?"Current":outDir); }}/* ----------------------[Co]Variance Estimation ---------------------- *//* CalcCovs: calculate covariance of speech data */void CalcCovs(void){ int x,y,s,V; float meanx,meany,varxy,n; Matrix fullMat; if (totalCount<2) HError(2021,"CalcCovs: Only %d speech frames accumulated",totalCount); if (trace&T_TOP) printf("%ld speech frames accumulated\n", totalCount); n = (float)totalCount; /* to prevent rounding to integer below */ for (s=1; s<=hset.swidth[0]; s++){ /* For each stream */ V = hset.swidth[s]; for (x=1; x<=V; x++) /* For each coefficient ... */ accs[s].meanSum[x] /= n; /* ... calculate mean */ for (x=1;x<=V;x++) { meanx = accs[s].meanSum[x]; /* ... and [co]variance */ if (fullcNeeded[s]) { for (y=1; y<=x; y++) { meany = accs[s].meanSum[y]; varxy = accs[s].squareSum.inv[x][y]/n - meanx*meany; accs[s].squareSum.inv[x][y] = (x != y || varxy > minVar) ? varxy : minVar; } } else { varxy = accs[s].squareSum.var[x]/n - meanx*meanx; accs[s].fixed.var[x] = (varxy > minVar) ? varxy :minVar; } } if (fullcNeeded[s]) { /* invert covariance matrix */ fullMat=CreateMatrix(&gstack,V,V); ZeroMatrix(fullMat); CovInvert(accs[s].squareSum.inv,fullMat); Mat2Tri(fullMat,accs[s].fixed.inv); FreeMatrix(&gstack,fullMat); } if (trace&T_COVS) { printf("Stream %d\n",s); if (meanUpdate) ShowVector(" Mean Vector ", accs[s].meanSum,12); if (fullcNeeded[s]) { ShowTriMat(" Covariance Matrix ",accs[s].squareSum.inv,12,12); } else ShowVector(" Variance Vector ", accs[s].fixed.var,12); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -