All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
fgmm-global-acc-stats-twofeats.cc File Reference
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "gmm/model-common.h"
#include "gmm/full-gmm.h"
#include "gmm/diag-gmm.h"
#include "gmm/mle-full-gmm.h"
Include dependency graph for fgmm-global-acc-stats-twofeats.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 29 of file fgmm-global-acc-stats-twofeats.cc.

References AccumFullGmm::AccumulateForComponent(), AccumFullGmm::AccumulateFromPosteriors(), VectorBase< Real >::ApplySoftMax(), FullGmm::ComponentPosteriors(), VectorBase< Real >::Dim(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, rnnlm::j, KALDI_ASSERT, KALDI_LOG, KALDI_VLOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), FullGmm::LogLikelihoodsPreselect(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumCols(), FullGmm::NumGauss(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), ParseOptions::Read(), FullGmm::Read(), ParseOptions::Register(), AccumFullGmm::Resize(), MatrixBase< Real >::Row(), VectorBase< Real >::Scale(), Input::Stream(), kaldi::StringToGmmFlags(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and kaldi::WriteKaldiObject().

29  {
30  try {
31  using namespace kaldi;
32 
33  const char *usage =
34  "Accumulate stats for training a full-covariance GMM, two-feature version\n"
35  "Usage: fgmm-global-acc-stats-twofeats [options] <model-in> "
36  "<feature1-rspecifier> <feature2-rspecifier> <stats-out>\n"
37  "e.g.: fgmm-global-acc-stats-twofeats 1.mdl scp:train.scp scp:train2.scp 1.acc\n";
38 
39  ParseOptions po(usage);
40  bool binary = true;
41  std::string update_flags_str = "mvw";
42  std::string gselect_rspecifier, weights_rspecifier;
43  po.Register("binary", &binary, "Write output in binary mode");
44  po.Register("update-flags", &update_flags_str, "Which GMM parameters will be "
45  "updated: subset of mvw.");
46  po.Register("gselect", &gselect_rspecifier, "rspecifier for gselect objects "
47  "to limit the #Gaussians accessed on each frame.");
48  po.Register("weights", &weights_rspecifier, "rspecifier for a vector of floats "
49  "for each utterance, that's a per-frame weight.");
50  po.Read(argc, argv);
51 
52  if (po.NumArgs() != 4) {
53  po.PrintUsage();
54  exit(1);
55  }
56 
57  std::string model_filename = po.GetArg(1),
58  feature1_rspecifier = po.GetArg(2),
59  feature2_rspecifier = po.GetArg(3),
60  accs_wxfilename = po.GetArg(4);
61 
62  FullGmm gmm;
63  {
64  bool binary_read;
65  Input ki(model_filename, &binary_read);
66  gmm.Read(ki.Stream(), binary_read);
67  }
68 
69  int32 new_dim = 0;
70  AccumFullGmm gmm_accs;
71  // will initialize once we know new_dim.
72  // gmm_accs.Resize(gmm, StringToGmmFlags(update_flags_str));
73 
74  double tot_like = 0.0, tot_weight = 0.0;
75 
76  SequentialBaseFloatMatrixReader feature1_reader(feature1_rspecifier);
77  RandomAccessBaseFloatMatrixReader feature2_reader(feature2_rspecifier);
78  RandomAccessInt32VectorVectorReader gselect_reader(gselect_rspecifier);
79  RandomAccessBaseFloatVectorReader weights_reader(weights_rspecifier);
80  int32 num_done = 0, num_err = 0;
81 
82  for (; !feature1_reader.Done(); feature1_reader.Next()) {
83  std::string key = feature1_reader.Key();
84  if (!feature2_reader.HasKey(key)) {
85  KALDI_WARN << "For utterance " << key << ", second features not present.";
86  num_err++;
87  continue;
88  }
89  const Matrix<BaseFloat> &mat1 = feature1_reader.Value();
90  const Matrix<BaseFloat> &mat2 = feature2_reader.Value(key);
91  int32 file_frames = mat1.NumRows();
92  KALDI_ASSERT(mat1.NumRows() == mat2.NumRows());
93  if (new_dim == 0) {
94  new_dim = mat2.NumCols();
95  gmm_accs.Resize(gmm.NumGauss(), new_dim,
96  StringToGmmFlags(update_flags_str));
97  }
98  BaseFloat file_like = 0.0,
99  file_weight = 0.0; // total of weights of frames (will each be 1 unless
100  // --weights option supplied.
101  Vector<BaseFloat> weights;
102  if (weights_rspecifier != "") { // We have per-frame weighting.
103  if (!weights_reader.HasKey(key)) {
104  KALDI_WARN << "No per-frame weights available for utterance " << key;
105  num_err++;
106  continue;
107  }
108  weights = weights_reader.Value(key);
109  if (weights.Dim() != file_frames) {
110  KALDI_WARN << "Weights for utterance " << key << " have wrong dim "
111  << weights.Dim() << " vs. " << file_frames;
112  num_err++;
113  continue;
114  }
115  }
116  if (gselect_rspecifier != "") {
117  if (!gselect_reader.HasKey(key)) {
118  KALDI_WARN << "No gselect information for utterance " << key;
119  num_err++;
120  continue;
121  }
122  const std::vector<std::vector<int32> > &gselect =
123  gselect_reader.Value(key);
124  if (gselect.size() != static_cast<size_t>(file_frames)) {
125  KALDI_WARN << "gselect information for utterance " << key
126  << " has wrong size " << gselect.size() << " vs. "
127  << file_frames;
128  num_err++;
129  continue;
130  }
131 
132  for (int32 i = 0; i < file_frames; i++) {
133  BaseFloat weight = (weights.Dim() != 0) ? weights(i) : 1.0;
134  if (weight == 0.0) continue;
135  file_weight += weight;
136  SubVector<BaseFloat> data1(mat1, i), data2(mat2, i);
137  const std::vector<int32> &this_gselect = gselect[i];
138  int32 gselect_size = this_gselect.size();
139  KALDI_ASSERT(gselect_size > 0);
140  Vector<BaseFloat> loglikes;
141  gmm.LogLikelihoodsPreselect(data1, this_gselect, &loglikes);
142  file_like += weight * loglikes.ApplySoftMax();
143  loglikes.Scale(weight);
144  for (int32 j = 0; j < loglikes.Dim(); j++)
145  gmm_accs.AccumulateForComponent(data2, this_gselect[j], loglikes(j));
146  }
147  } else { // no gselect..
148  Vector<BaseFloat> posteriors;
149  for (int32 i = 0; i < file_frames; i++) {
150  BaseFloat weight = (weights.Dim() != 0) ? weights(i) : 1.0;
151  if (weight == 0.0) continue;
152  file_weight += weight;
153  file_like += weight * gmm.ComponentPosteriors(mat1.Row(i), &posteriors);
154  posteriors.Scale(weight);
155  gmm_accs.AccumulateFromPosteriors(mat2.Row(i), posteriors);
156  }
157  }
158  KALDI_VLOG(2) << "File '" << key << "': Average likelihood = "
159  << (file_like/file_weight) << " over "
160  << file_weight <<" frames.";
161  tot_like += file_like;
162  tot_weight += file_weight;
163  num_done++;
164  }
165  KALDI_LOG << "Done " << num_done << " files; "
166  << num_err << " with errors.";
167  KALDI_LOG << "Overall likelihood per "
168  << "frame = " << (tot_like/tot_weight) << " over " << tot_weight
169  << " (weighted) frames.";
170 
171  WriteKaldiObject(gmm_accs, accs_wxfilename, binary);
172  KALDI_LOG << "Written accs to " << accs_wxfilename;
173  return (num_done != 0 ? 0 : 1);
174  } catch(const std::exception &e) {
175  std::cerr << e.what();
176  return -1;
177  }
178 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
GmmFlagsType StringToGmmFlags(std::string str)
Convert string which is some subset of "mSwa" to flags.
Definition: model-common.cc:26
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: full-gmm.h:58
Definition for Gaussian Mixture Model with full covariances.
Definition: full-gmm.h:40
Real ApplySoftMax()
Apply soft-max to vector and return normalizer (log sum of exponentials).
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
void Resize(int32 num_components, int32 dim, GmmFlagsType flags)
Allocates memory for accumulators.
Definition: mle-full-gmm.cc:37
float BaseFloat
Definition: kaldi-types.h:29
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
void AccumulateFromPosteriors(const VectorBase< BaseFloat > &data, const VectorBase< BaseFloat > &gauss_posteriors)
Accumulate for all components, given the posteriors.
void LogLikelihoodsPreselect(const VectorBase< BaseFloat > &data, const std::vector< int32 > &indices, Vector< BaseFloat > *loglikes) const
Outputs the per-component log-likelihoods of a subset of mixture components.
Definition: full-gmm.cc:613
BaseFloat ComponentPosteriors(const VectorBase< BaseFloat > &data, VectorBase< BaseFloat > *posterior) const
Computes the posterior probabilities of all Gaussian components given a data point.
Definition: full-gmm.cc:719
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
Class for computing the maximum-likelihood estimates of the parameters of a Gaussian mixture model...
Definition: mle-full-gmm.h:74
#define KALDI_WARN
Definition: kaldi-error.h:130
void Scale(Real alpha)
Multiplies all elements by this constant.
void Read(std::istream &is, bool binary)
Definition: full-gmm.cc:813
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
#define KALDI_VLOG(v)
Definition: kaldi-error.h:136
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
#define KALDI_LOG
Definition: kaldi-error.h:133
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:482
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:62
void AccumulateForComponent(const VectorBase< BaseFloat > &data, int32 comp_index, BaseFloat weight)
Accumulate for a single component, given the posterior.
Definition: mle-full-gmm.cc:96