gmm-latgen-faster.cc
Go to the documentation of this file.
1 // gmmbin/gmm-latgen-faster.cc
2 
3 // Copyright 2009-2012 Microsoft Corporation
4 // 2013-2014 Johns Hopkins University (author: Daniel Povey)
5 // 2014 Guoguo Chen
6 
7 // See ../../COPYING for clarification regarding multiple authors
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
17 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
18 // MERCHANTABLITY OR NON-INFRINGEMENT.
19 // See the Apache 2 License for the specific language governing permissions and
20 // limitations under the License.
21 
22 
23 #include "base/kaldi-common.h"
24 #include "util/common-utils.h"
25 #include "gmm/am-diag-gmm.h"
26 #include "tree/context-dep.h"
27 #include "hmm/transition-model.h"
28 #include "fstext/fstext-lib.h"
31 #include "base/timer.h"
32 #include "feat/feature-functions.h" // feature reversal
33 
34 int main(int argc, char *argv[]) {
35  try {
36  using namespace kaldi;
37  typedef kaldi::int32 int32;
38  using fst::SymbolTable;
39  using fst::Fst;
40  using fst::StdArc;
41 
42  const char *usage =
43  "Generate lattices using GMM-based model.\n"
44  "Usage: gmm-latgen-faster [options] model-in (fst-in|fsts-rspecifier) features-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,
55  "Scaling factor for acoustic likelihoods");
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),
69  fst_in_str = po.GetArg(2),
70  feature_rspecifier = po.GetArg(3),
71  lattice_wspecifier = po.GetArg(4),
72  words_wspecifier = po.GetOptArg(5),
73  alignment_wspecifier = po.GetOptArg(6);
74 
75  TransitionModel trans_model;
76  AmDiagGmm am_gmm;
77  {
78  bool binary;
79  Input ki(model_in_filename, &binary);
80  trans_model.Read(ki.Stream(), binary);
81  am_gmm.Read(ki.Stream(), binary);
82  }
83 
84  bool determinize = config.determinize_lattice;
85  CompactLatticeWriter compact_lattice_writer;
86  LatticeWriter lattice_writer;
87  if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
88  : lattice_writer.Open(lattice_wspecifier)))
89  KALDI_ERR << "Could not open table for writing lattices: "
90  << lattice_wspecifier;
91 
92  Int32VectorWriter words_writer(words_wspecifier);
93 
94  Int32VectorWriter alignment_writer(alignment_wspecifier);
95 
96  fst::SymbolTable *word_syms = NULL;
97  if (word_syms_filename != "")
98  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
99  KALDI_ERR << "Could not read symbol table from file "
100  << word_syms_filename;
101 
102  double tot_like = 0.0;
103  kaldi::int64 frame_count = 0;
104  int num_done = 0, num_err = 0;
105 
106  if (ClassifyRspecifier(fst_in_str, NULL, NULL) == kNoRspecifier) {
107  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
108  // Input FST is just one FST, not a table of FSTs.
109  Fst<StdArc> *decode_fst = fst::ReadFstKaldiGeneric(fst_in_str);
110  timer.Reset();
111 
112  {
113  LatticeFasterDecoder decoder(*decode_fst, config);
114 
115  for (; !feature_reader.Done(); feature_reader.Next()) {
116  std::string utt = feature_reader.Key();
117  Matrix<BaseFloat> features (feature_reader.Value());
118  feature_reader.FreeCurrent();
119  if (features.NumRows() == 0) {
120  KALDI_WARN << "Zero-length utterance: " << utt;
121  num_err++;
122  continue;
123  }
124 
125  DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model, features,
126  acoustic_scale);
127 
128  double like;
130  decoder, gmm_decodable, trans_model, word_syms, utt,
131  acoustic_scale, determinize, allow_partial, &alignment_writer,
132  &words_writer, &compact_lattice_writer, &lattice_writer,
133  &like)) {
134  tot_like += like;
135  frame_count += features.NumRows();
136  num_done++;
137  } else num_err++;
138  }
139  }
140  delete decode_fst; // delete this only after decoder goes out of scope.
141  } else { // We have different FSTs for different utterances.
142  SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_in_str);
143  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
144  for (; !fst_reader.Done(); fst_reader.Next()) {
145  std::string utt = fst_reader.Key();
146  if (!feature_reader.HasKey(utt)) {
147  KALDI_WARN << "Not decoding utterance " << utt
148  << " because no features available.";
149  num_err++;
150  continue;
151  }
152  const Matrix<BaseFloat> &features = feature_reader.Value(utt);
153  if (features.NumRows() == 0) {
154  KALDI_WARN << "Zero-length utterance: " << utt;
155  num_err++;
156  continue;
157  }
158 
159  LatticeFasterDecoder decoder(fst_reader.Value(), config);
160  DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model, features,
161  acoustic_scale);
162  double like;
164  decoder, gmm_decodable, trans_model, word_syms, utt,
165  acoustic_scale, determinize, allow_partial, &alignment_writer,
166  &words_writer, &compact_lattice_writer, &lattice_writer,
167  &like)) {
168  tot_like += like;
169  frame_count += features.NumRows();
170  num_done++;
171  } else num_err++;
172  }
173  }
174 
175  double elapsed = timer.Elapsed();
176  KALDI_LOG << "Time taken "<< elapsed
177  << "s: real-time factor assuming 100 frames/sec is "
178  << (elapsed*100.0/frame_count);
179  KALDI_LOG << "Done " << num_done << " utterances, failed for "
180  << num_err;
181  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count) << " over "
182  << frame_count << " frames.";
183 
184  delete word_syms;
185  if (num_done != 0) return 0;
186  else return 1;
187  } catch(const std::exception &e) {
188  std::cerr << e.what();
189  return -1;
190  }
191 }
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
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
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...
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
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
std::istream & Stream()
Definition: kaldi-io.cc:826
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)
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
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)
This is the "normal" lattice-generating decoder.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
int main(int argc, char *argv[])
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
std::string GetOptArg(int param) const