All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
gmm-decode-biglm-faster.cc File Reference
Include dependency graph for gmm-decode-biglm-faster.cc:

Go to the source code of this file.

Namespaces

 kaldi
 Relabels neural network egs with the read pdf-id alignments.
 

Functions

fst::Fst< fst::StdArc > * ReadNetwork (std::string filename)
 
int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 68 of file gmm-decode-biglm-faster.cc.

References fst::AcousticLatticeScale(), fst::ApplyProbabilityScale(), fst::ConvertLattice(), BiglmFasterDecoder::Decode(), SequentialTableReader< Holder >::Done(), Timer::Elapsed(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), BiglmFasterDecoder::GetBestPath(), fst::GetLinearSymbolSequence(), ParseOptions::GetOptArg(), rnnlm::i, TableWriter< Holder >::IsOpen(), KALDI_ERR, KALDI_LOG, KALDI_VLOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), BiglmFasterDecoder::ReachedFinal(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), fst::ReadFstKaldi(), kaldi::ReadNetwork(), FasterDecoderOptions::Register(), ParseOptions::Register(), fst::ScaleLattice(), Input::Stream(), SequentialTableReader< Holder >::Value(), LatticeWeightTpl< FloatType >::Value1(), LatticeWeightTpl< FloatType >::Value2(), words, and TableWriter< Holder >::Write().

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 }
int32 words[kMaxOrder]
This class wraps a conventional Fst, representing a language model, in the interface for "BackoffDete...
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
fst::Fst< fst::StdArc > * ReadNetwork(std::string filename)
fst::StdArc StdArc
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
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 Read(std::istream &is, bool binary)
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
vector< vector< double > > AcousticLatticeScale(double acwt)
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_WARN
Definition: kaldi-error.h:130
void Register(OptionsItf *opts, bool full)
bool GetLinearSymbolSequence(const Fst< Arc > &fst, vector< I > *isymbols_out, vector< I > *osymbols_out, typename Arc::Weight *tot_weight_out)
GetLinearSymbolSequence gets the symbol sequence from a linear FST.
void ApplyProbabilityScale(float scale, MutableFst< Arc > *fst)
ApplyProbabilityScale is applicable to FSTs in the log or tropical semiring.
void ScaleLattice(const vector< vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a...
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
#define KALDI_VLOG(v)
Definition: kaldi-error.h:136
double Elapsed() const
Returns time in seconds.
Definition: timer.h:74
#define KALDI_LOG
Definition: kaldi-error.h:133
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147