Exemple #1
0
/* DoGeneration: Generate parameter sequences from HMMs */
void DoGeneration(char *labfn)
{
   char labFn[MAXFNAMELEN], buf[MAXSTRLEN];
   int t;
   Boolean eSep;
   Transcription *tr;

   if (trace & T_TOP) {
      printf(" Generating Label %s\n", NameOf(labfn, buf));
      fflush(stdout);
   }

   /* load a given input label file */
   ResetHeap(&utt->transStack);
   MakeFN(labfn, labDir, labExt, labFn);
   tr = LOpen(&genStack, labFn, lff);

   /* compose a sentence HMM corresponding to the input label */
   InitialiseGenInfo(genInfo, tr, FALSE);

   /* set utterance informations for forward-backward algorithm */
   SetStreamWidths(hmset.pkind, hmset.vecSize, hmset.swidth, &eSep);
   utt->tr = tr;
   utt->Q = genInfo->labseqlen;
   utt->T = genInfo->tframe;
   utt->twoDataFiles = FALSE;
   utt->o = (Observation *) New(&gstack, utt->T * sizeof(Observation));
   utt->o--;
   for (t = 1; t <= utt->T; t++)
      utt->o[t] = MakeObservation(&gstack, hmset.swidth, hmset.pkind, FALSE, eSep);

   /* parameter generation */
   ParamGen(genInfo, utt, fbInfo, type);

   /* output state durations and generated parameter sequences */
   if (!stateAlign)
      WriteStateDurations(labfn, genInfo);
   WriteParms(labfn, genInfo);

   /* free memory */
   Dispose(&gstack, ++utt->o);
   ResetGenInfo(genInfo);

   /* increment total number of generated frames */
   totalT += utt->T;
   totalPr += utt->pr;

   return;
}
Exemple #2
0
/* SplitPath: last name in path is removed and prefixed to name, then
              this new name is suffixed to subdir and stored in tryspec */            
