39 const std::string &key,
51 lat_opts.
delta = delta;
53 for (
int32 i = 0;
i < num_loops;) {
55 if (lat.Start() == fst::kNoStateId) {
56 KALDI_WARN <<
"Detected empty lattice, skipping " << key;
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;
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.";
81 KALDI_WARN <<
"Determinization failed again; reducing beam again to " 82 << (cur_beam*beam_ratio) <<
" and re-trying.";
87 KALDI_WARN <<
"Decreased pruning beam --num-loops=" << num_loops
88 <<
" times and was not able to determinize: failed for " 95 int main(
int argc,
char *argv[]) {
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");
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.");
145 "If true, push and minimize after determinization");
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();
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 PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
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...
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)
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
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
fst::VectorFst< CompactLatticeArc > CompactLattice
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.
A hashing function-object for pairs of ints.