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

◆ main()

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::ReadFstKaldiGeneric(), 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::Fst;
42  using fst::StdArc;
44 
45  const char *usage =
46  "Decode features using GMM-based model.\n"
47  "Viterbi decoding, Only produces linear sequence; any lattice\n"
48  "produced is linear\n"
49  "\n"
50  "Usage: gmm-decode-simple [options] <model-in> <fst-in> "
51  "<features-rspecifier> <words-wspecifier> [<alignments-wspecifier>] "
52  "[<lattice-wspecifier>]";
53  ParseOptions po(usage);
54  Timer timer;
55  bool allow_partial = true;
56  BaseFloat acoustic_scale = 0.1;
57 
58  std::string word_syms_filename;
59  BaseFloat beam = 16.0;
60  po.Register("beam", &beam, "Decoding log-likelihood beam");
61  po.Register("acoustic-scale", &acoustic_scale,
62  "Scaling factor for acoustic likelihoods");
63  po.Register("word-symbol-table", &word_syms_filename,
64  "Symbol table for words [for debug output]");
65  po.Register("allow-partial", &allow_partial,
66  "Produce output even when final state was not reached");
67  po.Read(argc, argv);
68 
69  if (po.NumArgs() < 4 || po.NumArgs() > 6) {
70  po.PrintUsage();
71  exit(1);
72  }
73 
74  std::string model_in_filename = po.GetArg(1),
75  fst_in_filename = po.GetArg(2),
76  feature_rspecifier = po.GetArg(3),
77  words_wspecifier = po.GetArg(4),
78  alignment_wspecifier = po.GetOptArg(5),
79  lattice_wspecifier = po.GetOptArg(6);
80 
81  TransitionModel trans_model;
82  AmDiagGmm am_gmm;
83  {
84  bool binary;
85  Input ki(model_in_filename, &binary);
86  trans_model.Read(ki.Stream(), binary);
87  am_gmm.Read(ki.Stream(), binary);
88  }
89 
90  Fst<StdArc> *decode_fst = ReadFstKaldiGeneric(fst_in_filename);
91 
92  Int32VectorWriter words_writer(words_wspecifier);
93 
94  Int32VectorWriter alignment_writer(alignment_wspecifier);
95 
96  CompactLatticeWriter clat_writer(lattice_wspecifier);
97 
98  fst::SymbolTable *word_syms = NULL;
99  if (word_syms_filename != "")
100  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
101  KALDI_ERR << "Could not read symbol table from file "
102  << word_syms_filename;
103 
104  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
105 
106  BaseFloat tot_like = 0.0;
107  kaldi::int64 frame_count = 0;
108  int num_success = 0, num_fail = 0;
109  SimpleDecoder decoder(*decode_fst, beam);
110 
111  for (; !feature_reader.Done(); feature_reader.Next()) {
112  std::string utt = feature_reader.Key();
113  Matrix<BaseFloat> features (feature_reader.Value());
114  feature_reader.FreeCurrent();
115  if (features.NumRows() == 0) {
116  KALDI_WARN << "Zero-length utterance: " << utt;
117  num_fail++;
118  continue;
119  }
120 
121  DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model, features,
122  acoustic_scale);
123  decoder.Decode(&gmm_decodable);
124 
125  VectorFst<LatticeArc> decoded; // linear FST.
126 
127  if ( (allow_partial || decoder.ReachedFinal())
128  && decoder.GetBestPath(&decoded) ) {
129  if (!decoder.ReachedFinal())
130  KALDI_WARN << "Decoder did not reach end-state, "
131  << "outputting partial traceback since --allow-partial=true";
132  num_success++;
133 
134  std::vector<int32> alignment;
135  std::vector<int32> words;
136  LatticeWeight weight;
137  frame_count += features.NumRows();
138 
139  GetLinearSymbolSequence(decoded, &alignment, &words, &weight);
140 
141  words_writer.Write(utt, words);
142  if (alignment_wspecifier != "")
143  alignment_writer.Write(utt, alignment);
144  if (lattice_wspecifier != "") {
145  // We'll write the lattice without acoustic scaling.
146  if (acoustic_scale != 0.0)
147  fst::ScaleLattice(fst::AcousticLatticeScale(1.0 / acoustic_scale),
148  &decoded);
149  fst::VectorFst<CompactLatticeArc> clat;
150  ConvertLattice(decoded, &clat, true);
151  clat_writer.Write(utt, clat);
152  }
153  if (word_syms != NULL) {
154  std::cerr << utt << ' ';
155  for (size_t i = 0; i < words.size(); i++) {
156  std::string s = word_syms->Find(words[i]);
157  if (s == "")
158  KALDI_ERR << "Word-id " << words[i] <<" not in symbol table.";
159  std::cerr << s << ' ';
160  }
161  std::cerr << '\n';
162  }
163  BaseFloat like = -ConvertToCost(weight);
164  tot_like += like;
165  KALDI_LOG << "Log-like per frame for utterance " << utt << " is "
166  << (like / features.NumRows()) << " over "
167  << features.NumRows() << " frames.";
168  } else {
169  num_fail++;
170  KALDI_WARN << "Did not successfully decode utterance " << utt
171  << ", len = " << features.NumRows();
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_success << " utterances, failed for "
180  << num_fail;
181  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count) << " over "
182  << frame_count<<" frames.";
183 
184  delete word_syms;
185  delete decode_fst;
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 }
int32 words[kMaxOrder]
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Fst< StdArc > * ReadFstKaldiGeneric(std::string rxfilename, bool throw_on_err)
Definition: kaldi-fst-io.cc:45
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
bool GetLinearSymbolSequence(const Fst< Arc > &fst, std::vector< I > *isymbols_out, std::vector< I > *osymbols_out, typename Arc::Weight *tot_weight_out)
GetLinearSymbolSequence gets the symbol sequence from a linear FST.
std::vector< std::vector< double > > AcousticLatticeScale(double acwt)
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 ScaleLattice(const std::vector< std::vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a...
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
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
Simplest possible decoder, included largely for didactic purposes and as a means to debug more highly...
#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