lattice-to-smbr-post.cc
Go to the documentation of this file.
1 // latbin/lattice-to-smbr-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 posteriors for\n"
39  "the state-level minimum Bayes Risk criterion (SMBR), which\n"
40  "is like MPE with the criterion at a context-dependent state level.\n"
41  "The output may be fed into gmm-acc-stats2 or similar to train the\n"
42  "models discriminatively. The posteriors may be positive or negative.\n"
43  "Usage: lattice-to-smbr-post [options] <model> <num-posteriors-rspecifier>\n"
44  " <lats-rspecifier> <posteriors-wspecifier> \n"
45  "e.g.: lattice-to-smbr-post --acoustic-scale=0.1 1.mdl ark:num.post\n"
46  " ark:1.lats ark:1.post\n";
47 
48  kaldi::BaseFloat acoustic_scale = 1.0, lm_scale = 1.0;
49  bool one_silence_class = false;
50  std::string silence_phones_str;
51  kaldi::ParseOptions po(usage);
52  po.Register("acoustic-scale", &acoustic_scale,
53  "Scaling factor for acoustic likelihoods");
54  po.Register("lm-scale", &lm_scale,
55  "Scaling factor for \"graph costs\" (including LM costs)");
56  po.Register("one-silence-class", &one_silence_class, "If true, newer "
57  "behavior which will tend to reduce insertions.");
58  po.Register("silence-phones", &silence_phones_str, "Colon-separated "
59  "list of integer id's of silence phones, e.g. 46:47");
60  po.Read(argc, argv);
61 
62  if (po.NumArgs() != 4) {
63  po.PrintUsage();
64  exit(1);
65  }
66 
67  std::vector<int32> silence_phones;
68  if (!kaldi::SplitStringToIntegers(silence_phones_str, ":", false, &silence_phones))
69  KALDI_ERR << "Invalid silence-phones string " << silence_phones_str;
70  kaldi::SortAndUniq(&silence_phones);
71  if (silence_phones.empty())
72  KALDI_WARN << "No silence phones specified, make sure this is what you intended.";
73 
74  if (acoustic_scale == 0.0)
75  KALDI_ERR << "Do not use a zero acoustic scale (cannot be inverted)";
76 
77  std::string model_filename = po.GetArg(1),
78  alignments_rspecifier = po.GetArg(2),
79  lats_rspecifier = po.GetArg(3),
80  posteriors_wspecifier = po.GetArg(4);
81 
82  SequentialLatticeReader lattice_reader(lats_rspecifier);
83  PosteriorWriter posterior_writer(posteriors_wspecifier);
84  if (alignments_rspecifier.find("ali-to-post") != std::string::npos) {
85  KALDI_WARN << "Warning, this program has been changed to read alignments "
86  << "not posteriors. Remove ali-to-post from your scripts.";
87  }
88  RandomAccessInt32VectorReader alignments_reader(alignments_rspecifier);
89 
90  TransitionModel trans_model;
91  {
92  bool binary;
93  Input ki(model_filename, &binary);
94  trans_model.Read(ki.Stream(), binary);
95  }
96 
97  int32 num_done = 0, num_err = 0;
98  double total_lat_frame_acc = 0.0, lat_frame_acc;
99  double total_time = 0, lat_time;
100 
101 
102  for (; !lattice_reader.Done(); lattice_reader.Next()) {
103  std::string key = lattice_reader.Key();
104  kaldi::Lattice lat = lattice_reader.Value();
105  lattice_reader.FreeCurrent();
106  if (acoustic_scale != 1.0 || lm_scale != 1.0)
107  fst::ScaleLattice(fst::LatticeScale(lm_scale, acoustic_scale), &lat);
108 
109  kaldi::uint64 props = lat.Properties(fst::kFstProperties, false);
110  if (!(props & fst::kTopSorted)) {
111  if (fst::TopSort(&lat) == false)
112  KALDI_ERR << "Cycles detected in lattice.";
113  }
114 
115  if (!alignments_reader.HasKey(key)) {
116  KALDI_WARN << "No alignment for utterance " << key;
117  num_err++;
118  } else {
119  const std::vector<int32> &alignment = alignments_reader.Value(key);
120  Posterior post;
121  lat_frame_acc = LatticeForwardBackwardMpeVariants(
122  trans_model, silence_phones, lat, alignment,
123  "smbr", one_silence_class, &post);
124  total_lat_frame_acc += lat_frame_acc;
125  lat_time = post.size();
126  total_time += lat_time;
127  KALDI_VLOG(2) << "Processed lattice for utterance: " << key << "; found "
128  << lat.NumStates() << " states and " << fst::NumArcs(lat)
129  << " arcs. Average frame accuracies = " << (lat_frame_acc/lat_time)
130  << " over " << lat_time << " frames.";
131  posterior_writer.Write(key, post);
132  num_done++;
133  }
134  }
135 
136  KALDI_LOG << "Overall average frame-accuracy is "
137  << (total_lat_frame_acc/total_time) << " over " << total_time
138  << " frames.";
139  KALDI_LOG << "Done " << num_done << " lattices.";
140  return (num_done != 0 ? 0 : 1);
141  } catch(const std::exception &e) {
142  std::cerr << e.what();
143  return -1;
144  }
145 }
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
int main(int argc, char *argv[])
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).
#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