sgmm2-latgen-faster.cc
Go to the documentation of this file.
1 // sgmm2bin/sgmm2-latgen-faster.cc
2 
3 // Copyright 2009-2012 Saarland University; Microsoft Corporation;
4 // 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 #include <string>
23 using std::string;
24 
25 #include "base/kaldi-common.h"
26 #include "util/common-utils.h"
27 #include "sgmm2/am-sgmm2.h"
28 #include "hmm/transition-model.h"
29 #include "fstext/fstext-lib.h"
32 #include "base/timer.h"
33 
34 namespace kaldi {
35 
36 // the reference arguments at the beginning are not const as the style guide
37 // requires, but are best viewed as inputs.
39  const AmSgmm2 &am_sgmm,
40  const TransitionModel &trans_model,
41  double log_prune,
42  double acoustic_scale,
43  const Matrix<BaseFloat> &features,
46  const fst::SymbolTable *word_syms,
47  const std::string &utt,
48  bool determinize,
49  bool allow_partial,
50  Int32VectorWriter *alignments_writer,
51  Int32VectorWriter *words_writer,
52  CompactLatticeWriter *compact_lattice_writer,
53  LatticeWriter *lattice_writer,
54  double *like_ptr) { // puts utterance's like in like_ptr on success.
55  using fst::Fst;
56 
57  Sgmm2PerSpkDerivedVars spk_vars;
58  if (spkvecs_reader.IsOpen()) {
59  if (spkvecs_reader.HasKey(utt)) {
60  spk_vars.SetSpeakerVector(spkvecs_reader.Value(utt));
61  am_sgmm.ComputePerSpkDerivedVars(&spk_vars);
62  } else {
63  KALDI_WARN << "Cannot find speaker vector for " << utt << ", not decoding this utterance";
64  return false; // We could use zero, but probably the user would want to know about this
65  // (this would normally be a script error or some kind of failure).
66  }
67  }
68  if (!gselect_reader.HasKey(utt) ||
69  gselect_reader.Value(utt).size() != features.NumRows()) {
70  KALDI_WARN << "No Gaussian-selection info available for utterance "
71  << utt << " (or wrong size)";
72  }
73 
74  const std::vector<std::vector<int32> > &gselect =
75  gselect_reader.Value(utt);
76 
77  DecodableAmSgmm2Scaled sgmm_decodable(am_sgmm, trans_model, features, gselect,
78  log_prune, acoustic_scale, &spk_vars);
79 
81  decoder, sgmm_decodable, trans_model, word_syms, utt, acoustic_scale,
82  determinize, allow_partial, alignments_writer, words_writer,
83  compact_lattice_writer, lattice_writer, like_ptr);
84 }
85 
86 } // end namespace kaldi
87 
88 int main(int argc, char *argv[]) {
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 }
267 
268 
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
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
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
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 ComputePerSpkDerivedVars(Sgmm2PerSpkDerivedVars *vars) const
Computes the per-speaker derived vars; assumes vars->v_s is already set up.
Definition: am-sgmm2.cc:1369
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
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
#define KALDI_WARN
Definition: kaldi-error.h:150
bool HasKey(const std::string &key)
This is the "normal" lattice-generating decoder.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
const T & Value(const std::string &key)
void SetSpeakerVector(const Vector< BaseFloat > &v_s_in)
Definition: am-sgmm2.h:180
#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
int main(int argc, char *argv[])