lattice-determinize-non-compact.cc File Reference
Include dependency graph for lattice-determinize-non-compact.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:
 

Typedefs

typedef Lattice::StateId StateId
 
typedef Lattice::Arc Arc
 

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 95 of file lattice-determinize-non-compact.cc.

References fst::AcousticLatticeScale(), kaldi::CompactLatticeDepth(), kaldi::ComputeAcousticScoresMap(), fst::ConvertLattice(), 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(), kaldi::ReplaceAcousticScoresFromMap(), fst::ScaleLattice(), kaldi::TopSortCompactLatticeIfNeeded(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

95  {
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
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
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
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
#define KALDI_ERR
Definition: kaldi-error.h:147
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
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...
void TopSortCompactLatticeIfNeeded(CompactLattice *clat)
Topologically sort the compact lattice if not already topologically sorted.
#define KALDI_LOG
Definition: kaldi-error.h:153
A hashing function-object for pairs of ints.
Definition: stl-utils.h:235