gmm-decode-biglm-faster.cc
Go to the documentation of this file.
1 // gmmbin/gmm-decode-biglm-faster.cc
2 
3 // Copyright 2009-2011 Gilles Boulianne 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 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "gmm/am-diag-gmm.h"
24 #include "hmm/transition-model.h"
25 #include "fstext/fstext-lib.h"
28 #include "base/timer.h"
29 
30 namespace kaldi {
31 
32 fst::Fst<fst::StdArc> *ReadNetwork(std::string filename) {
33  // read decoding network FST
34  Input ki(filename); // use ki.Stream() instead of is.
35  if (!ki.Stream().good()) KALDI_ERR << "Could not open decoding-graph FST "
36  << filename;
37 
38  fst::FstHeader hdr;
39  if (!hdr.Read(ki.Stream(), "<unknown>")) {
40  KALDI_ERR << "Reading FST: error reading FST header.";
41  }
42  if (hdr.ArcType() != fst::StdArc::Type()) {
43  KALDI_ERR << "FST with arc type " << hdr.ArcType() << " not supported.";
44  }
45  fst::FstReadOptions ropts("<unspecified>", &hdr);
46 
47  fst::Fst<fst::StdArc> *decode_fst = NULL;
48 
49  if (hdr.FstType() == "vector") {
50  decode_fst = fst::VectorFst<fst::StdArc>::Read(ki.Stream(), ropts);
51  } else if (hdr.FstType() == "const") {
52  decode_fst = fst::ConstFst<fst::StdArc>::Read(ki.Stream(), ropts);
53  } else {
54  KALDI_ERR << "Reading FST: unsupported FST type: " << hdr.FstType();
55  }
56  if (decode_fst == NULL) { // fst code will warn.
57  KALDI_ERR << "Error reading FST (after reading header).";
58  return NULL;
59  } else {
60  return decode_fst;
61  }
62 }
63 
64 }
65 
66 
67 
68 int main(int argc, char *argv[])
69 {
70  try {
71  using namespace kaldi;
72  typedef kaldi::int32 int32;
73  using fst::SymbolTable;
74  using fst::VectorFst;
75  using fst::Fst;
76  using fst::StdArc;
77  using fst::ReadFstKaldi;
78 
79  const char *usage =
80  "Decode features using GMM-based model.\n"
81  "User supplies LM used to generate decoding graph, and desired LM;\n"
82  "this decoder applies the difference during decoding\n"
83  "Usage: gmm-decode-biglm-faster [options] model-in fst-in oldlm-fst-in newlm-fst-in features-rspecifier words-wspecifier [alignments-wspecifier [lattice-wspecifier]]\n";
84  ParseOptions po(usage);
85  bool allow_partial = true;
86  BaseFloat acoustic_scale = 0.1;
87 
88  std::string word_syms_filename;
89  BiglmFasterDecoderOptions decoder_opts;
90  decoder_opts.Register(&po, true); // true == include obscure settings.
91  po.Register("acoustic-scale", &acoustic_scale,
92  "Scaling factor for acoustic likelihoods");
93  po.Register("word-symbol-table", &word_syms_filename,
94  "Symbol table for words [for debug output]");
95  po.Register("allow-partial", &allow_partial,
96  "Produce output even when final state was not reached");
97 
98  po.Read(argc, argv);
99 
100  if (po.NumArgs() < 6 || po.NumArgs() > 8) {
101  po.PrintUsage();
102  exit(1);
103  }
104 
105  std::string model_rxfilename = po.GetArg(1),
106  fst_rxfilename = po.GetArg(2),
107  old_lm_fst_rxfilename = po.GetArg(3),
108  new_lm_fst_rxfilename = po.GetArg(4),
109  feature_rspecifier = po.GetArg(5),
110  words_wspecifier = po.GetArg(6),
111  alignment_wspecifier = po.GetOptArg(7),
112  lattice_wspecifier = po.GetOptArg(8);
113 
114  TransitionModel trans_model;
115  AmDiagGmm am_gmm;
116  {
117  bool binary;
118  Input ki(model_rxfilename, &binary);
119  trans_model.Read(ki.Stream(), binary);
120  am_gmm.Read(ki.Stream(), binary);
121  }
122 
123  Int32VectorWriter words_writer(words_wspecifier);
124 
125  Int32VectorWriter alignment_writer(alignment_wspecifier);
126 
127  CompactLatticeWriter clat_writer(lattice_wspecifier);
128 
129  fst::SymbolTable *word_syms = NULL;
130  if (word_syms_filename != "") {
131  word_syms = fst::SymbolTable::ReadText(word_syms_filename);
132  if (!word_syms)
133  KALDI_ERR << "Could not read symbol table from file "<<word_syms_filename;
134  }
135 
136  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
137 
138 
139  // It's important that we initialize decode_fst after feature_reader, as it
140  // can prevent crashes on systems installed without enough virtual memory.
141  // It has to do with what happens on UNIX systems if you call fork() on a
142  // large process: the page-table entries are duplicated, which requires a
143  // lot of virtual memory.
144  Fst<StdArc> *decode_fst = ReadNetwork(fst_rxfilename);
145 
146  VectorFst<StdArc> *old_lm_fst = ReadFstKaldi(old_lm_fst_rxfilename);
147  ApplyProbabilityScale(-1.0, old_lm_fst); // Negate old LM probs...
148 
149  VectorFst<StdArc> *new_lm_fst = ReadFstKaldi(new_lm_fst_rxfilename);
150 
151 
152  BaseFloat tot_like = 0.0;
153  kaldi::int64 frame_count = 0;
154  int num_success = 0, num_fail = 0;
155 
156  Timer timer;
157 
158  for (; !feature_reader.Done(); feature_reader.Next()) {
159  std::string key = feature_reader.Key();
160  Matrix<BaseFloat> features (feature_reader.Value());
161  feature_reader.FreeCurrent();
162  if (features.NumRows() == 0) {
163  KALDI_WARN << "Zero-length utterance: " << key;
164  num_fail++;
165  continue;
166  }
167  fst::BackoffDeterministicOnDemandFst<StdArc> old_lm_dfst(*old_lm_fst);
168  fst::BackoffDeterministicOnDemandFst<StdArc> new_lm_dfst(*new_lm_fst);
169  fst::ComposeDeterministicOnDemandFst<StdArc> compose_dfst(&old_lm_dfst,
170  &new_lm_dfst);
171  fst::CacheDeterministicOnDemandFst<StdArc> cache_dfst(&compose_dfst);
172 
173  BiglmFasterDecoder decoder(*decode_fst, decoder_opts, &cache_dfst);
174 
175  DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model, features,
176  acoustic_scale);
177  decoder.Decode(&gmm_decodable);
178 
179  std::cerr << "Length of file is "<<features.NumRows()<<'\n';
180 
181  fst::VectorFst<LatticeArc> decoded; // linear FST.
182 
183  if ( (allow_partial || decoder.ReachedFinal())
184  && decoder.GetBestPath(&decoded) ) {
185  if (!decoder.ReachedFinal())
186  KALDI_WARN << "Decoder did not reach end-state, "
187  << "outputting partial traceback since --allow-partial=true";
188  num_success++;
189  if (!decoder.ReachedFinal())
190  KALDI_WARN << "Decoder did not reach end-state, outputting partial traceback.";
191  std::vector<int32> alignment;
192  std::vector<int32> words;
193  LatticeWeight weight;
194  frame_count += features.NumRows();
195 
196  GetLinearSymbolSequence(decoded, &alignment, &words, &weight);
197 
198  words_writer.Write(key, words);
199  if (alignment_writer.IsOpen())
200  alignment_writer.Write(key, alignment);
201 
202  if (lattice_wspecifier != "") {
203  if (acoustic_scale != 0.0) // We'll write the lattice without acoustic scaling
204  fst::ScaleLattice(fst::AcousticLatticeScale(1.0 / acoustic_scale), &decoded);
205  fst::VectorFst<CompactLatticeArc> clat;
206  ConvertLattice(decoded, &clat, true);
207  clat_writer.Write(key, clat);
208  }
209 
210  if (word_syms != NULL) {
211  std::cerr << key << ' ';
212  for (size_t i = 0; i < words.size(); i++) {
213  std::string s = word_syms->Find(words[i]);
214  if (s == "")
215  KALDI_ERR << "Word-id " << words[i] <<" not in symbol table.";
216  std::cerr << s << ' ';
217  }
218  std::cerr << '\n';
219  }
220  BaseFloat like = -weight.Value1() -weight.Value2();
221  tot_like += like;
222  KALDI_LOG << "Log-like per frame for utterance " << key << " is "
223  << (like / features.NumRows()) << " over "
224  << features.NumRows() << " frames.";
225  KALDI_VLOG(2) << "Cost for utterance " << key << " is "
226  << weight.Value1() << " + " << weight.Value2();
227  } else {
228  num_fail++;
229  KALDI_WARN << "Did not successfully decode utterance " << key
230  << ", len = " << features.NumRows();
231  }
232  }
233 
234  double elapsed = timer.Elapsed();
235  KALDI_LOG << "Time taken [excluding initialization] "<< elapsed
236  << "s: real-time factor assuming 100 frames/sec is "
237  << (elapsed*100.0/frame_count);
238  KALDI_LOG << "Done " << num_success << " utterances, failed for "
239  << num_fail;
240  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count) << " over "
241  << frame_count<<" frames.";
242 
243  delete word_syms;
244  delete decode_fst;
245  delete old_lm_fst;
246  delete new_lm_fst;
247  return (num_success != 0 ? 0 : 1);
248  } catch(const std::exception &e) {
249  std::cerr << e.what();
250  return -1;
251  }
252 }
253 
254 
int32 words[kMaxOrder]
This class wraps an Fst, representing a language model, using the interface for "BackoffDeterministic...
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
fst::Fst< fst::StdArc > * ReadNetwork(std::string filename)
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
fst::StdArc StdArc
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
bool GetLinearSymbolSequence(const Fst< Arc > &fst, std::vector< I > *isymbols_out, std::vector< I > *osymbols_out, typename Arc::Weight *tot_weight_out)
GetLinearSymbolSequence gets the symbol sequence from a linear FST.
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
std::vector< std::vector< double > > AcousticLatticeScale(double acwt)
std::istream & Stream()
Definition: kaldi-io.cc:826
float BaseFloat
Definition: kaldi-types.h:29
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
This is as FasterDecoder, but does online composition between HCLG and the "difference language model...
void ScaleLattice(const std::vector< std::vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a...
void Read(std::istream &is, bool binary)
int main(int argc, char *argv[])
void ConvertLattice(const ExpandedFst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *ofst, bool invert)
Convert lattice from a normal FST to a CompactLattice FST.
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_ERR
Definition: kaldi-error.h:147
bool GetBestPath(fst::MutableFst< LatticeArc > *fst_out, bool use_final_probs=true)
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
void Register(OptionsItf *opts, bool full)
void ApplyProbabilityScale(float scale, MutableFst< Arc > *fst)
ApplyProbabilityScale is applicable to FSTs in the log or tropical semiring.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
void Decode(DecodableInterface *decodable)
#define KALDI_LOG
Definition: kaldi-error.h:153
double Elapsed() const
Returns time in seconds.
Definition: timer.h:74
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147
std::string GetOptArg(int param) const