97 using namespace kaldi;
99 typedef kaldi::int64 int64;
100 using fst::SymbolTable;
101 using fst::VectorFst;
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" 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";
120 int32 num_loops = 20;
121 int32 max_mem = 50000000;
122 int32 max_loop = 500000;
125 bool minimize =
false;
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");
149 if (po.NumArgs() != 2) {
154 std::string lats_rspecifier = po.GetArg(1),
155 lats_wspecifier = po.GetArg(2);
164 int32 n_done = 0, n_error = 0;
167 double sum_depth_in = 0.0,
168 sum_depth_out = 0.0, sum_t = 0.0;
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));
174 for (; !lattice_reader.Done(); lattice_reader.Next()) {
175 std::string key = lattice_reader.Key();
176 Lattice lat = lattice_reader.Value();
178 lattice_reader.FreeCurrent();
186 unordered_map<std::pair<int32,int32>, std::pair<BaseFloat, int32>,
194 beam, beam_ratio, max_mem, max_loop,
195 delta, num_loops, &clat)) {
205 sum_depth_in += lat.NumStates();
206 sum_depth_out += depth * t;
211 fst::TopSort(&out_lat);
219 lattice_writer.Write(key, out_lat);
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) <<
").";
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();
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
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 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...
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)
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
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...
fst::VectorFst< LatticeArc > Lattice
fst::VectorFst< CompactLatticeArc > CompactLattice
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.
A hashing function-object for pairs of ints.