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

References kaldi::ClassifyRspecifier(), kaldi::DecodeUtteranceLatticeFaster(), 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(), ParseOptions::NumArgs(), MatrixBase< Real >::NumRows(), TableWriter< Holder >::Open(), ParseOptions::PrintUsage(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), fst::ReadFstKaldiGeneric(), LatticeFasterDecoderConfig::Register(), ParseOptions::Register(), Timer::Reset(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), and SequentialTableReader< Holder >::Value().

34  {
35  try {
36  using namespace kaldi;
37  typedef kaldi::int32 int32;
38  using fst::SymbolTable;
39  using fst::Fst;
40  using fst::StdArc;
41 
42  const char *usage =
43  "Generate lattices using GMM-based model.\n"
44  "Usage: gmm-latgen-faster [options] model-in (fst-in|fsts-rspecifier) features-rspecifier"
45  " lattice-wspecifier [ words-wspecifier [alignments-wspecifier] ]\n";
46  ParseOptions po(usage);
47  Timer timer;
48  bool allow_partial = false;
49  BaseFloat acoustic_scale = 0.1;
51 
52  std::string word_syms_filename;
53  config.Register(&po);
54  po.Register("acoustic-scale", &acoustic_scale,
55  "Scaling factor for acoustic likelihoods");
56  po.Register("word-symbol-table", &word_syms_filename,
57  "Symbol table for words [for debug output]");
58  po.Register("allow-partial", &allow_partial,
59  "If true, produce output even if end state was not reached.");
60 
61  po.Read(argc, argv);
62 
63  if (po.NumArgs() < 4 || po.NumArgs() > 6) {
64  po.PrintUsage();
65  exit(1);
66  }
67 
68  std::string model_in_filename = po.GetArg(1),
69  fst_in_str = po.GetArg(2),
70  feature_rspecifier = po.GetArg(3),
71  lattice_wspecifier = po.GetArg(4),
72  words_wspecifier = po.GetOptArg(5),
73  alignment_wspecifier = po.GetOptArg(6);
74 
75  TransitionModel trans_model;
76  AmDiagGmm am_gmm;
77  {
78  bool binary;
79  Input ki(model_in_filename, &binary);
80  trans_model.Read(ki.Stream(), binary);
81  am_gmm.Read(ki.Stream(), binary);
82  }
83 
84  bool determinize = config.determinize_lattice;
85  CompactLatticeWriter compact_lattice_writer;
86  LatticeWriter lattice_writer;
87  if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
88  : lattice_writer.Open(lattice_wspecifier)))
89  KALDI_ERR << "Could not open table for writing lattices: "
90  << lattice_wspecifier;
91 
92  Int32VectorWriter words_writer(words_wspecifier);
93 
94  Int32VectorWriter alignment_writer(alignment_wspecifier);
95 
96  fst::SymbolTable *word_syms = NULL;
97  if (word_syms_filename != "")
98  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
99  KALDI_ERR << "Could not read symbol table from file "
100  << word_syms_filename;
101 
102  double tot_like = 0.0;
103  kaldi::int64 frame_count = 0;
104  int num_done = 0, num_err = 0;
105 
106  if (ClassifyRspecifier(fst_in_str, NULL, NULL) == kNoRspecifier) {
107  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
108  // Input FST is just one FST, not a table of FSTs.
109  Fst<StdArc> *decode_fst = fst::ReadFstKaldiGeneric(fst_in_str);
110  timer.Reset();
111 
112  {
113  LatticeFasterDecoder decoder(*decode_fst, config);
114 
115  for (; !feature_reader.Done(); feature_reader.Next()) {
116  std::string utt = feature_reader.Key();
117  Matrix<BaseFloat> features (feature_reader.Value());
118  feature_reader.FreeCurrent();
119  if (features.NumRows() == 0) {
120  KALDI_WARN << "Zero-length utterance: " << utt;
121  num_err++;
122  continue;
123  }
124 
125  DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model, features,
126  acoustic_scale);
127 
128  double like;
130  decoder, gmm_decodable, trans_model, word_syms, utt,
131  acoustic_scale, determinize, allow_partial, &alignment_writer,
132  &words_writer, &compact_lattice_writer, &lattice_writer,
133  &like)) {
134  tot_like += like;
135  frame_count += features.NumRows();
136  num_done++;
137  } else num_err++;
138  }
139  }
140  delete decode_fst; // delete this only after decoder goes out of scope.
141  } else { // We have different FSTs for different utterances.
142  SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_in_str);
143  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
144  for (; !fst_reader.Done(); fst_reader.Next()) {
145  std::string utt = fst_reader.Key();
146  if (!feature_reader.HasKey(utt)) {
147  KALDI_WARN << "Not decoding utterance " << utt
148  << " because no features available.";
149  num_err++;
150  continue;
151  }
152  const Matrix<BaseFloat> &features = feature_reader.Value(utt);
153  if (features.NumRows() == 0) {
154  KALDI_WARN << "Zero-length utterance: " << utt;
155  num_err++;
156  continue;
157  }
158 
159  LatticeFasterDecoder decoder(fst_reader.Value(), config);
160  DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model, features,
161  acoustic_scale);
162  double like;
164  decoder, gmm_decodable, trans_model, word_syms, utt,
165  acoustic_scale, determinize, allow_partial, &alignment_writer,
166  &words_writer, &compact_lattice_writer, &lattice_writer,
167  &like)) {
168  tot_like += like;
169  frame_count += features.NumRows();
170  num_done++;
171  } else num_err++;
172  }
173  }
174 
175  double elapsed = timer.Elapsed();
176  KALDI_LOG << "Time taken "<< elapsed
177  << "s: real-time factor assuming 100 frames/sec is "
178  << (elapsed*100.0/frame_count);
179  KALDI_LOG << "Done " << num_done << " utterances, failed for "
180  << num_err;
181  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count) << " over "
182  << frame_count << " frames.";
183 
184  delete word_syms;
185  if (num_done != 0) return 0;
186  else return 1;
187  } catch(const std::exception &e) {
188  std::cerr << e.what();
189  return -1;
190  }
191 }
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 > * 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:366
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
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)
bool DecodeUtteranceLatticeFaster(LatticeFasterDecoder &decoder, DecodableInterface &decodable, const TransitionModel &trans_model, const fst::SymbolTable *word_syms, std::string utt, double acoustic_scale, bool determinize, bool allow_partial, Int32VectorWriter *alignment_writer, Int32VectorWriter *words_writer, CompactLatticeWriter *compact_lattice_writer, LatticeWriter *lattice_writer, double *like_ptr)
This function DecodeUtteranceLatticeFaster is used in several decoders, and we have moved it here...
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
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