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

References fst::AcousticLatticeScale(), fst::ConvertLattice(), fst::ConvertToCost(), SimpleDecoder::Decode(), SequentialTableReader< Holder >::Done(), Timer::Elapsed(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), SimpleDecoder::GetBestPath(), fst::GetLinearSymbolSequence(), ParseOptions::GetOptArg(), rnnlm::i, KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), SimpleDecoder::ReachedFinal(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), fst::ReadFstKaldi(), ParseOptions::Register(), fst::ScaleLattice(), Input::Stream(), SequentialTableReader< Holder >::Value(), words, and TableWriter< Holder >::Write().

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  using fst::ReadFstKaldi;
43 
44  const char *usage =
45  "Decode features using GMM-based model.\n"
46  "Viterbi decoding, Only produces linear sequence; any lattice\n"
47  "produced is linear\n"
48  "\n"
49  "Usage: gmm-decode-simple [options] <model-in> <fst-in> "
50  "<features-rspecifier> <words-wspecifier> [<alignments-wspecifier>] "
51  "[<lattice-wspecifier>]";
52  ParseOptions po(usage);
53  Timer timer;
54  bool allow_partial = true;
55  BaseFloat acoustic_scale = 0.1;
56 
57  std::string word_syms_filename;
58  BaseFloat beam = 16.0;
59  po.Register("beam", &beam, "Decoding log-likelihood beam");
60  po.Register("acoustic-scale", &acoustic_scale,
61  "Scaling factor for acoustic likelihoods");
62  po.Register("word-symbol-table", &word_syms_filename,
63  "Symbol table for words [for debug output]");
64  po.Register("allow-partial", &allow_partial,
65  "Produce output even when final state was not reached");
66  po.Read(argc, argv);
67 
68  if (po.NumArgs() < 4 || po.NumArgs() > 6) {
69  po.PrintUsage();
70  exit(1);
71  }
72 
73  std::string model_in_filename = po.GetArg(1),
74  fst_in_filename = po.GetArg(2),
75  feature_rspecifier = po.GetArg(3),
76  words_wspecifier = po.GetArg(4),
77  alignment_wspecifier = po.GetOptArg(5),
78  lattice_wspecifier = po.GetOptArg(6);
79 
80  TransitionModel trans_model;
81  AmDiagGmm am_gmm;
82  {
83  bool binary;
84  Input ki(model_in_filename, &binary);
85  trans_model.Read(ki.Stream(), binary);
86  am_gmm.Read(ki.Stream(), binary);
87  }
88 
89  VectorFst<StdArc> *decode_fst = ReadFstKaldi(fst_in_filename);
90 
91  Int32VectorWriter words_writer(words_wspecifier);
92 
93  Int32VectorWriter alignment_writer(alignment_wspecifier);
94 
95  CompactLatticeWriter clat_writer(lattice_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  SimpleDecoder decoder(*decode_fst, beam);
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  decoder.Decode(&gmm_decodable);
123 
124  VectorFst<LatticeArc> decoded; // linear FST.
125 
126  if ( (allow_partial || decoder.ReachedFinal())
127  && decoder.GetBestPath(&decoded) ) {
128  if (!decoder.ReachedFinal())
129  KALDI_WARN << "Decoder did not reach end-state, "
130  << "outputting partial traceback since --allow-partial=true";
131  num_success++;
132 
133  std::vector<int32> alignment;
134  std::vector<int32> words;
135  LatticeWeight weight;
136  frame_count += features.NumRows();
137 
138  GetLinearSymbolSequence(decoded, &alignment, &words, &weight);
139 
140  words_writer.Write(utt, words);
141  if (alignment_wspecifier != "")
142  alignment_writer.Write(utt, alignment);
143  if (lattice_wspecifier != "") {
144  // We'll write the lattice without acoustic scaling.
145  if (acoustic_scale != 0.0)
146  fst::ScaleLattice(fst::AcousticLatticeScale(1.0 / acoustic_scale),
147  &decoded);
148  fst::VectorFst<CompactLatticeArc> clat;
149  ConvertLattice(decoded, &clat, true);
150  clat_writer.Write(utt, clat);
151  }
152  if (word_syms != NULL) {
153  std::cerr << utt << ' ';
154  for (size_t i = 0; i < words.size(); i++) {
155  std::string s = word_syms->Find(words[i]);
156  if (s == "")
157  KALDI_ERR << "Word-id " << words[i] <<" not in symbol table.";
158  std::cerr << s << ' ';
159  }
160  std::cerr << '\n';
161  }
162  BaseFloat like = -ConvertToCost(weight);
163  tot_like += like;
164  KALDI_LOG << "Log-like per frame for utterance " << utt << " is "
165  << (like / features.NumRows()) << " over "
166  << features.NumRows() << " frames.";
167  } else {
168  num_fail++;
169  KALDI_WARN << "Did not successfully decode utterance " << utt
170  << ", len = " << features.NumRows();
171  }
172  }
173 
174  double elapsed = timer.Elapsed();
175  KALDI_LOG << "Time taken "<< elapsed
176  << "s: real-time factor assuming 100 frames/sec is "
177  << (elapsed*100.0/frame_count);
178  KALDI_LOG << "Done " << num_success << " utterances, failed for "
179  << num_fail;
180  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count) << " over "
181  << frame_count<<" frames.";
182 
183  delete word_syms;
184  delete decode_fst;
185  if (num_success != 0) return 0;
186  else return 1;
187  } catch(const std::exception &e) {
188  std::cerr << e.what();
189  return -1;
190  }
191 }
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
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)
double ConvertToCost(const LatticeWeightTpl< Float > &w)
void ConvertLattice(const ExpandedFst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *ofst, bool invert)
Convert lattice from a normal FST to a CompactLattice FST.
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
vector< vector< double > > AcousticLatticeScale(double acwt)
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_WARN
Definition: kaldi-error.h:130
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 ScaleLattice(const vector< vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a...
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
Simplest possible decoder, included largely for didactic purposes and as a means to debug more highly...
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