All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
latgen-faster-mapped-parallel.cc File Reference
Include dependency graph for latgen-faster-mapped-parallel.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 34 of file latgen-faster-mapped-parallel.cc.

References kaldi::ClassifyRspecifier(), LatticeFasterDecoderConfig::determinize_lattice, SequentialTableReader< Holder >::Done(), Timer::Elapsed(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), ParseOptions::GetOptArg(), RandomAccessTableReader< Holder >::HasKey(), KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), kaldi::kNoRspecifier, SequentialTableReader< Holder >::Next(), TaskSequencerConfig::num_threads, ParseOptions::NumArgs(), MatrixBase< Real >::NumRows(), TableWriter< Holder >::Open(), ParseOptions::PrintUsage(), ParseOptions::Read(), fst::ReadFstKaldi(), kaldi::ReadKaldiObject(), TaskSequencerConfig::Register(), LatticeFasterDecoderConfig::Register(), ParseOptions::Register(), TaskSequencer< C >::Run(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TaskSequencer< C >::Wait().

34  {
35  try {
36  using namespace kaldi;
37  typedef kaldi::int32 int32;
38  using fst::SymbolTable;
39  using fst::VectorFst;
40  using fst::StdArc;
41 
42  const char *usage =
43  "Generate lattices, reading log-likelihoods as matrices, using multiple decoding threads\n"
44  " (model is needed only for the integer mappings in its transition-model)\n"
45  "Usage: latgen-faster-mapped-parallel [options] trans-model-in (fst-in|fsts-rspecifier) loglikes-rspecifier"
46  " lattice-wspecifier [ words-wspecifier [alignments-wspecifier] ]\n";
47  ParseOptions po(usage);
48  Timer timer;
49  bool allow_partial = false;
50  BaseFloat acoustic_scale = 0.1;
52  TaskSequencerConfig sequencer_config; // has --num-threads option
53 
54  std::string word_syms_filename;
55  config.Register(&po);
56  sequencer_config.Register(&po);
57 
58  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
59 
60  po.Register("word-symbol-table", &word_syms_filename, "Symbol table for words [for debug output]");
61  po.Register("allow-partial", &allow_partial, "If true, produce output even if end state was not reached.");
62 
63  po.Read(argc, argv);
64 
65  if (po.NumArgs() < 4 || po.NumArgs() > 6) {
66  po.PrintUsage();
67  exit(1);
68  }
69 
70  std::string model_in_filename = po.GetArg(1),
71  fst_in_str = po.GetArg(2),
72  feature_rspecifier = po.GetArg(3),
73  lattice_wspecifier = po.GetArg(4),
74  words_wspecifier = po.GetOptArg(5),
75  alignment_wspecifier = po.GetOptArg(6);
76 
77  TransitionModel trans_model;
78  ReadKaldiObject(model_in_filename, &trans_model);
79 
80  bool determinize = config.determinize_lattice;
81  CompactLatticeWriter compact_lattice_writer;
82  LatticeWriter lattice_writer;
83  if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
84  : lattice_writer.Open(lattice_wspecifier)))
85  KALDI_ERR << "Could not open table for writing lattices: "
86  << lattice_wspecifier;
87 
88  Int32VectorWriter words_writer(words_wspecifier);
89 
90  Int32VectorWriter alignment_writer(alignment_wspecifier);
91 
92  fst::SymbolTable *word_syms = NULL;
93  if (word_syms_filename != "")
94  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
95  KALDI_ERR << "Could not read symbol table from file "
96  << word_syms_filename;
97 
98  double tot_like = 0.0;
99  kaldi::int64 frame_count = 0;
100  int num_success = 0, num_fail = 0;
101  VectorFst<StdArc> *decode_fst = NULL; // only used if there is a single
102  // decoding graph.
103 
104  TaskSequencer<DecodeUtteranceLatticeFasterClass> sequencer(sequencer_config);
105  if (ClassifyRspecifier(fst_in_str, NULL, NULL) == kNoRspecifier) {
106  SequentialBaseFloatMatrixReader loglike_reader(feature_rspecifier);
107  // Input FST is just one FST, not a table of FSTs.
108  decode_fst = fst::ReadFstKaldi(fst_in_str);
109 
110  {
111  for (; !loglike_reader.Done(); loglike_reader.Next()) {
112  std::string utt = loglike_reader.Key();
113  Matrix<BaseFloat> *loglikes =
114  new Matrix<BaseFloat>(loglike_reader.Value());
115  loglike_reader.FreeCurrent();
116  if (loglikes->NumRows() == 0) {
117  KALDI_WARN << "Zero-length utterance: " << utt;
118  num_fail++;
119  delete loglikes;
120  continue;
121  }
122 
123  LatticeFasterDecoder *decoder = new LatticeFasterDecoder(*decode_fst,
124  config);
125  DecodableMatrixScaledMapped *decodable =
126  new DecodableMatrixScaledMapped(trans_model, acoustic_scale, loglikes);
129  decoder, decodable, trans_model, word_syms, utt,
130  acoustic_scale, determinize, allow_partial, &alignment_writer,
131  &words_writer, &compact_lattice_writer, &lattice_writer,
132  &tot_like, &frame_count, &num_success, &num_fail, NULL);
133 
134  sequencer.Run(task); // takes ownership of "task",
135  // and will delete it when done.
136  }
137  }
138  } else { // We have different FSTs for different utterances.
139  SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_in_str);
140  RandomAccessBaseFloatMatrixReader loglike_reader(feature_rspecifier);
141  for (; !fst_reader.Done(); fst_reader.Next()) {
142  std::string utt = fst_reader.Key();
143  if (!loglike_reader.HasKey(utt)) {
144  KALDI_WARN << "Not decoding utterance " << utt
145  << " because no loglikes available.";
146  num_fail++;
147  continue;
148  }
149  const Matrix<BaseFloat> *loglikes =
150  new Matrix<BaseFloat>(loglike_reader.Value(utt));
151  if (loglikes->NumRows() == 0) {
152  KALDI_WARN << "Zero-length utterance: " << utt;
153  num_fail++;
154  delete loglikes;
155  continue;
156  }
157  LatticeFasterDecoder *decoder =
158  new LatticeFasterDecoder(fst_reader.Value(), config);
159  DecodableMatrixScaledMapped *decodable = new
160  DecodableMatrixScaledMapped(trans_model, acoustic_scale, loglikes);
163  decoder, decodable, trans_model, word_syms, utt, acoustic_scale,
164  determinize, allow_partial, &alignment_writer, &words_writer,
165  &compact_lattice_writer, &lattice_writer, &tot_like,
166  &frame_count, &num_success, &num_fail, NULL);
167  sequencer.Run(task); // takes ownership of "task",
168  // and will delete it when done.
169  }
170  }
171  sequencer.Wait();
172 
173  delete decode_fst;
174 
175  double elapsed = timer.Elapsed();
176  KALDI_LOG << "Decoded with " << sequencer_config.num_threads << " threads.";
177  KALDI_LOG << "Time taken "<< elapsed
178  << "s: real-time factor per thread assuming 100 frames/sec is "
179  << (sequencer_config.num_threads*elapsed*100.0/frame_count);
180  KALDI_LOG << "Done " << num_success << " utterances, failed for "
181  << num_fail;
182  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count) << " over "
183  << frame_count<<" frames.";
184 
185  delete word_syms;
186  if (num_success != 0) return 0;
187  else return 1;
188  } catch(const std::exception &e) {
189  std::cerr << e.what();
190  return -1;
191  }
192 }
A bit more optimized version of the lattice decoder.
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
bool Open(const std::string &wspecifier)
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
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
float BaseFloat
Definition: kaldi-types.h:29
This class basically does the same job as the function DecodeUtteranceLatticeFaster, but in a way that allows us to build a multi-threaded command line program more easily, using code in ../thread/kaldi-task-sequence.h.
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
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *wxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
In kaldi-thread.h, the class MultiThreader, and the function RunMultiThreaded, provided a mechanism t...
#define KALDI_LOG
Definition: kaldi-error.h:133
void Register(OptionsItf *opts)