sgmm2-latgen-faster.cc File Reference
#include <string>
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "sgmm2/am-sgmm2.h"
#include "hmm/transition-model.h"
#include "fstext/fstext-lib.h"
#include "decoder/decoder-wrappers.h"
#include "sgmm2/decodable-am-sgmm2.h"
#include "base/timer.h"
Include dependency graph for sgmm2-latgen-faster.cc:

Go to the source code of this file.

Namespaces

 kaldi
 This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for mispronunciations detection tasks, the reference:
 

Functions

bool ProcessUtterance (LatticeFasterDecoder &decoder, const AmSgmm2 &am_sgmm, const TransitionModel &trans_model, double log_prune, double acoustic_scale, const Matrix< BaseFloat > &features, RandomAccessInt32VectorVectorReader &gselect_reader, RandomAccessBaseFloatVectorReaderMapped &spkvecs_reader, const fst::SymbolTable *word_syms, const std::string &utt, bool determinize, bool allow_partial, Int32VectorWriter *alignments_writer, Int32VectorWriter *words_writer, CompactLatticeWriter *compact_lattice_writer, LatticeWriter *lattice_writer, double *like_ptr)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

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

Definition at line 88 of file sgmm2-latgen-faster.cc.

References kaldi::ClassifyRspecifier(), LatticeFasterDecoderConfig::determinize_lattice, SequentialTableReader< Holder >::Done(), Timer::Elapsed(), ParseOptions::GetArg(), ParseOptions::GetOptArg(), RandomAccessTableReader< 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(), kaldi::ProcessUtterance(), ParseOptions::Read(), TransitionModel::Read(), AmSgmm2::Read(), fst::ReadFstKaldiGeneric(), LatticeFasterDecoderConfig::Register(), ParseOptions::Register(), Timer::Reset(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), and SequentialTableReader< Holder >::Value().

