latgen-faster-mapped.cc File Reference
Include dependency graph for latgen-faster-mapped.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 33 of file latgen-faster-mapped.cc.

References kaldi::ClassifyRspecifier(), kaldi::DecodeUtteranceLatticeFaster(), LatticeFasterDecoderConfig::determinize_lattice, SequentialTableReader< Holder >::Done(), Timer::Elapsed(), SequentialTableReader< Holder >::FreeCurrent(), 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(), ParseOptions::Read(), fst::ReadFstKaldiGeneric(), kaldi::ReadKaldiObject(), LatticeFasterDecoderConfig::Register(), ParseOptions::Register(), Timer::Reset(), RandomAccessTableReader< Holder >::Value(), and SequentialTableReader< Holder >::Value().

33  {
34  try {
35  using namespace kaldi;
36  typedef kaldi::int32 int32;
37  using fst::SymbolTable;
38  using fst::Fst;
39  using fst::StdArc;
40 
41  const char *usage =
42  "Generate lattices, reading log-likelihoods as matrices\n"
43  " (model is needed only for the integer mappings in its transition-model)\n"
44  "Usage: latgen-faster-mapped [options] trans-model-in (fst-in|fsts-rspecifier) loglikes-rspecifier"
45  " lattice-wspecifier [ words-wspecifier [alignments-wspecifier] ]\n";
46  ParseOptions po(usage);
47  Timer timer;
48  bool allow_partial = false;
49  BaseFloat acoustic_scale = 0.1;
51 
52  std::string word_syms_filename;
53  config.Register(&po);
54  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
55 
56  po.Register("word-symbol-table", &word_syms_filename, "Symbol table for words [for debug output]");
57  po.Register("allow-partial", &allow_partial, "If true, produce output even if end state was not reached.");
58 
59  po.Read(argc, argv);
60 
61  if (po.NumArgs() < 4 || po.NumArgs() > 6) {
62  po.PrintUsage();
63  exit(1);
64  }
65 
66  std::string model_in_filename = po.GetArg(1),
67  fst_in_str = po.GetArg(2),
68  feature_rspecifier = po.GetArg(3),
69  lattice_wspecifier = po.GetArg(4),
70  words_wspecifier = po.GetOptArg(5),
71  alignment_wspecifier = po.GetOptArg(6);
72 
73  TransitionModel trans_model;
74  ReadKaldiObject(model_in_filename, &trans_model);
75 
76  bool determinize = config.determinize_lattice;
77  CompactLatticeWriter compact_lattice_writer;
78  LatticeWriter lattice_writer;
79  if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
80  : lattice_writer.Open(lattice_wspecifier)))
81  KALDI_ERR << "Could not open table for writing lattices: "
82  << lattice_wspecifier;
83 
84  Int32VectorWriter words_writer(words_wspecifier);
85 
86  Int32VectorWriter alignment_writer(alignment_wspecifier);
87 
88  fst::SymbolTable *word_syms = NULL;
89  if (word_syms_filename != "")
90  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
91  KALDI_ERR << "Could not read symbol table from file "
92  << word_syms_filename;
93 
94  double tot_like = 0.0;
95  kaldi::int64 frame_count = 0;
96  int num_success = 0, num_fail = 0;
97 
98  if (ClassifyRspecifier(fst_in_str, NULL, NULL) == kNoRspecifier) {
99  SequentialBaseFloatMatrixReader loglike_reader(feature_rspecifier);
100  // Input FST is just one FST, not a table of FSTs.
101  Fst<StdArc> *decode_fst = fst::ReadFstKaldiGeneric(fst_in_str);
102  timer.Reset();
103 
104  {
105  LatticeFasterDecoder decoder(*decode_fst, config);
106 
107  for (; !loglike_reader.Done(); loglike_reader.Next()) {
108  std::string utt = loglike_reader.Key();
109  Matrix<BaseFloat> loglikes (loglike_reader.Value());
110  loglike_reader.FreeCurrent();
111  if (loglikes.NumRows() == 0) {
112  KALDI_WARN << "Zero-length utterance: " << utt;
113  num_fail++;
114  continue;
115  }
116 
117  DecodableMatrixScaledMapped decodable(trans_model, loglikes, acoustic_scale);
118 
119  double like;
121  decoder, decodable, trans_model, word_syms, utt,
122  acoustic_scale, determinize, allow_partial, &alignment_writer,
123  &words_writer, &compact_lattice_writer, &lattice_writer,
124  &like)) {
125  tot_like += like;
126  frame_count += loglikes.NumRows();
127  num_success++;
128  } else num_fail++;
129  }
130  }
131  delete decode_fst; // delete this only after decoder goes out of scope.
132  } else { // We have different FSTs for different utterances.
133  SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_in_str);
134  RandomAccessBaseFloatMatrixReader loglike_reader(feature_rspecifier);
135  for (; !fst_reader.Done(); fst_reader.Next()) {
136  std::string utt = fst_reader.Key();
137  if (!loglike_reader.HasKey(utt)) {
138  KALDI_WARN << "Not decoding utterance " << utt
139  << " because no loglikes available.";
140  num_fail++;
141  continue;
142  }
143  const Matrix<BaseFloat> &loglikes = loglike_reader.Value(utt);
144  if (loglikes.NumRows() == 0) {
145  KALDI_WARN << "Zero-length utterance: " << utt;
146  num_fail++;
147  continue;
148  }
149  LatticeFasterDecoder decoder(fst_reader.Value(), config);
150  DecodableMatrixScaledMapped decodable(trans_model, loglikes, acoustic_scale);
151  double like;
153  decoder, decodable, trans_model, word_syms, utt, acoustic_scale,
154  determinize, allow_partial, &alignment_writer, &words_writer,
155  &compact_lattice_writer, &lattice_writer, &like)) {
156  tot_like += like;
157  frame_count += loglikes.NumRows();
158  num_success++;
159  } else num_fail++;
160  }
161  }
162 
163  double elapsed = timer.Elapsed();
164  KALDI_LOG << "Time taken "<< elapsed
165  << "s: real-time factor assuming 100 frames/sec is "
166  << (elapsed*100.0/frame_count);
167  KALDI_LOG << "Done " << num_success << " utterances, failed for "
168  << num_fail;
169  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count) << " over "
170  << frame_count<<" frames.";
171 
172  delete word_syms;
173  if (num_success != 0) return 0;
174  else return 1;
175  } catch(const std::exception &e) {
176  std::cerr << e.what();
177  return -1;
178  }
179 }
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
void Reset()
Definition: timer.h:71
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 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
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
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
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