gmm-latgen-biglm-faster.cc
Go to the documentation of this file.
1 // gmmbin/gmm-latgen-biglm-faster.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 // 2013 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 
33 
34 namespace kaldi {
35 // Takes care of output. Returns true on success.
36 bool DecodeUtterance(LatticeBiglmFasterDecoder &decoder, // not const but is really an input.
37  DecodableInterface &decodable, // not const but is really an input.
38  const TransitionModel &trans_model,
39  const fst::SymbolTable *word_syms,
40  std::string utt,
41  double acoustic_scale,
42  bool determinize,
43  bool allow_partial,
44  Int32VectorWriter *alignment_writer,
45  Int32VectorWriter *words_writer,
46  CompactLatticeWriter *compact_lattice_writer,
47  LatticeWriter *lattice_writer,
48  double *like_ptr) { // puts utterance's like in like_ptr on success.
49  using fst::VectorFst;
50 
51  if (!decoder.Decode(&decodable)) {
52  KALDI_WARN << "Failed to decode file " << utt;
53  return false;
54  }
55  if (!decoder.ReachedFinal()) {
56  if (allow_partial) {
57  KALDI_WARN << "Outputting partial output for utterance " << utt
58  << " since no final-state reached\n";
59  } else {
60  KALDI_WARN << "Not producing output for utterance " << utt
61  << " since no final-state reached and "
62  << "--allow-partial=false.\n";
63  return false;
64  }
65  }
66 
67  double likelihood;
68  LatticeWeight weight;
69  int32 num_frames;
70  { // First do some stuff with word-level traceback...
71  VectorFst<LatticeArc> decoded;
72  decoder.GetBestPath(&decoded);
73  if (decoded.NumStates() == 0)
74  // Shouldn't really reach this point as already checked success.
75  KALDI_ERR << "Failed to get traceback for utterance " << utt;
76 
77  std::vector<int32> alignment;
78  std::vector<int32> words;
79  GetLinearSymbolSequence(decoded, &alignment, &words, &weight);
80  num_frames = alignment.size();
81  if (words_writer->IsOpen())
82  words_writer->Write(utt, words);
83  if (alignment_writer->IsOpen())
84  alignment_writer->Write(utt, alignment);
85  if (word_syms != NULL) {
86  std::cerr << utt << ' ';
87  for (size_t i = 0; i < words.size(); i++) {
88  std::string s = word_syms->Find(words[i]);
89  if (s == "")
90  KALDI_ERR << "Word-id " << words[i] <<" not in symbol table.";
91  std::cerr << s << ' ';
92  }
93  std::cerr << '\n';
94  }
95  likelihood = -(weight.Value1() + weight.Value2());
96  }
97 
98  // Get lattice, and do determinization if requested.
99  Lattice lat;
100  decoder.GetRawLattice(&lat);
101  if (lat.NumStates() == 0)
102  KALDI_ERR << "Unexpected problem getting lattice for utterance " << utt;
103  fst::Connect(&lat);
104  if (determinize) {
105  CompactLattice clat;
107  trans_model,
108  &lat,
109  decoder.GetOptions().lattice_beam,
110  &clat,
111  decoder.GetOptions().det_opts))
112  KALDI_WARN << "Determinization finished earlier than the beam for "
113  << "utterance " << utt;
114  // We'll write the lattice without acoustic scaling.
115  if (acoustic_scale != 0.0)
116  fst::ScaleLattice(fst::AcousticLatticeScale(1.0 / acoustic_scale), &clat);
117  compact_lattice_writer->Write(utt, clat);
118  } else {
119  Lattice fst;
120  decoder.GetRawLattice(&fst);
121  if (fst.NumStates() == 0)
122  KALDI_ERR << "Unexpected problem getting lattice for utterance "
123  << utt;
124  fst::Connect(&fst); // Will get rid of this later... shouldn't have any
125  // disconnected states there, but we seem to.
126  if (acoustic_scale != 0.0) // We'll write the lattice without acoustic scaling
127  fst::ScaleLattice(fst::AcousticLatticeScale(1.0 / acoustic_scale), &fst);
128  lattice_writer->Write(utt, fst);
129  }
130  KALDI_LOG << "Log-like per frame for utterance " << utt << " is "
131  << (likelihood / num_frames) << " over "
132  << num_frames << " frames.";
133  KALDI_VLOG(2) << "Cost for utterance " << utt << " is "
134  << weight.Value1() << " + " << weight.Value2();
135  *like_ptr = likelihood;
136  return true;
137 }
138 
139 }
140 
141 
142 
143 int main(int argc, char *argv[]) {
144  try {
145  using namespace kaldi;
146  typedef kaldi::int32 int32;
147  using fst::SymbolTable;
148  using fst::VectorFst;
149  using fst::Fst;
150  using fst::StdArc;
151  using fst::ReadFstKaldi;
152 
153  const char *usage =
154  "Generate lattices using GMM-based model.\n"
155  "User supplies LM used to generate decoding graph, and desired LM;\n"
156  "this decoder applies the difference during decoding\n"
157  "Usage: gmm-latgen-biglm-faster [options] model-in (fst-in|fsts-rspecifier) "
158  "oldlm-fst-in newlm-fst-in features-rspecifier"
159  " lattice-wspecifier [ words-wspecifier [alignments-wspecifier] ]\n";
160  ParseOptions po(usage);
161  Timer timer;
162  bool allow_partial = false;
163  BaseFloat acoustic_scale = 0.1;
165 
166  std::string word_syms_filename;
167  config.Register(&po);
168  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
169 
170  po.Register("word-symbol-table", &word_syms_filename, "Symbol table for words [for debug output]");
171  po.Register("allow-partial", &allow_partial, "If true, produce output even if end state was not reached.");
172 
173  po.Read(argc, argv);
174 
175  if (po.NumArgs() < 6 || po.NumArgs() > 8) {
176  po.PrintUsage();
177  exit(1);
178  }
179 
180  std::string model_in_filename = po.GetArg(1),
181  fst_in_str = po.GetArg(2),
182  old_lm_fst_rxfilename = po.GetArg(3),
183  new_lm_fst_rxfilename = po.GetArg(4),
184  feature_rspecifier = po.GetArg(5),
185  lattice_wspecifier = po.GetArg(6),
186  words_wspecifier = po.GetOptArg(7),
187  alignment_wspecifier = po.GetOptArg(8);
188 
189  TransitionModel trans_model;
190  AmDiagGmm am_gmm;
191  {
192  bool binary;
193  Input ki(model_in_filename, &binary);
194  trans_model.Read(ki.Stream(), binary);
195  am_gmm.Read(ki.Stream(), binary);
196  }
197 
198  VectorFst<StdArc> *old_lm_fst = fst::CastOrConvertToVectorFst(
199  fst::ReadFstKaldiGeneric(old_lm_fst_rxfilename));
200  ApplyProbabilityScale(-1.0, old_lm_fst); // Negate old LM probs...
201 
202  VectorFst<StdArc> *new_lm_fst = fst::CastOrConvertToVectorFst(
203  fst::ReadFstKaldiGeneric(new_lm_fst_rxfilename));
204 
205  fst::BackoffDeterministicOnDemandFst<StdArc> old_lm_dfst(*old_lm_fst);
206  fst::BackoffDeterministicOnDemandFst<StdArc> new_lm_dfst(*new_lm_fst);
207  fst::ComposeDeterministicOnDemandFst<StdArc> compose_dfst(&old_lm_dfst,
208  &new_lm_dfst);
209  fst::CacheDeterministicOnDemandFst<StdArc> cache_dfst(&compose_dfst);
210 
211  bool determinize = config.determinize_lattice;
212  CompactLatticeWriter compact_lattice_writer;
213  LatticeWriter lattice_writer;
214  if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
215  : lattice_writer.Open(lattice_wspecifier)))
216  KALDI_ERR << "Could not open table for writing lattices: "
217  << lattice_wspecifier;
218 
219  Int32VectorWriter words_writer(words_wspecifier);
220 
221  Int32VectorWriter alignment_writer(alignment_wspecifier);
222 
223  fst::SymbolTable *word_syms = NULL;
224  if (word_syms_filename != "")
225  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
226  KALDI_ERR << "Could not read symbol table from file "
227  << word_syms_filename;
228 
229  double tot_like = 0.0;
230  kaldi::int64 frame_count = 0;
231  int num_success = 0, num_fail = 0;
232 
233 
234  if (ClassifyRspecifier(fst_in_str, NULL, NULL) == kNoRspecifier) {
235  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
236  // Input FST is just one FST, not a table of FSTs.
237  Fst<StdArc> *decode_fst = fst::ReadFstKaldiGeneric(fst_in_str);
238 
239  {
240  LatticeBiglmFasterDecoder decoder(*decode_fst, config, &cache_dfst);
241 
242  for (; !feature_reader.Done(); feature_reader.Next()) {
243  std::string utt = feature_reader.Key();
244  Matrix<BaseFloat> features (feature_reader.Value());
245  feature_reader.FreeCurrent();
246  if (features.NumRows() == 0) {
247  KALDI_WARN << "Zero-length utterance: " << utt;
248  num_fail++;
249  continue;
250  }
251 
252  DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model, features,
253  acoustic_scale);
254 
255 
256  double like;
257  if (DecodeUtterance(decoder, gmm_decodable, trans_model, word_syms,
258  utt, acoustic_scale, determinize, allow_partial,
259  &alignment_writer, &words_writer,
260  &compact_lattice_writer, &lattice_writer,
261  &like)) {
262  tot_like += like;
263  frame_count += features.NumRows();
264  num_success++;
265  } else num_fail++;
266  }
267  }
268  delete decode_fst; // delete this only after decoder goes out of scope.
269  } else { // We have different FSTs for different utterances.
270  SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_in_str);
271  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
272  for (; !fst_reader.Done(); fst_reader.Next()) {
273  std::string utt = fst_reader.Key();
274  if (!feature_reader.HasKey(utt)) {
275  KALDI_WARN << "Not decoding utterance " << utt
276  << " because no features available.";
277  num_fail++;
278  continue;
279  }
280  const Matrix<BaseFloat> &features = feature_reader.Value(utt);
281  if (features.NumRows() == 0) {
282  KALDI_WARN << "Zero-length utterance: " << utt;
283  num_fail++;
284  continue;
285  }
286  LatticeBiglmFasterDecoder decoder(fst_reader.Value(), config,
287  &cache_dfst);
288  DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model, features,
289  acoustic_scale);
290  double like;
291  if (DecodeUtterance(decoder, gmm_decodable, trans_model, word_syms, utt,
292  acoustic_scale, determinize, allow_partial,
293  &alignment_writer, &words_writer,
294  &compact_lattice_writer, &lattice_writer,
295  &like)) {
296  tot_like += like;
297  frame_count += features.NumRows();
298  num_success++;
299  } else num_fail++;
300  }
301  }
302 
303  double elapsed = timer.Elapsed();
304  KALDI_LOG << "Time taken "<< elapsed
305  << "s: real-time factor assuming 100 frames/sec is "
306  << (elapsed*100.0/frame_count);
307  KALDI_LOG << "Done " << num_success << " utterances, failed for "
308  << num_fail;
309  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count) << " over "
310  << frame_count<<" frames.";
311 
312  delete word_syms;
313  if (num_success != 0) return 0;
314  else return 1;
315  } catch(const std::exception &e) {
316  std::cerr << e.what();
317  return -1;
318  }
319 }
This class wraps an Fst, representing a language model, using the interface for "BackoffDeterministic...
int32 words[kMaxOrder]
bool DecodeUtterance(LatticeBiglmFasterDecoder &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 code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool GetBestPath(fst::MutableFst< LatticeArc > *ofst, bool use_final_probs=true) const
bool Open(const std::string &wspecifier)
DecodableInterface provides a link between the (acoustic-modeling and feature-processing) code and th...
Definition: decodable-itf.h:82
Fst< StdArc > * ReadFstKaldiGeneric(std::string rxfilename, bool throw_on_err)
Definition: kaldi-fst-io.cc:45
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
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 Decode(DecodableInterface *decodable)
bool GetLinearSymbolSequence(const Fst< Arc > &fst, std::vector< I > *isymbols_out, std::vector< I > *osymbols_out, typename Arc::Weight *tot_weight_out)
GetLinearSymbolSequence gets the symbol sequence from a linear FST.
void Write(const std::string &key, const T &value) const
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::vector< std::vector< double > > AcousticLatticeScale(double acwt)
std::istream & Stream()
Definition: kaldi-io.cc:826
LatticeBiglmFasterDecoderConfig GetOptions()
int main(int argc, char *argv[])
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 ScaleLattice(const std::vector< std::vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a...
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
fst::VectorFst< LatticeArc > Lattice
Definition: kaldi-lattice.h:44
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
bool ReachedFinal() const
says whether a final-state was active on the last frame.
#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
This is as LatticeFasterDecoder, but does online composition between HCLG and the "difference languag...
bool GetRawLattice(fst::MutableFst< LatticeArc > *ofst, bool use_final_probs=true) const
bool HasKey(const std::string &key)
void ApplyProbabilityScale(float scale, MutableFst< Arc > *fst)
ApplyProbabilityScale is applicable to FSTs in the log or tropical semiring.
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
fst::DeterminizeLatticePhonePrunedOptions det_opts
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
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
VectorFst< StdArc > * CastOrConvertToVectorFst(Fst< StdArc > *fst)
Definition: kaldi-fst-io.cc:94
#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
bool DeterminizeLatticePhonePrunedWrapper(const kaldi::TransitionModel &trans_model, MutableFst< kaldi::LatticeArc > *ifst, double beam, MutableFst< kaldi::CompactLatticeArc > *ofst, DeterminizeLatticePhonePrunedOptions opts)
This function is a wrapper of DeterminizeLatticePhonePruned() that works for Lattice type FSTs...
std::string GetOptArg(int param) const