static void SplitPath(char *path, char *name, char *subdir, char *tryspec)
{
   char buf1[MAXFNAMELEN],buf2[MAXFNAMELEN],*p;
   char pch[2] = " ";
   
   pch[0] = PATHCHAR;
   PathOf(path,buf1); NameOf(path,buf2);
   if (strlen(name)>0 ) strcat(buf2,pch);    /* new name */
   strcat(buf2,name); strcpy(name,buf2);
   p = buf1+strlen(buf1)-1;                  /* new path */
   if (*p == PATHCHAR) *p = '\0';
   strcpy(path,buf1);   
   strcpy(buf1,subdir);                      /* tryspec */
   p = buf1+strlen(buf1)-1;
   if (*p != PATHCHAR) strcat(buf1,pch);
   strcat(buf1,name);
   strcpy(tryspec,buf1);
}
Exemple #3
0
/* OneIterMgeEval: */
static int OneIterMgeEval(int nIter)
{
   char *datafn, labfn[256], basefn[255];
   int nSent, nAdjNum, p, nTotalAdj;

   if (nIter > 1)
      nMaxBALen = 1;
   /* set all stat info to zero */
   ResetAllStatInfo(nIter);
   nTotalAdj = 0;
   /* process all data */
   for (nSent = 0; nSent < g_nDataFileNum; nSent++) {
      datafn = g_pDataFileList[nSent]->datafn;
      /* after first boundary adjustment, use the adjusted label files */
      if (nIter >= 1 && bBoundAdj && outLabDir != NULL)
         MakeFN(datafn, outLabDir, labExt, labfn);
      else
         MakeFN(datafn, labDir, labExt, labfn);
      NameOf(datafn, basefn);

      if (bBoundAdj) {
         nAdjNum = OneSentBoundAdjust(mtInfo, labfn, datafn, outLabDir, nMaxBAIter, nMaxBALen, FALSE);
         if (trace & T_PROC) {
            fprintf(stdout, "Total BA Number: %d\n", nAdjNum);
            fflush(stdout);
         }
         nTotalAdj += nAdjNum;
      }
      /* accumulation of generation error only */
      OneSentGenErrAcc(mtInfo, labfn, datafn);
      for (p = 1; p <= genInfo->nPdfStream[0]; p++) {
         total_T[p] += genInfo->pst[p].T;
      }
   }

   return nSent;
}
Exemple #4
0
/* Initialise: */
static void Initialise()
{
   char macroname[MAXSTRLEN];
   AdaptXForm *xform;

   /* load HMMs and init HMMSet related global variables */
   if (trace & T_TOP)
      printf("Loading HMM for updating ... ... \n");
   if (MakeHMMSet(&hset, hmmListFn) < SUCCESS)
      HError(2321, "Initialise: MakeHMMSet failed");
   if (LoadHMMSet(&hset, hmmDir, hmmExt) < SUCCESS)
      HError(2321, "Initialise: LoadHMMSet failed");
   SetParmHMMSet(&hset);
   /* variance inversion, MGE training only support INVDIAGC case */
   if (hset.ckind == DIAGC)
      ConvDiagC(&hset, TRUE);
   /* load transform */
   if (xformfn != NULL) {
      xform = LoadOneXForm(&hset, NameOf(xformfn, macroname), xformfn);
      SetXForm(&hset, &xfInfo, xform);
   }

   if (funcType == MGE_TRAIN)
      AttachAccToModel();

   /* reload the HMM for reference in training */
   if (mtInfo->bOrigHmmRef) {
      if (trace & T_TOP)
         printf("Loading HMM for reference ... ... \n");
      if (MakeHMMSet(&orighset, hmmListFn) < SUCCESS)
         HError(2321, "Initialise: MakeHMMSet failed");
      if (LoadHMMSet(&orighset, hmmDir, hmmExt) < SUCCESS)
         HError(2321, "Initialise: LoadHMMSet failed");
      SetParmHMMSet(&orighset);
      /* variance inversion, MGE training only support INVDIAGC case */
      if (hset.ckind == DIAGC)
         ConvDiagC(&orighset, TRUE);
      /* load transform */
      if (xformfn != NULL) {
         xform = LoadOneXForm(&orighset, NameOf(xformfn, macroname), xformfn);
         SetXForm(&orighset, &xfInfo, xform);
      }
   }

   if (trace & T_TOP) {
      printf("System is ");
      switch (hset.hsKind) {
      case PLAINHS:
         printf("PLAIN\n");
         break;
      case SHAREDHS:
         printf("SHARED\n");
         break;
      case TIEDHS:
         printf("TIED\n");
         break;
      case DISCRETEHS:
         printf("DISCRETE\n");
         break;
      }

      printf("%d Logical/%d Physical Models Loaded, VecSize=%d\n", hset.numLogHMM, hset.numPhyHMM, hset.vecSize);
      if (hset.numFiles > 0)
         printf("%d MMF input files\n", hset.numFiles);
      if (mmfFn != NULL)
         printf("Output to MMF file:  %s\n", mmfFn);
      fflush(stdout);
   }

   /* initialize a MgeTrnInfo for MGE training of HMM */
   InitMgeTrnInfo();
}
Exemple #5
0
/* OneIterMgeAdapt: */
static int OneIterMgeAdapt(int nIter)
{
   char *datafn, labfn[256], basefn[255];
   int nSent, nAdjNum, p, nTotalAdj;
   float stepSize;

   if (nIter > 1)
      nMaxBALen = 1;
   /* set all stat info to zero */
   ResetAllStatInfo(nIter);

   stepSize = 1 / (A_STEP + B_STEP * nSamples);
   nAdjNum = 0;
   nTotalAdj = 0;
   /* process all data */
   for (nSent = 0; nSent < g_nDataFileNum; nSent++) {
      datafn = g_pDataFileList[nSent]->datafn;
      /* after first boundary adjustment, use the adjusted label files */
      if (nIter > 1 && bBoundAdj && outLabDir != NULL)
         MakeFN(datafn, outLabDir, labExt, labfn);
      else
         MakeFN(datafn, labDir, labExt, labfn);
      NameOf(datafn, basefn);

      if (trace & T_TOP) {
         fprintf(stdout, "Prcessing %4d %s ... \n", nSent, basefn);
         fflush(stdout);
      }

      /* apply transform to related models */
      if (hset.curXForm != NULL)
         OneSentTransform(mtInfo, labfn, datafn);

      /* accumulation of generation error only */
      if (nIter == 0) {
         OneSentGenErrAcc(mtInfo, labfn, datafn);
      } else {
         if (bBoundAdj) {
            nAdjNum = OneSentBoundAdjust(mtInfo, labfn, datafn, outLabDir, nMaxBAIter, nMaxBALen, !bMgeUpdate);
            if (trace & T_TOP) {
               fprintf(stdout, "BA Number: %d\n", nAdjNum);
               fflush(stdout);
            }
            nTotalAdj += nAdjNum;
         }
         if (bMgeUpdate) {
            OneSentMgeAdapt(mtInfo, labfn, datafn, stepSize);
            nSamples++;
         }
      }

      for (p = 1; p <= genInfo->nPdfStream[0]; p++) {
         total_T[p] += genInfo->pst[p].T;
      }

      /* update step size */
      stepSize = 1 / (A_STEP + B_STEP * nSamples);
      /* fix variance floor */
      if ((uFlags & UPVARS) && nSent % 100 == 0)
         ApplyVFloor(&hset);
   }
   if (nIter > 0 && bBoundAdj) {
      if (trace & T_TOP) {
         fprintf(stdout, "Bound Adjust Number: %d\n", nTotalAdj);
         fflush(stdout);
      }
   }

   return nSent;
}
Exemple #6
0
/* OneIterMgeTrain: */
static int OneIterMgeTrain(int nIter)
{
   char *datafn, labfn[256], basefn[255];
   int nSent, nAdjNum, p, nTotalAdj;
   float stepSize;

   /* set all stat info to zero */
   ResetAllStatInfo(nIter);
   /* zero all acc info for batch mode updating */
   ZeroAccsParallel(&hset, uFlags, 1);

   stepSize = 1 / (A_STEP + B_STEP * nSamples);
   nAdjNum = 0;
   nTotalAdj = 0;
   /* first refine the boundary for all data */
   if (bBoundAdj && (nIter != 0 && nIter != startIter - 1)) {
      for (nSent = 0; nSent < g_nDataFileNum; nSent++) {
         datafn = g_pDataFileList[nSent]->datafn;
         /* after first boundary adjustment, use the adjusted label files */
         if (nIter > 1 && bBoundAdj && outLabDir != NULL)
            MakeFN(datafn, outLabDir, labExt, labfn);
         else
            MakeFN(datafn, labDir, labExt, labfn);
         NameOf(datafn, basefn);

         if (trace & T_TOP) {
            fprintf(stdout, "Boundary refining %4d %s ... \n", nSent, basefn);
            fflush(stdout);
         }
         nAdjNum = OneSentBoundAdjust(mtInfo, labfn, datafn, outLabDir, nMaxBAIter, nMaxBALen, !bMgeUpdate);
         if (trace & T_TOP) {
            fprintf(stdout, "BA Number: %d\n", nAdjNum);
            fflush(stdout);
         }
         nTotalAdj += nAdjNum;
      }
   }
   /* process all data */
   for (nSent = 0; nSent < g_nDataFileNum; nSent++) {
      datafn = g_pDataFileList[nSent]->datafn;
      /* after first boundary adjustment, use the adjusted label files */
      if (nIter >= 1 && bBoundAdj && outLabDir != NULL)
         MakeFN(datafn, outLabDir, labExt, labfn);
      else
         MakeFN(datafn, labDir, labExt, labfn);
      NameOf(datafn, basefn);

      if (trace & T_TOP) {
         fprintf(stdout, "Prcessing %4d %s ... \n", nSent, basefn);
         fflush(stdout);
      }
      /* accumulation of generation error only */
      if (nIter == 0 || nIter == startIter - 1) {
         OneSentGenErrAcc(mtInfo, labfn, datafn);
      } else if (bMgeUpdate) {
         OneSentMgeTrain(mtInfo, labfn, datafn, stepSize);
         nSamples++;
      }

      for (p = 1; p <= genInfo->nPdfStream[0]; p++) {
         total_T[p] += genInfo->pst[p].T;
      }
      /* update step size */
      stepSize = 1 / (A_STEP + B_STEP * nSamples);
      /* fix variance floor */
      if ((uFlags & UPVARS) && nSent % 100 == 0)
         ApplyVFloor(&hset);
   }

   if (nIter > 0 && bBoundAdj) {
      if (trace & T_TOP) {
         fprintf(stdout, "Bound Adjust Number: %d\n", nTotalAdj);
         fflush(stdout);
      }
   }

   if (nIter > 0 && bBoundAdj && (uFlags & UPMIXES))
      UpdateAllMSDWeight(mtInfo);

   return nSent;
}
Exemple #7
0
/* UpdateModels: update all models and save them in newDir if set,
   new files have newExt if set */
