All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
gmm-acc-hlda.cc File Reference
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "gmm/am-diag-gmm.h"
#include "hmm/transition-model.h"
#include "transform/hlda.h"
#include "hmm/posterior.h"
Include dependency graph for gmm-acc-hlda.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 31 of file gmm-acc-hlda.cc.

References MatrixBase< Real >::AddMatMat(), DiagGmm::ComponentPosteriors(), kaldi::ConvertPosteriorToPdfs(), AmDiagGmm::Dim(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), AmDiagGmm::GetPdf(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, rnnlm::j, KALDI_ASSERT, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), kaldi::kNoTrans, kaldi::kTrans, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), Matrix< Real >::Read(), ParseOptions::Register(), MatrixBase< Real >::Row(), VectorBase< Real >::Scale(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and kaldi::WriteKaldiObject().

31  {
32  using namespace kaldi;
33  try {
34  const char *usage =
35  "Accumulate HLDA statistics\n"
36  "Usage: gmm-acc-hlda [options] <model-in> <orig-transform-in> <orig-feature-rspecifier> <posteriors-rspecifier> <stats-out>\n"
37  "Note: orig-transform-in must be the current truncated HLDA transform (e.g. from LDA)."
38  "e.g.: \n"
39  " gmm-acc-hlda 1.mdl 1.hlda \"ark:splice-feats scp:train.scp |\" ark:1.post 1.hacc\n";
40 
41  ParseOptions po(usage);
42  bool binary = true;
43  BaseFloat speedup = 1.0;
44  po.Register("binary", &binary, "Write output in binary mode");
45  po.Register("speedup", &speedup, "Proportion of data to accumulate full HLDA stats with");
46  po.Read(argc, argv);
47 
48  if (po.NumArgs() != 5) {
49  po.PrintUsage();
50  exit(1);
51  }
52 
53  std::string model_filename = po.GetArg(1),
54  cur_transform_filename = po.GetArg(2),
55  feature_rspecifier = po.GetArg(3),
56  posteriors_rspecifier = po.GetArg(4),
57  accs_wxfilename = po.GetArg(5);
58 
59  using namespace kaldi;
60  typedef kaldi::int32 int32;
61 
62  AmDiagGmm am_gmm;
63  TransitionModel trans_model;
64  {
65  bool binary;
66  Input ki(model_filename, &binary);
67  trans_model.Read(ki.Stream(), binary);
68  am_gmm.Read(ki.Stream(), binary);
69  }
70 
71  Matrix<BaseFloat> cur_transform;
72  {
73  bool binary;
74  Input ki(cur_transform_filename, &binary);
75  cur_transform.Read(ki.Stream(), binary);
76  KALDI_ASSERT(cur_transform.NumRows() == am_gmm.Dim() &&
77  "Transform num-rows must match model dim (need truncated transform)");
78  }
79  HldaAccsDiagGmm hlda_accs(am_gmm, cur_transform.NumCols(), speedup);
80 
81  double tot_like = 0.0;
82  double tot_t = 0.0;
83 
84  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
85  RandomAccessPosteriorReader posteriors_reader(posteriors_rspecifier);
86 
87  int32 num_done = 0, num_no_posterior = 0, num_other_error = 0;
88  for (; !feature_reader.Done(); feature_reader.Next()) {
89  std::string key = feature_reader.Key();
90  if (!posteriors_reader.HasKey(key)) {
91  num_no_posterior++;
92  } else {
93  const Matrix<BaseFloat> &mat = feature_reader.Value();
94  const Posterior &posterior = posteriors_reader.Value(key);
95 
96  // Compute transformed features (need them in order to compute
97  // Gaussian-level posteriors)
98  Matrix<BaseFloat> transformed_mat(mat.NumRows(), am_gmm.Dim());
99  transformed_mat.AddMatMat(1.0, mat, kNoTrans, cur_transform, kTrans, 0.0);
100 
101  if (static_cast<int32>(posterior.size()) != mat.NumRows()) {
102  KALDI_WARN << "Posterior vector has wrong size "<< (posterior.size()) << " vs. "<< (mat.NumRows());
103  num_other_error++;
104  continue;
105  }
106 
107  num_done++;
108  BaseFloat tot_like_this_file = 0.0, tot_weight = 0.0;
109 
110  Posterior pdf_posterior;
111  ConvertPosteriorToPdfs(trans_model, posterior, &pdf_posterior);
112  for (size_t i = 0; i < posterior.size(); i++) {
113  for (size_t j = 0; j < pdf_posterior[i].size(); j++) {
114  int32 pdf_id = pdf_posterior[i][j].first;
115  BaseFloat weight = pdf_posterior[i][j].second;
116 
117  Vector<BaseFloat> posteriors;
118  const DiagGmm &gmm = am_gmm.GetPdf(pdf_id);
119  tot_like_this_file +=
120  weight * gmm.ComponentPosteriors(transformed_mat.Row(i),
121  &posteriors);
122  tot_weight += weight;
123  posteriors.Scale(weight);
124  hlda_accs.AccumulateFromPosteriors(pdf_id,
125  gmm,
126  mat.Row(i),
127  posteriors);
128  }
129  }
130  KALDI_LOG << "Average like for this file is "
131  << (tot_like_this_file/tot_weight) << " over "
132  << tot_weight <<" frames.";
133  tot_like += tot_like_this_file;
134  tot_t += tot_weight;
135  if (num_done % 10 == 0)
136  KALDI_LOG << "Avg like per frame so far is "
137  << (tot_like/tot_t);
138  }
139  }
140 
141  KALDI_LOG << "Done " << num_done << " files, " << num_no_posterior
142  << " with no posteriors, " << num_other_error
143  << " with other errors.";
144 
145  KALDI_LOG << "Overall avg like per frame (Gaussian only) = "
146  << (tot_like/tot_t) << " over " << tot_t << " frames.";
147 
148  WriteKaldiObject(hlda_accs, accs_wxfilename, binary);
149  KALDI_LOG << "Written accs.";
150  if (num_done != 0) return 0;
151  else return 1;
152  } catch(const std::exception &e) {
153  std::cerr << e.what();
154  return -1;
155  }
156 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
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
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
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:43
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
int32 Dim() const
Definition: am-diag-gmm.h:79
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
This class stores the compact form of the HLDA statistics, given a diagonal GMM.
Definition: hlda.h:38
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
#define KALDI_WARN
Definition: kaldi-error.h:130
void Scale(Real alpha)
Multiplies all elements by this constant.
DiagGmm & GetPdf(int32 pdf_index)
Accessors.
Definition: am-diag-gmm.h:119
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Definition for Gaussian Mixture Model with diagonal covariances.
Definition: diag-gmm.h:42
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
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:133
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147