88  {
89  try {
90  using namespace kaldi;
91  typedef kaldi::int32 int32;
92  using fst::SymbolTable;
93  using fst::Fst;
94  using fst::StdArc;
95 
96  const char *usage =
97  "Decode features using SGMM-based model.\n"
98  "Usage: sgmm2-latgen-faster [options] <model-in> (<fst-in>|<fsts-rspecifier>) "
99  "<features-rspecifier> <lattices-wspecifier> [<words-wspecifier> [<alignments-wspecifier>] ]\n";
100  ParseOptions po(usage);
101  BaseFloat acoustic_scale = 0.1;
102  bool allow_partial = false;
103  BaseFloat log_prune = 5.0;
104  string word_syms_filename, gselect_rspecifier, spkvecs_rspecifier,
105  utt2spk_rspecifier;
106 
107  LatticeFasterDecoderConfig decoder_opts;
108  decoder_opts.Register(&po);
109 
110  po.Register("acoustic-scale", &acoustic_scale,
111  "Scaling factor for acoustic likelihoods");
112  po.Register("log-prune", &log_prune,
113  "Pruning beam used to reduce number of exp() evaluations.");
114  po.Register("word-symbol-table", &word_syms_filename,
115  "Symbol table for words [for debug output]");
116  po.Register("allow-partial", &allow_partial,
117  "Produce output even when final state was not reached");
118  po.Register("gselect", &gselect_rspecifier,
119  "rspecifier for precomputed per-frame Gaussian indices.");
120  po.Register("spk-vecs", &spkvecs_rspecifier,
121  "rspecifier for speaker vectors");
122  po.Register("utt2spk", &utt2spk_rspecifier,
123  "rspecifier for utterance to speaker map");
124  po.Read(argc, argv);
125 
126  if (po.NumArgs() < 4 || po.NumArgs() > 6) {
127  po.PrintUsage();
128  exit(1);
129  }
130 
131  if (gselect_rspecifier == "")
132  KALDI_ERR << "--gselect option is required.";
133 
134  std::string model_in_filename = po.GetArg(1),
135  fst_in_str = po.GetArg(2),
136  feature_rspecifier = po.GetArg(3),
137  lattice_wspecifier = po.GetArg(4),
138  words_wspecifier = po.GetOptArg(5),
139  alignment_wspecifier = po.GetOptArg(6);
140 
141  TransitionModel trans_model;
142  kaldi::AmSgmm2 am_sgmm;
143  {
144  bool binary;
145  Input ki(model_in_filename, &binary);
146  trans_model.Read(ki.Stream(), binary);
147  am_sgmm.Read(ki.Stream(), binary);
148  }
149 
150  CompactLatticeWriter compact_lattice_writer;
151  LatticeWriter lattice_writer;
152  bool determinize = decoder_opts.determinize_lattice;
153  if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
154  : lattice_writer.Open(lattice_wspecifier)))
155  KALDI_ERR << "Could not open table for writing lattices: "
156  << lattice_wspecifier;
157 
158  Int32VectorWriter words_writer(words_wspecifier);
159 
160  Int32VectorWriter alignment_writer(alignment_wspecifier);
161 
162  fst::SymbolTable *word_syms = NULL;
163  if (word_syms_filename != "")
164  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
165  KALDI_ERR << "Could not read symbol table from file "
166  << word_syms_filename;
167 
168  RandomAccessInt32VectorVectorReader gselect_reader(gselect_rspecifier);
169  RandomAccessBaseFloatVectorReaderMapped spkvecs_reader(spkvecs_rspecifier,
170  utt2spk_rspecifier);
171 
172  BaseFloat tot_like = 0.0;
173  kaldi::int64 frame_count = 0;
174  int num_success = 0, num_err = 0;
175 
176  Timer timer;
177 
178  if (ClassifyRspecifier(fst_in_str, NULL, NULL) == kNoRspecifier) { // a single FST.
179  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
180  // It's important that we initialize decode_fst after feature_reader, as it
181  // can prevent crashes on systems installed without enough virtual memory.
182  // It has to do with what happens on UNIX systems if you call fork() on a
183  // large process: the page-table entries are duplicated, which requires a
184  // lot of virtual memory.
185  Fst<StdArc> *decode_fst = fst::ReadFstKaldiGeneric(fst_in_str);
186  timer.Reset(); // exclude graph loading time.
187 
188  {
189  LatticeFasterDecoder decoder(*decode_fst, decoder_opts);
190 
191  const std::vector<std::vector<int32> > empty_gselect;
192 
193  for (; !feature_reader.Done(); feature_reader.Next()) {
194  string utt = feature_reader.Key();
195  const Matrix<BaseFloat> &features(feature_reader.Value());
196  if (features.NumRows() == 0) {
197  KALDI_WARN << "Zero-length utterance: " << utt;
198  num_err++;
199  continue;
200  }
201  double like;
202  if (ProcessUtterance(decoder, am_sgmm, trans_model, log_prune, acoustic_scale,
203  features, gselect_reader, spkvecs_reader, word_syms,
204  utt, determinize, allow_partial,
205  &alignment_writer, &words_writer, &compact_lattice_writer,
206  &lattice_writer, &like)) {
207  tot_like += like;
208  frame_count += features.NumRows();
209  KALDI_LOG << "Log-like per frame for utterance " << utt << " is "
210  << (like / features.NumRows()) << " over "
211  << features.NumRows() << " frames.";
212  num_success++;
213  } else { num_err++; }
214  }
215  }
216  delete decode_fst; // only safe to do this after decoder goes out of scope.
217  } else { // We have different FSTs for different utterances.
218  SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_in_str);
219  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
220  for (; !fst_reader.Done(); fst_reader.Next()) {
221  std::string utt = fst_reader.Key();
222  if (!feature_reader.HasKey(utt)) {
223  KALDI_WARN << "Not decoding utterance " << utt
224  << " because no features available.";
225  num_err++;
226  continue;
227  }
228  const Matrix<BaseFloat> &features = feature_reader.Value(utt);
229  if (features.NumRows() == 0) {
230  KALDI_WARN << "Zero-length utterance: " << utt;
231  num_err++;
232  continue;
233  }
234  LatticeFasterDecoder decoder(fst_reader.Value(), decoder_opts);
235  double like;
236 
237  if (ProcessUtterance(decoder, am_sgmm, trans_model, log_prune, acoustic_scale,
238  features, gselect_reader, spkvecs_reader, word_syms,
239  utt, determinize, allow_partial,
240  &alignment_writer, &words_writer, &compact_lattice_writer,
241  &lattice_writer, &like)) {
242  tot_like += like;
243  frame_count += features.NumRows();
244  KALDI_LOG << "Log-like per frame for utterance " << utt << " is "
245  << (like / features.NumRows()) << " over "
246  << features.NumRows() << " frames.";
247  num_success++;
248  } else { num_err++; }
249  }
250  }
251  double elapsed = timer.Elapsed();
252  KALDI_LOG << "Time taken [excluding initialization] "<< elapsed
253  << "s: real-time factor assuming 100 frames/sec is "
254  << (elapsed*100.0/frame_count);
255  KALDI_LOG << "Done " << num_success << " utterances, failed for "
256  << num_err;
257  KALDI_LOG << "Overall log-likelihood per frame = " << (tot_like/frame_count)
258  << " over " << frame_count << " frames.";
259 
260  delete word_syms;
261  return (num_success != 0 ? 0 : 1);
262  } catch(const std::exception &e) {
263  std::cerr << e.what();
264  return -1;
265  }
266 }
void ProcessUtterance(const AmSgmm2 &am_sgmm, const TransitionModel &trans_model, double log_prune, double acoustic_scale, const Matrix< BaseFloat > &features, RandomAccessInt32VectorVectorReader &gselect_reader, RandomAccessBaseFloatVectorReaderMapped &spkvecs_reader, const fst::SymbolTable *word_syms, const std::string &utt, bool determinize, bool allow_partial, Int32VectorWriter *alignments_writer, Int32VectorWriter *words_writer, CompactLatticeWriter *compact_lattice_writer, LatticeWriter *lattice_writer, LatticeFasterDecoder *decoder, double *like_sum, int64 *frame_sum, int32 *num_done, int32 *num_err, TaskSequencer< DecodeUtteranceLatticeFasterClass > *sequencer)
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Class for definition of the subspace Gmm acoustic model.
Definition: am-sgmm2.h:231
bool Open(const std::string &wspecifier)
Fst< StdArc > * ReadFstKaldiGeneric(std::string rxfilename, bool throw_on_err)
Definition: kaldi-fst-io.cc:45
void Reset()
Definition: timer.h:71
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
void Read(std::istream &is, bool binary)
Definition: am-sgmm2.cc:89
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
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
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)
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