All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lattice-to-post.cc File Reference
Include dependency graph for lattice-to-post.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 27 of file lattice-to-post.cc.

References SequentialTableReader< Holder >::Done(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), ParseOptions::GetOptArg(), TableWriter< Holder >::IsOpen(), KALDI_ERR, KALDI_LOG, KALDI_VLOG, SequentialTableReader< Holder >::Key(), kaldi::LatticeForwardBackward(), fst::LatticeScale(), SequentialTableReader< Holder >::Next(), fst::NumArcs(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::Register(), fst::ScaleLattice(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

27  {
28  try {
29  typedef kaldi::int32 int32;
30  using fst::SymbolTable;
31  using fst::VectorFst;
32  using fst::StdArc;
33 
34  const char *usage =
35  "Do forward-backward and collect posteriors over lattices.\n"
36  "Usage: lattice-to-post [options] lats-rspecifier posts-wspecifier [loglikes-wspecifier]\n"
37  " e.g.: lattice-to-post --acoustic-scale=0.1 ark:1.lats ark:1.post\n"
38  "See also: lattice-to-ctm-conf, post-to-pdf-post, lattice-arc-post\n";
39 
40  kaldi::BaseFloat acoustic_scale = 1.0, lm_scale = 1.0;
41  kaldi::ParseOptions po(usage);
42  po.Register("acoustic-scale", &acoustic_scale,
43  "Scaling factor for acoustic likelihoods");
44  po.Register("lm-scale", &lm_scale,
45  "Scaling factor for \"graph costs\" (including LM costs)");
46  po.Read(argc, argv);
47 
48  if (po.NumArgs() < 2 || po.NumArgs() > 3) {
49  po.PrintUsage();
50  exit(1);
51  }
52 
53  if (acoustic_scale == 0.0)
54  KALDI_ERR << "Do not use a zero acoustic scale (cannot be inverted)";
55 
56  std::string lats_rspecifier = po.GetArg(1),
57  posteriors_wspecifier = po.GetArg(2),
58  loglikes_wspecifier = po.GetOptArg(3);
59 
60  // Read as regular lattice
61  kaldi::SequentialLatticeReader lattice_reader(lats_rspecifier);
62 
63  kaldi::PosteriorWriter posterior_writer(posteriors_wspecifier);
64  kaldi::BaseFloatWriter loglikes_writer(loglikes_wspecifier);
65 
66  int32 n_done = 0;
67  double total_like = 0.0, lat_like;
68  double total_ac_like = 0.0, lat_ac_like; // acoustic likelihood weighted by posterior.
69  double total_time = 0, lat_time;
70 
71  for (; !lattice_reader.Done(); lattice_reader.Next()) {
72  std::string key = lattice_reader.Key();
73  kaldi::Lattice lat = lattice_reader.Value();
74  // FreeCurrent() is an optimization that prevents the lattice from being
75  // copied unnecessarily (OpenFst does copy-on-write).
76  lattice_reader.FreeCurrent();
77  if (acoustic_scale != 1.0 || lm_scale != 1.0)
78  fst::ScaleLattice(fst::LatticeScale(lm_scale, acoustic_scale), &lat);
79 
80  kaldi::uint64 props = lat.Properties(fst::kFstProperties, false);
81  if (!(props & fst::kTopSorted)) {
82  if (fst::TopSort(&lat) == false)
83  KALDI_ERR << "Cycles detected in lattice.";
84  }
85 
86  kaldi::Posterior post;
87  lat_like = kaldi::LatticeForwardBackward(lat, &post, &lat_ac_like);
88  total_like += lat_like;
89  lat_time = post.size();
90  total_time += lat_time;
91  total_ac_like += lat_ac_like;
92 
93  KALDI_VLOG(2) << "Processed lattice for utterance: " << key << "; found "
94  << lat.NumStates() << " states and " << fst::NumArcs(lat)
95  << " arcs. Average log-likelihood = " << (lat_like/lat_time)
96  << " over " << lat_time << " frames. Average acoustic log-like"
97  << " per frame is " << (lat_ac_like/lat_time);
98 
99  if (loglikes_writer.IsOpen())
100  loglikes_writer.Write(key, lat_like);
101 
102  posterior_writer.Write(key, post);
103  n_done++;
104  }
105 
106  KALDI_LOG << "Overall average log-like/frame is "
107  << (total_like/total_time) << " over " << total_time
108  << " frames. Average acoustic like/frame is "
109  << (total_ac_like/total_time);
110  KALDI_LOG << "Done " << n_done << " lattices.";
111  return (n_done != 0 ? 0 : 1);
112  } catch(const std::exception &e) {
113  std::cerr << e.what();
114  return -1;
115  }
116 }
fst::StdArc StdArc
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
float BaseFloat
Definition: kaldi-types.h:29
std::vector< std::vector< std::pair< int32, BaseFloat > > > Posterior
Posterior is a typedef for storing acoustic-state (actually, transition-id) posteriors over an uttera...
Definition: posterior.h:43
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
BaseFloat LatticeForwardBackward(const Lattice &lat, Posterior *post, double *acoustic_like_sum)
This function does the forward-backward over lattices and computes the posterior probabilities of the...
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:127
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...
#define KALDI_VLOG(v)
Definition: kaldi-error.h:136
vector< vector< double > > LatticeScale(double lmwt, double acwt)
Arc::StateId NumArcs(const ExpandedFst< Arc > &fst)
Returns the total number of arcs in an FST.
#define KALDI_LOG
Definition: kaldi-error.h:133