gmm-post-to-gpost.cc File Reference
Include dependency graph for gmm-post-to-gpost.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 28 of file gmm-post-to-gpost.cc.

References DiagGmm::ComponentPosteriors(), kaldi::ConvertPosteriorToPdfs(), VectorBase< Real >::Dim(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), AmDiagGmm::GetPdf(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, VectorBase< Real >::IsZero(), rnnlm::j, KALDI_LOG, KALDI_VLOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), kaldi::RandPrune(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), ParseOptions::Register(), MatrixBase< Real >::Row(), VectorBase< Real >::Scale(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

28  {
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 }
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.
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
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
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
void Read(std::istream &is, bool binary)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_WARN
Definition: kaldi-error.h:150
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
void Scale(Real alpha)
Multiplies all elements by this constant.
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