All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
decode-faster-mapped.cc File Reference
Include dependency graph for decode-faster-mapped.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-mapped.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(), kaldi::ReadKaldiObject(), 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 as matrices\n"
42  " (model is needed only for the integer mappings in its transition-model)\n"
43  "Usage: decode-faster-mapped [options] <model-in> <fst-in> "
44  "<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("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
54  po.Register("allow-partial", &allow_partial, "Produce output even when final state was not reached");
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() < 4 || po.NumArgs() > 5) {
60  po.PrintUsage();
61  exit(1);
62  }
63 
64  std::string model_in_filename = po.GetArg(1),
65  fst_in_filename = po.GetArg(2),
66  loglikes_rspecifier = po.GetArg(3),
67  words_wspecifier = po.GetArg(4),
68  alignment_wspecifier = po.GetOptArg(5);
69 
70  TransitionModel trans_model;
71  ReadKaldiObject(model_in_filename, &trans_model);
72 
73  Int32VectorWriter words_writer(words_wspecifier);
74 
75  Int32VectorWriter alignment_writer(alignment_wspecifier);
76 
77  fst::SymbolTable *word_syms = NULL;
78  if (word_syms_filename != "") {
79  word_syms = fst::SymbolTable::ReadText(word_syms_filename);
80  if (!word_syms)
81  KALDI_ERR << "Could not read symbol table from file "<<word_syms_filename;
82  }
83 
84  SequentialBaseFloatMatrixReader loglikes_reader(loglikes_rspecifier);
85 
86  // It's important that we initialize decode_fst after loglikes_reader, as it
87  // can prevent crashes on systems installed without enough virtual memory.
88  // It has to do with what happens on UNIX systems if you call fork() on a
89  // large process: the page-table entries are duplicated, which requires a
90  // lot of virtual memory.
91  VectorFst<StdArc> *decode_fst = fst::ReadFstKaldi(fst_in_filename);
92 
93  BaseFloat tot_like = 0.0;
94  kaldi::int64 frame_count = 0;
95  int num_success = 0, num_fail = 0;
96  FasterDecoder decoder(*decode_fst, decoder_opts);
97 
98  Timer timer;
99 
100  for (; !loglikes_reader.Done(); loglikes_reader.Next()) {
101  std::string key = loglikes_reader.Key();
102  const Matrix<BaseFloat> &loglikes (loglikes_reader.Value());
103 
104  if (loglikes.NumRows() == 0) {
105  KALDI_WARN << "Zero-length utterance: " << key;
106  num_fail++;
107  continue;
108  }
109 
110  DecodableMatrixScaledMapped decodable(trans_model, loglikes, acoustic_scale);
111  decoder.Decode(&decodable);
112 
113  VectorFst<LatticeArc> decoded; // linear FST.
114 
115  if ( (allow_partial || decoder.ReachedFinal())
116  && decoder.GetBestPath(&decoded) ) {
117  num_success++;
118  if (!decoder.ReachedFinal())
119  KALDI_WARN << "Decoder did not reach end-state, outputting partial traceback.";
120 
121  std::vector<int32> alignment;
122  std::vector<int32> words;
123  LatticeWeight weight;
124  frame_count += loglikes.NumRows();
125 
126  GetLinearSymbolSequence(decoded, &alignment, &words, &weight);
127 
128  words_writer.Write(key, words);
129  if (alignment_writer.IsOpen())
130  alignment_writer.Write(key, alignment);
131  if (word_syms != NULL) {
132  std::cerr << key << ' ';
133  for (size_t i = 0; i < words.size(); i++) {
134  std::string s = word_syms->Find(words[i]);
135  if (s == "")
136  KALDI_ERR << "Word-id " << words[i] <<" not in symbol table.";
137  std::cerr << s << ' ';
138  }
139  std::cerr << '\n';
140  }
141  BaseFloat like = -weight.Value1() -weight.Value2();
142  tot_like += like;
143  KALDI_LOG << "Log-like per frame for utterance " << key << " is "
144  << (like / loglikes.NumRows()) << " over "
145  << loglikes.NumRows() << " frames.";
146 
147  } else {
148  num_fail++;
149  KALDI_WARN << "Did not successfully decode utterance " << key
150  << ", len = " << loglikes.NumRows();
151  }
152  }
153 
154  double elapsed = timer.Elapsed();
155  KALDI_LOG << "Time taken [excluding initialization] "<< elapsed
156  << "s: real-time factor assuming 100 frames/sec is "
157  << (elapsed*100.0/frame_count);
158  KALDI_LOG << "Done " << num_success << " utterances, failed for "
159  << num_fail;
160  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count)
161  << " over " << frame_count << " frames.";
162 
163  delete word_syms;
164  delete decode_fst;
165  if (num_success != 0) return 0;
166  else return 1;
167  } catch(const std::exception &e) {
168  std::cerr << e.what();
169  return -1;
170  }
171 }
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
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:818
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