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

References TransitionModel::Accumulate(), 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(), TransitionModel::TransitionIdToPdf(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), AccumAmDiagGmm::Write(), and VectorBase< Real >::Write().

29  {
30  using namespace kaldi;
31  typedef kaldi::int32 int32;
32  typedef kaldi::int64 int64;
33  try {
34  const char *usage =
35  "Accumulate stats for GMM training (from posteriors)\n"
36  "This version writes two accumulators (e.g. num and den),\n"
37  "and puts the positive accumulators in num, negative in den\n"
38  "Usage: gmm-acc-stats2 [options] <model> <feature-rspecifier>"
39  "<posteriors-rspecifier> <num-stats-out> <den-stats-out>\n"
40  "e.g.:\n"
41  "gmm-acc-stats 1.mdl \"$feats\" ark:1.post 1.num_acc 1.den_acc\n";
42 
43  ParseOptions po(usage);
44  bool binary = true;
45  std::string update_flags_str = "mvwt"; // note: t is ignored, we acc
46  // transition stats regardless.
47  po.Register("binary", &binary, "Write stats in binary mode");
48  po.Register("update-flags", &update_flags_str, "Which GMM parameters to "
49  "update: subset of mvwt.");
50  po.Read(argc, argv);
51 
52  if (po.NumArgs() != 5) {
53  po.PrintUsage();
54  exit(1);
55  }
56 
57  std::string model_rxfilename = po.GetArg(1),
58  feature_rspecifier = po.GetArg(2),
59  posteriors_rspecifier = po.GetArg(3),
60  num_accs_wxfilename = po.GetArg(4),
61  den_accs_wxfilename = po.GetArg(5);
62 
63 
64  AmDiagGmm am_gmm;
65  TransitionModel trans_model;
66  {
67  bool binary;
68  Input ki(model_rxfilename, &binary);
69  trans_model.Read(ki.Stream(), binary);
70  am_gmm.Read(ki.Stream(), binary);
71  }
72 
73  Vector<double> num_trans_accs, den_trans_accs;
74  trans_model.InitStats(&num_trans_accs);
75  trans_model.InitStats(&den_trans_accs);
76  AccumAmDiagGmm num_gmm_accs, den_gmm_accs;
77  num_gmm_accs.Init(am_gmm, StringToGmmFlags(update_flags_str));
78  den_gmm_accs.Init(am_gmm, StringToGmmFlags(update_flags_str));
79 
80  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
81  RandomAccessPosteriorReader posteriors_reader(posteriors_rspecifier);
82 
83 
84  BaseFloat tot_like = 0.0, tot_weight = 0.0;
85  // tot_like is total weighted likelihood (note: weighted
86  // by both +ve and -ve numbers)
87  // tot_t is total weight in posteriors (will often be about zero).
88  int64 tot_frames = 0.0;
89 
90  int32 num_done = 0, num_err = 0;
91  for (; !feature_reader.Done(); feature_reader.Next()) {
92  std::string key = feature_reader.Key();
93  if (!posteriors_reader.HasKey(key)) {
94  num_err++;
95  } else {
96  const Matrix<BaseFloat> &mat = feature_reader.Value();
97  const Posterior &posterior = posteriors_reader.Value(key);
98 
99  if (static_cast<int32>(posterior.size()) != mat.NumRows()) {
100  KALDI_WARN << "Posterior vector has wrong size "
101  << (posterior.size()) << " vs. "
102  << (mat.NumRows());
103  num_err++;
104  continue;
105  }
106 
107  BaseFloat tot_like_this_file = 0.0, tot_weight_this_file = 0.0;
108 
109  num_done++;
110  for (size_t i = 0; i < posterior.size(); i++) {
111  for (size_t j = 0; j < posterior[i].size(); j++) {
112  int32 tid = posterior[i][j].first,
113  pdf_id = trans_model.TransitionIdToPdf(tid);
114  BaseFloat weight = posterior[i][j].second;
115  trans_model.Accumulate(fabs(weight), tid,
116  (weight > 0.0 ?
117  &num_trans_accs : &den_trans_accs));
118  tot_like_this_file +=
119  (weight > 0.0 ? &num_gmm_accs : &den_gmm_accs) ->
120  AccumulateForGmm(am_gmm, mat.Row(i), pdf_id, fabs(weight)) * weight;
121  tot_weight_this_file += weight;
122  }
123  }
124  tot_like += tot_like_this_file;
125  tot_weight += tot_weight_this_file;
126  tot_frames += static_cast<int32>(posterior.size());
127  }
128  }
129 
130  KALDI_LOG << "Done " << num_done << " files, " << num_err
131  << " had errors.";
132 
133  KALDI_LOG << "Overall weighted acoustic likelihood per frame was "
134  << (tot_like/tot_frames) << " over " << tot_frames << " frames;"
135  << " average weight per frame was " << (tot_weight / tot_frames);
136 
137  {
138  Output ko(num_accs_wxfilename, binary);
139  num_trans_accs.Write(ko.Stream(), binary);
140  num_gmm_accs.Write(ko.Stream(), binary);
141  }
142  {
143  Output ko(den_accs_wxfilename, binary);
144  den_trans_accs.Write(ko.Stream(), binary);
145  den_gmm_accs.Write(ko.Stream(), binary);
146  }
147  KALDI_LOG << "Written accs.";
148  return (num_done != 0 ? 0 : 1);
149  } catch(const std::exception &e) {
150  std::cerr << e.what();
151  return -1;
152  }
153 }
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).
kaldi::int32 int32
int32 TransitionIdToPdf(int32 trans_id) const
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
#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.