All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
gmm-decode-nbest.cc File Reference
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "gmm/am-diag-gmm.h"
#include "hmm/transition-model.h"
#include "fst/fstlib.h"
#include "fstext/fstext-lib.h"
#include "decoder/nbest-decoder.h"
#include "gmm/decodable-am-diag-gmm.h"
#include "base/timer.h"
#include "lat/kaldi-lattice.h"
#include "fstext/lattice-utils.h"
Include dependency graph for gmm-decode-nbest.cc:

Go to the source code of this file.

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-nbest.cc.

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

68  {
69  try {
70  typedef kaldi::int32 int32;
71 
72  const char *usage =
73  "Decode features using GMM-based model, producing N-best lattice output.\n"
74  "Note: this program was mainly intended to validate the lattice generation\n"
75  "algorithm and is not very useful; in general, processing the\n"
76  "lattices into n-best lists will be more efficient.\n"
77  "Usage: gmm-decode-nbest [options] <model-in> <fst-in> <features-rspecifier> "
78  "<nbest-lattice-wspecifier> <words-wspecifier> [<alignments-wspecifier>]\n";
79  ParseOptions po(usage);
80  bool allow_partial = true;
81  BaseFloat acoustic_scale = 0.1;
82 
83  std::string word_syms_filename;
84  NBestDecoderOptions decoder_opts;
85  decoder_opts.Register(&po, true); // true == include obscure settings.
86  po.Register("acoustic-scale", &acoustic_scale,
87  "Scaling factor for acoustic likelihoods");
88  po.Register("word-symbol-table", &word_syms_filename,
89  "Symbol table for words [for debug output]");
90  po.Register("allow-partial", &allow_partial,
91  "Produce output even when final state was not reached");
92  po.Read(argc, argv);
93 
94  if (po.NumArgs() < 4 || po.NumArgs() > 6) {
95  po.PrintUsage();
96  exit(1);
97  }
98 
99  std::string model_in_filename = po.GetArg(1),
100  fst_in_filename = po.GetArg(2),
101  feature_rspecifier = po.GetArg(3),
102  lattice_wspecifier = po.GetArg(4),
103  words_wspecifier = po.GetOptArg(5),
104  alignment_wspecifier = po.GetOptArg(6);
105 
106  TransitionModel trans_model;
107  AmDiagGmm am_gmm;
108  {
109  bool binary;
110  Input ki(model_in_filename, &binary);
111  trans_model.Read(ki.Stream(), binary);
112  am_gmm.Read(ki.Stream(), binary);
113  }
114 
115  CompactLatticeWriter compact_lattice_writer;
116  if (!compact_lattice_writer.Open(lattice_wspecifier)) {
117  KALDI_ERR << "Could not open table for writing lattices: "
118  << lattice_wspecifier;
119  }
120 
121  Int32VectorWriter words_writer(words_wspecifier);
122 
123  Int32VectorWriter alignment_writer(alignment_wspecifier);
124 
125  fst::SymbolTable *word_syms = NULL;
126  if (word_syms_filename != "")
127  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
128  KALDI_ERR << "Could not read symbol table from file "
129  << word_syms_filename;
130 
131  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
132 
133  // It's important that we initialize decode_fst after feature_reader, as it
134  // can prevent crashes on systems installed without enough virtual memory.
135  // It has to do with what happens on UNIX systems if you call fork() on a
136  // large process: the page-table entries are duplicated, which requires a
137  // lot of virtual memory.
138  fst::Fst<fst::StdArc> *decode_fst = ReadNetwork(fst_in_filename);
139 
140  BaseFloat tot_like = 0.0;
141  kaldi::int64 frame_count = 0;
142  int num_success = 0, num_fail = 0;
143  NBestDecoder decoder(*decode_fst, decoder_opts);
144 
145  Timer timer;
146 
147  for (; !feature_reader.Done(); feature_reader.Next()) {
148  std::string key = feature_reader.Key();
149  Matrix<BaseFloat> features (feature_reader.Value());
150  feature_reader.FreeCurrent();
151  if (features.NumRows() == 0) {
152  KALDI_WARN << "Zero-length utterance: " << key;
153  num_fail++;
154  continue;
155  }
156 
157  DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model, features,
158  acoustic_scale);
159  decoder.Decode(&gmm_decodable);
160 
161  fst::VectorFst<CompactLatticeArc> decoded; // output FST.
162  bool was_final;
163  int32 nbest;
164  BaseFloat nbest_beam;
165  if (decoder.GetNBestLattice(&decoded, &was_final, &nbest, &nbest_beam)) {
166  if (!was_final) {
167  if (allow_partial) {
168  KALDI_WARN << "Decoder did not reach end-state, "
169  << "outputting partial traceback since --allow-partial=true";
170  } else {
171  KALDI_WARN << "Decoder did not reach end-state, "
172  << "output partial traceback with --allow-partial=true";
173  num_fail++;
174  KALDI_WARN << "Did not successfully decode utterance " << key
175  << ", len = " << features.NumRows();
176  continue; // next utterance
177  }
178  }
179  num_success++;
180  KALDI_LOG << "retrieved:" << nbest << " tokens, effective beam:" << nbest_beam;
181 
182 // std::cout << "n-best paths:\n";
183 // fst::FstPrinter<CompactLatticeArc> fstprinter(decoded, NULL, NULL, NULL, false, true);
184 // fstprinter.Print(&std::cout, "standard output");
185 
186  if (acoustic_scale != 0.0) // We'll write the lattice without acoustic scaling
187  fst::ScaleLattice(fst::AcousticLatticeScale(1.0 / acoustic_scale), &decoded);
188  compact_lattice_writer.Write(key, decoded);
189 
190  fst::VectorFst<CompactLatticeArc> decoded1;
191  ShortestPath(decoded, &decoded1);
192  fst::VectorFst<LatticeArc> utterance;
193  ConvertLattice(decoded1, &utterance, true);
194 
195  std::vector<int32> alignment;
196  std::vector<int32> words;
197  LatticeWeight weight;
198  frame_count += features.NumRows();
199 
200  GetLinearSymbolSequence(utterance, &alignment, &words, &weight);
201 
202  words_writer.Write(key, words);
203  if (alignment_writer.IsOpen())
204  alignment_writer.Write(key, alignment);
205  if (word_syms != NULL) {
206  std::cerr << key << ' ';
207  for (size_t i = 0; i < words.size(); i++) {
208  std::string s = word_syms->Find(words[i]);
209  if (s == "")
210  KALDI_ERR << "Word-id " << words[i] <<" not in symbol table.";
211  std::cerr << s << ' ';
212  }
213  std::cerr << '\n';
214  }
215  BaseFloat like = -(weight.Value1() - weight.Value2());
216  // KALDI_LOG << "final weight:" << weight.Value1() << "," << weight.Value2();
217  tot_like += like;
218  KALDI_LOG << "Log-like per frame for utterance " << key << " is "
219  << (like / features.NumRows()) << " over "
220  << features.NumRows() << " frames.";
221  } else {
222  num_fail++;
223  KALDI_WARN << "Did not successfully decode utterance " << key
224  << ", len = " << features.NumRows();
225  }
226  }
227 
228  double elapsed = timer.Elapsed();
229  KALDI_LOG << "Time taken [excluding initialization] "<< elapsed
230  << "s: real-time factor assuming 100 frames/sec is "
231  << (elapsed*100.0/frame_count);
232  KALDI_LOG << "Done " << num_success << " utterances, failed for "
233  << num_fail;
234  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count) << " over "
235  << frame_count<<" frames.";
236 
237  delete word_syms;
238  delete decode_fst;
239  if (num_success != 0) return 0;
240  else return 1;
241  } catch(const std::exception &e) {
242  std::cerr << e.what();
243  return -1;
244  }
245 }
int32 words[kMaxOrder]
bool Open(const std::string &wspecifier)
void Write(const std::string &key, const T &value) const
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
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
fst::Fst< fst::StdArc > * ReadNetwork(std::string filename)
#define KALDI_WARN
Definition: kaldi-error.h:130
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 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 Register(OptionsItf *opts, bool full)
Definition: nbest-decoder.h:44
Caution: this decoder was written essentially as testing code for lattice-faster-decoder, and is not very useful; it would be better to pipe the lattices from lattice-faster-decoder into lattice-to-nbest if you want an n-best list.
Definition: nbest-decoder.h:68
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
fst::Fst<fst::StdArc>* ReadNetwork ( std::string  filename)