void UpdateModels(HMMSet *hset, ParmBuf pbuf2)
{
   int maxM;
   static char str[100];
   BufferInfo info2;
   char macroname[MAXSTRLEN];

   if (trace&T_UPD){
      printf("Starting Model Update\n"); fflush(stdout);
   }
   if (parMode == -1) {
      ForceDiagC(hset); /* invert the variances again */
      ConvExpWt(hset);
   }
   maxM = MaxMixInSet(hset);

   /* 
      This routine tidies up the semi-tied transform and 
      tidies the model up. The transition and priors are 
      not updated 
   */
   if (uFlags & UPSEMIT) {
      UpdateSemiTiedModels(hset, &xfInfo);
      uFlags = UPDSet(uFlags & ~(UPMEANS|UPVARS));
   }

   if (uFlags & UPMAP)
     MAPUpdateModels(hset, uFlags);
   else {
     MLUpdateModels(hset, uFlags);     
   }
   
   if(varFloorPercent){
      int s;
      printf("Flooring all vars to the %f'th percentile of distribution... ", varFloorPercent);
      for(s=1;s<=hset->swidth[0];s++)
         FloorVars(hset,s);
   }

   if (trace&T_TOP){
      if (mmfFn == NULL)
         printf("Saving hmm's to dir %s\n",(newDir==NULL)?"Current":newDir); 
      else
         printf("Saving hmm's to MMF %s\n",mmfFn);
      fflush(stdout);
   }
   ClearSeenFlags(hset,CLR_ALL);
   if (twoDataFiles){
      if (parMode == 0){
         SetChannel("HPARM2");
         nParm = GetConfig("HPARM2", TRUE, cParm, MAXGLOBS);
         if (GetConfStr(cParm,nParm,"TARGETKIND",str))
            hset->pkind = Str2ParmKind(str);
	 if (GetConfStr(cParm,nParm,"MATTRANFN",str)) {
            /* The transform needs to be set-up */
            hset->xf = LoadInputXForm(hset,NameOf(str,macroname),str);
         }
      } else {
         GetBufferInfo(pbuf2,&info2);
         hset->pkind = info2.tgtPK;
	 hset->xf = (InputXForm*) info2.xform;
      }
   }
   SaveHMMSet(hset,newDir,newExt,NULL,saveBinary);
   if (trace&T_TOP) {
      printf("Reestimation complete - average log prob per frame = %e\n",
             totalPr/totalT);
      printf("     - total frames seen          = %e\n", (double)totalT);
   }
}
Exemple #8
0
ITEM *Clone_Item(ITEM *i, short f)	/* 1=true dup 0=normal dup */
{
	DUP *d;
	ITEM *n=CreateItem(NameOf(i),i->it_Adjective,i->it_Noun);
	SUB *s;
	n->it_ActorTable=i->it_ActorTable;
	n->it_ActionTable=i->it_ActionTable;
	n->it_State=i->it_State;
	n->it_Class=i->it_Class;
	n->it_Perception=i->it_Perception;
	n->it_SubjectTable=NULL;
	n->it_ObjectTable=NULL;
	n->it_DaemonTable=NULL;
	/* Cheat: We want the clone to behave as the original, but that
	   would mean copying the tables which is slow and uses lots of
	   memory. We use the new subclassing feature to cheat and make
	   the original superclass of the clones. */
	LockItem(i);
	n->it_Superclass=i;

	if(IsRoom(i))
		Clone_Room(n,i);
	if(IsPlayer(i))
		Clone_Player(n,i);
	if(IsObject(i))
		Clone_Object(n,i);
	if(FindSub(i,KEY_GENEXIT))
		Clone_GenExit(n,i);
	s=i->it_Properties;
	while(s)
	{
		switch(s->pr_Key)
		{
		case KEY_MSGEXIT:Clone_MsgExit(n,(MSGEXIT *)s);
				 break;
		case KEY_CONTAINER:
				Clone_Container(n,(CONTAINER *)s);
				break;
		case KEY_CHAIN:
				Clone_Chain(n,(CHAIN *)s);
				break;
		case KEY_USERFLAG:
		case KEY_USERFLAG2:
				Clone_UserFlag(n,(USERFLAG *)s);
				break;
		case KEY_INHERIT:
				Clone_Inherit(n,(INHERIT *)s);
				break;
		case KEY_USERTEXT:
				Clone_UserText(i,n);
				break;
		case KEY_INOUTHERE:
				SetInMsg(n,TextOf(((INOUTHERE *)s)->io_InMsg));
				SetOutMsg(n,TextOf(((INOUTHERE *)s)->io_OutMsg));
				SetHereMsg(n,TextOf(((INOUTHERE *)s)->io_HereMsg));
				break;
		case KEY_CONDEXIT:
				MakeCondExit(n,((CONDEXIT *)s)->ce_Dest,
					((CONDEXIT *)s)->ce_Table,
					((CONDEXIT *)s)->ce_ExitNumber);
				break;
		}
		s=s->pr_Next;
	}
	if(!f)
	{
		d=(DUP *)AllocSub(n,KEY_DUPED,sizeof(DUP));
		d->du_Master=i;
		LockItem(i);
	}
	return(n);
}