gmm-decode-faster-regtree-fmllr.cc File Reference
Include dependency graph for gmm-decode-faster-regtree-fmllr.cc:

Go to the source code of this file.

Classes

struct  DecodeInfo
 

Functions

bool DecodeUtterance (kaldi::FasterDecoder *decoder, kaldi::DecodableInterface *decodable, DecodeInfo *info, const string &uttid, int32 num_frames, BaseFloat *total_like)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ DecodeUtterance()

bool DecodeUtterance ( kaldi::FasterDecoder decoder,
kaldi::DecodableInterface decodable,
DecodeInfo info,
const string &  uttid,
int32  num_frames,
BaseFloat *  total_like 
)

Definition at line 70 of file gmm-decode-faster-regtree-fmllr.cc.

References DecodeInfo::alignment_writer, DecodeInfo::allow_partial, FasterDecoder::Decode(), FasterDecoder::GetBestPath(), fst::GetLinearSymbolSequence(), rnnlm::i, TableWriter< Holder >::IsOpen(), KALDI_ERR, KALDI_LOG, KALDI_WARN, FasterDecoder::ReachedFinal(), DecodeInfo::word_syms, words, DecodeInfo::words_writer, and TableWriter< Holder >::Write().

Referenced by main().

75  {
76  decoder->Decode(decodable);
77  KALDI_LOG << "Length of file is " << num_frames;
78 
79  VectorFst<LatticeArc> decoded; // linear FST.
80  if ( (info->allow_partial || decoder->ReachedFinal())
81  && decoder->GetBestPath(&decoded) ) {
82  if (!decoder->ReachedFinal())
83  KALDI_WARN << "Decoder did not reach end-state, outputting partial "
84  "traceback.";
85 
86  vector<kaldi::int32> alignment, words;
87  LatticeWeight weight;
88  GetLinearSymbolSequence(decoded, &alignment, &words, &weight);
89 
90  info->words_writer.Write(uttid, words);
91  if (info->alignment_writer.IsOpen())
92  info->alignment_writer.Write(uttid, alignment);
93  if (info->word_syms != NULL) {
94  std::ostringstream ss;
95  ss << uttid << ' ';
96  for (size_t i = 0; i < words.size(); i++) {
97  string s = info->word_syms->Find(words[i]);
98  if (s == "")
99  KALDI_ERR << "Word-id " << words[i] << " not in symbol table.";
100  ss << s << ' ';
101  }
102  ss << '\n';
103  KALDI_LOG << ss.str();
104  }
105 
106  BaseFloat like = -weight.Value1() -weight.Value2();
107  KALDI_LOG << "Log-like per frame = " << (like/num_frames);
108  (*total_like) += like;
109  return true;
110  } else {
111  KALDI_WARN << "Did not successfully decode utterance, length = "
112  << num_frames;
113  return false;
114  }
115 }
int32 words[kMaxOrder]
const kaldi::Int32VectorWriter & alignment_writer
void Decode(DecodableInterface *decodable)
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 Write(const std::string &key, const T &value) const
bool GetBestPath(fst::MutableFst< LatticeArc > *fst_out, bool use_final_probs=true)
GetBestPath gets the decoding traceback.
float BaseFloat
Definition: kaldi-types.h:29
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
fst::SymbolTable * word_syms
#define KALDI_LOG
Definition: kaldi-error.h:153
bool ReachedFinal() const
Returns true if a final state was active on the last frame.
const kaldi::Int32VectorWriter & words_writer

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 117 of file gmm-decode-faster-regtree-fmllr.cc.

References DecodeInfo::acoustic_scale, DecodeInfo::alignment_writer, DecodeInfo::allow_partial, kaldi::ApplyAffineTransform(), DecodeInfo::decoder, DecodeUtterance(), SequentialTableReader< Holder >::Done(), Timer::Elapsed(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), ParseOptions::GetOptArg(), RandomAccessTableReaderMapped< Holder >::HasKey(), rnnlm::i, KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), DecodableAmDiagGmmRegtreeFmllr::NumFramesReady(), DecodableAmDiagGmmUnmapped::NumFramesReady(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), RegressionTree::Read(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), fst::ReadFstKaldi(), FasterDecoderOptions::Register(), ParseOptions::Register(), Input::Stream(), DecodeInfo::trans_model, SequentialTableReader< Holder >::Value(), RandomAccessTableReaderMapped< Holder >::Value(), DecodeInfo::word_syms, and DecodeInfo::words_writer.

