lattice-to-mpe-post.cc
Go to the documentation of this file.
1 // latbin/lattice-to-mpe-post.cc
2 
3 // Copyright 2009-2012 Chao Weng
4 // 2013 Johns Hopkins University (author: Daniel Povey)
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 #include "gmm/am-diag-gmm.h"
27 #include "hmm/transition-model.h"
28 
29 int main(int argc, char *argv[]) {
30  try {
31  typedef kaldi::int32 int32;
32  using fst::SymbolTable;
33  using fst::VectorFst;
34  using fst::StdArc;
35  using namespace kaldi;
36 
37  const char *usage =
38  "Do forward-backward and collect frame level MPE posteriors over\n"
39  "lattices, which can be fed into gmm-acc-stats2 to do MPE traning.\n"
40  "Caution: this is not really MPE, this is MPFE (minimum phone frame\n"
41  "error). The posteriors may be positive or negative.\n"
42  "Usage: lattice-to-mpe-post [options] <model> <num-posteriors-rspecifier>\n"
43  " <lats-rspecifier> <posteriors-wspecifier> \n"
44  "e.g.: lattice-to-mpe-post --acoustic-scale=0.1 1.mdl ark:num.post\n"
45  " ark:1.lats ark:1.post\n";
46 
47  kaldi::BaseFloat acoustic_scale = 1.0, lm_scale = 1.0;
48  bool one_silence_class = false;
49  std::string silence_phones_str;
50  kaldi::ParseOptions po(usage);
51  po.Register("acoustic-scale", &acoustic_scale,
52  "Scaling factor for acoustic likelihoods");
53  po.Register("lm-scale", &lm_scale,
54  "Scaling factor for \"graph costs\" (including LM costs)");
55  po.Register("one-silence-class", &one_silence_class, "If true, newer "
56  "behavior which will tend to reduce insertions.");
57  po.Register("silence-phones", &silence_phones_str,
58  "Colon-separated list of integer id's of silence phones, e.g. 46:47");
59  po.Read(argc, argv);
60 
61  if (po.NumArgs() != 4) {
62  po.PrintUsage();
63  exit(1);
64  }
65 
66  std::vector<int32> silence_phones;
67  if (!kaldi::SplitStringToIntegers(silence_phones_str, ":", false, &silence_phones))
68  KALDI_ERR << "Invalid silence-phones string " << silence_phones_str;
69  kaldi::SortAndUniq(&silence_phones);
70  if (silence_phones.empty())
71  KALDI_WARN << "No silence phones specified, make sure this is what you intended.";
72 
73  if (acoustic_scale == 0.0)
74  KALDI_ERR << "Do not use a zero acoustic scale (cannot be inverted)";
75 
76  std::string model_filename = po.GetArg(1),
77  alignments_rspecifier = po.GetArg(2),
78  lats_rspecifier = po.GetArg(3),
79  posteriors_wspecifier = po.GetArg(4);
80 
81  SequentialLatticeReader lattice_reader(lats_rspecifier);
82  PosteriorWriter posterior_writer(posteriors_wspecifier);
83  if (alignments_rspecifier.find("ali-to-post") != std::string::npos) {
84  KALDI_WARN << "Warning, this program has been changed to read alignments "
85  << "not posteriors. Remove ali-to-post from your scripts.";
86  }
87  RandomAccessInt32VectorReader alignments_reader(alignments_rspecifier);
88 
89  TransitionModel trans_model;
90  {
91  bool binary;
92  Input ki(model_filename, &binary);
93  trans_model.Read(ki.Stream(), binary);
94  }
95 
96  int32 num_done = 0, num_err = 0;
97  double total_lat_frame_acc = 0.0, lat_frame_acc;
98  double total_time = 0, lat_time;
99 
100 
101  for (; !lattice_reader.Done(); lattice_reader.Next()) {
102  std::string key = lattice_reader.Key();
103  kaldi::Lattice lat = lattice_reader.Value();
104  lattice_reader.FreeCurrent();
105  if (acoustic_scale != 1.0 || lm_scale != 1.0)
106  fst::ScaleLattice(fst::LatticeScale(lm_scale, acoustic_scale), &lat);
107 
108  kaldi::uint64 props = lat.Properties(fst::kFstProperties, false);
109  if (!(props & fst::kTopSorted)) {
110  if (fst::TopSort(&lat) == false)
111  KALDI_ERR << "Cycles detected in lattice.";
112  }
113 
114  if (!alignments_reader.HasKey(key)) {
115  KALDI_WARN << "No alignment for utterance " << key;
116  num_err++;
117  } else {
118  const std::vector<int32> &alignment = alignments_reader.Value(key);
119  Posterior post;
120  lat_frame_acc = LatticeForwardBackwardMpeVariants(
121  trans_model, silence_phones, lat, alignment,
122  "mpfe", one_silence_class, &post);
123  total_lat_frame_acc += lat_frame_acc;
124  lat_time = post.size();
125  total_time += lat_time;
126  KALDI_VLOG(2) << "Processed lattice for utterance: " << key << "; found "
127  << lat.NumStates() << " states and " << fst::NumArcs(lat)
128  << " arcs. Average frame accuracies = " << (lat_frame_acc/lat_time)
129  << " over " << lat_time << " frames.";
130  posterior_writer.Write(key, post);
131  num_done++;
132  }
133  }
134 
135  KALDI_LOG << "Overall average frame-accuracy is "
136  << (total_lat_frame_acc/total_time) << " over " << total_time
137  << " frames.";
138  KALDI_LOG << "Done " << num_done << " lattices.";
139  return (num_done != 0 ? 0 : 1);
140  } catch(const std::exception &e) {
141  std::cerr << e.what();
142  return -1;
143  }
144 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
Definition: text-utils.h:68
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 SortAndUniq(std::vector< T > *vec)
Sorts and uniq&#39;s (removes duplicates) from a vector.
Definition: stl-utils.h:39
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
std::istream & Stream()
Definition: kaldi-io.cc:826
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
const T & Value(const std::string &key)
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 Read(std::istream &is, bool binary)
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
BaseFloat LatticeForwardBackwardMpeVariants(const TransitionModel &trans, const std::vector< int32 > &silence_phones, const Lattice &lat, const std::vector< int32 > &num_ali, std::string criterion, bool one_silence_class, Posterior *post)
This function implements either the MPFE (minimum phone frame error) or SMBR (state-level minimum bay...
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
#define KALDI_WARN
Definition: kaldi-error.h:150
bool HasKey(const std::string &key)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
int main(int argc, char *argv[])
#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