sum-post.cc File Reference
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "util/stl-utils.h"
#include "hmm/posterior.h"
Include dependency graph for sum-post.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 26 of file sum-post.cc.

References SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), kaldi::MergePosteriors(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::Register(), kaldi::ScalePosterior(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

26  {
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 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
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
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_WARN
Definition: kaldi-error.h:150
void ScalePosterior(BaseFloat scale, Posterior *post)
Scales the BaseFloat (weight) element in the posterior entries.
Definition: posterior.cc:218
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