PocketSphinx  0.6
phone_loop_search.c
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 2008 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 
42 #include <sphinxbase/err.h>
43 
44 #include "phone_loop_search.h"
45 
46 static int phone_loop_search_start(ps_search_t *search);
47 static int phone_loop_search_step(ps_search_t *search, int frame_idx);
48 static int phone_loop_search_finish(ps_search_t *search);
49 static int phone_loop_search_reinit(ps_search_t *search, dict_t *dict, dict2pid_t *d2p);
50 static void phone_loop_search_free(ps_search_t *search);
51 static char const *phone_loop_search_hyp(ps_search_t *search, int32 *out_score, int32 *out_is_final);
52 static int32 phone_loop_search_prob(ps_search_t *search);
53 static ps_seg_t *phone_loop_search_seg_iter(ps_search_t *search, int32 *out_score);
54 
55 static ps_searchfuncs_t phone_loop_search_funcs = {
56  /* name: */ "phone_loop",
57  /* start: */ phone_loop_search_start,
58  /* step: */ phone_loop_search_step,
59  /* finish: */ phone_loop_search_finish,
60  /* reinit: */ phone_loop_search_reinit,
61  /* free: */ phone_loop_search_free,
62  /* lattice: */ NULL,
63  /* hyp: */ phone_loop_search_hyp,
64  /* prob: */ phone_loop_search_prob,
65  /* seg_iter: */ phone_loop_search_seg_iter,
66 };
67 
68 static int
69 phone_loop_search_reinit(ps_search_t *search, dict_t *dict, dict2pid_t *d2p)
70 {
72  cmd_ln_t *config = ps_search_config(search);
73  acmod_t *acmod = ps_search_acmod(search);
74  int i;
75 
76  /* Free old dict2pid, dict, if necessary. */
77  ps_search_base_reinit(search, dict, d2p);
78 
79  /* Initialize HMM context. */
80  if (pls->hmmctx)
82  pls->hmmctx = hmm_context_init(bin_mdef_n_emit_state(acmod->mdef),
83  acmod->tmat->tp, NULL, acmod->mdef->sseq);
84  if (pls->hmmctx == NULL)
85  return -1;
86 
87  /* Initialize phone HMMs. */
88  if (pls->phones) {
89  for (i = 0; i < pls->n_phones; ++i)
90  hmm_deinit((hmm_t *)&pls->phones[i]);
91  ckd_free(pls->phones);
92  }
93  pls->n_phones = bin_mdef_n_ciphone(acmod->mdef);
94  pls->phones = ckd_calloc(pls->n_phones, sizeof(*pls->phones));
95  for (i = 0; i < pls->n_phones; ++i) {
96  pls->phones[i].ciphone = i;
97  hmm_init(pls->hmmctx, (hmm_t *)&pls->phones[i],
98  FALSE,
99  bin_mdef_pid2ssid(acmod->mdef, i),
100  bin_mdef_pid2tmatid(acmod->mdef, i));
101  }
102  pls->beam = logmath_log(acmod->lmath, cmd_ln_float64_r(config, "-pl_beam"));
103  pls->pbeam = logmath_log(acmod->lmath, cmd_ln_float64_r(config, "-pl_pbeam"));
104  pls->pip = logmath_log(acmod->lmath, cmd_ln_float64_r(config, "-pip"));
105  E_INFO("State beam %d Phone exit beam %d Insertion penalty %d\n",
106  pls->beam, pls->pbeam, pls->pip);
107 
108  return 0;
109 }
110 
111 ps_search_t *
112 phone_loop_search_init(cmd_ln_t *config,
113  acmod_t *acmod,
114  dict_t *dict)
115 {
116  phone_loop_search_t *pls;
117 
118  /* Allocate and initialize. */
119  pls = ckd_calloc(1, sizeof(*pls));
120  ps_search_init(ps_search_base(pls), &phone_loop_search_funcs,
121  config, acmod, dict, NULL);
122  phone_loop_search_reinit(ps_search_base(pls), ps_search_dict(pls),
123  ps_search_dict2pid(pls));
124 
125  return ps_search_base(pls);
126 }
127 
128 static void
129 phone_loop_search_free_renorm(phone_loop_search_t *pls)
130 {
131  gnode_t *gn;
132  for (gn = pls->renorm; gn; gn = gnode_next(gn))
133  ckd_free(gnode_ptr(gn));
134  glist_free(pls->renorm);
135  pls->renorm = NULL;
136 }
137 
138 static void
139 phone_loop_search_free(ps_search_t *search)
140 {
141  phone_loop_search_t *pls = (phone_loop_search_t *)search;
142  int i;
143 
144  ps_search_deinit(search);
145  for (i = 0; i < pls->n_phones; ++i)
146  hmm_deinit((hmm_t *)&pls->phones[i]);
147  phone_loop_search_free_renorm(pls);
148  ckd_free(pls->phones);
149  hmm_context_free(pls->hmmctx);
150  ckd_free(pls);
151 }
152 
153 static int
154 phone_loop_search_start(ps_search_t *search)
155 {
156  phone_loop_search_t *pls = (phone_loop_search_t *)search;
157  int i;
158 
159  /* Reset and enter all phone HMMs. */
160  for (i = 0; i < pls->n_phones; ++i) {
161  hmm_t *hmm = (hmm_t *)&pls->phones[i];
162  hmm_clear(hmm);
163  hmm_enter(hmm, 0, -1, 0);
164  }
165  phone_loop_search_free_renorm(pls);
166  pls->best_score = 0;
167 
168  return 0;
169 }
170 
171 static void
172 renormalize_hmms(phone_loop_search_t *pls, int frame_idx, int32 norm)
173 {
174  phone_loop_renorm_t *rn = ckd_calloc(1, sizeof(*rn));
175  int i;
176 
177  pls->renorm = glist_add_ptr(pls->renorm, rn);
178  rn->frame_idx = frame_idx;
179  rn->norm = norm;
180 
181  for (i = 0; i < pls->n_phones; ++i) {
182  hmm_normalize((hmm_t *)&pls->phones[i], norm);
183  }
184 }
185 
186 static int32
187 evaluate_hmms(phone_loop_search_t *pls, int16 const *senscr, int frame_idx)
188 {
189  int32 bs = WORST_SCORE;
190  int i;
191 
192  hmm_context_set_senscore(pls->hmmctx, senscr);
193 
194  for (i = 0; i < pls->n_phones; ++i) {
195  hmm_t *hmm = (hmm_t *)&pls->phones[i];
196  int32 score;
197 
198  if (hmm_frame(hmm) < frame_idx)
199  continue;
200  score = hmm_vit_eval(hmm);
201  if (score BETTER_THAN bs) {
202  bs = score;
203  }
204  }
205  pls->best_score = bs;
206  return bs;
207 }
208 
209 static void
210 prune_hmms(phone_loop_search_t *pls, int frame_idx)
211 {
212  int32 thresh = pls->best_score + pls->beam;
213  int nf = frame_idx + 1;
214  int i;
215 
216  /* Check all phones to see if they remain active in the next frame. */
217  for (i = 0; i < pls->n_phones; ++i) {
218  hmm_t *hmm = (hmm_t *)&pls->phones[i];
219 
220  if (hmm_frame(hmm) < frame_idx)
221  continue;
222  /* Retain if score better than threshold. */
223  if (hmm_bestscore(hmm) BETTER_THAN thresh) {
224  hmm_frame(hmm) = nf;
225  }
226  else
227  hmm_clear_scores(hmm);
228  }
229 }
230 
231 static void
232 phone_transition(phone_loop_search_t *pls, int frame_idx)
233 {
234  int32 thresh = pls->best_score + pls->pbeam;
235  int nf = frame_idx + 1;
236  int i;
237 
238  /* Now transition out of phones whose last states are inside the
239  * phone transition beam. */
240  for (i = 0; i < pls->n_phones; ++i) {
241  hmm_t *hmm = (hmm_t *)&pls->phones[i];
242  int32 newphone_score;
243  int j;
244 
245  if (hmm_frame(hmm) != nf)
246  continue;
247 
248  newphone_score = hmm_out_score(hmm) + pls->pip;
249  if (newphone_score BETTER_THAN thresh) {
250  /* Transition into all phones using the usual Viterbi rule. */
251  for (j = 0; j < pls->n_phones; ++j) {
252  hmm_t *nhmm = (hmm_t *)&pls->phones[j];
253 
254  if (hmm_frame(nhmm) < frame_idx
255  || newphone_score BETTER_THAN hmm_in_score(nhmm)) {
256  hmm_enter(nhmm, newphone_score, hmm_out_history(hmm), nf);
257  }
258  }
259  }
260  }
261 }
262 
263 static int
264 phone_loop_search_step(ps_search_t *search, int frame_idx)
265 {
266  phone_loop_search_t *pls = (phone_loop_search_t *)search;
267  acmod_t *acmod = ps_search_acmod(search);
268  int16 const *senscr;
269  int i;
270 
271  /* All CI senones are active all the time. */
272  if (!ps_search_acmod(pls)->compallsen)
273  for (i = 0; i < pls->n_phones; ++i)
274  acmod_activate_hmm(acmod, (hmm_t *)&pls->phones[i]);
275 
276  /* Calculate senone scores for current frame. */
277  senscr = acmod_score(acmod, &frame_idx);
278 
279  /* Renormalize, if necessary. */
280  if (pls->best_score + (2 * pls->beam) WORSE_THAN WORST_SCORE) {
281  E_INFO("Renormalizing Scores at frame %d, best score %d\n",
282  frame_idx, pls->best_score);
283  renormalize_hmms(pls, frame_idx, pls->best_score);
284  }
285 
286  /* Evaluate phone HMMs for current frame. */
287  pls->best_score = evaluate_hmms(pls, senscr, frame_idx);
288 
289  /* Prune phone HMMs. */
290  prune_hmms(pls, frame_idx);
291 
292  /* Do phone transitions. */
293  phone_transition(pls, frame_idx);
294 
295  return 0;
296 }
297 
298 static int
299 phone_loop_search_finish(ps_search_t *search)
300 {
301  /* Actually nothing to do here really. */
302  return 0;
303 }
304 
305 static char const *
306 phone_loop_search_hyp(ps_search_t *search, int32 *out_score, int32 *out_is_final)
307 {
308  E_WARN("Hypotheses are not returned from phone loop search");
309  return NULL;
310 }
311 
312 static int32
313 phone_loop_search_prob(ps_search_t *search)
314 {
315  /* FIXME: Actually... they ought to be. */
316  E_WARN("Posterior probabilities are not returned from phone loop search");
317  return 0;
318 }
319 
320 static ps_seg_t *
321 phone_loop_search_seg_iter(ps_search_t *search, int32 *out_score)
322 {
323  E_WARN("Hypotheses are not returned from phone loop search");
324  return NULL;
325 }
Base structure for search module.
void hmm_init(hmm_context_t *ctx, hmm_t *hmm, int mpx, int ssid, int tmatid)
Populate a previously-allocated HMM structure, allocating internal data.
Definition: hmm.c:89
int32 pbeam
Phone exit pruning beam width.
hmm_context_t * hmmctx
HMM context structure.
void ps_search_base_reinit(ps_search_t *search, dict_t *dict, dict2pid_t *d2p)
Re-initialize base structure with new dictionary.
An individual HMM among the HMM search space.
uint8 *** tp
The transition matrices; kept in the same scale as acoustic scores; tp[tmatid][from-state][to-state]...
Definition: tmat.h:110
logmath_t * lmath
Log-math computation.
Definition: acmod.h:151
uint16 ** sseq
Unique senone sequences (2D array built at load time)
Definition: bin_mdef.h:137
void hmm_deinit(hmm_t *hmm)
Free an HMM structure, releasing internal data (but not the HMM structure itself).
Definition: hmm.c:111
void acmod_activate_hmm(acmod_t *acmod, hmm_t *hmm)
Activate senones associated with an HMM.
Definition: acmod.c:1191
void ps_search_init(ps_search_t *search, ps_searchfuncs_t *vt, cmd_ln_t *config, acmod_t *acmod, dict_t *dict, dict2pid_t *d2p)
Initialize base structure.
int32 norm
Normalization constant.
int32 hmm_vit_eval(hmm_t *hmm)
Viterbi evaluation of given HMM.
Definition: hmm.c:789
void hmm_normalize(hmm_t *h, int32 bestscr)
Renormalize the scores in this HMM based on the given best score.
Definition: hmm.c:209
hmm_context_t * hmm_context_init(int32 n_emit_state, uint8 **const *tp, int16 const *senscore, uint16 *const *sseq)
Create an HMM context.
Definition: hmm.c:56
#define WORST_SCORE
Large "bad" score.
Definition: hmm.h:88
tmat_t * tmat
Transition matrices.
Definition: acmod.h:160
int16 ciphone
Context-independent phone ID.
void hmm_enter(hmm_t *h, int32 score, int32 histid, int frame)
Enter an HMM with the given path score and history ID.
Definition: hmm.c:201
phone_loop_t * phones
Array of phone arcs.
#define hmm_context_set_senscore(ctx, senscr)
Change the senone score array for a context.
Definition: hmm.h:231
a structure for a dictionary.
Definition: dict.h:79
#define WORSE_THAN
Is one score worse than another?
Definition: hmm.h:104
void hmm_clear(hmm_t *h)
Reset the states of the HMM to the invalid condition.
Definition: hmm.c:183
Fast and rough context-independent phoneme loop search.
int32 best_score
Best Viterbi score in current frame.
int32 beam
HMM pruning beam width.
#define BETTER_THAN
Is one score better than another?
Definition: hmm.h:99
void hmm_clear_scores(hmm_t *h)
Reset the scores of the HMM.
Definition: hmm.c:170
int32 pip
Phone insertion penalty ("language score").
void hmm_context_free(hmm_context_t *ctx)
Free an HMM context.
Definition: hmm.c:80
bin_mdef_t * mdef
Model definition.
Definition: acmod.h:159
V-table for search algorithm.
Base structure for hypothesis segmentation iterator.
int frame_idx
Frame of renormalization.
Acoustic model structure.
Definition: acmod.h:148
Building composite triphone (as well as word internal triphones) with the dictionary.
Definition: dict2pid.h:148
void ps_search_deinit(ps_search_t *search)
De-initialize base structure.
int16 n_phones
Size of phone array.
Phone loop search structure.
glist_t renorm
List of renormalizations.
int16 const * acmod_score(acmod_t *acmod, int *inout_frame_idx)
Score one frame of data.
Definition: acmod.c:1088
Renormalization event.