online-gmm-decode-faster.cc File Reference
Include dependency graph for online-gmm-decode-faster.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 31 of file online-gmm-decode-faster.cc.

References OnlineFasterDecoderOpts::batch_size, OnlineFasterDecoder::Decode(), OnlineFasterDecoder::FinishTraceBack(), FrameExtractionOptions::frame_length_ms, MfccOptions::frame_opts, FrameExtractionOptions::frame_shift_ms, ParseOptions::GetArg(), fst::GetLinearSymbolSequence(), ParseOptions::GetOptArg(), FasterDecoder::InitDecoding(), KALDI_ERR, KALDI_LOG, KALDI_WARN, OnlineFasterDecoder::kEndFeats, OnlineFasterDecoder::kEndUtt, ParseOptions::NumArgs(), DeltaFeaturesOptions::order, OnlineFasterDecoder::PartialTraceback(), kaldi::PrintPartialResult(), ParseOptions::PrintUsage(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), Matrix< Real >::Read(), kaldi::ReadDecodeGraph(), OnlineFasterDecoderOpts::Register(), DeltaFeaturesOptions::Register(), ParseOptions::Register(), OnlineFeatureMatrixOptions::Register(), kaldi::SplitStringToIntegers(), Input::Stream(), OnlinePaSource::TimedOut(), and MfccOptions::use_energy.

