gmm-acc-stats.cc File Reference
Include dependency graph for gmm-acc-stats.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 30 of file gmm-acc-stats.cc.

References TransitionModel::Accumulate(), AccumAmDiagGmm::AccumulateForGmm(), kaldi::ConvertPosteriorToPdfs(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, AccumAmDiagGmm::Init(), TransitionModel::InitStats(), rnnlm::j, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), ParseOptions::Register(), MatrixBase< Real >::Row(), Output::Stream(), Input::Stream(), kaldi::StringToGmmFlags(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), AccumAmDiagGmm::Write(), and VectorBase< Real >::Write().

30  {
31  using namespace kaldi;
32  typedef kaldi::int32 int32;
33  try {
34  const char *usage =
35  "Accumulate stats for GMM training (reading in posteriors).\n"
36  "Usage: gmm-acc-stats [options] <model-in> <feature-rspecifier>"
37  "<posteriors-rspecifier> <stats-out>\n"
38  "e.g.: \n"
39  " gmm-acc-stats 1.mdl scp:train.scp ark:1.post 1.acc\n";
40 
41  ParseOptions po(usage);
42  bool binary = true;
43  std::string update_flags_str = "mvwt"; // note: t is ignored, we acc
44  // transition stats regardless.
45  po.Register("binary", &binary, "Write output in binary mode");
46  po.Register("update-flags", &update_flags_str, "Which GMM parameters will be "
47  "updated: subset of mvwt.");
48  po.Read(argc, argv);
49 
50  if (po.NumArgs() != 4) {
51  po.PrintUsage();
52  exit(1);
53  }
54 
55  std::string model_filename = po.GetArg(1),
56  feature_rspecifier = po.GetArg(2),
57  posteriors_rspecifier = po.GetArg(3),
58  accs_wxfilename = po.GetArg(4);
59 
60 
61  AmDiagGmm am_gmm;
62  TransitionModel trans_model;
63  {
64  bool binary;
65  Input ki(model_filename, &binary);
66  trans_model.Read(ki.Stream(), binary);
67  am_gmm.Read(ki.Stream(), binary);
68  }
69 
70  Vector<double> transition_accs;
71  trans_model.InitStats(&transition_accs);
72  AccumAmDiagGmm gmm_accs;
73  gmm_accs.Init(am_gmm, StringToGmmFlags(update_flags_str));
74 
75  double tot_like = 0.0;
76  double tot_t = 0.0;
77 
78  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
79  RandomAccessPosteriorReader posteriors_reader(posteriors_rspecifier);
80 
81  int32 num_done = 0, num_err = 0;
82  for (; !feature_reader.Done(); feature_reader.Next()) {
83  std::string key = feature_reader.Key();
84  if (!posteriors_reader.HasKey(key)) {
85  KALDI_WARN << "Could not find posteriors for utterance " << key;
86  num_err++;
87  } else {
88  const Matrix<BaseFloat> &mat = feature_reader.Value();
89  const Posterior &posterior = posteriors_reader.Value(key);
90 
91  if (static_cast<int32>(posterior.size()) != mat.NumRows()) {
92  KALDI_WARN << "Posterior vector has wrong size "
93  << (posterior.size()) << " vs. "
94  << (mat.NumRows());
95  num_err++;
96  continue;
97  }
98 
99  num_done++;
100  BaseFloat tot_like_this_file = 0.0, tot_weight = 0.0;
101 
102  Posterior pdf_posterior;
103  ConvertPosteriorToPdfs(trans_model, posterior, &pdf_posterior);
104  for (size_t i = 0; i < posterior.size(); i++) {
105  // Accumulates for GMM.
106  for (size_t j = 0; j < pdf_posterior[i].size(); j++) {
107  int32 pdf_id = pdf_posterior[i][j].first;
108  BaseFloat weight = pdf_posterior[i][j].second;
109  tot_like_this_file += gmm_accs.AccumulateForGmm(am_gmm, mat.Row(i), pdf_id, weight)
110  * weight;
111  tot_weight += weight;
112  }
113 
114  // Accumulates for transitions.
115  for (size_t j = 0; j < posterior[i].size(); j++) {
116  int32 tid = posterior[i][j].first;
117  BaseFloat weight = posterior[i][j].second;
118  trans_model.Accumulate(weight, tid, &transition_accs);
119  }
120  }
121  if (num_done % 50 == 0) {
122  KALDI_LOG << "Processed " << num_done << " utterances; for utterance "
123  << key << " avg. like is " << (tot_like_this_file/tot_weight)
124  << " over " << tot_weight <<" frames.";
125  }
126  tot_like += tot_like_this_file;
127  tot_t += tot_weight;
128  }
129  }
130 
131  KALDI_LOG << "Done " << num_done << " files, " << num_err
132  << " with errors.";
133 
134  KALDI_LOG << "Overall avg like per frame (Gaussian only) = "
135  << (tot_like/tot_t) << " over " << tot_t << " frames.";
136 
137  {
138  Output ko(accs_wxfilename, binary);
139  transition_accs.Write(ko.Stream(), binary);
140  gmm_accs.Write(ko.Stream(), binary);
141  }
142  KALDI_LOG << "Written accs.";
143  return (num_done != 0 ? 0 : 1);
144  } catch(const std::exception &e) {
145  std::cerr << e.what();
146  return -1;
147  }
148 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
GmmFlagsType StringToGmmFlags(std::string str)
Convert string which is some subset of "mSwa" to flags.
Definition: model-common.cc:26
void Write(std::ostream &Out, bool binary) const
Writes to C++ stream (option to write in binary).
BaseFloat AccumulateForGmm(const AmDiagGmm &model, const VectorBase< BaseFloat > &data, int32 gmm_index, BaseFloat weight)
Accumulate stats for a single GMM in the model; returns log likelihood.
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
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
void InitStats(Vector< double > *stats) const
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)
void Accumulate(BaseFloat prob, int32 trans_id, Vector< double > *stats) const
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 NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void Write(std::ostream &out_stream, bool binary) const
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
void Init(const AmDiagGmm &model, GmmFlagsType flags)
Initializes accumulators for each GMM based on the number of components and dimension.