hmm-utils.h
Go to the documentation of this file.
1 // hmm/hmm-utils.h
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #ifndef KALDI_HMM_HMM_UTILS_H_
21 #define KALDI_HMM_HMM_UTILS_H_
22 
23 #include "hmm/hmm-topology.h"
24 #include "hmm/transition-model.h"
25 #include "lat/kaldi-lattice.h"
26 
27 namespace kaldi {
28 
29 
33 
42 
44  transition_scale(1.0),
45  nonterm_phones_offset(-1) { }
46 
47  void Register (OptionsItf *opts) {
48  opts->Register("transition-scale", &transition_scale,
49  "Scale of transition probs (relative to LM)");
50  opts->Register("nonterm-phones-offset", &nonterm_phones_offset,
51  "The integer id of #nonterm_bos in phones.txt, if present. "
52  "Only needs to be set if you are doing grammar decoding, "
53  "see doc/grammar.dox.");
54  }
55 };
56 
57 
58 struct HmmCacheHash {
59  int operator () (const std::pair<int32, std::vector<int32> >&p) const {
61  int32 prime = 103049;
62  return prime*p.first + v(p.second);
63  }
64 };
65 
68 typedef unordered_map<std::pair<int32, std::vector<int32> >,
69  fst::VectorFst<fst::StdArc>*,
71 
72 
93 fst::VectorFst<fst::StdArc> *GetHmmAsFsa(
94  std::vector<int32> context_window,
95  const ContextDependencyInterface &ctx_dep,
96  const TransitionModel &trans_model,
97  const HTransducerConfig &config,
98  HmmCacheType *cache = NULL);
99 
100 
104 fst::VectorFst<fst::StdArc>*
105 GetHmmAsFsaSimple(std::vector<int32> context_window,
106  const ContextDependencyInterface &ctx_dep,
107  const TransitionModel &trans_model,
108  BaseFloat prob_scale);
109 
110 
126 fst::VectorFst<fst::StdArc>*
127 GetHTransducer(const std::vector<std::vector<int32> > &ilabel_info,
128  const ContextDependencyInterface &ctx_dep,
129  const TransitionModel &trans_model,
130  const HTransducerConfig &config,
131  std::vector<int32> *disambig_syms_left);
132 
149 void GetIlabelMapping(const std::vector<std::vector<int32> > &ilabel_info_old,
150  const ContextDependencyInterface &ctx_dep,
151  const TransitionModel &trans_model,
152  std::vector<int32> *old2new_map);
153 
154 
155 
185 void AddSelfLoops(const TransitionModel &trans_model,
186  const std::vector<int32> &disambig_syms, // used as a check only.
187  BaseFloat self_loop_scale,
188  bool reorder,
189  bool check_no_self_loops,
190  fst::VectorFst<fst::StdArc> *fst);
191 
209 void AddTransitionProbs(const TransitionModel &trans_model,
210  const std::vector<int32> &disambig_syms,
212  BaseFloat self_loop_scale,
213  fst::VectorFst<fst::StdArc> *fst);
214 
219 void AddTransitionProbs(const TransitionModel &trans_model,
221  BaseFloat self_loop_scale,
222  Lattice *lat);
223 
224 
227 fst::VectorFst<fst::StdArc>*
229 
232 void ConvertTransitionIdsToPdfs(const TransitionModel &trans_model,
233  const std::vector<int32> &disambig_syms,
234  fst::VectorFst<fst::StdArc> *fst);
235 
237 
240 
251 bool SplitToPhones(const TransitionModel &trans_model,
252  const std::vector<int32> &alignment,
253  std::vector<std::vector<int32> > *split_alignment);
254 
287 bool ConvertAlignment(const TransitionModel &old_trans_model,
288  const TransitionModel &new_trans_model,
289  const ContextDependencyInterface &new_ctx_dep,
290  const std::vector<int32> &old_alignment,
291  int32 subsample_factor, // 1 in the normal case -> no subsampling.
292  bool repeat_frames,
293  bool reorder,
294  const std::vector<int32> *phone_map, // may be NULL
295  std::vector<int32> *new_alignment);
296 
297 // ConvertPhnxToProns is only needed in bin/phones-to-prons.cc and
298 // isn't closely related with HMMs, but we put it here as there isn't
299 // any other obvious place for it and it needs to be tested.
300 // This function takes a phone-sequence with word-start and word-end
301 // markers in it, and a word-sequence, and outputs the pronunciations
302 // "prons"... the format of "prons" is, each element is a vector,
303 // where the first element is the word (or zero meaning no word, e.g.
304 // for optional silence introduced by the lexicon), and the remaining
305 // elements are the phones in the word's pronunciation.
306 // It returns false if it encounters a problem of some kind, e.g.
307 // if the phone-sequence doesn't seem to have the right number of
308 // words in it.
309 bool ConvertPhnxToProns(const std::vector<int32> &phnx,
310  const std::vector<int32> &words,
311  int32 word_start_sym,
312  int32 word_end_sym,
313  std::vector<std::vector<int32> > *prons);
314 
315 
316 /* Generates a random alignment for this phone, of length equal to
317  alignment->size(), which is required to be at least the MinLength() of the
318  topology for this phone, or this function will crash.
319  The alignment will be without 'reordering'.
320 */
322  const TransitionModel &trans_model,
323  const std::vector<int32> &phone_window,
324  std::vector<int32> *alignment);
325 
326 /*
327  If the alignment was non-reordered makes it reordered, and vice versa.
328 */
329 void ChangeReorderingOfAlignment(const TransitionModel &trans_model,
330  std::vector<int32> *alignment);
331 
332 
333 // GetPdfToPhonesMap creates a map which maps each pdf-id into its
334 // corresponding monophones.
335 void GetPdfToPhonesMap(const TransitionModel &trans_model,
336  std::vector<std::set<int32> > *pdf2phones);
337 
339 
340 } // end namespace kaldi
341 
342 
343 #endif
int32 words[kMaxOrder]
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
A hashing function-object for vectors.
Definition: stl-utils.h:216
void Register(OptionsItf *opts)
Definition: hmm-utils.h:47
void GetRandomAlignmentForPhone(const ContextDependencyInterface &ctx_dep, const TransitionModel &trans_model, const std::vector< int32 > &phone_window, std::vector< int32 > *alignment)
Definition: hmm-utils.cc:1207
bool ConvertPhnxToProns(const std::vector< int32 > &phnx, const std::vector< int32 > &words, int32 word_start_sym, int32 word_end_sym, std::vector< std::vector< int32 > > *prons)
Definition: hmm-utils.cc:1161
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
void AddSelfLoops(const TransitionModel &trans_model, const std::vector< int32 > &disambig_syms, BaseFloat self_loop_scale, bool reorder, bool check_no_self_loops, fst::VectorFst< fst::StdArc > *fst)
For context, see AddSelfLoops().
Definition: hmm-utils.cc:602
void GetIlabelMapping(const std::vector< std::vector< int32 > > &ilabel_info_old, const ContextDependencyInterface &ctx_dep, const TransitionModel &trans_model, std::vector< int32 > *old2new_map)
GetIlabelMapping produces a mapping that&#39;s similar to HTK&#39;s logical-to-physical model mapping (i...
Definition: hmm-utils.cc:335
unordered_map< std::pair< int32, std::vector< int32 > >, fst::VectorFst< fst::StdArc > *, HmmCacheHash > HmmCacheType
HmmCacheType is a map from (central-phone, sequence of pdf-ids) to FST, used as cache in GetHmmAsFsa...
Definition: hmm-utils.h:70
kaldi::int32 int32
virtual void Register(const std::string &name, bool *ptr, const std::string &doc)=0
bool SplitToPhones(const TransitionModel &trans_model, const std::vector< int32 > &alignment, std::vector< std::vector< int32 > > *split_alignment)
SplitToPhones splits up the TransitionIds in "alignment" into their individual phones (one vector per...
Definition: hmm-utils.cc:723
Configuration class for the GetHTransducer() function; see The HTransducerConfig configuration class ...
Definition: hmm-utils.h:36
void AddTransitionProbs(const TransitionModel &trans_model, const std::vector< int32 > &disambig_syms, BaseFloat transition_scale, BaseFloat self_loop_scale, fst::VectorFst< fst::StdArc > *fst)
Adds transition-probs, with the supplied scales (see Scaling of transition and acoustic probabilities...
Definition: hmm-utils.cc:1088
float BaseFloat
Definition: kaldi-types.h:29
void GetPdfToPhonesMap(const TransitionModel &trans_model, std::vector< std::set< int32 > > *pdf2phones)
Definition: hmm-utils.cc:1292
fst::VectorFst< LatticeArc > Lattice
Definition: kaldi-lattice.h:44
fst::VectorFst< fst::StdArc > * GetHTransducer(const std::vector< std::vector< int32 > > &ilabel_info, const ContextDependencyInterface &ctx_dep, const TransitionModel &trans_model, const HTransducerConfig &config, std::vector< int32 > *disambig_syms_left)
Returns the H tranducer; result owned by caller.
Definition: hmm-utils.cc:254
context-dep-itf.h provides a link between the tree-building code in ../tree/, and the FST code in ...
BaseFloat transition_scale
Transition log-prob scale, see Scaling of transition and acoustic probabilities.
Definition: hmm-utils.h:40
fst::VectorFst< fst::StdArc > * GetHmmAsFsa(std::vector< int32 > phone_window, const ContextDependencyInterface &ctx_dep, const TransitionModel &trans_model, const HTransducerConfig &config, HmmCacheType *cache)
Called by GetHTransducer() and probably will not need to be called directly; it creates and returns t...
Definition: hmm-utils.cc:32
void ConvertTransitionIdsToPdfs(const TransitionModel &trans_model, const std::vector< int32 > &disambig_syms, fst::VectorFst< fst::StdArc > *fst)
Converts all transition-ids in the FST to pdfs plus one.
void ChangeReorderingOfAlignment(const TransitionModel &trans_model, std::vector< int32 > *alignment)
Definition: hmm-utils.cc:1260
bool ConvertAlignment(const TransitionModel &old_trans_model, const TransitionModel &new_trans_model, const ContextDependencyInterface &new_ctx_dep, const std::vector< int32 > &old_alignment, int32 subsample_factor, bool repeat_frames, bool new_is_reordered, const std::vector< int32 > *phone_map, std::vector< int32 > *new_alignment)
ConvertAlignment converts an alignment that was created using one model, to another model...
Definition: hmm-utils.cc:1013
fst::VectorFst< fst::StdArc > * GetHmmAsFsaSimple(std::vector< int32 > phone_window, const ContextDependencyInterface &ctx_dep, const TransitionModel &trans_model, BaseFloat prob_scale)
Included mainly as a form of documentation, not used in any other code currently. ...
Definition: hmm-utils.cc:155
fst::VectorFst< fst::StdArc > * GetPdfToTransitionIdTransducer(const TransitionModel &trans_model)
Returns a transducer from pdfs plus one (input) to transition-ids (output).
Definition: hmm-utils.cc:407