All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
thresh-post.cc File Reference
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "gmm/am-diag-gmm.h"
#include "hmm/transition-model.h"
#include "hmm/hmm-utils.h"
#include "hmm/posterior.h"
Include dependency graph for thresh-post.cc:

Go to the source code of this file.

Functions

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

Function Documentation

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

Definition at line 30 of file thresh-post.cc.

References SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), rnnlm::i, rnnlm::j, KALDI_ASSERT, KALDI_LOG, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::Register(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

30  {
31  using namespace kaldi;
32  typedef kaldi::int32 int32;
33  try {
34  const char *usage =
35  "Down-weight posteriors that are lower than a supplied confidence threshold.\n"
36  "(for those below the weight, rather than set to zero we downweight according\n"
37  "to the --weight option)\n"
38  "\n"
39  "Usage: thresh-post [options] <posteriors-rspecifier> <posteriors-wspecifier>\n"
40  "e.g.: thresh-post --threshold=0.9 --scale=0.1 ark:- ark:-\n";
41 
42  ParseOptions po(usage);
43 
44  BaseFloat threshold = 0.9;
45  BaseFloat scale = 0.1;
46 
47  po.Register("threshold", &threshold, "Threshold below which we down-weight posteriors.");
48  po.Register("scale", &scale, "Scale which we apply to posteriors below the threshold.");
49 
50  po.Read(argc, argv);
51 
52  if (po.NumArgs() != 2) {
53  po.PrintUsage();
54  exit(1);
55  }
56 
57  std::string posteriors_rspecifier = po.GetArg(1),
58  posteriors_wspecifier = po.GetArg(2);
59 
60  KALDI_ASSERT(threshold < 1.0 && threshold >= 0.0 && scale >= 0.0 && scale <= 1.0);
61 
62  int32 num_posteriors = 0;
63  double total_weight_in = 0.0, total_weight_out = 0.0;
64  SequentialPosteriorReader posterior_reader(posteriors_rspecifier);
65  PosteriorWriter posterior_writer(posteriors_wspecifier);
66 
67  for (; !posterior_reader.Done(); posterior_reader.Next()) {
68  num_posteriors++;
69  // Posterior is vector<vector<pair<int32, BaseFloat> > >
70  const Posterior &posterior = posterior_reader.Value();
71  // Posterior is vector<vector<pair<int32, BaseFloat> > >
72  Posterior new_post(posterior.size());
73  for (size_t i = 0; i < posterior.size(); i++) {
74  for (size_t j = 0; j < posterior[i].size(); j++) {
75  int32 tid = posterior[i][j].first;
76  double weight = posterior[i][j].second;
77  total_weight_in += weight;
78  if (weight < threshold) weight *= scale;
79  total_weight_out += weight;
80  if (weight != 0.0)
81  new_post[i].push_back(std::make_pair(tid, static_cast<BaseFloat>(weight)));
82  }
83  }
84  posterior_writer.Write(posterior_reader.Key(), new_post);
85  }
86  KALDI_LOG << "thresh-post: thresholded " << num_posteriors
87  << " posteriors, reduced them by a factor of "
88  << (total_weight_out/total_weight_in) << " on average.";
89  } catch(const std::exception &e) {
90  std::cerr << e.what();
91  return -1;
92  }
93 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
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:43
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_ASSERT(cond)
Definition: kaldi-error.h:169
#define KALDI_LOG
Definition: kaldi-error.h:133