fgmm-global-acc-stats.cc
Go to the documentation of this file.
1 // fgmmbin/fgmm-global-acc-stats.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation; Saarland University
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "gmm/model-common.h"
24 #include "gmm/full-gmm.h"
25 #include "gmm/diag-gmm.h"
26 #include "gmm/mle-full-gmm.h"
27 
28 
29 int main(int argc, char *argv[]) {
30  try {
31  using namespace kaldi;
32 
33  const char *usage =
34  "Accumulate stats for training a full-covariance GMM.\n"
35  "Usage: fgmm-global-acc-stats [options] <model-in> <feature-rspecifier> "
36  "<stats-out>\n"
37  "e.g.: fgmm-global-acc-stats 1.mdl scp:train.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() != 3) {
53  po.PrintUsage();
54  exit(1);
55  }
56 
57  std::string model_filename = po.GetArg(1),
58  feature_rspecifier = po.GetArg(2),
59  accs_wxfilename = po.GetArg(3);
60 
61  FullGmm fgmm;
62  {
63  bool binary_read;
64  Input ki(model_filename, &binary_read);
65  fgmm.Read(ki.Stream(), binary_read);
66  }
67 
68  AccumFullGmm fgmm_accs;
69  fgmm_accs.Resize(fgmm, StringToGmmFlags(update_flags_str));
70 
71  double tot_like = 0.0, tot_weight = 0.0;
72 
73  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
74  RandomAccessInt32VectorVectorReader gselect_reader(gselect_rspecifier);
75  RandomAccessBaseFloatVectorReader weights_reader(weights_rspecifier);
76  int32 num_done = 0, num_err = 0;
77 
78  for (; !feature_reader.Done(); feature_reader.Next()) {
79  std::string key = feature_reader.Key();
80  const Matrix<BaseFloat> &mat = feature_reader.Value();
81  int32 file_frames = mat.NumRows();
82  BaseFloat file_like = 0.0,
83  file_weight = 0.0; // total of weights of frames (will each be 1 unless
84  // --weights option supplied.
85  Vector<BaseFloat> weights;
86  if (weights_rspecifier != "") { // We have per-frame weighting.
87  if (!weights_reader.HasKey(key)) {
88  KALDI_WARN << "No per-frame weights available for utterance " << key;
89  num_err++;
90  continue;
91  }
92  weights = weights_reader.Value(key);
93  if (weights.Dim() != file_frames) {
94  KALDI_WARN << "Weights for utterance " << key << " have wrong dim "
95  << weights.Dim() << " vs. " << file_frames;
96  num_err++;
97  continue;
98  }
99  }
100 
101  if (gselect_rspecifier != "") {
102  if (!gselect_reader.HasKey(key)) {
103  KALDI_WARN << "No gselect information for utterance " << key;
104  num_err++;
105  continue;
106  }
107  const std::vector<std::vector<int32> > &gselect =
108  gselect_reader.Value(key);
109  if (gselect.size() != static_cast<size_t>(file_frames)) {
110  KALDI_WARN << "gselect information for utterance " << key
111  << " has wrong size " << gselect.size() << " vs. "
112  << file_frames;
113  num_err++;
114  continue;
115  }
116 
117  for (int32 i = 0; i < file_frames; i++) {
118  BaseFloat weight = (weights.Dim() != 0) ? weights(i) : 1.0;
119  if (weight == 0.0) continue;
120  file_weight += weight;
121  SubVector<BaseFloat> data(mat, i);
122  const std::vector<int32> &this_gselect = gselect[i];
123  int32 gselect_size = this_gselect.size();
124  KALDI_ASSERT(gselect_size > 0);
125  Vector<BaseFloat> loglikes;
126  fgmm.LogLikelihoodsPreselect(data, this_gselect, &loglikes);
127  file_like += weight * loglikes.ApplySoftMax();
128  loglikes.Scale(weight);
129  for (int32 j = 0; j < loglikes.Dim(); j++)
130  fgmm_accs.AccumulateForComponent(data, this_gselect[j], loglikes(j));
131  }
132  } else { // no gselect...
133  for (int32 i = 0; i < file_frames; i++) {
134  BaseFloat weight = (weights.Dim() != 0) ? weights(i) : 1.0;
135  if (weight == 0.0) continue;
136  file_weight += weight;
137  file_like += weight *
138  fgmm_accs.AccumulateFromFull(fgmm, mat.Row(i), weight);
139  }
140  }
141  KALDI_VLOG(2) << "File '" << key << "': Average likelihood = "
142  << (file_like/file_weight) << " over "
143  << file_weight <<" frames.";
144  tot_like += file_like;
145  tot_weight += file_weight;
146  num_done++;
147  }
148  KALDI_LOG << "Done " << num_done << " files; "
149  << num_err << " with errors.";
150  KALDI_LOG << "Overall likelihood per "
151  << "frame = " << (tot_like/tot_weight) << " over " << tot_weight
152  << " (weighted) frames.";
153 
154  WriteKaldiObject(fgmm_accs, accs_wxfilename, binary);
155  KALDI_LOG << "Written accs to " << accs_wxfilename;
156  return (num_done != 0 ? 0 : 1);
157  } catch(const std::exception &e) {
158  std::cerr << e.what();
159  return -1;
160  }
161 }
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 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
Definition for Gaussian Mixture Model with full covariances.
Definition: full-gmm.h:40
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
kaldi::int32 int32
Real ApplySoftMax()
Apply soft-max to vector and return normalizer (log sum of exponentials).
void Register(const std::string &name, bool *ptr, const std::string &doc)
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
std::istream & Stream()
Definition: kaldi-io.cc:826
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
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
const T & Value(const std::string &key)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
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:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
void Scale(Real alpha)
Multiplies all elements by this constant.
bool HasKey(const std::string &key)
void Read(std::istream &is, bool binary)
Definition: full-gmm.cc:813
int NumArgs() const
Number of positional parameters (c.f. argc-1).
A class representing a vector.
Definition: kaldi-vector.h:406
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
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
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
int main(int argc, char *argv[])
BaseFloat AccumulateFromFull(const FullGmm &gmm, const VectorBase< BaseFloat > &data, BaseFloat frame_posterior)
Accumulate for all components given a full-covariance GMM.
#define KALDI_LOG
Definition: kaldi-error.h:153
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501
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