weight-post.cc
Go to the documentation of this file.
1 // bin/weight-post.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  int32 length_tolerance = 2;
32 
33  const char *usage =
34  "Takes archives (typically per-utterance) of posteriors and per-frame weights,\n"
35  "and weights the posteriors by the per-frame weights\n"
36  "\n"
37  "Usage: weight-post <post-rspecifier> <weights-rspecifier> <post-wspecifier>\n";
38 
39  ParseOptions po(usage);
40 
41  po.Register("length-tolerance", &length_tolerance,
42  "Tolerate this many frames of length mismatch between "
43  "posteriors and weights");
44 
45  po.Read(argc, argv);
46 
47  if (po.NumArgs() != 3) {
48  po.PrintUsage();
49  exit(1);
50  }
51 
52  std::string post_rspecifier = po.GetArg(1),
53  weights_rspecifier = po.GetArg(2),
54  post_wspecifier = po.GetArg(3);
55 
56  SequentialPosteriorReader posterior_reader(post_rspecifier);
57  RandomAccessBaseFloatVectorReader weights_reader(weights_rspecifier);
58  PosteriorWriter post_writer(post_wspecifier);
59 
60  int32 num_done = 0, num_err = 0;
61 
62  for (; !posterior_reader.Done(); posterior_reader.Next()) {
63  std::string key = posterior_reader.Key();
64  Posterior post = posterior_reader.Value();
65  if (!weights_reader.HasKey(key)) {
66  KALDI_WARN << "No weights for utterance " << key;
67  num_err++;
68  continue;
69  }
70  const Vector<BaseFloat> &weights = weights_reader.Value(key);
71  if (std::abs(weights.Dim() - static_cast<int32>(post.size())) >
72  length_tolerance) {
73  KALDI_WARN << "Weights for utterance " << key
74  << " have wrong size, " << weights.Dim()
75  << " vs. " << post.size();
76  num_err++;
77  continue;
78  }
79  for (size_t i = 0; i < post.size(); i++) {
80  if (weights(i) == 0.0) post[i].clear();
81  for (size_t j = 0; j < post[i].size(); j++)
82  post[i][j].second *= i < weights.Dim() ? weights(i) : 0.0;
83  }
84  post_writer.Write(key, post);
85  num_done++;
86  }
87  KALDI_LOG << "Scaled " << num_done << " posteriors; errors on " << num_err;
88  return (num_done != 0 ? 0 : 1);
89  } catch(const std::exception &e) {
90  std::cerr << e.what();
91  return -1;
92  }
93 }
94 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int main(int argc, char *argv[])
Definition: weight-post.cc:26
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
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.
#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.
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
bool HasKey(const std::string &key)
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