libsent/src/anlz/paramselect.c

説明を見る。
00001 
00041 /*
00042  * Copyright (c) 1991-2006 Kawahara Lab., Kyoto University
00043  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00044  * Copyright (c) 2005-2006 Julius project team, Nagoya Institute of Technology
00045  * All rights reserved
00046  */
00047 
00048 #include <sent/stddefs.h>
00049 #include <sent/htk_param.h>
00050 #include <sent/htk_hmm.h>
00051 
00052 
00053 /* vector mark & delete */
00054 static int *vmark;              
00055 static int vlen;                
00056 static int vnewlen;             
00057 static short src_type;          
00058 
00064 static void
00065 init_mark(HTK_Param *param)
00066 {
00067   int i;
00068   vmark = (int *)mymalloc(sizeof(int) * param->veclen);
00069   vlen = param->veclen;
00070   for (i=0;i<vlen;i++) {
00071     vmark[i] = 0;
00072   }
00073 }
00074 
00079 static void
00080 free_mark()
00081 {
00082   free(vmark);
00083 }
00084 
00091 static void
00092 mark_exclude_vector(int loc, int len)
00093 {
00094   int i;
00095 #ifdef DEBUG
00096   j_printf("delmark: %d-%d\n",loc, loc+len-1);
00097 #endif
00098   for (i=0;i<len;i++) {
00099     if (loc + i >= vlen) {
00100       j_error("delmark buffer exceeded!!\n");
00101     }
00102     vmark[loc+i] = 1;
00103   }
00104 #ifdef DEBUG
00105   j_printf("now :");
00106   for (i=0;i<vlen;i++) {
00107     if (vmark[i] == 1) {
00108       j_printf("-");
00109     } else {
00110       j_printf("O");
00111     }
00112   }
00113   j_printf("\n");
00114 #endif
00115 }
00116 
00128 static void
00129 exec_exclude_vectors(HTK_Param *new, HTK_Param *src)
00130 {
00131   int i, loc;
00132   unsigned int t;
00133   /* src ---(vmark)--> new */
00134 
00135   /* new length */
00136   new->veclen = vnewlen;
00137 #ifdef DEBUG
00138   j_printf("new length = %d\n", new->veclen);
00139 #endif
00140 
00141   /* malloc */
00142   new->samplenum = src->samplenum;
00143   new->parvec = (VECT **)mymalloc(sizeof(VECT *) * new->samplenum);
00144   
00145   for(t = 0; t < src->samplenum; t++) {
00146     new->parvec[t] = (VECT *)mymalloc(sizeof(VECT) * new->veclen);
00147     loc = 0;
00148     for (i=0;i<src->veclen;i++) {
00149       if (vmark[i] == 0) {      /* not delete == copy */
00150         new->parvec[t][loc] = src->parvec[t][i];
00151         loc++;
00152       }
00153     }
00154   }
00155 }
00156 
00167 int
00168 exec_exclude_one_vector(VECT *vec, int len)
00169 {
00170   int i,loc;
00171   
00172   loc = 0;
00173   for (i=0;i<len;i++) {
00174     if (vmark[i] == 0) {        /* not delete == copy */
00175       vec[loc] = vec[i];
00176       loc++;
00177     }
00178   }
00179   return(loc);
00180 }
00181 
00182 
00183 
00184 
00194 int
00195 guess_basenum(HTK_Param *p, short qualtype)
00196 {
00197   int size;
00198   int compnum;
00199   
00200   compnum = 1 + ((qualtype & F_DELTA) ? 1 : 0) + ((qualtype & F_ACCL) ? 1 : 0);
00201   
00202   size = p->veclen;
00203   if (p->header.samptype & F_ENERGY_SUP) size += 1;
00204   if ((size % compnum) != 0) {
00205     j_printerr("ERROR: illegal vector length (should not happen)!\n");
00206     return -1;
00207   }
00208   size /= compnum;
00209   if (p->header.samptype & F_ENERGY) size -= 1;
00210   if (p->header.samptype & F_ZEROTH) size -= 1;
00211 
00212   return(size);
00213 }
00214 
00215 /* can add: _N */
00216 /* can sub: _E_D_A_0 */
00217 
00226 boolean
00227 select_param_vmark(HTK_Param *src, short dst_type_arg)
00228 {
00229   short dst_type;
00230   short del_type, add_type;
00231   int basenum, pb[3],pe[3],p0[3]; /* location */
00232   int i, len;
00233   char srcstr[80], dststr[80], buf[80];
00234 
00235   src_type = src->header.samptype & ~(F_COMPRESS | F_CHECKSUM);
00236   src_type &= ~(F_BASEMASK);    /* only qualifier code needed */
00237   srcstr[0] = '\0';
00238   param_qualcode2str(srcstr, src_type, FALSE);
00239   dst_type = dst_type_arg & ~(F_COMPRESS | F_CHECKSUM);
00240   dst_type &= ~(F_BASEMASK);    /* only qualifier code needed */
00241   dststr[0] = '\0';
00242   param_qualcode2str(dststr, dst_type, FALSE);
00243 
00244 #ifdef DEBUG
00245   j_printf("try to select qualifiers: %s -> %s\n", srcstr, dststr);
00246 #endif
00247 
00248   if (dst_type == F_ERR_INVALID) {
00249     j_printerr("ERROR: unknown parameter kind for selection: %s\n", dststr);
00250     return(FALSE);
00251   }
00252   
00253   /* guess base coefficient num */
00254   basenum = guess_basenum(src, src_type);
00255   if (basenum < 0) {            /* error */
00256     return(FALSE);
00257   }
00258 #ifdef DEBUG
00259   j_printf("base num = %d\n", basenum);
00260 #endif
00261 
00262   /* determine which component to use */
00263   del_type = src_type & (~(dst_type));
00264   add_type = (~(src_type)) & dst_type;
00265 
00266   init_mark(src);
00267 
00268   /* vector layout for exclusion*/
00269   pb[0] = 0;
00270   if ((src_type & F_ENERGY) && (src_type & F_ZEROTH)){
00271     p0[0] = basenum;
00272     pe[0] = basenum + 1;
00273     len = basenum + 2;
00274   } else if ((src_type & F_ENERGY) || (src_type & F_ZEROTH)){
00275     p0[0] = pe[0] = basenum;
00276     len = basenum + 1;
00277   } else {
00278     p0[0] = pe[0] = 0;
00279     len = basenum;
00280   }
00281   for (i=1;i<3;i++) {
00282     pb[i] = pb[i-1] + len;
00283     pe[i] = pe[i-1] + len;
00284     p0[i] = p0[i-1] + len;
00285   }
00286   if (src_type & F_ENERGY_SUP) {
00287     pe[0] = 0;
00288     for (i=1;i<3;i++) {
00289       pb[i]--;
00290       pe[i]--;
00291       p0[i]--;
00292     }
00293   }
00294   
00295   /* modification begin */
00296   /* qualifier addition: "_N" */
00297 #ifdef DEBUG
00298   buf[0] = '\0';
00299   j_printf("try to add: %s\n", param_qualcode2str(buf, add_type, FALSE));
00300 #endif
00301   
00302   if (add_type & F_ENERGY_SUP) {
00303     if (src_type & F_ENERGY) {
00304       mark_exclude_vector(pe[0], 1);
00305       src_type = src_type | F_ENERGY_SUP;
00306     } else if (src_type & F_ZEROTH) {
00307       mark_exclude_vector(p0[0], 1);
00308       src_type = src_type | F_ENERGY_SUP;
00309     } else {
00310       j_printerr("WARNING: \"_N\" needs \"_E\" or \"_0\". ignored\n");
00311     }
00312     add_type = add_type & (~(F_ENERGY_SUP)); /* set to 0 */
00313   }
00314   if (add_type != 0) {          /* others left */
00315     buf[0] = '\0';
00316     j_printerr("WARNING: can do only parameter exclusion. qualifiers %s ignored\n", param_qualcode2str(buf, add_type, FALSE));
00317   }
00318   
00319   /* qualifier excludeion: "_D","_A","_0","_E" */
00320 #ifdef DEBUG
00321   buf[0] = '\0';
00322   j_printf("try to del: %s\n", param_qualcode2str(buf, del_type, FALSE));
00323 #endif
00324 
00325   if (del_type & F_DELTA) del_type |= F_ACCL;
00326   /* mark delete vector */
00327   if (del_type & F_ACCL) {
00328     mark_exclude_vector(pb[2], len);
00329     src_type &= ~(F_ACCL);
00330     del_type &= ~(F_ACCL);
00331   }
00332   if (del_type & F_DELTA) {
00333     mark_exclude_vector(pb[1], len);
00334     src_type &= ~(F_DELTA);
00335     del_type &= ~(F_DELTA);
00336   }
00337   
00338   if (del_type & F_ENERGY) {
00339     mark_exclude_vector(pe[2], 1);
00340     mark_exclude_vector(pe[1], 1);
00341     if (!(src_type & F_ENERGY_SUP)) {
00342       mark_exclude_vector(pe[0], 1);
00343     }
00344     src_type &= ~(F_ENERGY | F_ENERGY_SUP);
00345     del_type &= ~(F_ENERGY | F_ENERGY_SUP);
00346   }
00347   if (del_type & F_ZEROTH) {
00348     mark_exclude_vector(p0[2], 1);
00349     mark_exclude_vector(p0[1], 1);
00350     if (!(src_type & F_ENERGY_SUP)) {
00351       mark_exclude_vector(p0[0], 1);
00352     }
00353     src_type &= ~(F_ZEROTH | F_ENERGY_SUP);
00354     del_type &= ~(F_ZEROTH | F_ENERGY_SUP);
00355   }
00356   
00357   if (del_type != 0) {          /* left */
00358     buf[0] = '\0';
00359     j_printerr("WARNING: cannot exclude qualifiers %s. selection ignored\n", param_qualcode2str(buf, del_type, FALSE));
00360   }
00361 
00362   vnewlen = 0;
00363   for (i=0;i<vlen;i++) {
00364     if (vmark[i] == 0) vnewlen++;
00365   }
00366 
00367   return(TRUE);
00368   
00369 }
00370 
00371 
00381 HTK_Param *
00382 new_select_param_kind(HTK_Param *src, short dst_type_arg)
00383 {
00384   HTK_Param *new;
00385 
00386   /* prepare new parameter */
00387   new = new_param();
00388   /* mark to determine operation */
00389   if (select_param_vmark(src, dst_type_arg) == FALSE) return(NULL);
00390   /* execute deletion (copy needed to new param)*/
00391   exec_exclude_vectors(new, src);
00392   
00393   /* copy & set header info */
00394   new->header.samplenum = src->header.samplenum;
00395   new->header.wshift = src->header.wshift;
00396   new->header.sampsize = new->veclen * sizeof(VECT);
00397   new->header.samptype = src_type | (src->header.samptype & F_BASEMASK);
00398   
00399 #ifdef DEBUG
00400   j_printf("new param made: %s\n", param_code2str(buf, new->header.samptype, FALSE));
00401 #endif
00402   
00403   free_mark();
00404 
00405   return(new);
00406 }
00407 
00428 HTK_Param *
00429 new_param_check_and_adjust(HTK_HMM_INFO *hmminfo, HTK_Param *param, boolean vflag)
00430 {
00431   HTK_Param *selected_param;
00432   char pbuf[80],hbuf[80];
00433   
00434   param_code2str(pbuf, (short)(param->header.samptype & ~(F_COMPRESS | F_CHECKSUM)), FALSE);
00435   param_code2str(hbuf, hmminfo->opt.param_type, FALSE);  
00436   if (!check_param_basetype(hmminfo, param)) {
00437     /* error if base type not match */
00438     j_printerr("Error: incompatible parameter type\n");
00439     j_printerr("  HMM  trained  by %s(%d)\n", hbuf, hmminfo->opt.vec_size);
00440     j_printerr("input parameter is %s(%d)\n", pbuf, param->veclen);
00441     return NULL;
00442   }
00443   if (!check_param_coherence(hmminfo, param)) {
00444     /* try to select needed parameter vector */
00445     if (vflag) j_printerr("attach %s", pbuf);
00446     selected_param = new_select_param_kind(param, hmminfo->opt.param_type);
00447     if (selected_param == NULL) {
00448       if (vflag) j_printerr("->%s failed\n", hbuf);
00449       j_printerr("Error: incompatible parameter type\n");
00450       j_printerr("  HMM  trained  by %s(%d)\n", hbuf, hmminfo->opt.vec_size);
00451       j_printerr("input parameter is %s(%d)\n", pbuf, param->veclen);
00452       return NULL;
00453     }
00454     param_code2str(pbuf, selected_param->header.samptype, FALSE);
00455     if (vflag) j_printerr("->%s\n", pbuf);
00456     free_param(param);
00457     return(selected_param);
00458   }
00459   return(param);
00460 }

Juliusに対してTue Dec 26 16:19:28 2006に生成されました。  doxygen 1.5.0