lattice-to-post.cc
Go to the documentation of this file.
1 // latbin/lattice-to-post.cc
2 
3 // Copyright 2009-2011 Saarland University
4 // Author: Arnab Ghoshal
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "fstext/fstext-lib.h"
24 #include "lat/kaldi-lattice.h"
25 #include "lat/lattice-functions.h"
26 
27 int main(int argc, char *argv[]) {
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 }
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
fst::StdArc StdArc
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
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:42
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...
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...
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
std::vector< std::vector< double > > LatticeScale(double lmwt, double acwt)
fst::VectorFst< LatticeArc > Lattice
Definition: kaldi-lattice.h:44
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_ERR
Definition: kaldi-error.h:147
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
Arc::StateId NumArcs(const ExpandedFst< Arc > &fst)
Returns the total number of arcs in an FST.
#define KALDI_LOG
Definition: kaldi-error.h:153
int main(int argc, char *argv[])
std::string GetOptArg(int param) const