gmm-basis-fmllr-accs-gpost.cc
Go to the documentation of this file.
1 // gmmbin/gmm-basis-fmllr-accs-gpost.cc
2 
3 // Copyright 2012 Carnegie Mellon University (author: Yajie Miao)
4 // 2014 Guoguo Chen
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include <string>
22 using std::string;
23 #include <vector>
24 using std::vector;
25 
26 #include "base/kaldi-common.h"
27 #include "util/common-utils.h"
28 #include "gmm/am-diag-gmm.h"
29 #include "hmm/transition-model.h"
32 #include "hmm/posterior.h"
33 
34 namespace kaldi {
36  const GaussPost &gpost,
37  const TransitionModel &trans_model,
38  const AmDiagGmm &am_gmm,
39  FmllrDiagGmmAccs *spk_stats) {
40  for (size_t i = 0; i < gpost.size(); i++) {
41  for (size_t j = 0; j < gpost[i].size(); j++) {
42  int32 pdf_id = gpost[i][j].first;
43  const Vector<BaseFloat> & posterior(gpost[i][j].second);
44  spk_stats->AccumulateFromPosteriors(am_gmm.GetPdf(pdf_id),
45  feats.Row(i), posterior);
46  }
47  }
48 }
49 
50 
51 }
52 
53 int main(int argc, char *argv[]) {
54  try {
55  typedef kaldi::int32 int32;
56  using namespace kaldi;
57  const char *usage =
58  "Accumulate gradient scatter from training set, either per utterance or \n"
59  "for the supplied set of speakers (spk2utt option). Reads Gaussian-level \n"
60  "posterior to accumulate fMLLR stats for each speaker/utterance. Writes \n"
61  "gradient scatter matrix.\n"
62  "Usage: gmm-basis-fmllr-accs-gpost [options] <model-in> <feature-rspecifier>"
63  "<post-rspecifier> <accs-wspecifier>\n";
64 
65  bool binary_write = true;
66  string spk2utt_rspecifier;
67  ParseOptions po(usage);
68  po.Register("binary", &binary_write, "Write output in binary mode");
69  po.Register("spk2utt", &spk2utt_rspecifier, "rspecifier for speaker to "
70  "utterance-list map");
71 
72  po.Read(argc, argv);
73  if (po.NumArgs() != 4) {
74  po.PrintUsage();
75  exit(1);
76  }
77 
78  string
79  model_rxfilename = po.GetArg(1),
80  feature_rspecifier = po.GetArg(2),
81  gpost_rspecifier = po.GetArg(3),
82  accs_wspecifier = po.GetArg(4);
83 
84  TransitionModel trans_model;
85  AmDiagGmm am_gmm;
86  {
87  bool binary;
88  Input ki(model_rxfilename, &binary);
89  trans_model.Read(ki.Stream(), binary);
90  am_gmm.Read(ki.Stream(), binary);
91  }
92 
93  RandomAccessGaussPostReader gpost_reader(gpost_rspecifier);
94  BasisFmllrAccus basis_accs(am_gmm.Dim());
95 
96  int32 num_done = 0, num_no_post = 0, num_other_error = 0;
97  if (spk2utt_rspecifier != "") { // per-speaker mode
98  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
99  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
100 
101  int32 num_spk = 0;
102  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
103  FmllrDiagGmmAccs spk_stats(am_gmm.Dim());
104  string spk = spk2utt_reader.Key();
105  const vector<string> &uttlist = spk2utt_reader.Value();
106  for (size_t i = 0; i < uttlist.size(); i++) {
107  std::string utt = uttlist[i];
108  if (!feature_reader.HasKey(utt)) {
109  KALDI_WARN << "Did not find features for utterance " << utt;
110  num_other_error++;
111  continue;
112  }
113  if (!gpost_reader.HasKey(utt)) {
114  KALDI_WARN << "Did not find posteriors for utterance " << utt;
115  num_no_post++;
116  continue;
117  }
118  const Matrix<BaseFloat> &feats = feature_reader.Value(utt);
119  const GaussPost &gpost = gpost_reader.Value(utt);
120  if (static_cast<int32>(gpost.size()) != feats.NumRows()) {
121  KALDI_WARN << "GaussPost has wrong size " << (gpost.size())
122  << " vs. " << (feats.NumRows());
123  num_other_error++;
124  continue;
125  }
126 
127  AccumulateForUtterance(feats, gpost, trans_model, am_gmm, &spk_stats);
128 
129  num_done++;
130  } // end looping over all utterances of this speaker
131  basis_accs.AccuGradientScatter(spk_stats);
132  num_spk++;
133  } // end looping over speakers
134  KALDI_LOG << "Accumulate statistics from " << num_spk << " speakers";
135 
136  } else { // per-utterance mode
137  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
138  for (; !feature_reader.Done(); feature_reader.Next()) {
139  string utt = feature_reader.Key();
140  if (!gpost_reader.HasKey(utt)) {
141  KALDI_WARN << "Did not find posts for utterance "
142  << utt;
143  num_no_post++;
144  continue;
145  }
146  const Matrix<BaseFloat> &feats = feature_reader.Value();
147  const GaussPost &gpost = gpost_reader.Value(utt);
148 
149  if (static_cast<int32>(gpost.size()) != feats.NumRows()) {
150  KALDI_WARN << "GaussPost has wrong size " << (gpost.size())
151  << " vs. " << (feats.NumRows());
152  num_other_error++;
153  continue;
154  }
155  // Accumulate stats for this utterance
156  FmllrDiagGmmAccs utt_stats(am_gmm.Dim());
157  AccumulateForUtterance(feats, gpost, trans_model, am_gmm, &utt_stats);
158  num_done++;
159 
160  basis_accs.AccuGradientScatter(utt_stats);
161  } // end looping over all utterances
162  }
163  // Write out accumulations
164  {
165  Output ko(accs_wspecifier, binary_write);
166  basis_accs.Write(ko.Stream(), binary_write);
167  }
168  KALDI_LOG << "Done " << num_done << " files, " << num_no_post
169  << " with no posts, " << num_other_error << " with other errors.";
170  KALDI_LOG << "Written gradient scatter to " << accs_wspecifier;
171  return (num_done != 0 ? 0 : 1);
172  } catch(const std::exception& e) {
173  std::cerr << e.what();
174  return -1;
175  }
176 }
177 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
This does not work with multiple feature transforms.
void AccumulateFromPosteriors(const DiagGmm &gmm, const VectorBase< BaseFloat > &data, const VectorBase< BaseFloat > &posteriors)
Accumulate stats for a GMM, given supplied posteriors.
void AccumulateForUtterance(const Matrix< BaseFloat > &feats, const GaussPost &gpost, const TransitionModel &trans_model, const AmDiagGmm &am_gmm, FmllrDiagGmmAccs *spk_stats)
kaldi::int32 int32
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
std::istream & Stream()
Definition: kaldi-io.cc:826
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
std::ostream & Stream()
Definition: kaldi-io.cc:701
Stats for fMLLR subspace estimation.
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)
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
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#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.
bool HasKey(const std::string &key)
int32 Dim() const
Definition: am-diag-gmm.h:79
int NumArgs() const
Number of positional parameters (c.f. argc-1).
DiagGmm & GetPdf(int32 pdf_index)
Accessors.
Definition: am-diag-gmm.h:119
int main(int argc, char *argv[])
A class representing a vector.
Definition: kaldi-vector.h:406
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
std::vector< std::vector< std::pair< int32, Vector< BaseFloat > > > > GaussPost
GaussPost is a typedef for storing Gaussian-level posteriors for an utterance.
Definition: posterior.h:51
#define KALDI_LOG
Definition: kaldi-error.h:153
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147