sum-post.cc
Go to the documentation of this file.
1 // bin/sum-post.cc
2 
3 // Copyright 2011-2013 Johns Hopkins University (Author: Daniel Povey) Chao Weng
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "util/stl-utils.h"
24 #include "hmm/posterior.h"
25 
26 int main(int argc, char *argv[]) {
27  try {
28  using namespace kaldi;
29  typedef kaldi::int32 int32;
30  typedef kaldi::int64 int64;
31 
32  const char *usage =
33  "Sum two sets of posteriors for each utterance, e.g. useful in fMMI.\n"
34  "To take the difference of posteriors, use e.g. --scale2=-1.0\n"
35  "\n"
36  "Usage: sum-post <post-rspecifier1> <post-rspecifier2> <post-wspecifier>\n";
37 
38  BaseFloat scale1 = 1.0, scale2 = 1.0;
39  bool merge = true;
40  bool drop_frames = false;
41  ParseOptions po(usage);
42  po.Register("scale1", &scale1, "Scale for first set of posteriors");
43  po.Register("scale2", &scale2, "Scale for second set of posteriors");
44  po.Register("merge", &merge, "If true, merge posterior entries for "
45  "same transition-id (canceling positive and negative parts)");
46  po.Register("zero-if-disjoint", &drop_frames, "If true, zero "
47  "posteriors on all frames when the two sets of posteriors are "
48  "disjoint (this option for back-compatibility only; use "
49  "'--drop-frames'");
50  po.Register("drop-frames", &drop_frames, "If true, zero "
51  "posteriors on all frames when the two sets of posteriors are "
52  "disjoint");
53  po.Read(argc, argv);
54 
55  if (po.NumArgs() != 3) {
56  po.PrintUsage();
57  exit(1);
58  }
59 
60  std::string post_rspecifier1 = po.GetArg(1),
61  post_rspecifier2 = po.GetArg(2),
62  post_wspecifier = po.GetArg(3);
63 
64  kaldi::SequentialPosteriorReader posterior_reader1(post_rspecifier1);
65  kaldi::RandomAccessPosteriorReader posterior_reader2(post_rspecifier2);
66  kaldi::PosteriorWriter posterior_writer(post_wspecifier);
67 
68  int32 num_done = 0, num_err = 0;
69  int64 num_frames_tot = 0, num_frames_disjoint = 0;
70 
71  for (; !posterior_reader1.Done(); posterior_reader1.Next()) {
72  std::string key = posterior_reader1.Key();
73  kaldi::Posterior posterior1 = posterior_reader1.Value();
74  if (!posterior_reader2.HasKey(key)) {
75  KALDI_WARN << "Second set of posteriors has nothing for key "
76  << key << ", producing no output.";
77  num_err++;
78  continue;
79  }
80  kaldi::Posterior posterior2 = posterior_reader2.Value(key);
81  if (posterior2.size() != posterior1.size()) {
82  KALDI_WARN << "Posteriors have mismatched sizes " << posterior1.size()
83  << " vs. " << posterior2.size() << " for key " << key;
84  num_err++;
85  continue;
86  }
87 
88  ScalePosterior(scale1, &posterior1);
89  ScalePosterior(scale2, &posterior2);
90  kaldi::Posterior posterior_out;
91  num_frames_disjoint += MergePosteriors(posterior1, posterior2, merge,
92  drop_frames, &posterior_out);
93  num_frames_tot += static_cast<int64>(posterior1.size());
94  posterior_writer.Write(key, posterior_out);
95  num_done++;
96  }
97  KALDI_LOG << "Processed " << num_frames_tot << " frames; for "
98  << num_frames_disjoint << " frames there was no overlap, i.e. "
99  << (num_frames_disjoint * 100.0 / num_frames_tot)
100  << "% (e.g. numerator path not in denominator lattice)";
101  KALDI_LOG << "Done adding " << num_done << " posteriors; " << num_err
102  << " with errors.";
103  return (num_done != 0 ? 0 : 1);
104  } catch(const std::exception &e) {
105  std::cerr << e.what();
106  return -1;
107  }
108 }
109 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
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)
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
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: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)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
int main(int argc, char *argv[])
Definition: sum-post.cc:26
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
bool HasKey(const std::string &key)
void ScalePosterior(BaseFloat scale, Posterior *post)
Scales the BaseFloat (weight) element in the posterior entries.
Definition: posterior.cc:218
int NumArgs() const
Number of positional parameters (c.f. argc-1).
int32 MergePosteriors(const Posterior &post1, const Posterior &post2, bool merge, bool drop_frames, Posterior *post)
Merge two sets of posteriors, which must have the same length.
Definition: posterior.cc:258
#define KALDI_LOG
Definition: kaldi-error.h:153