post-to-weights.cc
Go to the documentation of this file.
1 // bin/post-to-weights.cc
2 
3 // Copyright 2009-2011 Chao Weng Microsoft Corporation
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 "hmm/posterior.h"
24 
25 
26 int main(int argc, char *argv[]) {
27  try {
28  using namespace kaldi;
29  typedef kaldi::int32 int32;
30 
31  const char *usage =
32  "Turn posteriors into per-frame weights (typically most useful after\n"
33  "weight-silence-post, to get silence weights)\n"
34  "See also: weight-silence-post, post-to-pdf-post, post-to-phone-post\n"
35  "post-to-feats, get-post-on-ali\n"
36  "Usage: post-to-weights <post-rspecifier> <weights-wspecifier>\n";
37 
38  ParseOptions po(usage);
39  po.Read(argc, argv);
40 
41  if (po.NumArgs() != 2) {
42  po.PrintUsage();
43  exit(1);
44  }
45 
46  std::string post_rspecifier = po.GetArg(1),
47  weights_wspecifier = po.GetArg(2);
48 
49  SequentialPosteriorReader posterior_reader(post_rspecifier);
50  BaseFloatVectorWriter weights_writer(weights_wspecifier);
51 
52  int32 num_done = 0;
53 
54  for (; !posterior_reader.Done(); posterior_reader.Next()) {
55  std::string key = posterior_reader.Key();
56  const Posterior &posterior = posterior_reader.Value();
57  int32 num_frames = static_cast<int32>(posterior.size());
58  Vector<BaseFloat> weights(num_frames);
59  for (int32 i = 0; i < num_frames; i++) {
60  BaseFloat sum = 0.0;
61  for (size_t j = 0; j < posterior[i].size(); j++)
62  sum += posterior[i][j].second;
63  weights(i) = sum;
64  }
65  weights_writer.Write(key, weights);
66  num_done++;
67  }
68  KALDI_LOG << "Done converting " << num_done << " posteriors to weights.";
69  return (num_done != 0 ? 0 : 1);
70  } catch(const std::exception &e) {
71  std::cerr << e.what();
72  return -1;
73  }
74 }
75 
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
int main(int argc, char *argv[])
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
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
A class representing a vector.
Definition: kaldi-vector.h:406
#define KALDI_LOG
Definition: kaldi-error.h:153