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

References kaldi::DecodeUtteranceLatticeSimple(), LatticeSimpleDecoderConfig::determinize_lattice, SequentialTableReader< Holder >::Done(), Timer::Elapsed(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), ParseOptions::GetOptArg(), KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), TableWriter< Holder >::Open(), ParseOptions::PrintUsage(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), fst::ReadFstKaldi(), LatticeSimpleDecoderConfig::Register(), ParseOptions::Register(), Input::Stream(), and SequentialTableReader< Holder >::Value().

35  {
36  try {
37  using namespace kaldi;
38  typedef kaldi::int32 int32;
39  using fst::SymbolTable;
40  using fst::VectorFst;
41  using fst::StdArc;
42 
43  const char *usage =
44  "Generate lattices using GMM-based model.\n"
45  "Usage: gmm-latgen-simple [options] model-in fst-in features-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 
53  std::string word_syms_filename;
54  config.Register(&po);
55  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
56 
57  po.Register("word-symbol-table", &word_syms_filename, "Symbol table for words [for debug output]");
58  po.Register("allow-partial", &allow_partial, "If true, produce output even if end state was not reached.");
59 
60  po.Read(argc, argv);
61 
62  if (po.NumArgs() < 4 || po.NumArgs() > 6) {
63  po.PrintUsage();
64  exit(1);
65  }
66 
67  std::string model_in_filename = po.GetArg(1),
68  fst_in_filename = po.GetArg(2),
69  feature_rspecifier = po.GetArg(3),
70  lattice_wspecifier = po.GetArg(4),
71  words_wspecifier = po.GetOptArg(5),
72  alignment_wspecifier = po.GetOptArg(6);
73 
74  TransitionModel trans_model;
75  AmDiagGmm am_gmm;
76  {
77  bool binary;
78  Input ki(model_in_filename, &binary);
79  trans_model.Read(ki.Stream(), binary);
80  am_gmm.Read(ki.Stream(), binary);
81  }
82 
83  VectorFst<StdArc> *decode_fst = fst::ReadFstKaldi(fst_in_filename);
84 
85  bool determinize = config.determinize_lattice;
86  CompactLatticeWriter compact_lattice_writer;
87  LatticeWriter lattice_writer;
88  if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
89  : lattice_writer.Open(lattice_wspecifier)))
90  KALDI_ERR << "Could not open table for writing lattices: "
91  << lattice_wspecifier;
92 
93  Int32VectorWriter words_writer(words_wspecifier);
94 
95  Int32VectorWriter alignment_writer(alignment_wspecifier);
96 
97  fst::SymbolTable *word_syms = NULL;
98  if (word_syms_filename != "")
99  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
100  KALDI_ERR << "Could not read symbol table from file "
101  << word_syms_filename;
102 
103  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
104 
105  BaseFloat tot_like = 0.0;
106  kaldi::int64 frame_count = 0;
107  int num_success = 0, num_fail = 0;
108  LatticeSimpleDecoder decoder(*decode_fst, config);
109 
110  for (; !feature_reader.Done(); feature_reader.Next()) {
111  std::string utt = feature_reader.Key();
112  Matrix<BaseFloat> features (feature_reader.Value());
113  feature_reader.FreeCurrent();
114  if (features.NumRows() == 0) {
115  KALDI_WARN << "Zero-length utterance: " << utt;
116  num_fail++;
117  continue;
118  }
119 
120  DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model, features,
121  acoustic_scale);
122 
123  double like;
125  decoder, gmm_decodable, trans_model, word_syms, utt,
126  acoustic_scale, determinize, allow_partial, &alignment_writer,
127  &words_writer, &compact_lattice_writer, &lattice_writer, &like)) {
128  tot_like += like;
129  frame_count += features.NumRows();
130  num_success++;
131  } else num_fail++;
132  }
133 
134  double elapsed = timer.Elapsed();
135  KALDI_LOG << "Time taken "<< elapsed
136  << "s: real-time factor assuming 100 frames/sec is "
137  << (elapsed*100.0/frame_count);
138  KALDI_LOG << "Done " << num_success << " utterances, failed for "
139  << num_fail;
140  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count) << " over "
141  << frame_count<<" frames.";
142 
143  delete decode_fst;
144  delete word_syms;
145  if (num_success != 0) return 0;
146  else return 1;
147  } catch(const std::exception &e) {
148  std::cerr << e.what();
149  return -1;
150  }
151 }
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
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)
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
Simplest possible decoder, included largely for didactic purposes and as a means to debug more highly...
bool DecodeUtteranceLatticeSimple(LatticeSimpleDecoder &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)
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
#define KALDI_LOG
Definition: kaldi-error.h:133
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147