gmm-est-basis-fmllr.cc
Go to the documentation of this file.
1 // gmmbin/gmm-est-basis-fmllr.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 Posterior &post,
37  const TransitionModel &trans_model,
38  const AmDiagGmm &am_gmm,
39  FmllrDiagGmmAccs *spk_stats) {
40  Posterior pdf_post;
41  ConvertPosteriorToPdfs(trans_model, post, &pdf_post);
42  for (size_t i = 0; i < post.size(); i++) {
43  for (size_t j = 0; j < pdf_post[i].size(); j++) {
44  int32 pdf_id = pdf_post[i][j].first;
45  spk_stats->AccumulateForGmm(am_gmm.GetPdf(pdf_id),
46  feats.Row(i),
47  pdf_post[i][j].second);
48  }
49  }
50 }
51 
52 
53 }
54 
55 int main(int argc, char *argv[]) {
56  try {
57  typedef kaldi::int32 int32;
58  using namespace kaldi;
59  const char *usage =
60  "Perform basis fMLLR adaptation in testing stage, either per utterance or\n"
61  "for the supplied set of speakers (spk2utt option). Reads posterior to\n"
62  "accumulate fMLLR stats for each speaker/utterance. Writes to a table of\n"
63  "matrices.\n"
64  "Usage: gmm-est-basis-fmllr [options] <model-in> <basis-rspecifier> <feature-rspecifier> "
65  "<post-rspecifier> <transform-wspecifier>\n";
66 
67  ParseOptions po(usage);
68  BasisFmllrOptions basis_fmllr_opts;
69  string spk2utt_rspecifier;
70  string weights_out_filename;
71 
72  po.Register("spk2utt", &spk2utt_rspecifier, "Rspecifier for speaker to "
73  "utterance-list map");
74  po.Register("write-weights", &weights_out_filename, "File to write base "
75  "weights to.");
76 
77  basis_fmllr_opts.Register(&po);
78 
79  po.Read(argc, argv);
80  if (po.NumArgs() != 5) {
81  po.PrintUsage();
82  exit(1);
83  }
84 
85  string
86  model_rxfilename = po.GetArg(1),
87  basis_rspecifier = po.GetArg(2),
88  feature_rspecifier = po.GetArg(3),
89  post_rspecifier = po.GetArg(4),
90  trans_wspecifier = po.GetArg(5);
91 
92  TransitionModel trans_model;
93  AmDiagGmm am_gmm;
94  {
95  bool binary;
96  Input ki(model_rxfilename, &binary);
97  trans_model.Read(ki.Stream(), binary);
98  am_gmm.Read(ki.Stream(), binary);
99  }
100 
101  BasisFmllrEstimate basis_est;
102  ReadKaldiObject(basis_rspecifier, &basis_est);
103 
104  RandomAccessPosteriorReader post_reader(post_rspecifier);
105 
106  double tot_impr = 0.0, tot_t = 0.0;
107 
108  BaseFloatMatrixWriter transform_writer(trans_wspecifier);
109  BaseFloatVectorWriter weights_writer;
110  if (!weights_out_filename.empty()) {
111  weights_writer.Open(weights_out_filename);
112  }
113 
114  int32 num_done = 0, num_no_post = 0, num_other_error = 0;
115  if (spk2utt_rspecifier != "") { // per-speaker adaptation
116  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
117  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
118 
119  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
120  FmllrDiagGmmAccs spk_stats(am_gmm.Dim());
121  string spk = spk2utt_reader.Key();
122  const vector<string> &uttlist = spk2utt_reader.Value();
123  for (size_t i = 0; i < uttlist.size(); i++) {
124  std::string utt = uttlist[i];
125  if (!feature_reader.HasKey(utt)) {
126  KALDI_WARN << "Did not find features for utterance " << utt;
127  num_other_error++;
128  continue;
129  }
130  if (!post_reader.HasKey(utt)) {
131  KALDI_WARN << "Did not find posteriors for utterance " << utt;
132  num_no_post++;
133  continue;
134  }
135  const Matrix<BaseFloat> &feats = feature_reader.Value(utt);
136  const Posterior &post = post_reader.Value(utt);
137  if (static_cast<int32>(post.size()) != feats.NumRows()) {
138  KALDI_WARN << "Posterior vector has wrong size " << (post.size())
139  << " vs. " << (feats.NumRows());
140  num_other_error++;
141  continue;
142  }
143 
144  AccumulateForUtterance(feats, post, trans_model, am_gmm, &spk_stats);
145  num_done++;
146  } // end looping over all utterances of the current speaker
147 
148  double impr, spk_tot_t; int32 wgt_size;
149  {
150  // Compute the transform and write it out.
151  Matrix<BaseFloat> transform(am_gmm.Dim(), am_gmm.Dim() + 1);
152  transform.SetUnit();
153  Vector<BaseFloat> weights; // size will be adjusted
154  impr = basis_est.ComputeTransform(spk_stats, &transform,
155  &weights, basis_fmllr_opts);
156  spk_tot_t = spk_stats.beta_;
157  wgt_size = weights.Dim();
158  transform_writer.Write(spk, transform);
159  // Optionally write out the base weights
160  if (!weights_out_filename.empty() && weights.Dim() > 0)
161  weights_writer.Write(spk, weights);
162  }
163  KALDI_LOG << "For speaker " << spk << ", auxf-impr from Basis fMLLR is "
164  << (impr / spk_tot_t) << ", over " << spk_tot_t << " frames, "
165  << "the top " << wgt_size << " basis elements have been used";
166  tot_impr += impr;
167  tot_t += spk_tot_t;
168  } // end looping over speakers
169  } else { // per-utterance adaptation
170  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
171  for (; !feature_reader.Done(); feature_reader.Next()) {
172  string utt = feature_reader.Key();
173  if (!post_reader.HasKey(utt)) {
174  KALDI_WARN << "Did not find posts for utterance " << utt;
175  num_no_post++;
176  continue;
177  }
178  const Matrix<BaseFloat> &feats = feature_reader.Value();
179  const Posterior &post = post_reader.Value(utt);
180 
181  if (static_cast<int32>(post.size()) != feats.NumRows()) {
182  KALDI_WARN << "Posterior has wrong size " << (post.size())
183  << " vs. " << (feats.NumRows());
184  num_other_error++;
185  continue;
186  }
187 
188  FmllrDiagGmmAccs spk_stats(am_gmm.Dim());
189  AccumulateForUtterance(feats, post, trans_model, am_gmm, &spk_stats);
190  num_done++;
191 
192  BaseFloat impr, utt_tot_t; int32 wgt_size;
193  { // Compute the transform and write it out.
194  Matrix<BaseFloat> transform(am_gmm.Dim(), am_gmm.Dim()+1);
195  transform.SetUnit();
196  Vector<BaseFloat> weights(am_gmm.Dim() * (am_gmm.Dim() + 1)); // size will be adjusted
197  impr = basis_est.ComputeTransform(spk_stats, &transform,
198  &weights, basis_fmllr_opts);
199  utt_tot_t = spk_stats.beta_;
200  wgt_size = weights.Dim();
201  transform_writer.Write(utt, transform);
202  // Optionally write out the base weights
203  if (!weights_out_filename.empty() && weights.Dim() > 0)
204  weights_writer.Write(utt, weights);
205  }
206  KALDI_LOG << "For utterance " << utt << ", auxf-impr from Basis fMLLR is "
207  << (impr / utt_tot_t) << ", over " << utt_tot_t << " frames, "
208  << "the top " << wgt_size << " basis elements have been used";
209  tot_impr += impr;
210  tot_t += utt_tot_t;
211  } // end looping over all the utterances
212  }
213 
214  KALDI_LOG << "Done " << num_done << " files, " << num_no_post
215  << " with no posts, " << num_other_error << " with other errors.";
216  KALDI_LOG << "Overall fMLLR auxf-impr per frame is "
217  << (tot_impr / tot_t) << " over " << tot_t << " frames.";
218  return (num_done != 0 ? 0 : 1);
219  } catch(const std::exception& e) {
220  std::cerr << e.what();
221  return -1;
222  }
223 }
224 
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].
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
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
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)
int main(int argc, char *argv[])
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.
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
bool HasKey(const std::string &key)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
A class representing a vector.
Definition: kaldi-vector.h:406
void ConvertPosteriorToPdfs(const TransitionModel &tmodel, const Posterior &post_in, Posterior *post_out)
Converts a posterior over transition-ids to be a posterior over pdf-ids.
Definition: posterior.cc:322
#define KALDI_LOG
Definition: kaldi-error.h:153
Estimation functions for basis fMLLR.