gmm-post-to-gpost.cc
Go to the documentation of this file.
1 // gmmbin/gmm-post-to-gpost.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 // 2014 Guoguo Chen
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 
22 #include "base/kaldi-common.h"
23 #include "util/common-utils.h"
24 #include "gmm/am-diag-gmm.h"
25 #include "hmm/transition-model.h"
26 #include "hmm/posterior.h"
27 
28 int main(int argc, char *argv[]) {
29  using namespace kaldi;
30  try {
31  const char *usage =
32  "Convert state-level posteriors to Gaussian-level posteriors\n"
33  "Usage: gmm-post-to-gpost [options] <model-in> <feature-rspecifier> <posteriors-rspecifier> "
34  "<gpost-wspecifier>\n"
35  "e.g.: \n"
36  " gmm-post-to-gpost 1.mdl scp:train.scp ark:1.post ark:1.gpost\n";
37 
38  ParseOptions po(usage);
39  bool binary = true;
40  BaseFloat rand_prune = 0.0;
41  po.Register("binary", &binary, "Write output in binary mode");
42  po.Register("rand-prune", &rand_prune, "Randomized pruning of posteriors less than this");
43  po.Read(argc, argv);
44 
45  if (po.NumArgs() != 4) {
46  po.PrintUsage();
47  exit(1);
48  }
49 
50  std::string model_filename = po.GetArg(1),
51  feature_rspecifier = po.GetArg(2),
52  posteriors_rspecifier = po.GetArg(3),
53  gpost_wspecifier = po.GetArg(4);
54 
55  using namespace kaldi;
56  typedef kaldi::int32 int32;
57 
58  AmDiagGmm am_gmm;
59  TransitionModel trans_model;
60  {
61  bool binary;
62  Input ki(model_filename, &binary);
63  trans_model.Read(ki.Stream(), binary);
64  am_gmm.Read(ki.Stream(), binary);
65  }
66 
67  double tot_like = 0.0;
68  double tot_t = 0.0;
69 
70  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
71  RandomAccessPosteriorReader posteriors_reader(posteriors_rspecifier);
72 
73  GaussPostWriter gpost_writer(gpost_wspecifier);
74 
75  int32 num_done = 0, num_no_posterior = 0, num_other_error = 0;
76  for (; !feature_reader.Done(); feature_reader.Next()) {
77  std::string key = feature_reader.Key();
78  if (!posteriors_reader.HasKey(key)) {
79  num_no_posterior++;
80  } else {
81  const Matrix<BaseFloat> &mat = feature_reader.Value();
82  const Posterior &posterior = posteriors_reader.Value(key);
83  GaussPost gpost(posterior.size());
84 
85  if (posterior.size() != mat.NumRows()) {
86  KALDI_WARN << "Posterior vector has wrong size "<< (posterior.size()) << " vs. "<< (mat.NumRows());
87  num_other_error++;
88  continue;
89  }
90 
91  num_done++;
92  BaseFloat tot_like_this_file = 0.0, tot_weight = 0.0;
93 
94  Posterior pdf_posterior;
95  ConvertPosteriorToPdfs(trans_model, posterior, &pdf_posterior);
96  for (size_t i = 0; i < posterior.size(); i++) {
97  gpost[i].reserve(pdf_posterior[i].size());
98  for (size_t j = 0; j < pdf_posterior[i].size(); j++) {
99  int32 pdf_id = pdf_posterior[i][j].first;
100  BaseFloat weight = pdf_posterior[i][j].second;
101  const DiagGmm &gmm = am_gmm.GetPdf(pdf_id);
102  Vector<BaseFloat> this_post_vec;
103  BaseFloat like =
104  gmm.ComponentPosteriors(mat.Row(i), &this_post_vec);
105  this_post_vec.Scale(weight);
106  if (rand_prune > 0.0)
107  for (int32 k = 0; k < this_post_vec.Dim(); k++)
108  this_post_vec(k) = RandPrune(this_post_vec(k),
109  rand_prune);
110  if (!this_post_vec.IsZero())
111  gpost[i].push_back(std::make_pair(pdf_id, this_post_vec));
112  tot_like_this_file += like * weight;
113  tot_weight += weight;
114  }
115  }
116  KALDI_VLOG(1) << "Average like for this file is "
117  << (tot_like_this_file/tot_weight) << " over "
118  << tot_weight <<" frames.";
119  tot_like += tot_like_this_file;
120  tot_t += tot_weight;
121  gpost_writer.Write(key, gpost);
122  }
123  }
124  KALDI_LOG << "Done " << num_done << " files, " << num_no_posterior
125  << " with no posteriors, " << num_other_error
126  << " with other errors.";
127 
128  KALDI_LOG << "Overall avg like per frame (Gaussian only) = "
129  << (tot_like/tot_t) << " over " << tot_t << " frames.";
130 
131  KALDI_LOG << "Done converting post to gpost";
132  if (num_done != 0) return 0;
133  else return 1;
134  } catch(const std::exception &e) {
135  std::cerr << e.what();
136  return -1;
137  }
138 }
139 
140 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool IsZero(Real cutoff=1.0e-06) const
Returns true if matrix is all zeros.
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
Float RandPrune(Float post, BaseFloat prune_thresh, struct RandomState *state=NULL)
Definition: kaldi-math.h:174
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::istream & Stream()
Definition: kaldi-io.cc:826
BaseFloat ComponentPosteriors(const VectorBase< BaseFloat > &data, Vector< BaseFloat > *posteriors) const
Computes the posterior probabilities of all Gaussian components given a data point.
Definition: diag-gmm.cc:601
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
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
const T & Value(const std::string &key)
void Read(std::istream &is, bool binary)
int main(int argc, char *argv[])
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
void Scale(Real alpha)
Multiplies all elements by this constant.
bool HasKey(const std::string &key)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
DiagGmm & GetPdf(int32 pdf_index)
Accessors.
Definition: am-diag-gmm.h:119
A class representing a vector.
Definition: kaldi-vector.h:406
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
Definition for Gaussian Mixture Model with diagonal covariances.
Definition: diag-gmm.h:42
std::vector< std::vector< std::pair< int32, Vector< BaseFloat > > > > GaussPost
GaussPost is a typedef for storing Gaussian-level posteriors for an utterance.
Definition: posterior.h:51
void ConvertPosteriorToPdfs(const TransitionModel &tmodel, const Posterior &post_in, Posterior *post_out)
Converts a posterior over transition-ids to be a posterior over pdf-ids.
Definition: posterior.cc:322
#define KALDI_LOG
Definition: kaldi-error.h:153
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147