117  {
118  try {
119  using namespace kaldi;
120  typedef kaldi::int32 int32;
121 
122  const char *usage = "Decode features using GMM-based model.\n"
123  "Usage: gmm-decode-faster-regtree-fmllr [options] model-in fst-in "
124  "regtree-in features-rspecifier transforms-rspecifier "
125  "words-wspecifier [alignments-wspecifier]\n";
126  ParseOptions po(usage);
127  bool binary = true;
128  bool allow_partial = true;
129  BaseFloat acoustic_scale = 0.1;
130 
131  std::string word_syms_filename, utt2spk_rspecifier;
132  FasterDecoderOptions decoder_opts;
133  decoder_opts.Register(&po, true); // true == include obscure settings.
134  po.Register("utt2spk", &utt2spk_rspecifier, "rspecifier for utterance to "
135  "speaker map");
136  po.Register("binary", &binary, "Write output in binary mode");
137  po.Register("acoustic-scale", &acoustic_scale,
138  "Scaling factor for acoustic likelihoods");
139  po.Register("word-symbol-table", &word_syms_filename,
140  "Symbol table for words [for debug output]");
141  po.Register("allow-partial", &allow_partial,
142  "Produce output even when final state was not reached");
143  po.Read(argc, argv);
144 
145  if (po.NumArgs() < 6 || po.NumArgs() > 7) {
146  po.PrintUsage();
147  exit(1);
148  }
149 
150  std::string model_in_filename = po.GetArg(1),
151  fst_in_filename = po.GetArg(2),
152  regtree_filename = po.GetArg(3),
153  feature_rspecifier = po.GetArg(4),
154  xforms_rspecifier = po.GetArg(5),
155  words_wspecifier = po.GetArg(6),
156  alignment_wspecifier = po.GetOptArg(7);
157 
158  TransitionModel trans_model;
159  AmDiagGmm am_gmm;
160  {
161  bool binary_read;
162  Input ki(model_in_filename, &binary_read);
163  trans_model.Read(ki.Stream(), binary_read);
164  am_gmm.Read(ki.Stream(), binary_read);
165  }
166 
167  VectorFst<StdArc> *decode_fst = fst::ReadFstKaldi(fst_in_filename);
168 
169  RegressionTree regtree;
170  {
171  bool binary_read;
172  Input in(regtree_filename, &binary_read);
173  regtree.Read(in.Stream(), binary_read, am_gmm);
174  }
175 
176  RandomAccessRegtreeFmllrDiagGmmReaderMapped fmllr_reader(xforms_rspecifier,
177  utt2spk_rspecifier);
178 
179  Int32VectorWriter words_writer(words_wspecifier);
180 
181  Int32VectorWriter alignment_writer(alignment_wspecifier);
182 
183  fst::SymbolTable *word_syms = NULL;
184  if (word_syms_filename != "") {
185  word_syms = fst::SymbolTable::ReadText(word_syms_filename);
186  if (!word_syms) {
187  KALDI_ERR << "Could not read symbol table from file "
188  << word_syms_filename;
189  }
190  }
191 
192  BaseFloat tot_like = 0.0;
193  kaldi::int64 frame_count = 0;
194  int num_success = 0, num_fail = 0;
195  FasterDecoder decoder(*decode_fst, decoder_opts);
196 
197  Timer timer;
198 
199  DecodeInfo decode_info(am_gmm, trans_model, &decoder, acoustic_scale,
200  allow_partial, words_writer, alignment_writer,
201  word_syms);
202 
203  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
204  for (; !feature_reader.Done(); feature_reader.Next()) {
205  string utt = feature_reader.Key();
206 
207  Matrix<BaseFloat> features(feature_reader.Value());
208  feature_reader.FreeCurrent();
209  if (features.NumRows() == 0) {
210  KALDI_WARN << "Zero-length utterance: " << utt;
211  num_fail++;
212  continue;
213  }
214 
215  if (!fmllr_reader.HasKey(utt)) { // Decode without FMLLR if none found
216  KALDI_WARN << "No FMLLR transform for key " << utt <<
217  ", decoding without fMLLR.";
218  kaldi::DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model,
219  features,
220  acoustic_scale);
221  if (DecodeUtterance(&decoder, &gmm_decodable, &decode_info,
222  utt, features.NumRows(), &tot_like)) {
223  frame_count += gmm_decodable.NumFramesReady();
224  num_success++;
225  } else {
226  num_fail++;
227  }
228  continue;
229  }
230 
231  // If found, load the transforms for the current utterance.
232  RegtreeFmllrDiagGmm fmllr(fmllr_reader.Value(utt));
233  if (fmllr.NumRegClasses() == 1) {
234  Matrix<BaseFloat> xformed_features(features);
235  Matrix<BaseFloat> fmllr_matrix;
236  fmllr.GetXformMatrix(0, &fmllr_matrix);
237  for (int32 i = 0; i < xformed_features.NumRows(); i++) {
238  SubVector<BaseFloat> row(xformed_features, i);
239  ApplyAffineTransform(fmllr_matrix, &row);
240  }
241  kaldi::DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model,
242  xformed_features,
243  acoustic_scale);
244 
245  if (DecodeUtterance(&decoder, &gmm_decodable, &decode_info,
246  utt, xformed_features.NumRows(), &tot_like)) {
247  frame_count += gmm_decodable.NumFramesReady();
248  num_success++;
249  } else {
250  num_fail++;
251  }
252  } else {
253  kaldi::DecodableAmDiagGmmRegtreeFmllr gmm_decodable(am_gmm, trans_model,
254  features, fmllr,
255  regtree,
256  acoustic_scale);
257  if (DecodeUtterance(&decoder, &gmm_decodable, &decode_info,
258  utt, features.NumRows(), &tot_like)) {
259  frame_count += gmm_decodable.NumFramesReady();
260  num_success++;
261  } else {
262  num_fail++;
263  }
264  }
265  } // end looping over all utterances
266 
267  KALDI_LOG << "Average log-likelihood per frame is " << (tot_like
268  / frame_count) << " over " << frame_count << " frames.";
269 
270  double elapsed = timer.Elapsed();
271  KALDI_LOG << "Time taken [excluding initialization] " << elapsed
272  << "s: real-time factor assuming 100 frames/sec is "
273  << (elapsed * 100.0 / frame_count);
274  KALDI_LOG << "Done " << num_success << " utterances, failed for "
275  << num_fail;
276 
277  delete word_syms;
278  delete decode_fst;
279  if (num_success != 0)
280  return 0;
281  else
282  return 1;
283  }
284  catch(const std::exception &e) {
285  std::cerr << e.what();
286  return -1;
287  }
288 }
bool DecodeUtterance(LatticeBiglmFasterDecoder &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 Read(std::istream &in, bool binary, const AmDiagGmm &am)
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
This class is for when you are reading something in random access, but it may actually be stored per-...
Definition: kaldi-table.h:432
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
An FMLLR (feature-space MLLR) transformation, also called CMLLR (constrained MLLR) is an affine trans...
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
A regression tree is a clustering of Gaussian densities in an acoustic model, such that the group of ...
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:147
#define KALDI_WARN
Definition: kaldi-error.h:150
void Register(OptionsItf *opts, bool full)
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
void ApplyAffineTransform(const MatrixBase< BaseFloat > &xform, VectorBase< BaseFloat > *vec)
Applies the affine transform &#39;xform&#39; to the vector &#39;vec&#39; and overwrites the contents of &#39;vec&#39;...
#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
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501