scale-post.cc File Reference
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "hmm/posterior.h"
Include dependency graph for scale-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 27 of file scale-post.cc.

References kaldi::ClassifyRspecifier(), kaldi::ConvertStringToReal(), SequentialTableReader< Holder >::Done(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), KALDI_ERR, KALDI_LOG, SequentialTableReader< Holder >::Key(), kaldi::kNoRspecifier, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ScalePosterior(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

27  {
28  try {
29  using namespace kaldi;
30  typedef kaldi::int32 int32;
31 
32  const char *usage =
33  "Scale posteriors with either a global scale, or a different scale for "
34  " each utterance.\n"
35  "Usage: scale-post <post-rspecifier> (<scale-rspecifier>|<scale>) <post-wspecifier>\n";
36 
37  ParseOptions po(usage);
38  po.Read(argc, argv);
39 
40  if (po.NumArgs() != 3) {
41  po.PrintUsage();
42  exit(1);
43  }
44 
45  std::string post_rspecifier = po.GetArg(1),
46  scale_or_scale_rspecifier = po.GetArg(2),
47  post_wspecifier = po.GetArg(3);
48 
49  double global_scale = 0.0;
50  if (ClassifyRspecifier(scale_or_scale_rspecifier, NULL, NULL) == kNoRspecifier) {
51  // treat second arg as a floating-point scale.
52  if (!ConvertStringToReal(scale_or_scale_rspecifier, &global_scale))
53  KALDI_ERR << "Bad second argument " << scale_or_scale_rspecifier
54  << " (expected scale or scale rspecifier)";
55  scale_or_scale_rspecifier = ""; // So the archive won't be opened.
56  }
57 
58 
59  SequentialPosteriorReader posterior_reader(post_rspecifier);
60  RandomAccessBaseFloatReader scale_reader(scale_or_scale_rspecifier);
61  PosteriorWriter posterior_writer(post_wspecifier);
62 
63  int32 num_scaled = 0, num_no_scale = 0;
64 
65  for (; !posterior_reader.Done(); posterior_reader.Next()) {
66  std::string key = posterior_reader.Key();
67  Posterior posterior = posterior_reader.Value();
68  posterior_reader.FreeCurrent();
69  if (scale_or_scale_rspecifier != "" && !scale_reader.HasKey(key)) {
70  num_no_scale++;
71  } else {
72  BaseFloat post_scale = (scale_or_scale_rspecifier == "" ? global_scale
73  : scale_reader.Value(key));
74  ScalePosterior(post_scale, &posterior);
75  num_scaled++;
76  posterior_writer.Write(key, posterior);
77  }
78  }
79  KALDI_LOG << "Done " << num_scaled << " posteriors; " << num_no_scale
80  << " had no scales.";
81  return (num_scaled != 0 ? 0 : 1);
82  } catch(const std::exception &e) {
83  std::cerr << e.what();
84  return -1;
85  }
86 }
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
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
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_ERR
Definition: kaldi-error.h:147
bool ConvertStringToReal(const std::string &str, T *out)
ConvertStringToReal converts a string into either float or double and returns false if there was any ...
Definition: text-utils.cc:238
void ScalePosterior(BaseFloat scale, Posterior *post)
Scales the BaseFloat (weight) element in the posterior entries.
Definition: posterior.cc:218
#define KALDI_LOG
Definition: kaldi-error.h:153