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

Go to the source code of this file.

Functions

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

Function Documentation

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

Definition at line 32 of file decode-faster.cc.

References FasterDecoder::Decode(), SequentialTableReader< Holder >::Done(), Timer::Elapsed(), ParseOptions::GetArg(), FasterDecoder::GetBestPath(), fst::GetLinearSymbolSequence(), ParseOptions::GetOptArg(), rnnlm::i, TableWriter< Holder >::IsOpen(), KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), FasterDecoder::ReachedFinal(), ParseOptions::Read(), fst::ReadFstKaldi(), FasterDecoderOptions::Register(), ParseOptions::Register(), SequentialTableReader< Holder >::Value(), LatticeWeightTpl< FloatType >::Value1(), LatticeWeightTpl< FloatType >::Value2(), words, and TableWriter< Holder >::Write().

32  {
33  try {
34  using namespace kaldi;
35  typedef kaldi::int32 int32;
36  using fst::SymbolTable;
37  using fst::VectorFst;
38  using fst::StdArc;
39 
40  const char *usage =
41  "Decode, reading log-likelihoods (of transition-ids or whatever symbol is on the graph)\n"
42  "as matrices. Note: you'll usually want decode-faster-mapped rather than this program.\n"
43  "\n"
44  "Usage: decode-faster [options] <fst-in> <loglikes-rspecifier> <words-wspecifier> [<alignments-wspecifier>]\n";
45  ParseOptions po(usage);
46  bool binary = true;
47  BaseFloat acoustic_scale = 0.1;
48  bool allow_partial = true;
49  std::string word_syms_filename;
50  FasterDecoderOptions decoder_opts;
51  decoder_opts.Register(&po, true); // true == include obscure settings.
52  po.Register("binary", &binary, "Write output in binary mode");
53  po.Register("allow-partial", &allow_partial, "Produce output even when final state was not reached");
54  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
55  po.Register("word-symbol-table", &word_syms_filename, "Symbol table for words [for debug output]");
56 
57  po.Read(argc, argv);
58 
59  if (po.NumArgs() < 3 || po.NumArgs() > 4) {
60  po.PrintUsage();
61  exit(1);
62  }
63 
64  std::string fst_in_filename = po.GetArg(1),
65  loglikes_rspecifier = po.GetArg(2),
66  words_wspecifier = po.GetArg(3),
67  alignment_wspecifier = po.GetOptArg(4);
68 
69  Int32VectorWriter words_writer(words_wspecifier);
70 
71  Int32VectorWriter alignment_writer(alignment_wspecifier);
72 
73  fst::SymbolTable *word_syms = NULL;
74  if (word_syms_filename != "") {
75  word_syms = fst::SymbolTable::ReadText(word_syms_filename);
76  if (!word_syms)
77  KALDI_ERR << "Could not read symbol table from file "<<word_syms_filename;
78  }
79 
80  SequentialBaseFloatMatrixReader loglikes_reader(loglikes_rspecifier);
81 
82  // It's important that we initialize decode_fst after loglikes_reader, as it
83  // can prevent crashes on systems installed without enough virtual memory.
84  // It has to do with what happens on UNIX systems if you call fork() on a
85  // large process: the page-table entries are duplicated, which requires a
86  // lot of virtual memory.
87  VectorFst<StdArc> *decode_fst = fst::ReadFstKaldi(fst_in_filename);
88 
89  BaseFloat tot_like = 0.0;
90  kaldi::int64 frame_count = 0;
91  int num_success = 0, num_fail = 0;
92  FasterDecoder decoder(*decode_fst, decoder_opts);
93 
94  Timer timer;
95 
96  for (; !loglikes_reader.Done(); loglikes_reader.Next()) {
97  std::string key = loglikes_reader.Key();
98  const Matrix<BaseFloat> &loglikes (loglikes_reader.Value());
99 
100  if (loglikes.NumRows() == 0) {
101  KALDI_WARN << "Zero-length utterance: " << key;
102  num_fail++;
103  continue;
104  }
105 
106  DecodableMatrixScaled decodable(loglikes, acoustic_scale);
107  decoder.Decode(&decodable);
108 
109  VectorFst<LatticeArc> decoded; // linear FST.
110 
111  if ( (allow_partial || decoder.ReachedFinal())
112  && decoder.GetBestPath(&decoded) ) {
113  num_success++;
114  if (!decoder.ReachedFinal())
115  KALDI_WARN << "Decoder did not reach end-state, outputting partial traceback.";
116 
117  std::vector<int32> alignment;
118  std::vector<int32> words;
119  LatticeWeight weight;
120  frame_count += loglikes.NumRows();
121 
122  GetLinearSymbolSequence(decoded, &alignment, &words, &weight);
123 
124  words_writer.Write(key, words);
125  if (alignment_writer.IsOpen())
126  alignment_writer.Write(key, alignment);
127  if (word_syms != NULL) {
128  std::cerr << key << ' ';
129  for (size_t i = 0; i < words.size(); i++) {
130  std::string s = word_syms->Find(words[i]);
131  if (s == "")
132  KALDI_ERR << "Word-id " << words[i] <<" not in symbol table.";
133  std::cerr << s << ' ';
134  }
135  std::cerr << '\n';
136  }
137  BaseFloat like = -weight.Value1() -weight.Value2();
138  tot_like += like;
139  KALDI_LOG << "Log-like per frame for utterance " << key << " is "
140  << (like / loglikes.NumRows()) << " over "
141  << loglikes.NumRows() << " frames.";
142 
143  } else {
144  num_fail++;
145  KALDI_WARN << "Did not successfully decode utterance " << key
146  << ", len = " << loglikes.NumRows();
147  }
148  }
149 
150  double elapsed = timer.Elapsed();
151  KALDI_LOG << "Time taken [excluding initialization] "<< elapsed
152  << "s: real-time factor assuming 100 frames/sec is "
153  << (elapsed*100.0/frame_count);
154  KALDI_LOG << "Done " << num_success << " utterances, failed for "
155  << num_fail;
156  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count)
157  << " over " << frame_count << " frames.";
158 
159  delete word_syms;
160  delete decode_fst;
161  if (num_success != 0) return 0;
162  else return 1;
163  } catch(const std::exception &e) {
164  std::cerr << e.what();
165  return -1;
166  }
167 }
int32 words[kMaxOrder]
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
fst::StdArc StdArc
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
double Elapsed()
Returns time in seconds.
Definition: timer.h:65
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
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#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 ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
#define KALDI_LOG
Definition: kaldi-error.h:133