lattice-determinize-non-compact.cc
Go to the documentation of this file.
1 // latbin/lattice-determinize-non-compact.cc
2 
3 // Copyright 2009-2012 Microsoft Corporation
4 // 2012-2013 Johns Hopkins University (Author: Daniel Povey)
5 // 2015 Vimal Manohar
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 "util/stl-utils.h"
26 #include "fstext/fstext-lib.h"
27 #include "lat/kaldi-lattice.h"
28 #include "lat/lattice-functions.h"
29 #include "lat/push-lattice.h"
30 #include "lat/minimize-lattice.h"
31 
32 namespace kaldi {
33 
35 typedef Lattice::Arc Arc;
36 
37 // This function is a copy of the function in the program lattice-determinize
39  const std::string &key,
40  bool prune,
41  BaseFloat beam,
42  BaseFloat beam_ratio,
43  int32 max_mem,
44  int32 max_loop,
45  BaseFloat delta,
46  int32 num_loops,
47  CompactLattice *clat) {
49  lat_opts.max_mem = max_mem;
50  lat_opts.max_loop = max_loop;
51  lat_opts.delta = delta;
52  BaseFloat cur_beam = beam;
53  for (int32 i = 0; i < num_loops;) { // we increment i below.
54 
55  if (lat.Start() == fst::kNoStateId) {
56  KALDI_WARN << "Detected empty lattice, skipping " << key;
57  return false;
58  }
59 
60  // The work gets done in the next line.
61  if (DeterminizeLattice(lat, clat, lat_opts, NULL)) {
62  if (prune) PruneLattice(cur_beam, clat);
63  return true;
64  } else { // failed to determinize..
65  KALDI_WARN << "Failed to determinize lattice (presumably max-states "
66  << "reached), reducing lattice-beam to "
67  << (cur_beam*beam_ratio) << " and re-trying.";
68  for (; i < num_loops; i++) {
69  cur_beam *= beam_ratio;
70  Lattice pruned_lat(lat);
71  PruneLattice(cur_beam, &pruned_lat);
72  if (NumArcs(lat) == NumArcs(pruned_lat)) {
73  cur_beam *= beam_ratio;
74  KALDI_WARN << "Pruning did not have an effect on the original "
75  << "lattice size; reducing beam to "
76  << cur_beam << " and re-trying.";
77  } else if (DeterminizeLattice(pruned_lat, clat, lat_opts, NULL)) {
78  if (prune) PruneLattice(cur_beam, clat);
79  return true;
80  } else {
81  KALDI_WARN << "Determinization failed again; reducing beam again to "
82  << (cur_beam*beam_ratio) << " and re-trying.";
83  }
84  }
85  }
86  }
87  KALDI_WARN << "Decreased pruning beam --num-loops=" << num_loops
88  << " times and was not able to determinize: failed for "
89  << key;
90  return false;
91 }
92 
93 }
94 
95 int main(int argc, char *argv[]) {
96  try {
97  using namespace kaldi;
98  typedef kaldi::int32 int32;
99  typedef kaldi::int64 int64;
100  using fst::SymbolTable;
101  using fst::VectorFst;
102  using fst::StdArc;
103 
104  const char *usage =
105  "lattice-determinize lattices (and apply a pruning beam)\n"
106  " (see http://kaldi-asr.org/doc/lattices.html for more explanation)\n"
107  "This version of the program retains the original "
108  "acoustic scores of arcs in the determinized lattice and writes it "
109  "as a normal (non-compact) lattice. \n"
110  " note: this program is tyically only useful if you generated state-level\n"
111  " lattices, e.g. called gmm-latgen-simple with --determinize=false\n"
112  "\n"
113  "Usage: lattice-determinize-non-compact [options] lattice-rspecifier lattice-wspecifier\n"
114  " e.g.: lattice-determinize-non-compact --acoustic-scale=0.1 --beam=15.0 ark:1.lats ark:det.lats\n";
115 
116  ParseOptions po(usage);
117  BaseFloat acoustic_scale = 1.0;
118  BaseFloat beam = 10.0;
119  BaseFloat beam_ratio = 0.9;
120  int32 num_loops = 20;
121  int32 max_mem = 50000000; // 50 MB
122  int32 max_loop = 500000;
123  BaseFloat delta = fst::kDelta;
124  bool prune = false;
125  bool minimize = false;
126 
127  po.Register("acoustic-scale", &acoustic_scale,
128  "Scaling factor for acoustic likelihoods");
129  po.Register("beam", &beam,
130  "Pruning beam [applied after acoustic scaling]-- also used "
131  "to handle determinization failures, set --prune=false to "
132  "disable routine pruning");
133  po.Register("delta", &delta, "Tolerance used in determinization");
134  po.Register("prune", &prune, "If true, prune determinized lattices "
135  "with the --beam option.");
136  po.Register("max-mem", &max_mem, "Maximum approximate memory usage in "
137  "determinization (real usage might be many times this)");
138  po.Register("max-loop", &max_loop, "Option to detect a certain "
139  "type of failure in lattice determinization (not critical)");
140  po.Register("beam-ratio", &beam_ratio, "Ratio by which to "
141  "decrease beam if we reach the max-arcs.");
142  po.Register("num-loops", &num_loops, "Number of times to "
143  "decrease beam by beam-ratio if determinization fails.");
144  po.Register("minimize", &minimize,
145  "If true, push and minimize after determinization");
146 
147  po.Read(argc, argv);
148 
149  if (po.NumArgs() != 2) {
150  po.PrintUsage();
151  exit(1);
152  }
153 
154  std::string lats_rspecifier = po.GetArg(1),
155  lats_wspecifier = po.GetArg(2);
156 
157  // Read as regular lattice-- this is the form we need it in for efficient
158  // pruning.
159  SequentialLatticeReader lattice_reader(lats_rspecifier);
160 
161  // Write as regular lattice.
162  LatticeWriter lattice_writer(lats_wspecifier);
163 
164  int32 n_done = 0, n_error = 0;
165 
166  // depth stats (for diagnostics).
167  double sum_depth_in = 0.0,
168  sum_depth_out = 0.0, sum_t = 0.0;
169 
170  if (acoustic_scale == 0.0)
171  KALDI_ERR << "Do not use a zero acoustic scale (cannot be inverted)";
172  LatticeWeight beam_weight(beam, static_cast<BaseFloat>(0.0));
173 
174  for (; !lattice_reader.Done(); lattice_reader.Next()) {
175  std::string key = lattice_reader.Key();
176  Lattice lat = lattice_reader.Value();
177 
178  lattice_reader.FreeCurrent();
179 
180  fst::TopSort(&lat);
181 
182  fst::ScaleLattice(fst::AcousticLatticeScale(acoustic_scale), &lat);
183 
184 
185  // Compute a map from each (t, tid) to (sum_of_acoustic_scores, count)
186  unordered_map<std::pair<int32,int32>, std::pair<BaseFloat, int32>,
187  PairHasher<int32> > acoustic_scores;
188  ComputeAcousticScoresMap(lat, &acoustic_scores);
189 
190  Invert(&lat); // make it so word labels are on the input.
191 
192  CompactLattice clat;
193  if (DeterminizeLatticeWrapper(lat, key, prune,
194  beam, beam_ratio, max_mem, max_loop,
195  delta, num_loops, &clat)) {
196  if (minimize) {
199  MinimizeCompactLattice(&clat);
200  }
201 
202  int32 t;
204  double depth = CompactLatticeDepth(clat, &t);
205  sum_depth_in += lat.NumStates();
206  sum_depth_out += depth * t;
207  sum_t += t;
208 
209  Lattice out_lat;
210  fst::ConvertLattice(clat, &out_lat);
211  fst::TopSort(&out_lat);
212 
213  // Replace each arc (t, tid) with the averaged acoustic score from
214  // the computed map
215  ReplaceAcousticScoresFromMap(acoustic_scores, &out_lat);
216 
217  fst::ScaleLattice(fst::AcousticLatticeScale(1.0/acoustic_scale),
218  &out_lat);
219  lattice_writer.Write(key, out_lat);
220  n_done++;
221  } else {
222  n_error++; // will have already printed warning.
223  }
224  }
225 
226  if (sum_t != 0.0) {
227  KALDI_LOG << "Average input-lattice depth (measured at at state level) is "
228  << (sum_depth_in / sum_t) << ", output depth is "
229  << (sum_depth_out / sum_t) << ", over " << sum_t << "frames "
230  << " (average num-frames = " << (sum_t / n_done) << ").";
231  }
232  KALDI_LOG << "Done " << n_done << " lattices, errors on " << n_error;
233  return (n_done != 0 ? 0 : 1);
234  } catch(const std::exception &e) {
235  std::cerr << e.what();
236  return -1;
237  }
238 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Lattice::StateId StateId
bool DeterminizeLatticeWrapper(const Lattice &lat, const std::string &key, bool prune, BaseFloat beam, BaseFloat beam_ratio, int32 max_mem, int32 max_loop, BaseFloat delta, int32 num_loops, CompactLattice *clat)
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
fst::StdArc StdArc
void ReplaceAcousticScoresFromMap(const unordered_map< std::pair< int32, int32 >, std::pair< BaseFloat, int32 >, PairHasher< int32 > > &acoustic_scores, Lattice *lat)
This function restores acoustic scores computed using the function ComputeAcousticScoresMap into the ...
bool PushCompactLatticeStrings(MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *clat)
This function pushes the transition-ids as far towards the start as they will go. ...
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
int main(int argc, char *argv[])
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
bool PushCompactLatticeWeights(MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *clat)
This function pushes the weights in the CompactLattice so that all states except possibly the start s...
std::vector< std::vector< double > > AcousticLatticeScale(double acwt)
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
bool MinimizeCompactLattice(MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, IntType > > > *clat, float delta)
This function minimizes the compact lattice.
BaseFloat CompactLatticeDepth(const CompactLattice &clat, int32 *num_frames)
Returns the depth of the lattice, defined as the average number of arcs crossing any given frame...
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 ConvertLattice(const ExpandedFst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *ofst, bool invert)
Convert lattice from a normal FST to a CompactLattice FST.
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.
#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
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
int NumArgs() const
Number of positional parameters (c.f. argc-1).
void ComputeAcousticScoresMap(const Lattice &lat, unordered_map< std::pair< int32, int32 >, std::pair< BaseFloat, int32 >, PairHasher< int32 > > *acoustic_scores)
This function computes the mapping from the pair (frame-index, transition-id) to the pair (sum-of-aco...
bool PruneLattice(BaseFloat beam, LatType *lat)
bool DeterminizeLattice(const Fst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< Weight > > *ofst, DeterminizeLatticeOptions opts, bool *debug_ptr)
This function implements the normal version of DeterminizeLattice, in which the output strings are re...
void TopSortCompactLatticeIfNeeded(CompactLattice *clat)
Topologically sort the compact lattice if not already topologically sorted.
Arc::StateId NumArcs(const ExpandedFst< Arc > &fst)
Returns the total number of arcs in an FST.
#define KALDI_LOG
Definition: kaldi-error.h:153
A hashing function-object for pairs of ints.
Definition: stl-utils.h:235