lattice-determinize.cc File Reference
Include dependency graph for lattice-determinize.cc:

Go to the source code of this file.

Namespaces

 kaldi
 This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for mispronunciations detection tasks, the reference:
 

Functions

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)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 89 of file lattice-determinize.cc.

References fst::AcousticLatticeScale(), kaldi::CompactLatticeDepth(), kaldi::DeterminizeLatticeWrapper(), SequentialTableReader< Holder >::Done(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), KALDI_ERR, KALDI_LOG, SequentialTableReader< Holder >::Key(), fst::MinimizeCompactLattice(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), fst::PushCompactLatticeStrings(), fst::PushCompactLatticeWeights(), ParseOptions::Read(), ParseOptions::Register(), fst::ScaleLattice(), kaldi::TopSortCompactLatticeIfNeeded(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

89  {
90  try {
91  using namespace kaldi;
92  typedef kaldi::int32 int32;
93  typedef kaldi::int64 int64;
94  using fst::SymbolTable;
95  using fst::VectorFst;
96  using fst::StdArc;
97 
98  const char *usage =
99  "This program is deprecated, please used lattice-determinize-pruned.\n"
100  "lattice-determinize lattices (and apply a pruning beam)\n"
101  " (see http://kaldi-asr.org/doc/lattices.html for more explanation)\n"
102  " note: this program is tyically only useful if you generated state-level\n"
103  " lattices, e.g. called gmm-latgen-simple with --determinize=false\n"
104  "\n"
105  "Usage: lattice-determinize [options] lattice-rspecifier lattice-wspecifier\n"
106  " e.g.: lattice-determinize --acoustic-scale=0.1 --beam=15.0 ark:1.lats ark:det.lats\n";
107 
108  ParseOptions po(usage);
109  BaseFloat acoustic_scale = 1.0;
110  BaseFloat beam = 10.0;
111  BaseFloat beam_ratio = 0.9;
112  int32 num_loops = 20;
113  int32 max_mem = 50000000; // 50 MB
114  int32 max_loop = 500000;
115  BaseFloat delta = fst::kDelta;
116  bool prune = false;
117  bool minimize = false;
118 
119  po.Register("acoustic-scale", &acoustic_scale,
120  "Scaling factor for acoustic likelihoods");
121  po.Register("beam", &beam,
122  "Pruning beam [applied after acoustic scaling]-- also used "
123  "to handle determinization failures, set --prune=false to "
124  "disable routine pruning");
125  po.Register("delta", &delta, "Tolerance used in determinization");
126  po.Register("prune", &prune, "If true, prune determinized lattices "
127  "with the --beam option.");
128  po.Register("max-mem", &max_mem, "Maximum approximate memory usage in "
129  "determinization (real usage might be many times this)");
130  po.Register("max-loop", &max_loop, "Option to detect a certain "
131  "type of failure in lattice determinization (not critical)");
132  po.Register("beam-ratio", &beam_ratio, "Ratio by which to "
133  "decrease beam if we reach the max-arcs.");
134  po.Register("num-loops", &num_loops, "Number of times to "
135  "decrease beam by beam-ratio if determinization fails.");
136  po.Register("minimize", &minimize,
137  "If true, push and minimize after determinization");
138 
139  po.Read(argc, argv);
140 
141  if (po.NumArgs() != 2) {
142  po.PrintUsage();
143  exit(1);
144  }
145 
146  std::string lats_rspecifier = po.GetArg(1),
147  lats_wspecifier = po.GetArg(2);
148 
149 
150  // Read as regular lattice-- this is the form we need it in for efficient
151  // pruning.
152  SequentialLatticeReader lattice_reader(lats_rspecifier);
153 
154  // Write as compact lattice.
155  CompactLatticeWriter compact_lattice_writer(lats_wspecifier);
156 
157  int32 n_done = 0, n_error = 0;
158 
159  // depth stats (for diagnostics).
160  double sum_depth_in = 0.0,
161  sum_depth_out = 0.0, sum_t = 0.0;
162 
163  if (acoustic_scale == 0.0)
164  KALDI_ERR << "Do not use a zero acoustic scale (cannot be inverted)";
165  LatticeWeight beam_weight(beam, static_cast<BaseFloat>(0.0));
166 
167  for (; !lattice_reader.Done(); lattice_reader.Next()) {
168  std::string key = lattice_reader.Key();
169  Lattice lat = lattice_reader.Value();
170  Invert(&lat); // make it so word labels are on the input.
171 
172  lattice_reader.FreeCurrent();
173  fst::ScaleLattice(fst::AcousticLatticeScale(acoustic_scale), &lat);
174 
175  CompactLattice clat;
176  if (DeterminizeLatticeWrapper(lat, key, prune,
177  beam, beam_ratio, max_mem, max_loop,
178  delta, num_loops, &clat)) {
179  if (minimize) {
182  MinimizeCompactLattice(&clat);
183  }
184 
185  int32 t;
187  double depth = CompactLatticeDepth(clat, &t);
188  sum_depth_in += lat.NumStates();
189  sum_depth_out += depth * t;
190  sum_t += t;
191 
192  fst::ScaleLattice(fst::AcousticLatticeScale(1.0/acoustic_scale), &clat);
193  compact_lattice_writer.Write(key, clat);
194  n_done++;
195  } else {
196  n_error++; // will have already printed warning.
197  }
198  }
199 
200  if (sum_t != 0.0) {
201  KALDI_LOG << "Average input-lattice depth (measured at at state level) is "
202  << (sum_depth_in / sum_t) << ", output depth is "
203  << (sum_depth_out / sum_t) << ", over " << sum_t << " frames "
204  << " (average num-frames = " << (sum_t / n_done) << ").";
205  }
206  KALDI_LOG << "Done " << n_done << " lattices, errors on " << n_error;
207  return (n_done != 0 ? 0 : 1);
208  } catch(const std::exception &e) {
209  std::cerr << e.what();
210  return -1;
211  }
212 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
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)
fst::StdArc StdArc
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
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...
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
#define KALDI_ERR
Definition: kaldi-error.h:147
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
void TopSortCompactLatticeIfNeeded(CompactLattice *clat)
Topologically sort the compact lattice if not already topologically sorted.
#define KALDI_LOG
Definition: kaldi-error.h:153