gmm-est-basis-fmllr-gpost.cc
Go to the documentation of this file.
1 // gmmbin/gmm-est-basis-fmllr-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 {
35 void AccumulateForUtterance(const Matrix<BaseFloat> &feats,
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  "Perform basis fMLLR adaptation in testing stage, 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  "to a table of matrices.\n"
62  "Usage: gmm-est-basis-fmllr-gpost [options] <model-in> <basis-rspecifier> "
63  "<feature-rspecifier> <post-rspecifier> <transform-wspecifier>\n";
64 
65  ParseOptions po(usage);
66  BasisFmllrOptions basis_fmllr_opts;
67  string spk2utt_rspecifier;
68  string weights_out_filename;
69 
70  po.Register("spk2utt", &spk2utt_rspecifier, "Rspecifier for speaker to "
71  "utterance-list map");
72  po.Register("write-weights", &weights_out_filename, "File to write base "
73  "weights to.");
74 
75  basis_fmllr_opts.Register(&po);
76 
77  po.Read(argc, argv);
78  if (po.NumArgs() != 5) {
79  po.PrintUsage();
80  exit(1);
81  }
82 
83  string
84  model_rxfilename = po.GetArg(1),
85  basis_rspecifier = po.GetArg(2),
86  feature_rspecifier = po.GetArg(3),
87  gpost_rspecifier = po.GetArg(4),
88  trans_wspecifier = po.GetArg(5);
89 
90  TransitionModel trans_model;
91  AmDiagGmm am_gmm;
92  {
93  bool binary;
94  Input ki(model_rxfilename, &binary);
95  trans_model.Read(ki.Stream(), binary);
96  am_gmm.Read(ki.Stream(), binary);
97  }
98 
99  BasisFmllrEstimate basis_est;
100  ReadKaldiObject(basis_rspecifier, &basis_est);
101 
102  RandomAccessGaussPostReader gpost_reader(gpost_rspecifier);
103 
104  double tot_impr = 0.0, tot_t = 0.0;
105 
106  BaseFloatMatrixWriter transform_writer(trans_wspecifier);
107  BaseFloatVectorWriter weights_writer;
108  if (!weights_out_filename.empty()) {
109  weights_writer.Open(weights_out_filename);
110  }
111 
112  int32 num_done = 0, num_no_post = 0, num_other_error = 0;
113  if (spk2utt_rspecifier != "") { // per-speaker adaptation
114  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
115  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
116 
117  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
118  FmllrDiagGmmAccs spk_stats(am_gmm.Dim());
119  string spk = spk2utt_reader.Key();
120  const vector<string> &uttlist = spk2utt_reader.Value();
121  for (size_t i = 0; i < uttlist.size(); i++) {
122  std::string utt = uttlist[i];
123  if (!feature_reader.HasKey(utt)) {
124  KALDI_WARN << "Did not find features for utterance " << utt;
125  num_other_error++;
126  continue;
127  }
128  if (!gpost_reader.HasKey(utt)) {
129  KALDI_WARN << "Did not find posteriors for utterance " << utt;
130  num_no_post++;
131  continue;
132  }
133  const Matrix<BaseFloat> &feats = feature_reader.Value(utt);
134  const GaussPost &gpost = gpost_reader.Value(utt);
135  if (static_cast<int32>(gpost.size()) != feats.NumRows()) {
136  KALDI_WARN << "GaussPost has wrong size " << (gpost.size())
137  << " vs. " << (feats.NumRows());
138  num_other_error++;
139  continue;
140  }
141 
142  AccumulateForUtterance(feats, gpost, trans_model, am_gmm, &spk_stats);
143  num_done++;
144  } // end looping over all utterances of the current speaker
145 
146  double impr, spk_tot_t; int32 wgt_size;
147  {
148  // Compute the transform and write it out.
149  Matrix<BaseFloat> transform(am_gmm.Dim(), am_gmm.Dim() + 1);
150  transform.SetUnit();
151  Vector<BaseFloat> weights;
152  impr = basis_est.ComputeTransform(spk_stats, &transform,
153  &weights, basis_fmllr_opts);
154  spk_tot_t = spk_stats.beta_;
155  wgt_size = weights.Dim();
156  transform_writer.Write(spk, transform);
157  // Optionally write out the base weights
158  if (!weights_out_filename.empty() && weights.Dim() > 0)
159  weights_writer.Write(spk, weights);
160  }
161 
162  KALDI_LOG << "For speaker " << spk << ", auxf-impr from Basis fMLLR is "
163  << (impr / spk_tot_t) << ", over " << spk_tot_t << " frames, "
164  << "the top " << wgt_size << " basis elements have been used";
165  tot_impr += impr;
166  tot_t += spk_tot_t;
167  } // end looping over speakers
168  } else { // per-utterance adaptation
169  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
170  for (; !feature_reader.Done(); feature_reader.Next()) {
171  string utt = feature_reader.Key();
172  if (!gpost_reader.HasKey(utt)) {
173  KALDI_WARN << "Did not find posts for utterance " << utt;
174  num_no_post++;
175  continue;
176  }
177  const Matrix<BaseFloat> &feats = feature_reader.Value();
178  const GaussPost &gpost = gpost_reader.Value(utt);
179 
180  if (static_cast<int32>(gpost.size()) != feats.NumRows()) {
181  KALDI_WARN << "GaussPost has wrong size " << (gpost.size())
182  << " vs. " << (feats.NumRows());
183  num_other_error++;
184  continue;
185  }
186 
187  FmllrDiagGmmAccs spk_stats(am_gmm.Dim());
188  AccumulateForUtterance(feats, gpost, trans_model, am_gmm, &spk_stats);
189  num_done++;
190 
191  BaseFloat impr, utt_tot_t; int32 wgt_size;
192  { // Compute the transform and write it out.
193  Matrix<BaseFloat> transform(am_gmm.Dim(), am_gmm.Dim()+1);
194  transform.SetUnit();
195  Vector<BaseFloat> weights;
196  impr = basis_est.ComputeTransform(spk_stats, &transform,
197  &weights, basis_fmllr_opts);
198  utt_tot_t = spk_stats.beta_;
199  wgt_size = weights.Dim();
200  transform_writer.Write(utt, transform);
201  // Optionally write out the base weights
202  if (!weights_out_filename.empty() && weights.Dim() > 0)
203  weights_writer.Write(utt, weights);
204  }
205  KALDI_LOG << "For utterance " << utt << ", auxf-impr from Basis fMLLR is "
206  << (impr / utt_tot_t) << ", over " << utt_tot_t << " frames, "
207  << "the top " << wgt_size << " basis elements have been used";
208  tot_impr += impr;
209  tot_t += utt_tot_t;
210  } // end looping over all the utterances
211  }
212 
213  KALDI_LOG << "Done " << num_done << " files, " << num_no_post
214  << " with no posts, " << num_other_error << " with other errors.";
215  KALDI_LOG << "Overall fMLLR auxf-impr per frame is "
216  << (tot_impr / tot_t) << " over " << tot_t << " frames.";
217  return (num_done != 0 ? 0 : 1);
218  } catch(const std::exception& e) {
219  std::cerr << e.what();
220  return -1;
221  }
222 }
223 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool Open(const std::string &wspecifier)
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
int main(int argc, char *argv[])
void AccumulateForUtterance(const Matrix< BaseFloat > &feats, const GaussPost &gpost, const TransitionModel &trans_model, const AmDiagGmm &am_gmm, FmllrDiagGmmAccs *spk_stats)
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
double ComputeTransform(const AffineXformStats &spk_stats, Matrix< BaseFloat > *out_xform, Vector< BaseFloat > *coefficients, BasisFmllrOptions options) const
This function performs speaker adaptation, computing the fMLLR matrix based on speaker statistics...
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
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
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 T & Value(const std::string &key)
void Register(OptionsItf *opts)
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)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
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
Estimation functions for basis fMLLR.