Definition at line 35 of file gmm-decode-nbest.cc.

References KALDI_ERR, and Input::Stream().

35  {
36  // read decoding network FST
37  Input ki(filename); // use ki.Stream() instead of is.
38  if (!ki.Stream().good()) KALDI_ERR << "Could not open decoding-graph FST "
39  << filename;
40 
41  fst::FstHeader hdr;
42  if (!hdr.Read(ki.Stream(), "<unknown>")) {
43  KALDI_ERR << "Reading FST: error reading FST header.";
44  }
45  if (hdr.ArcType() != fst::StdArc::Type()) {
46  KALDI_ERR << "FST with arc type " << hdr.ArcType() << " not supported.";
47  }
48  fst::FstReadOptions ropts("<unspecified>", &hdr);
49 
50  fst::Fst<fst::StdArc> *decode_fst = NULL;
51 
52  if (hdr.FstType() == "vector") {
53  decode_fst = fst::VectorFst<fst::StdArc>::Read(ki.Stream(), ropts);
54  } else if (hdr.FstType() == "const") {
55  decode_fst = fst::ConstFst<fst::StdArc>::Read(ki.Stream(), ropts);
56  } else {
57  KALDI_ERR << "Reading FST: unsupported FST type: " << hdr.FstType();
58  }
59  if (decode_fst == NULL) { // fst code will warn.
60  KALDI_ERR << "Error reading FST (after reading header).";
61  return NULL;
62  } else {
63  return decode_fst;
64  }
65 }
#define KALDI_ERR
Definition: kaldi-error.h:127