All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lattice-lmrescore.cc File Reference
Include dependency graph for lattice-lmrescore.cc:

Go to the source code of this file.

Functions

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

Function Documentation

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

Definition at line 28 of file lattice-lmrescore.cc.

References fst::ConvertLattice(), fst::DeterminizeLattice(), SequentialTableReader< Holder >::Done(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), fst::GraphLatticeScale(), KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), fst::ReadFstKaldi(), ParseOptions::Register(), fst::ScaleLattice(), fst::TableCompose(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

28  {
29  try {
30  using namespace kaldi;
31  typedef kaldi::int32 int32;
32  typedef kaldi::int64 int64;
33  using fst::SymbolTable;
34  using fst::VectorFst;
35  using fst::StdArc;
36  using fst::ReadFstKaldi;
37 
38  const char *usage =
39  "Add lm_scale * [cost of best path through LM FST] to graph-cost of\n"
40  "paths through lattice. Does this by composing with LM FST, then\n"
41  "lattice-determinizing (it has to negate weights first if lm_scale<0)\n"
42  "Usage: lattice-lmrescore [options] <lattice-rspecifier> <lm-fst-in> <lattice-wspecifier>\n"
43  " e.g.: lattice-lmrescore --lm-scale=-1.0 ark:in.lats 'fstproject --project_output=true data/lang/G.fst|' ark:out.lats\n";
44 
45  ParseOptions po(usage);
46  BaseFloat lm_scale = 1.0;
47  int32 num_states_cache = 50000;
48 
49  po.Register("lm-scale", &lm_scale, "Scaling factor for language model costs; frequently 1.0 or -1.0");
50  po.Register("num-states-cache", &num_states_cache,
51  "Number of states we cache when mapping LM FST to lattice type. "
52  "More -> more memory but faster.");
53 
54  po.Read(argc, argv);
55 
56  if (po.NumArgs() != 3) {
57  po.PrintUsage();
58  exit(1);
59  }
60 
61  std::string lats_rspecifier = po.GetArg(1),
62  fst_rxfilename = po.GetArg(2),
63  lats_wspecifier = po.GetArg(3);
64 
65 
66 
67  VectorFst<StdArc> *std_lm_fst = ReadFstKaldi(fst_rxfilename);
68  if (std_lm_fst->Properties(fst::kILabelSorted, true) == 0) {
69  // Make sure LM is sorted on ilabel.
70  fst::ILabelCompare<StdArc> ilabel_comp;
71  fst::ArcSort(std_lm_fst, ilabel_comp);
72  }
73 
74  // mapped_fst is the LM fst interpreted using the LatticeWeight semiring,
75  // with all the cost on the first member of the pair (since it's a graph
76  // weight).
77  fst::CacheOptions cache_opts(true, num_states_cache);
79  fst::MapFst<StdArc, LatticeArc, fst::StdToLatticeMapper<BaseFloat> >
80  lm_fst(*std_lm_fst, mapper, cache_opts);
81  delete std_lm_fst;
82 
83  // The next fifteen or so lines are a kind of optimization and
84  // can be ignored if you just want to understand what is going on.
85  // Change the options for TableCompose to match the input
86  // (because it's the arcs of the LM FST we want to do lookup
87  // on).
89  true, fst::SEQUENCE_FILTER,
90  fst::MATCH_INPUT);
91 
92  // The following is an optimization for the TableCompose
93  // composition: it stores certain tables that enable fast
94  // lookup of arcs during composition.
95  fst::TableComposeCache<fst::Fst<LatticeArc> > lm_compose_cache(compose_opts);
96 
97  // Read as regular lattice-- this is the form we need it in for efficient
98  // composition and determinization.
99  SequentialLatticeReader lattice_reader(lats_rspecifier);
100 
101  // Write as compact lattice.
102  CompactLatticeWriter compact_lattice_writer(lats_wspecifier);
103 
104  int32 n_done = 0, n_fail = 0;
105 
106  for (; !lattice_reader.Done(); lattice_reader.Next()) {
107  std::string key = lattice_reader.Key();
108  Lattice lat = lattice_reader.Value();
109  lattice_reader.FreeCurrent();
110  if (lm_scale != 0.0) {
111  // Only need to modify it if LM scale nonzero.
112  // Before composing with the LM FST, we scale the lattice weights
113  // by the inverse of "lm_scale". We'll later scale by "lm_scale".
114  // We do it this way so we can determinize and it will give the
115  // right effect (taking the "best path" through the LM) regardless
116  // of the sign of lm_scale.
117  fst::ScaleLattice(fst::GraphLatticeScale(1.0 / lm_scale), &lat);
118  ArcSort(&lat, fst::OLabelCompare<LatticeArc>());
119 
120  Lattice composed_lat;
121  // Could just do, more simply: Compose(lat, lm_fst, &composed_lat);
122  // and not have lm_compose_cache at all.
123  // The command below is faster, though; it's constant not
124  // logarithmic in vocab size.
125  TableCompose(lat, lm_fst, &composed_lat, &lm_compose_cache);
126 
127  Invert(&composed_lat); // make it so word labels are on the input.
128  CompactLattice determinized_lat;
129  DeterminizeLattice(composed_lat, &determinized_lat);
130  fst::ScaleLattice(fst::GraphLatticeScale(lm_scale), &determinized_lat);
131  if (determinized_lat.Start() == fst::kNoStateId) {
132  KALDI_WARN << "Empty lattice for utterance " << key << " (incompatible LM?)";
133  n_fail++;
134  } else {
135  compact_lattice_writer.Write(key, determinized_lat);
136  n_done++;
137  }
138  } else {
139  // zero scale so nothing to do.
140  n_done++;
141  CompactLattice compact_lat;
142  ConvertLattice(lat, &compact_lat);
143  compact_lattice_writer.Write(key, compact_lat);
144  }
145  }
146 
147  KALDI_LOG << "Done " << n_done << " lattices, failed for " << n_fail;
148  return (n_done != 0 ? 0 : 1);
149  } catch(const std::exception &e) {
150  std::cerr << e.what();
151  return -1;
152  }
153 }
vector< vector< double > > GraphLatticeScale(double lmwt)
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
void TableCompose(const Fst< Arc > &ifst1, const Fst< Arc > &ifst2, MutableFst< Arc > *ofst, const TableComposeOptions &opts=TableComposeOptions())
fst::StdArc StdArc
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
TableComposeCache lets us do multiple compositions while caching the same matcher.
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
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
TableMatcher is a matcher specialized for the case where the output side of the left FST always has e...
Definition: table-matcher.h:42
#define KALDI_WARN
Definition: kaldi-error.h:130
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
void ScaleLattice(const vector< vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a...
Class StdToLatticeMapper maps a normal arc (StdArc) to a LatticeArc by putting the StdArc weight as t...
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
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...
#define KALDI_LOG
Definition: kaldi-error.h:133