PocketSphinx  0.6
ms_mgau.c
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 1999-2004 Carnegie Mellon University. All rights
4  * reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * This work was supported in part by funding from the Defense Advanced
19  * Research Projects Agency and the National Science Foundation of the
20  * United States of America, and the CMU Sphinx Speech Consortium.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * ====================================================================
35  *
36  */
37 /*
38  * ms_mgau.c -- Essentially a wrapper that wrap up gauden and
39  * senone. It supports multi-stream.
40  *
41  *
42  * **********************************************
43  * CMU ARPA Speech Project
44  *
45  * Copyright (c) 1997 Carnegie Mellon University.
46  * ALL RIGHTS RESERVED.
47  * **********************************************
48  * HISTORY
49  * $Log$
50  * Revision 1.2 2006/02/22 16:56:01 arthchan2003
51  * Merged from SPHINX3_5_2_RCI_IRII_BRANCH: Added ms_mgau.[ch] into the trunk. It is a wrapper of ms_gauden and ms_senone
52  *
53  * Revision 1.1.2.4 2005/09/25 18:55:19 arthchan2003
54  * Added a flag to turn on and off precomputation.
55  *
56  * Revision 1.1.2.3 2005/08/03 18:53:44 dhdfu
57  * Add memory deallocation functions. Also move all the initialization
58  * of ms_mgau_model_t into ms_mgau_init (duh!), which entails removing it
59  * from decode_anytopo and friends.
60  *
61  * Revision 1.1.2.2 2005/08/02 21:05:38 arthchan2003
62  * 1, Added dist and mgau_active as intermediate variable for computation. 2, Added ms_cont_mgau_frame_eval, which is a multi stream version of GMM computation mainly s3.0 family of tools. 3, Fixed dox-doc.
63  *
64  * Revision 1.1.2.1 2005/07/20 19:37:09 arthchan2003
65  * Added a multi-stream cont_mgau (ms_mgau) which is a wrapper of both gauden and senone. Add ms_mgau_init and model_set_mllr. This allow eliminating 600 lines of code in decode_anytopo/align/allphone.
66  *
67  *
68  *
69  */
70 
71 /* Local headers. */
72 #include "ms_mgau.h"
73 
74 static ps_mgaufuncs_t ms_mgau_funcs = {
75  "ms",
76  ms_cont_mgau_frame_eval, /* frame_eval */
77  ms_mgau_mllr_transform, /* transform */
78  ms_mgau_free /* free */
79 };
80 
81 ps_mgau_t *
82 ms_mgau_init(acmod_t *acmod, logmath_t *lmath, bin_mdef_t *mdef)
83 {
84  /* Codebooks */
85  ms_mgau_model_t *msg;
86  ps_mgau_t *mg;
87  gauden_t *g;
88  senone_t *s;
89  cmd_ln_t *config;
90  int i;
91 
92  config = acmod->config;
93 
94  msg = (ms_mgau_model_t *) ckd_calloc(1, sizeof(ms_mgau_model_t));
95  msg->config = config;
96  msg->g = NULL;
97  msg->s = NULL;
98 
99  g = msg->g = gauden_init(cmd_ln_str_r(config, "-mean"),
100  cmd_ln_str_r(config, "-var"),
101  cmd_ln_float32_r(config, "-varfloor"),
102  lmath);
103 
104  /* Verify n_feat and veclen, against acmod. */
105  if (g->n_feat != feat_dimension1(acmod->fcb)) {
106  E_ERROR("Number of streams does not match: %d != %d\n",
107  g->n_feat, feat_dimension1(acmod->fcb));
108  goto error_out;
109  }
110  for (i = 0; i < g->n_feat; ++i) {
111  if (g->featlen[i] != feat_dimension2(acmod->fcb, i)) {
112  E_ERROR("Dimension of stream %d does not match: %d != %d\n", i,
113  g->featlen[i], feat_dimension2(acmod->fcb, i));
114  goto error_out;
115  }
116  }
117 
118  s = msg->s = senone_init(msg->g,
119  cmd_ln_str_r(config, "-mixw"),
120  cmd_ln_str_r(config, "-senmgau"),
121  cmd_ln_float32_r(config, "-mixwfloor"),
122  lmath, mdef);
123 
124  s->aw = cmd_ln_int32_r(config, "-aw");
125 
126  /* Verify senone parameters against gauden parameters */
127  if (s->n_feat != g->n_feat)
128  E_FATAL("#Feature mismatch: gauden= %d, senone= %d\n", g->n_feat,
129  s->n_feat);
130  if (s->n_cw != g->n_density)
131  E_FATAL("#Densities mismatch: gauden= %d, senone= %d\n",
132  g->n_density, s->n_cw);
133  if (s->n_gauden > g->n_mgau)
134  E_FATAL("Senones need more codebooks (%d) than present (%d)\n",
135  s->n_gauden, g->n_mgau);
136  if (s->n_gauden < g->n_mgau)
137  E_ERROR("Senones use fewer codebooks (%d) than present (%d)\n",
138  s->n_gauden, g->n_mgau);
139 
140  msg->topn = cmd_ln_int32_r(config, "-topn");
141  E_INFO("The value of topn: %d\n", msg->topn);
142  if (msg->topn == 0 || msg->topn > msg->g->n_density) {
143  E_WARN
144  ("-topn argument (%d) invalid or > #density codewords (%d); set to latter\n",
145  msg->topn, msg->g->n_density);
146  msg->topn = msg->g->n_density;
147  }
148 
149  msg->dist = (gauden_dist_t ***)
150  ckd_calloc_3d(g->n_mgau, g->n_feat, msg->topn,
151  sizeof(gauden_dist_t));
152  msg->mgau_active = ckd_calloc(g->n_mgau, sizeof(int8));
153 
154  mg = (ps_mgau_t *)msg;
155  mg->vt = &ms_mgau_funcs;
156  return mg;
157 error_out:
158  ms_mgau_free(ps_mgau_base(msg));
159  return NULL;
160 }
161 
162 void
163 ms_mgau_free(ps_mgau_t * mg)
164 {
165  ms_mgau_model_t *msg = (ms_mgau_model_t *)mg;
166  if (msg == NULL)
167  return;
168 
169  if (msg->g)
170  gauden_free(msg->g);
171  if (msg->s)
172  senone_free(msg->s);
173  if (msg->dist)
174  ckd_free_3d((void *) msg->dist);
175  if (msg->mgau_active)
176  ckd_free(msg->mgau_active);
177 
178  ckd_free(msg);
179 }
180 
181 int
182 ms_mgau_mllr_transform(ps_mgau_t *s,
183  ps_mllr_t *mllr)
184 {
185  ms_mgau_model_t *msg = (ms_mgau_model_t *)s;
186  return gauden_mllr_transform(msg->g, mllr, msg->config);
187 }
188 
189 int32
190 ms_cont_mgau_frame_eval(ps_mgau_t * mg,
191  int16 *senscr,
192  uint8 *senone_active,
193  int32 n_senone_active,
194  mfcc_t ** feat,
195  int32 frame,
196  int32 compallsen)
197 {
198  ms_mgau_model_t *msg = (ms_mgau_model_t *)mg;
199  int32 gid;
200  int32 topn;
201  int32 best;
202  gauden_t *g;
203  senone_t *sen;
204 
205  topn = ms_mgau_topn(msg);
206  g = ms_mgau_gauden(msg);
207  sen = ms_mgau_senone(msg);
208 
209  if (compallsen) {
210  int32 s;
211 
212  for (gid = 0; gid < g->n_mgau; gid++)
213  gauden_dist(g, gid, topn, feat, msg->dist[gid]);
214 
215  best = (int32) 0x7fffffff;
216  for (s = 0; s < sen->n_sen; s++) {
217  senscr[s] = senone_eval(sen, s, msg->dist[sen->mgau[s]], topn);
218  if (best > senscr[s]) {
219  best = senscr[s];
220  }
221  }
222 
223  /* Normalize senone scores */
224  for (s = 0; s < sen->n_sen; s++) {
225  int32 bs = senscr[s] - best;
226  if (bs > 32767)
227  bs = 32767;
228  if (bs < -32768)
229  bs = -32768;
230  senscr[s] = bs;
231  }
232  }
233  else {
234  int32 i, n;
235  /* Flag all active mixture-gaussian codebooks */
236  for (gid = 0; gid < g->n_mgau; gid++)
237  msg->mgau_active[gid] = 0;
238 
239  n = 0;
240  for (i = 0; i < n_senone_active; i++) {
241  /* senone_active consists of deltas. */
242  int32 s = senone_active[i] + n;
243  msg->mgau_active[sen->mgau[s]] = 1;
244  n = s;
245  }
246 
247  /* Compute topn gaussian density values (for active codebooks) */
248  for (gid = 0; gid < g->n_mgau; gid++) {
249  if (msg->mgau_active[gid])
250  gauden_dist(g, gid, topn, feat, msg->dist[gid]);
251  }
252 
253  best = (int32) 0x7fffffff;
254  n = 0;
255  for (i = 0; i < n_senone_active; i++) {
256  int32 s = senone_active[i] + n;
257  senscr[s] = senone_eval(sen, s, msg->dist[sen->mgau[s]], topn);
258  if (best > senscr[s]) {
259  best = senscr[s];
260  }
261  n = s;
262  }
263 
264  /* Normalize senone scores */
265  n = 0;
266  for (i = 0; i < n_senone_active; i++) {
267  int32 s = senone_active[i] + n;
268  int32 bs = senscr[s] - best;
269  if (bs > 32767)
270  bs = 32767;
271  if (bs < -32768)
272  bs = -32768;
273  senscr[s] = bs;
274  n = s;
275  }
276  }
277 
278  return 0;
279 }
(Sphinx 3.0 specific) A module that wraps up the code of gauden and senone because they are closely r...
int32 n_density
Number gaussian densities in each codebook-feature stream.
Definition: ms_gauden.h:93
void gauden_free(gauden_t *g)
Release memory allocated by gauden_init.
Definition: ms_gauden.c:399
int32 aw
Inverse acoustic weight.
Definition: ms_senone.h:163
Structure to store distance (density) values for a given input observation wrt density values in some...
Definition: ms_gauden.h:74
int32 gauden_mllr_transform(gauden_t *s, ps_mllr_t *mllr, cmd_ln_t *config)
Transform Gaussians according to an MLLR matrix (or, eventually, more).
Definition: ms_gauden.c:550
gauden_t * gauden_init(char const *meanfile, char const *varfile, float32 varfloor, logmath_t *lmath)
Read mixture gaussian codebooks from the given files.
Definition: ms_gauden.c:361
uint32 n_sen
Number senones in this set.
Definition: ms_senone.h:156
gauden_t * g
The codebook.
Definition: ms_mgau.h:116
uint32 * mgau
senone-id -> mgau-id mapping for senones in this set
Definition: ms_senone.h:161
8-bit senone PDF structure.
Definition: ms_senone.h:147
uint32 n_feat
Number feature streams.
Definition: ms_senone.h:157
cmd_ln_t * config
Configuration.
Definition: acmod.h:150
int32 * featlen
feature length for each feature
Definition: ms_gauden.h:94
int topn
Top-n gaussian will be computed.
Definition: ms_mgau.h:118
int32 senone_eval(senone_t *s, int id, gauden_dist_t **dist, int n_top)
Evaluate the score for the given senone wrt to the given top N gaussian codewords.
int32 n_mgau
Number codebooks.
Definition: ms_gauden.h:91
Feature space linear transform structure.
Definition: acmod.h:82
feat_t * fcb
Dynamic feature computation.
Definition: acmod.h:156
void senone_free(senone_t *s)
Release memory allocated by senone_init.
Definition: ms_senone.c:341
int32 n_feat
Number feature streams in each codebook.
Definition: ms_gauden.h:92
uint32 n_cw
Number codewords per codebook,stream.
Definition: ms_senone.h:158
uint32 n_gauden
Number gaussian density codebooks referred to by senones.
Definition: ms_senone.h:159
ps_mgaufuncs_t * vt
vtable of mgau functions.
Definition: acmod.h:114
Multivariate gaussian mixture density parameters.
Definition: ms_gauden.h:85
int32 gauden_dist(gauden_t *g, int mgau, int n_top, mfcc_t **obs, gauden_dist_t **out_dist)
Compute gaussian density values for the given input observation vector wrt the specified mixture gaus...
senone_t * senone_init(gauden_t *g, char const *mixwfile, char const *mgau_mapfile, float32 mixwfloor, logmath_t *lmath, bin_mdef_t *mdef)
Load a set of senones (mixing weights and mixture gaussian codebook mappings) from the given files...
Definition: ms_senone.c:275
Acoustic model structure.
Definition: acmod.h:148
senone_t * s
The senone.
Definition: ms_mgau.h:117