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

Go to the source code of this file.

Classes

class  ArcPosteriorComputer
 

Namespaces

 kaldi
 Relabels neural network egs with the read pdf-id alignments.
 

Functions

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

Function Documentation

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

Definition at line 112 of file lattice-arc-post.cc.

References SequentialTableReader< Holder >::Done(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), fst::LatticeScale(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ArcPosteriorComputer::OutputPosteriors(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadKaldiObject(), ParseOptions::Register(), fst::ScaleLattice(), Output::Stream(), kaldi::TopSortCompactLatticeIfNeeded(), and SequentialTableReader< Holder >::Value().

112  {
113  try {
114  typedef kaldi::int32 int32;
115  using fst::SymbolTable;
116  using fst::VectorFst;
117  using fst::StdArc;
118 
119  const char *usage =
120  "Print out information regarding posteriors of lattice arcs\n"
121  "This program computes posteriors from a lattice and prints out\n"
122  "information for each arc (the format is reminiscent of ctm, but\n"
123  "contains information from multiple paths). Each line is:\n"
124  " <utterance-id> <start-frame> <num-frames> <posterior> <word> [<ali>] [<phone1> <phone2>...]\n"
125  "for instance:\n"
126  "2013a04-bk42\t104\t26\t0.95\t0\t11,242,242,242,71,894,894,62,63,63,63,63\t2 8 9\n"
127  "where the --print-alignment option determines whether the alignments (i.e. the\n"
128  "sequences of transition-ids) are printed, and the phones are printed only if the\n"
129  "<model> is supplied on the command line. Note, there are tabs between the major\n"
130  "fields, but the phones are separated by spaces.\n"
131  "Usage: lattice-arc-post [<model>] <lattices-rspecifier> <output-wxfilename>\n"
132  "e.g.: lattice-arc-post --acoustic-scale=0.1 final.mdl 'ark:gunzip -c lat.1.gz|' post.txt\n"
133  "You will probably want to word-align the lattices (e.g. lattice-align-words or\n"
134  "lattice-align-words-lexicon) before this program, apply an acoustic scale either\n"
135  "via the --acoustic-scale option or using lattice-scale.\n"
136  "See also: lattice-post, lattice-to-ctm-conf, nbest-to-ctm\n";
137 
138  kaldi::BaseFloat acoustic_scale = 1.0, lm_scale = 1.0;
139  kaldi::BaseFloat min_post = 0.0001;
140  bool print_alignment = false;
141 
142  kaldi::ParseOptions po(usage);
143  po.Register("acoustic-scale", &acoustic_scale,
144  "Scaling factor for acoustic likelihoods");
145  po.Register("lm-scale", &lm_scale,
146  "Scaling factor for \"graph costs\" (including LM costs)");
147  po.Register("print-alignment", &print_alignment,
148  "If true, print alignments (i.e. sequences of transition-ids) for each\n"
149  "arc.");
150  po.Register("min-post", &min_post,
151  "Arc posteriors below this value will be pruned away");
152  po.Read(argc, argv);
153 
154  if (po.NumArgs() < 2 || po.NumArgs() > 3) {
155  po.PrintUsage();
156  exit(1);
157  }
158 
159  if (acoustic_scale == 0.0)
160  KALDI_ERR << "Do not use a zero acoustic scale (cannot be inverted)";
161 
162  kaldi::TransitionModel trans_model;
163 
164  std::string lats_rspecifier, output_wxfilename;
165  if (po.NumArgs() == 3) {
166  ReadKaldiObject(po.GetArg(1), &trans_model);
167  lats_rspecifier = po.GetArg(2);
168  output_wxfilename = po.GetArg(3);
169  } else {
170  lats_rspecifier = po.GetArg(1);
171  output_wxfilename = po.GetArg(2);
172  }
173 
174 
175  kaldi::Output output(output_wxfilename, false);
176 
177  // Read as regular lattice
178  kaldi::SequentialCompactLatticeReader clat_reader(lats_rspecifier);
179 
180  int64 tot_post = 0;
181  int32 num_lat_done = 0, num_lat_err = 0;
182 
183  for (; !clat_reader.Done(); clat_reader.Next()) {
184  std::string key = clat_reader.Key();
185  kaldi::CompactLattice clat = clat_reader.Value();
186  // FreeCurrent() is an optimization that prevents the lattice from being
187  // copied unnecessarily (OpenFst does copy-on-write).
188  clat_reader.FreeCurrent();
189  fst::ScaleLattice(fst::LatticeScale(lm_scale, acoustic_scale), &clat);
191 
193  clat, min_post, print_alignment,
194  (po.NumArgs() == 3 ? &trans_model : NULL));
195 
196  int32 num_post = computer.OutputPosteriors(key, output.Stream());
197  if (num_post != 0) {
198  num_lat_done++;
199  tot_post += num_post;
200  } else {
201  num_lat_err++;
202  KALDI_WARN << "No posterior printed for " << key;
203  }
204  }
205  KALDI_LOG << "Printed posteriors for " << num_lat_done << " lattices ("
206  << num_lat_err << " with errors); on average printed "
207  << (tot_post / (num_lat_done == 0 ? 1 : num_lat_done))
208  << " posteriors per lattice.";
209  return (num_lat_done > 0 ? 0 : 1);
210  } catch(const std::exception &e) {
211  std::cerr << e.what();
212  return -1;
213  }
214 }
fst::StdArc StdArc
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:818
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
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_ERR
Definition: kaldi-error.h:127
#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...
vector< vector< double > > LatticeScale(double lmwt, double acwt)
void TopSortCompactLatticeIfNeeded(CompactLattice *clat)
Topologically sort the compact lattice if not already topologically sorted.
#define KALDI_LOG
Definition: kaldi-error.h:133