31  {
32 #ifndef KALDI_NO_PORTAUDIO
33  try {
34  using namespace kaldi;
35  using namespace fst;
36 
37  typedef kaldi::int32 int32;
38  typedef OnlineFeInput<Mfcc> FeInput;
39 
40  // Up to delta-delta derivative features are calculated (unless LDA is used)
41  const int32 kDeltaOrder = 2;
42  // Time out interval for the PortAudio source
43  const int32 kTimeout = 500; // half second
44  // Input sampling frequency is fixed to 16KHz
45  const int32 kSampleFreq = 16000;
46  // PortAudio's internal ring buffer size in bytes
47  const int32 kPaRingSize = 32768;
48  // Report interval for PortAudio buffer overflows in number of feat. batches
49  const int32 kPaReportInt = 4;
50 
51  const char *usage =
52  "Decode speech, using microphone input(PortAudio)\n\n"
53  "Utterance segmentation is done on-the-fly.\n"
54  "Feature splicing/LDA transform is used, if the optional(last) argument "
55  "is given.\n"
56  "Otherwise delta/delta-delta(2-nd order) features are produced.\n\n"
57  "Usage: online-gmm-decode-faster [options] <model-in>"
58  "<fst-in> <word-symbol-table> <silence-phones> [<lda-matrix-in>]\n\n"
59  "Example: online-gmm-decode-faster --rt-min=0.3 --rt-max=0.5 "
60  "--max-active=4000 --beam=12.0 --acoustic-scale=0.0769 "
61  "model HCLG.fst words.txt '1:2:3:4:5' lda-matrix";
62  ParseOptions po(usage);
63  BaseFloat acoustic_scale = 0.1;
64  int32 cmn_window = 600, min_cmn_window = 100;
65  int32 right_context = 4, left_context = 4;
66 
67  kaldi::DeltaFeaturesOptions delta_opts;
68  delta_opts.Register(&po);
69  OnlineFasterDecoderOpts decoder_opts;
70  OnlineFeatureMatrixOptions feature_reading_opts;
71  decoder_opts.Register(&po, true);
72  feature_reading_opts.Register(&po);
73 
74  po.Register("left-context", &left_context, "Number of frames of left context");
75  po.Register("right-context", &right_context, "Number of frames of right context");
76  po.Register("acoustic-scale", &acoustic_scale,
77  "Scaling factor for acoustic likelihoods");
78  po.Register("cmn-window", &cmn_window,
79  "Number of feat. vectors used in the running average CMN calculation");
80  po.Register("min-cmn-window", &min_cmn_window,
81  "Minumum CMN window used at start of decoding (adds "
82  "latency only at start)");
83 
84  po.Read(argc, argv);
85  if (po.NumArgs() != 4 && po.NumArgs() != 5) {
86  po.PrintUsage();
87  return 1;
88  }
89 
90  std::string model_rxfilename = po.GetArg(1),
91  fst_rxfilename = po.GetArg(2),
92  word_syms_filename = po.GetArg(3),
93  silence_phones_str = po.GetArg(4),
94  lda_mat_rspecifier = po.GetOptArg(5);
95 
96  Matrix<BaseFloat> lda_transform;
97  if (lda_mat_rspecifier != "") {
98  bool binary_in;
99  Input ki(lda_mat_rspecifier, &binary_in);
100  lda_transform.Read(ki.Stream(), binary_in);
101  }
102 
103  std::vector<int32> silence_phones;
104  if (!SplitStringToIntegers(silence_phones_str, ":", false, &silence_phones))
105  KALDI_ERR << "Invalid silence-phones string " << silence_phones_str;
106  if (silence_phones.empty())
107  KALDI_ERR << "No silence phones given!";
108 
109  TransitionModel trans_model;
110  AmDiagGmm am_gmm;
111  {
112  bool binary;
113  Input ki(model_rxfilename, &binary);
114  trans_model.Read(ki.Stream(), binary);
115  am_gmm.Read(ki.Stream(), binary);
116  }
117 
118  fst::SymbolTable *word_syms = NULL;
119  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
120  KALDI_ERR << "Could not read symbol table from file "
121  << word_syms_filename;
122 
123  fst::Fst<fst::StdArc> *decode_fst = ReadDecodeGraph(fst_rxfilename);
124 
125  // We are not properly registering/exposing MFCC and frame extraction options,
126  // because there are parts of the online decoding code, where some of these
127  // options are hardwired(ToDo: we should fix this at some point)
128  MfccOptions mfcc_opts;
129  mfcc_opts.use_energy = false;
130  int32 frame_length = mfcc_opts.frame_opts.frame_length_ms = 25;
131  int32 frame_shift = mfcc_opts.frame_opts.frame_shift_ms = 10;
132 
133  int32 window_size = right_context + left_context + 1;
134  decoder_opts.batch_size = std::max(decoder_opts.batch_size, window_size);
135  OnlineFasterDecoder decoder(*decode_fst, decoder_opts,
136  silence_phones, trans_model);
137  VectorFst<LatticeArc> out_fst;
138  OnlinePaSource au_src(kTimeout, kSampleFreq, kPaRingSize, kPaReportInt);
139  Mfcc mfcc(mfcc_opts);
140  FeInput fe_input(&au_src, &mfcc,
141  frame_length * (kSampleFreq / 1000),
142  frame_shift * (kSampleFreq / 1000));
143  OnlineCmnInput cmn_input(&fe_input, cmn_window, min_cmn_window);
144  OnlineFeatInputItf *feat_transform = 0;
145  if (lda_mat_rspecifier != "") {
146  feat_transform = new OnlineLdaInput(
147  &cmn_input, lda_transform,
148  left_context, right_context);
149  } else {
151  opts.order = kDeltaOrder;
152  feat_transform = new OnlineDeltaInput(opts, &cmn_input);
153  }
154 
155  // feature_reading_opts contains number of retries, batch size.
156  OnlineFeatureMatrix feature_matrix(feature_reading_opts,
157  feat_transform);
158 
159  OnlineDecodableDiagGmmScaled decodable(am_gmm, trans_model, acoustic_scale,
160  &feature_matrix);
161  bool partial_res = false;
162  decoder.InitDecoding();
163  while (1) {
164  OnlineFasterDecoder::DecodeState dstate = decoder.Decode(&decodable);
165  if (dstate & (decoder.kEndFeats | decoder.kEndUtt)) {
166  std::vector<int32> word_ids;
167  decoder.FinishTraceBack(&out_fst);
169  static_cast<vector<int32> *>(0),
170  &word_ids,
171  static_cast<LatticeArc::Weight*>(0));
172  PrintPartialResult(word_ids, word_syms, partial_res || word_ids.size());
173  partial_res = false;
174  if (dstate == decoder.kEndFeats) {
175  if (au_src.TimedOut())
176  KALDI_WARN << "PortAudio time out detected!";
177  KALDI_LOG << "No more features available from PortAudio!";
178  break;
179  }
180  } else {
181  std::vector<int32> word_ids;
182  if (decoder.PartialTraceback(&out_fst)) {
184  static_cast<vector<int32> *>(0),
185  &word_ids,
186  static_cast<LatticeArc::Weight*>(0));
187  PrintPartialResult(word_ids, word_syms, false);
188  if (!partial_res)
189  partial_res = (word_ids.size() > 0);
190  }
191  }
192  }
193 
194  delete feat_transform;
195  delete word_syms;
196  delete decode_fst;
197  return 0;
198  } catch(const std::exception& e) {
199  std::cerr << e.what();
200  return -1;
201  }
202 #endif
203 } // main()
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void Register(OptionsItf *opts, bool full)
MfccOptions contains basic options for computing MFCC features.
Definition: feature-mfcc.h:38
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
Definition: text-utils.h:68
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.
void PrintPartialResult(const std::vector< int32 > &words, const fst::SymbolTable *word_syms, bool line_break)
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
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
FrameExtractionOptions frame_opts
Definition: feature-mfcc.h:39
void Read(std::istream &is, bool binary)
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
fst::Fst< fst::StdArc > * ReadDecodeGraph(const std::string &filename)
void Register(OptionsItf *opts)
This templated class is intended for offline feature extraction, i.e.
#define KALDI_LOG
Definition: kaldi-error.h:153
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147