• Main Page
  • Related Pages
  • Data Structures
  • Files
  • File List
  • Globals

src/libpocketsphinx/tmat.c

00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
00002 /* ====================================================================
00003  * Copyright (c) 1999-2004 Carnegie Mellon University.  All rights
00004  * reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer. 
00012  *
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in
00015  *    the documentation and/or other materials provided with the
00016  *    distribution.
00017  *
00018  * This work was supported in part by funding from the Defense Advanced 
00019  * Research Projects Agency and the National Science Foundation of the 
00020  * United States of America, and the CMU Sphinx Speech Consortium.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
00023  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
00024  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00025  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
00026  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00028  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
00029  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
00030  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00031  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00032  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * ====================================================================
00035  *
00036  */
00037 /*
00038  * tmat.c
00039  *
00040  * **********************************************
00041  * CMU ARPA Speech Project
00042  *
00043  * Copyright (c) 1997 Carnegie Mellon University.
00044  * ALL RIGHTS RESERVED.
00045  * **********************************************
00046  * 
00047  * HISTORY
00048  * $Log: tmat.c,v $
00049  * Revision 1.1.1.1  2006/05/23 18:45:01  dhuggins
00050  * re-importation
00051  *
00052  * Revision 1.4  2005/11/14 16:14:34  dhuggins
00053  * Use LOG() instead of logs3() for loading tmats, makes startup
00054  * ***much*** faster.
00055  *
00056  * Revision 1.3  2005/10/10 14:50:35  dhuggins
00057  * Deal properly with empty transition matrices.
00058  *
00059  * Revision 1.2  2005/09/30 15:01:23  dhuggins
00060  * More robust tmat reading - read the tmat in accordance with the fixed s2 topology
00061  *
00062  * Revision 1.1  2005/09/29 21:51:19  dhuggins
00063  * Add support for Sphinx3 tmat files.  Amazingly enough, it Just Works
00064  * (but it isn't terribly robust)
00065  *
00066  * Revision 1.6  2005/07/05 13:12:39  dhdfu
00067  * Add new arguments to logs3_init() in some tests, main_ep
00068  *
00069  * Revision 1.5  2005/06/21 19:23:35  arthchan2003
00070  * 1, Fixed doxygen documentation. 2, Added $ keyword.
00071  *
00072  * Revision 1.5  2005/05/03 04:09:09  archan
00073  * Implemented the heart of word copy search. For every ci-phone, every word end, a tree will be allocated to preserve its pathscore.  This is different from 3.5 or below, only the best score for a particular ci-phone, regardless of the word-ends will be preserved at every frame.  The graph propagation will not collect unused word tree at this point. srch_WST_propagate_wd_lv2 is also as the most stupid in the century.  But well, after all, everything needs a start.  I will then really get the results from the search and see how it looks.
00074  *
00075  * Revision 1.4  2005/04/21 23:50:26  archan
00076  * Some more refactoring on the how reporting of structures inside kbcore_t is done, it is now 50% nice. Also added class-based LM test case into test-decode.sh.in.  At this moment, everything in search mode 5 is already done.  It is time to test the idea whether the search can really be used.
00077  *
00078  * Revision 1.3  2005/03/30 01:22:47  archan
00079  * Fixed mistakes in last updates. Add
00080  *
00081  * 
00082  * 20.Apr.2001  RAH (rhoughton@mediasite.com, ricky.houghton@cs.cmu.edu)
00083  *              Added tmat_free to free allocated memory 
00084  *
00085  * 29-Feb-2000  M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.
00086  *              Added tmat_chk_1skip(), and made tmat_chk_uppertri() public.
00087  * 
00088  * 10-Dec-1999  M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.
00089  *              Made tmat_dump() public.
00090  * 
00091  * 11-Mar-97    M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University.
00092  *              Started based on original S3 implementation.
00093  */
00094 
00095 /* System headers. */
00096 #include <string.h>
00097 
00098 /* SphinxBase headers. */
00099 #include <logmath.h>
00100 #include <err.h>
00101 #include <ckd_alloc.h>
00102 
00103 /* Local headers. */
00104 #include "tmat.h"
00105 #include "bio.h"
00106 #include "vector.h"
00107 
00108 #define TMAT_PARAM_VERSION              "1.0"
00109 
00110 
00115 static int32 tmat_chk_uppertri(tmat_t *tmat, logmath_t *lmath);
00116 
00117 
00124 static int32 tmat_chk_1skip(tmat_t *tmat, logmath_t *lmath);
00125 
00126 
00127 void
00128 tmat_dump(tmat_t * tmat, FILE * fp)
00129 {
00130     int32 i, src, dst;
00131 
00132     for (i = 0; i < tmat->n_tmat; i++) {
00133         fprintf(fp, "TMAT %d = %d x %d\n", i, tmat->n_state,
00134                 tmat->n_state + 1);
00135         for (src = 0; src < tmat->n_state; src++) {
00136             for (dst = 0; dst <= tmat->n_state; dst++)
00137                 fprintf(fp, " %12d", tmat->tp[i][src][dst]);
00138             fprintf(fp, "\n");
00139         }
00140         fprintf(fp, "\n");
00141     }
00142     fflush(fp);
00143 }
00144 
00145 
00146 /*
00147  * Check model tprob matrices that they conform to upper-triangular assumption;
00148  * i.e. no "backward" transitions allowed.
00149  */
00150 int32
00151 tmat_chk_uppertri(tmat_t * tmat, logmath_t *lmath)
00152 {
00153     int32 i, src, dst;
00154 
00155     /* Check that each tmat is upper-triangular */
00156     for (i = 0; i < tmat->n_tmat; i++) {
00157         for (dst = 0; dst < tmat->n_state; dst++)
00158             for (src = dst + 1; src < tmat->n_state; src++)
00159                 if (tmat->tp[i][src][dst] > logmath_get_zero(lmath)) {
00160                     E_ERROR("tmat[%d][%d][%d] = %d\n",
00161                             i, src, dst, tmat->tp[i][src][dst]);
00162                     return -1;
00163                 }
00164     }
00165 
00166     return 0;
00167 }
00168 
00169 
00170 int32
00171 tmat_chk_1skip(tmat_t * tmat, logmath_t *lmath)
00172 {
00173     int32 i, src, dst;
00174 
00175     for (i = 0; i < tmat->n_tmat; i++) {
00176         for (src = 0; src < tmat->n_state; src++)
00177             for (dst = src + 3; dst <= tmat->n_state; dst++)
00178                 if (tmat->tp[i][src][dst] > logmath_get_zero(lmath)) {
00179                     E_ERROR("tmat[%d][%d][%d] = %d\n",
00180                             i, src, dst, tmat->tp[i][src][dst]);
00181                     return -1;
00182                 }
00183     }
00184 
00185     return 0;
00186 }
00187 
00188 
00189 tmat_t *
00190 tmat_init(char const *file_name, logmath_t *lmath, float64 tpfloor, int32 breport)
00191 {
00192     char tmp;
00193     int32 n_src, n_dst;
00194     FILE *fp;
00195     int32 byteswap, chksum_present;
00196     uint32 chksum;
00197     float32 **tp;
00198     int32 i, j, k, tp_per_tmat;
00199     char **argname, **argval;
00200     tmat_t *t;
00201 
00202 
00203     if (breport) {
00204         E_INFO("Reading HMM transition probability matrices: %s\n",
00205                file_name);
00206     }
00207 
00208     t = (tmat_t *) ckd_calloc(1, sizeof(tmat_t));
00209 
00210     if ((fp = fopen(file_name, "rb")) == NULL)
00211         E_FATAL_SYSTEM("fopen(%s,rb) failed\n", file_name);
00212 
00213     /* Read header, including argument-value info and 32-bit byteorder magic */
00214     if (bio_readhdr(fp, &argname, &argval, &byteswap) < 0)
00215         E_FATAL("bio_readhdr(%s) failed\n", file_name);
00216 
00217     /* Parse argument-value list */
00218     chksum_present = 0;
00219     for (i = 0; argname[i]; i++) {
00220         if (strcmp(argname[i], "version") == 0) {
00221             if (strcmp(argval[i], TMAT_PARAM_VERSION) != 0)
00222                 E_WARN("Version mismatch(%s): %s, expecting %s\n",
00223                        file_name, argval[i], TMAT_PARAM_VERSION);
00224         }
00225         else if (strcmp(argname[i], "chksum0") == 0) {
00226             chksum_present = 1; /* Ignore the associated value */
00227         }
00228     }
00229     bio_hdrarg_free(argname, argval);
00230     argname = argval = NULL;
00231 
00232     chksum = 0;
00233 
00234     /* Read #tmat, #from-states, #to-states, arraysize */
00235     if ((bio_fread(&(t->n_tmat), sizeof(int32), 1, fp, byteswap, &chksum)
00236          != 1)
00237         || (bio_fread(&n_src, sizeof(int32), 1, fp, byteswap, &chksum) !=
00238             1)
00239         || (bio_fread(&n_dst, sizeof(int32), 1, fp, byteswap, &chksum) !=
00240             1)
00241         || (bio_fread(&i, sizeof(int32), 1, fp, byteswap, &chksum) != 1)) {
00242         E_FATAL("bio_fread(%s) (arraysize) failed\n", file_name);
00243     }
00244     if (t->n_tmat >= MAX_INT32) /* Comparison is always false... */
00245         E_FATAL("%s: #tmat (%d) exceeds limit (%d)\n", file_name,
00246                 t->n_tmat, MAX_INT32);
00247     if (n_dst != n_src + 1)
00248         E_FATAL("%s: #from-states(%d) != #to-states(%d)-1\n", file_name,
00249                 n_src, n_dst);
00250     t->n_state = n_src;
00251 
00252     if (i != t->n_tmat * n_src * n_dst) {
00253         E_FATAL
00254             ("%s: #float32s(%d) doesn't match dimensions: %d x %d x %d\n",
00255              file_name, i, t->n_tmat, n_src, n_dst);
00256     }
00257 
00258     /* Allocate memory for tmat data */
00259     t->tp =
00260         (int32 ***) ckd_calloc_3d(t->n_tmat, n_src, n_dst, sizeof(int32));
00261 
00262     /* Temporary structure to read in the float data */
00263     tp = (float32 **) ckd_calloc_2d(n_src, n_dst, sizeof(float32));
00264 
00265     /* Read transition matrices, normalize and floor them, and convert to log domain */
00266     tp_per_tmat = n_src * n_dst;
00267     for (i = 0; i < t->n_tmat; i++) {
00268         if (bio_fread(tp[0], sizeof(float32), tp_per_tmat, fp,
00269                       byteswap, &chksum) != tp_per_tmat) {
00270             E_FATAL("fread(%s) (arraydata) failed\n", file_name);
00271         }
00272 
00273         /* Normalize and floor */
00274         for (j = 0; j < n_src; j++) {
00275             if (vector_sum_norm(tp[j], n_dst) == 0.0)
00276                 E_WARN("Normalization failed for tmat %d from state %d\n",
00277                        i, j);
00278             vector_nz_floor(tp[j], n_dst, tpfloor);
00279             vector_sum_norm(tp[j], n_dst);
00280 
00281             /* Convert to logs3. */
00282             for (k = 0; k < n_dst; k++) {
00283                 /* For these ones, we floor them even if they are
00284                  * zero, otherwise HMM evaluation goes nuts. */
00285                 if (k >= j && k-j < 3 && tp[j][k] == 0.0f)
00286                     tp[j][k] = (float32)tpfloor;
00287                 t->tp[i][j][k] = logmath_log(lmath, tp[j][k]);
00288             }
00289         }
00290     }
00291 
00292     ckd_free_2d((void **) tp);
00293 
00294     if (chksum_present)
00295         bio_verify_chksum(fp, byteswap, chksum);
00296 
00297     if (fread(&tmp, 1, 1, fp) == 1)
00298         E_ERROR("Non-empty file beyond end of data\n");
00299 
00300     fclose(fp);
00301 
00302 
00303     if (tmat_chk_uppertri(t, lmath) < 0)
00304         E_FATAL("Tmat not upper triangular\n");
00305     if (tmat_chk_1skip(t, lmath) < 0)
00306         E_FATAL("Topology not Left-to-Right or Bakis\n");
00307 
00308     return t;
00309 }
00310 
00311 void
00312 tmat_report(tmat_t * t)
00313 {
00314     E_INFO_NOFN("Initialization of tmat_t, report:\n");
00315     E_INFO_NOFN("Read %d transition matrices of size %dx%d\n",
00316                 t->n_tmat, t->n_state, t->n_state + 1);
00317     E_INFO_NOFN("\n");
00318 
00319 }
00320 
00321 /* 
00322  *  RAH, Free memory allocated in tmat_init ()
00323  */
00324 void
00325 tmat_free(tmat_t * t)
00326 {
00327     if (t) {
00328         if (t->tp)
00329             ckd_free_3d((void ***) t->tp);
00330         ckd_free((void *) t);
00331     }
00332 }

Generated on Thu Jan 27 2011 for PocketSphinx by  doxygen 1.7.1