latgen-incremental-mapped.cc
Go to the documentation of this file.
1 // bin/latgen-incremental-mapped.cc
2 
3 // Copyright 2019 Zhehuai Chen
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #include "base/kaldi-common.h"
21 #include "util/common-utils.h"
22 #include "tree/context-dep.h"
23 #include "hmm/transition-model.h"
24 #include "fstext/fstext-lib.h"
27 #include "base/timer.h"
28 
29 int main(int argc, char *argv[]) {
30  try {
31  using namespace kaldi;
32  typedef kaldi::int32 int32;
33  using fst::SymbolTable;
34  using fst::Fst;
35  using fst::StdArc;
36 
37  const char *usage =
38  "Generate lattices, reading log-likelihoods as matrices\n"
39  " (model is needed only for the integer mappings in its transition-model)\n"
40  "The lattice determinization algorithm here can operate\n"
41  "incrementally.\n"
42  "Usage: latgen-incremental-mapped [options] trans-model-in "
43  "(fst-in|fsts-rspecifier) loglikes-rspecifier"
44  " lattice-wspecifier [ words-wspecifier [alignments-wspecifier] ]\n";
45  ParseOptions po(usage);
46  Timer timer;
47  bool allow_partial = false;
48  BaseFloat acoustic_scale = 0.1;
50 
51  std::string word_syms_filename;
52  config.Register(&po);
53  po.Register("acoustic-scale", &acoustic_scale,
54  "Scaling factor for acoustic likelihoods");
55 
56  po.Register("word-symbol-table", &word_syms_filename,
57  "Symbol table for words [for debug output]");
58  po.Register("allow-partial", &allow_partial,
59  "If true, produce output even if end state was not reached.");
60 
61  po.Read(argc, argv);
62 
63  if (po.NumArgs() < 4 || po.NumArgs() > 6) {
64  po.PrintUsage();
65  exit(1);
66  }
67 
68  std::string model_in_filename = po.GetArg(1), fst_in_str = po.GetArg(2),
69  feature_rspecifier = po.GetArg(3), 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 = true;
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 " << word_syms_filename;
92 
93  double tot_like = 0.0;
94  kaldi::int64 frame_count = 0;
95  int num_success = 0, num_fail = 0;
96 
97  if (ClassifyRspecifier(fst_in_str, NULL, NULL) == kNoRspecifier) {
98  SequentialBaseFloatMatrixReader loglike_reader(feature_rspecifier);
99  // Input FST is just one FST, not a table of FSTs.
100  Fst<StdArc> *decode_fst = fst::ReadFstKaldiGeneric(fst_in_str);
101  timer.Reset();
102 
103  {
104  LatticeIncrementalDecoder decoder(*decode_fst, trans_model, config);
105 
106  for (; !loglike_reader.Done(); loglike_reader.Next()) {
107  std::string utt = loglike_reader.Key();
108  Matrix<BaseFloat> loglikes(loglike_reader.Value());
109  loglike_reader.FreeCurrent();
110  if (loglikes.NumRows() == 0) {
111  KALDI_WARN << "Zero-length utterance: " << utt;
112  num_fail++;
113  continue;
114  }
115 
116  DecodableMatrixScaledMapped decodable(trans_model, loglikes,
117  acoustic_scale);
118 
119  double like;
121  decoder, decodable, trans_model, word_syms, utt, acoustic_scale,
122  determinize, allow_partial, &alignment_writer, &words_writer,
123  &compact_lattice_writer, &lattice_writer, &like)) {
124  tot_like += like;
125  frame_count += loglikes.NumRows();
126  num_success++;
127  } else {
128  num_fail++;
129  }
130  }
131  }
132  delete decode_fst; // delete this only after decoder goes out of scope.
133  } else { // We have different FSTs for different utterances.
134  SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_in_str);
135  RandomAccessBaseFloatMatrixReader loglike_reader(feature_rspecifier);
136  for (; !fst_reader.Done(); fst_reader.Next()) {
137  std::string utt = fst_reader.Key();
138  if (!loglike_reader.HasKey(utt)) {
139  KALDI_WARN << "Not decoding utterance " << utt
140  << " because no loglikes available.";
141  num_fail++;
142  continue;
143  }
144  const Matrix<BaseFloat> &loglikes = loglike_reader.Value(utt);
145  if (loglikes.NumRows() == 0) {
146  KALDI_WARN << "Zero-length utterance: " << utt;
147  num_fail++;
148  continue;
149  }
150  LatticeIncrementalDecoder decoder(fst_reader.Value(), trans_model, config);
151  DecodableMatrixScaledMapped decodable(trans_model, loglikes, acoustic_scale);
152  double like;
154  decoder, decodable, trans_model, word_syms, utt, acoustic_scale,
155  determinize, allow_partial, &alignment_writer, &words_writer,
156  &compact_lattice_writer, &lattice_writer, &like)) {
157  tot_like += like;
158  frame_count += loglikes.NumRows();
159  num_success++;
160  } else {
161  num_fail++;
162  }
163  }
164  }
165 
166  double elapsed = timer.Elapsed();
167  KALDI_LOG << "Time taken " << elapsed
168  << "s: real-time factor assuming 100 frames/sec is "
169  << (elapsed * 100.0 / frame_count);
170  KALDI_LOG << "Done " << num_success << " utterances, failed for " << num_fail;
171  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like / frame_count)
172  << " over " << frame_count << " frames.";
173 
174  delete word_syms;
175  if (num_success != 0)
176  return 0;
177  else
178  return 1;
179  } catch (const std::exception &e) {
180  std::cerr << e.what();
181  return -1;
182  }
183 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool DecodeUtteranceLatticeIncremental(LatticeIncrementalDecoderTpl< 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)
TODO.
bool Open(const std::string &wspecifier)
Fst< StdArc > * ReadFstKaldiGeneric(std::string rxfilename, bool throw_on_err)
Definition: kaldi-fst-io.cc:45
This is an extention to the "normal" lattice-generating decoder.
void Reset()
Definition: timer.h:71
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
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
void Register(const std::string &name, bool *ptr, const std::string &doc)
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
const T & Value(const std::string &key)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
bool HasKey(const std::string &key)
int main(int argc, char *argv[])
int NumArgs() const
Number of positional parameters (c.f. argc-1).
The normal decoder, lattice-faster-decoder.h, sometimes has an issue when doing real-time application...
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
std::string GetOptArg(int param) const