gmm-latgen-faster-parallel.cc File Reference
Include dependency graph for gmm-latgen-faster-parallel.cc:

Go to the source code of this file.

Functions

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

Function Documentation

◆ main()

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

Definition at line 36 of file gmm-latgen-faster-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(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), fst::ReadFstKaldiGeneric(), LatticeFasterDecoderConfig::Register(), ParseOptions::Register(), TaskSequencerConfig::Register(), Timer::Reset(), TaskSequencer< C >::Run(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TaskSequencer< C >::Wait().

36  {
37  try {
38  using namespace kaldi;
39  typedef kaldi::int32 int32;
40  using fst::SymbolTable;
41  using fst::VectorFst;
42  using fst::Fst;
43  using fst::StdArc;
44 
45  const char *usage =
46  "Decode features using GMM-based model. Uses multiple decoding threads,\n"
47  "but interface and behavior is otherwise the same as gmm-latgen-faster\n"
48  "Usage: gmm-latgen-faster-parallel [options] model-in (fst-in|fsts-rspecifier) "
49  "features-rspecifier lattice-wspecifier [ words-wspecifier [alignments-wspecifier] ]\n";
50  ParseOptions po(usage);
51  Timer timer;
52  bool allow_partial = false;
53  BaseFloat acoustic_scale = 0.1;
54  BaseFloat log_sum_exp_prune = 0.0;
55  LatticeFasterDecoderConfig latgen_config;
56  TaskSequencerConfig sequencer_config; // has --num-threads option
57 
58  std::string word_syms_filename;
59  latgen_config.Register(&po);
60  sequencer_config.Register(&po);
61  po.Register("acoustic-scale", &acoustic_scale,
62  "Scaling factor for acoustic likelihoods");
63  po.Register("log-sum-exp-prune", &log_sum_exp_prune,
64  "If >0, pruning parameter to minimize exp()'s. Suggest 3 to 5; "
65  "larger is more exact.");
66  po.Register("word-symbol-table", &word_syms_filename,
67  "Symbol table for words [for debug output]");
68  po.Register("allow-partial", &allow_partial,
69  "If true, produce output even if end state was not reached.");
70 
71  po.Read(argc, argv);
72 
73  if (po.NumArgs() < 4 || po.NumArgs() > 6) {
74  po.PrintUsage();
75  exit(1);
76  }
77 
78  std::string model_in_filename = po.GetArg(1),
79  fst_in_str = po.GetArg(2),
80  feature_rspecifier = po.GetArg(3),
81  lattice_wspecifier = po.GetArg(4),
82  words_wspecifier = po.GetOptArg(5),
83  alignment_wspecifier = po.GetOptArg(6);
84 
85  TransitionModel trans_model;
86  AmDiagGmm am_gmm;
87  {
88  bool binary;
89  Input ki(model_in_filename, &binary);
90  trans_model.Read(ki.Stream(), binary);
91  am_gmm.Read(ki.Stream(), binary);
92  }
93 
94  bool determinize = latgen_config.determinize_lattice;
95  CompactLatticeWriter compact_lattice_writer;
96  LatticeWriter lattice_writer;
97  if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
98  : lattice_writer.Open(lattice_wspecifier)))
99  KALDI_ERR << "Could not open table for writing lattices: "
100  << lattice_wspecifier;
101 
102  Int32VectorWriter words_writer(words_wspecifier);
103 
104  Int32VectorWriter alignment_writer(alignment_wspecifier);
105 
106  fst::SymbolTable *word_syms = NULL;
107  if (word_syms_filename != "")
108  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
109  KALDI_ERR << "Could not read symbol table from file "
110  << word_syms_filename;
111 
112  double tot_like = 0.0;
113  kaldi::int64 frame_count = 0;
114  int num_done = 0, num_err = 0;
115  Fst<StdArc> *decode_fst = NULL; // only used if there is a single
116  // decoding graph.
117 
118  TaskSequencer<DecodeUtteranceLatticeFasterClass> sequencer(sequencer_config);
119 
120  if (ClassifyRspecifier(fst_in_str, NULL, NULL) == kNoRspecifier) {
121  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
122  // Input FST is just one FST, not a table of FSTs.
123 
124  decode_fst = fst::ReadFstKaldiGeneric(fst_in_str);
125  timer.Reset();
126 
127  {
128  for (; !feature_reader.Done(); feature_reader.Next()) {
129  std::string utt = feature_reader.Key();
130  Matrix<BaseFloat> *features =
131  new Matrix<BaseFloat>(feature_reader.Value());
132  feature_reader.FreeCurrent();
133  if (features->NumRows() == 0) {
134  KALDI_WARN << "Zero-length utterance: " << utt;
135  num_err++;
136  delete features;
137  continue;
138  }
139 
140  LatticeFasterDecoder *decoder = new LatticeFasterDecoder(*decode_fst,
141  latgen_config);
142  // takes ownership of "features"
143  DecodableAmDiagGmmScaled *gmm_decodable =
144  new DecodableAmDiagGmmScaled(am_gmm, trans_model,
145  acoustic_scale,
146  log_sum_exp_prune,
147  features);
148 
151  decoder, gmm_decodable, // takes ownership of these two.
152  trans_model, word_syms, utt, acoustic_scale, determinize,
153  allow_partial, &alignment_writer, &words_writer,
154  &compact_lattice_writer, &lattice_writer,
155  &tot_like, &frame_count, &num_done, &num_err, NULL);
156 
157  sequencer.Run(task); // takes ownership of "task",
158  // and will delete it when done.
159  }
160  }
161  } else { // We have different FSTs for different utterances.
162  SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_in_str);
163  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
164  for (; !fst_reader.Done(); fst_reader.Next()) {
165  std::string utt = fst_reader.Key();
166  if (!feature_reader.HasKey(utt)) {
167  KALDI_WARN << "Not decoding utterance " << utt
168  << " because no features available.";
169  num_err++;
170  continue;
171  }
172  Matrix<BaseFloat> *features = new Matrix<BaseFloat>(
173  feature_reader.Value(utt));
174  if (features->NumRows() == 0) {
175  KALDI_WARN << "Zero-length utterance: " << utt;
176  num_err++;
177  delete features;
178  continue;
179  }
180 
181  // the "decoder" object takes ownership of the new FST object.
183  latgen_config,
184  new VectorFst<StdArc>(fst_reader.Value()));
185 
186  // The "decodable" object takes ownership of the features.
187  DecodableAmDiagGmmScaled *gmm_decodable =
188  new DecodableAmDiagGmmScaled(am_gmm, trans_model, acoustic_scale,
189  log_sum_exp_prune, features);
190 
193  decoder, gmm_decodable, // takes ownership of these two.
194  trans_model, word_syms, utt, acoustic_scale, determinize,
195  allow_partial, &alignment_writer, &words_writer,
196  &compact_lattice_writer, &lattice_writer,
197  &tot_like, &frame_count, &num_done, &num_err, NULL);
198  sequencer.Run(task); // takes ownership of "task",
199  // and will delete it when done.
200  }
201  }
202  sequencer.Wait();
203 
204  delete decode_fst;
205 
206  double elapsed = timer.Elapsed();
207  KALDI_LOG << "Decoded with " << sequencer_config.num_threads << " threads.";
208  KALDI_LOG << "Time taken "<< elapsed
209  << "s: real-time factor per thread assuming 100 frames/sec is "
210  << (sequencer_config.num_threads * elapsed * 100.0 / frame_count);
211  KALDI_LOG << "Done " << num_done << " utterances, failed for "
212  << num_err;
213  KALDI_LOG << "Overall log-likelihood per frame is "
214  << (tot_like/frame_count) << " over "
215  << frame_count << " frames.";
216 
217  delete word_syms;
218  if (num_done != 0) return 0;
219  else return 1;
220  } catch(const std::exception &e) {
221  std::cerr << e.what();
222  return -1;
223  }
224 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool Open(const std::string &wspecifier)
Fst< StdArc > * ReadFstKaldiGeneric(std::string rxfilename, bool throw_on_err)
Definition: kaldi-fst-io.cc:45
void Reset()
Definition: timer.h:71
fst::StdArc StdArc
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
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.
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)
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:147
#define KALDI_WARN
Definition: kaldi-error.h:150
This is the "normal" lattice-generating decoder.
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
LatticeFasterDecoderTpl< fst::StdFst, decoder::StdToken > LatticeFasterDecoder
#define KALDI_LOG
Definition: kaldi-error.h:153
double Elapsed() const
Returns time in seconds.
Definition: timer.h:74
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147
void Register(OptionsItf *opts)
Definition: kaldi-thread.h:160