gmm-latgen-faster-regtree-fmllr.cc File Reference
Include dependency graph for gmm-latgen-faster-regtree-fmllr.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 37 of file gmm-latgen-faster-regtree-fmllr.cc.

References kaldi::ClassifyRspecifier(), kaldi::DecodeUtteranceLatticeFaster(), LatticeFasterDecoderConfig::determinize_lattice, SequentialTableReader< Holder >::Done(), Timer::Elapsed(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), ParseOptions::GetOptArg(), RandomAccessTableReaderMapped< Holder >::HasKey(), KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), kaldi::kNoRspecifier, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumRows(), TableWriter< Holder >::Open(), ParseOptions::PrintUsage(), RegressionTree::Read(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), fst::ReadFstKaldiGeneric(), LatticeFasterDecoderConfig::Register(), ParseOptions::Register(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and RandomAccessTableReaderMapped< Holder >::Value().

37  {
38  try {
39  using namespace kaldi;
40  typedef kaldi::int32 int32;
41  using fst::SymbolTable;
42  using fst::Fst;
43  using fst::StdArc;
44 
45  const char *usage =
46  "Generate lattices using GMM-based model and RegTree-FMLLR adaptation.\n"
47  "Usage: gmm-latgen-faster-regtree-fmllr [options] model-in regtree-in (fst-in|fsts-rspecifier) features-rspecifier transform-rspecifier"
48  " lattice-wspecifier [ words-wspecifier [alignments-wspecifier] ]\n";
49  ParseOptions po(usage);
50  Timer timer;
51  bool allow_partial = false;
52  BaseFloat acoustic_scale = 0.1;
54 
55  std::string word_syms_filename, utt2spk_rspecifier;
56  config.Register(&po);
57  po.Register("utt2spk", &utt2spk_rspecifier, "rspecifier for utterance to "
58  "speaker map used to load the transform");
59  po.Register("acoustic-scale", &acoustic_scale,
60  "Scaling factor for acoustic likelihoods");
61  po.Register("word-symbol-table", &word_syms_filename,
62  "Symbol table for words [for debug output]");
63  po.Register("allow-partial", &allow_partial,
64  "If true, produce output even if end state was not reached.");
65 
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  regtree_in_str = po.GetArg(2),
75  fst_in_str = po.GetArg(3),
76  feature_rspecifier = po.GetArg(4),
77  xforms_rspecifier = po.GetArg(5),
78  lattice_wspecifier = po.GetArg(6),
79  words_wspecifier = po.GetOptArg(7),
80  alignment_wspecifier = po.GetOptArg(8);
81 
82  TransitionModel trans_model;
83  AmDiagGmm am_gmm;
84  {
85  bool binary;
86  Input ki(model_in_filename, &binary);
87  trans_model.Read(ki.Stream(), binary);
88  am_gmm.Read(ki.Stream(), binary);
89  }
90 
91  RegressionTree regtree;
92  {
93  bool binary_read;
94  Input in(regtree_in_str, &binary_read);
95  regtree.Read(in.Stream(), binary_read, am_gmm);
96  }
97 
98  RandomAccessRegtreeFmllrDiagGmmReaderMapped fmllr_reader(xforms_rspecifier,
99  utt2spk_rspecifier);
100 
101  bool determinize = config.determinize_lattice;
102  CompactLatticeWriter compact_lattice_writer;
103  LatticeWriter lattice_writer;
104  if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
105  : lattice_writer.Open(lattice_wspecifier)))
106  KALDI_ERR << "Could not open table for writing lattices: "
107  << lattice_wspecifier;
108 
109  Int32VectorWriter words_writer(words_wspecifier);
110 
111  Int32VectorWriter alignment_writer(alignment_wspecifier);
112 
113  fst::SymbolTable *word_syms = NULL;
114  if (word_syms_filename != "")
115  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
116  KALDI_ERR << "Could not read symbol table from file "
117  << word_syms_filename;
118 
119  double tot_like = 0.0;
120  kaldi::int64 frame_count = 0;
121  int num_done = 0, num_err = 0;
122 
123  if (ClassifyRspecifier(fst_in_str, NULL, NULL) == kNoRspecifier) {
124  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
125  // Input FST is just one FST, not a table of FSTs.
126  Fst<StdArc> *decode_fst = fst::ReadFstKaldiGeneric(fst_in_str);
127 
128  {
129  LatticeFasterDecoder decoder(*decode_fst, config);
130 
131  for (; !feature_reader.Done(); feature_reader.Next()) {
132  std::string utt = feature_reader.Key();
133  Matrix<BaseFloat> features (feature_reader.Value());
134  feature_reader.FreeCurrent();
135  if (features.NumRows() == 0) {
136  KALDI_WARN << "Zero-length utterance: " << utt;
137  num_err++;
138  continue;
139  }
140  if (!fmllr_reader.HasKey(utt)) {
141  KALDI_WARN << "Not decoding utterance " << utt
142  << " because no transform available.";
143  num_err++;
144  continue;
145  }
146 
147  RegtreeFmllrDiagGmm fmllr(fmllr_reader.Value(utt));
148 
149  kaldi::DecodableAmDiagGmmRegtreeFmllr gmm_decodable(am_gmm, trans_model,
150  features, fmllr,
151  regtree,
152  acoustic_scale);
153  double like;
155  decoder, gmm_decodable, trans_model, word_syms, utt, acoustic_scale,
156  determinize, allow_partial, &alignment_writer, &words_writer,
157  &compact_lattice_writer, &lattice_writer, &like)) {
158  tot_like += like;
159  frame_count += features.NumRows();
160  num_done++;
161  } else num_err++;
162  }
163  }
164  delete decode_fst; // delete this only after decoder goes out of scope.
165  } else { // We have different FSTs for different utterances.
166  SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_in_str);
167  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
168  for (; !fst_reader.Done(); fst_reader.Next()) {
169  std::string utt = fst_reader.Key();
170  const Matrix<BaseFloat> &features = feature_reader.Value(utt);
171  if (features.NumRows() == 0) {
172  KALDI_WARN << "Zero-length utterance: " << utt;
173  num_err++;
174  continue;
175  }
176  if (!fmllr_reader.HasKey(utt)) {
177  KALDI_WARN << "Not decoding utterance " << utt
178  << " because no transform available.";
179  num_err++;
180  continue;
181  }
182 
183  RegtreeFmllrDiagGmm fmllr(fmllr_reader.Value(utt));
184  kaldi::DecodableAmDiagGmmRegtreeFmllr gmm_decodable(am_gmm, trans_model,
185  features, fmllr,
186  regtree,
187  acoustic_scale);
188 
189  LatticeFasterDecoder decoder(fst_reader.Value(), config);
190  double like;
192  decoder, gmm_decodable, trans_model, word_syms, utt, acoustic_scale,
193  determinize, allow_partial, &alignment_writer, &words_writer,
194  &compact_lattice_writer, &lattice_writer, &like)) {
195  tot_like += like;
196  frame_count += features.NumRows();
197  num_done++;
198  } else num_err++;
199  }
200  }
201 
202  double elapsed = timer.Elapsed();
203  KALDI_LOG << "Time taken "<< elapsed
204  << "s: real-time factor assuming 100 frames/sec is "
205  << (elapsed*100.0/frame_count);
206  KALDI_LOG << "Done " << num_done << " utterances, failed for "
207  << num_err;
208  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count) << " over "
209  << frame_count << " frames.";
210 
211  delete word_syms;
212  if (num_done != 0) return 0;
213  else return 1;
214  } catch(const std::exception &e) {
215  std::cerr << e.what();
216  return -1;
217  }
218 }
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
bool Open(const std::string &wspecifier)
Fst< StdArc > * ReadFstKaldiGeneric(std::string rxfilename, bool throw_on_err)
Definition: kaldi-fst-io.cc:45
fst::StdArc StdArc
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
bool DecodeUtteranceLatticeFaster(LatticeFasterDecoderTpl< FST > &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)
This function DecodeUtteranceLatticeFaster is used in several decoders, and we have moved it here...
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
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
This is the "normal" lattice-generating decoder.
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
#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