gmm-est-regtree-fmllr.cc
Go to the documentation of this file.
1 // gmmbin/gmm-est-regtree-fmllr.cc
2 
3 // Copyright 2009-2011 Saarland University; Microsoft Corporation
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"
30 #include "hmm/posterior.h"
32 
33 int main(int argc, char *argv[]) {
34  try {
35  typedef kaldi::int32 int32;
36  using namespace kaldi;
37  const char *usage =
38  "Compute FMLLR transforms per-utterance (default) or per-speaker for "
39  "the supplied set of speakers (spk2utt option). Note: writes RegtreeFmllrDiagGmm objects\n"
40  "Usage: gmm-est-regtree-fmllr [options] <model-in> <feature-rspecifier> "
41  "<posteriors-rspecifier> <regression-tree> <transforms-wspecifier>\n";
42 
43  ParseOptions po(usage);
44  string spk2utt_rspecifier;
45  bool binary = true;
46  po.Register("spk2utt", &spk2utt_rspecifier, "rspecifier for speaker to "
47  "utterance-list map");
48  po.Register("binary", &binary, "Write output in binary mode");
49  // register other modules
51  opts.Register(&po);
52 
53  po.Read(argc, argv);
54 
55  if (po.NumArgs() != 5) {
56  po.PrintUsage();
57  exit(1);
58  }
59 
60  string model_filename = po.GetArg(1),
61  feature_rspecifier = po.GetArg(2),
62  posteriors_rspecifier = po.GetArg(3),
63  regtree_filename = po.GetArg(4),
64  xforms_wspecifier = po.GetArg(5);
65 
66  RandomAccessPosteriorReader posteriors_reader(posteriors_rspecifier);
67  RegtreeFmllrDiagGmmWriter fmllr_writer(xforms_wspecifier);
68 
69  AmDiagGmm am_gmm;
70  TransitionModel trans_model;
71  {
72  bool binary;
73  Input ki(model_filename, &binary);
74  trans_model.Read(ki.Stream(), binary);
75  am_gmm.Read(ki.Stream(), binary);
76  }
77  RegressionTree regtree;
78  {
79  bool binary;
80  Input in(regtree_filename, &binary);
81  regtree.Read(in.Stream(), binary, am_gmm);
82  }
83 
84  RegtreeFmllrDiagGmm fmllr_xforms;
85  RegtreeFmllrDiagGmmAccs fmllr_accs;
86  fmllr_accs.Init(regtree.NumBaseclasses(), am_gmm.Dim());
87 
88  double tot_like = 0.0, tot_t = 0;
89 
90  int32 num_done = 0, num_no_posterior = 0, num_other_error = 0;
91  double tot_objf_impr = 0.0, tot_t_objf = 0.0;
92  if (spk2utt_rspecifier != "") { // per-speaker adaptation
93  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
94  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
95  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
96  string spk = spk2utt_reader.Key();
97  fmllr_accs.SetZero();
98  const vector<string> &uttlist = spk2utt_reader.Value();
99  for (vector<string>::const_iterator utt_itr = uttlist.begin(),
100  itr_end = uttlist.end(); utt_itr != itr_end; ++utt_itr) {
101  if (!feature_reader.HasKey(*utt_itr)) {
102  KALDI_WARN << "Did not find features for utterance " << *utt_itr;
103  continue;
104  }
105  if (!posteriors_reader.HasKey(*utt_itr)) {
106  KALDI_WARN << "Did not find posteriors for utterance "
107  << *utt_itr;
108  num_no_posterior++;
109  continue;
110  }
111  const Matrix<BaseFloat> &feats = feature_reader.Value(*utt_itr);
112  const Posterior &posterior = posteriors_reader.Value(*utt_itr);
113  if (static_cast<int32>(posterior.size()) != feats.NumRows()) {
114  KALDI_WARN << "Posteriors has wrong size " << (posterior.size())
115  << " vs. " << (feats.NumRows());
116  num_other_error++;
117  continue;
118  }
119 
120  BaseFloat file_like = 0.0, file_t = 0.0;
121  Posterior pdf_posterior;
122  ConvertPosteriorToPdfs(trans_model, posterior, &pdf_posterior);
123  for (size_t i = 0; i < posterior.size(); i++) {
124  for (size_t j = 0; j < pdf_posterior[i].size(); j++) {
125  int32 pdf_id = pdf_posterior[i][j].first;
126  BaseFloat prob = pdf_posterior[i][j].second;
127  file_like += fmllr_accs.AccumulateForGmm(regtree, am_gmm,
128  feats.Row(i), pdf_id,
129  prob);
130  file_t += prob;
131  }
132  }
133  KALDI_VLOG(2) << "Average like for this file is " << (file_like/file_t)
134  << " over " << file_t << " frames.";
135  tot_like += file_like;
136  tot_t += file_t;
137  num_done++;
138  if (num_done % 10 == 0)
139  KALDI_VLOG(1) << "Avg like per frame so far is "
140  << (tot_like / tot_t);
141  } // end looping over all utterances of the current speaker
142  BaseFloat objf_impr, t;
143  fmllr_accs.Update(regtree, opts, &fmllr_xforms, &objf_impr, &t);
144  KALDI_LOG << "fMLLR objf improvement for speaker " << spk << " is "
145  << (objf_impr/(t+1.0e-10)) << " per frame over " << t
146  << " frames.";
147  tot_objf_impr += objf_impr;
148  tot_t_objf += t;
149  fmllr_writer.Write(spk, fmllr_xforms);
150  } // end looping over speakers
151  } else { // per-utterance adaptation
152  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
153  for (; !feature_reader.Done(); feature_reader.Next()) {
154  string key = feature_reader.Key();
155  if (!posteriors_reader.HasKey(key)) {
156  KALDI_WARN << "Did not find posteriors for utterance "
157  << key;
158  num_no_posterior++;
159  continue;
160  }
161  const Matrix<BaseFloat> &feats = feature_reader.Value();
162  const Posterior &posterior = posteriors_reader.Value(key);
163 
164  if (static_cast<int32>(posterior.size()) != feats.NumRows()) {
165  KALDI_WARN << "Posteriors has wrong size " << (posterior.size())
166  << " vs. " << (feats.NumRows());
167  num_other_error++;
168  continue;
169  }
170 
171  num_done++;
172  BaseFloat file_like = 0.0, file_t = 0.0;
173  fmllr_accs.SetZero();
174  Posterior pdf_posterior;
175  ConvertPosteriorToPdfs(trans_model, posterior, &pdf_posterior);
176  for (size_t i = 0; i < posterior.size(); i++) {
177  for (size_t j = 0; j < pdf_posterior[i].size(); j++) {
178  int32 pdf_id = pdf_posterior[i][j].first;
179  BaseFloat prob = pdf_posterior[i][j].second;
180  file_like += fmllr_accs.AccumulateForGmm(regtree, am_gmm,
181  feats.Row(i), pdf_id,
182  prob);
183  file_t += prob;
184  }
185  }
186  KALDI_VLOG(2) << "Average like for this file is " << (file_like/file_t)
187  << " over " << file_t << " frames.";
188  tot_like += file_like;
189  tot_t += file_t;
190  if (num_done % 10 == 0)
191  KALDI_VLOG(1) << "Avg like per frame so far is "
192  << (tot_like / tot_t);
193  BaseFloat objf_impr, t;
194  fmllr_accs.Update(regtree, opts, &fmllr_xforms, &objf_impr, &t);
195  KALDI_LOG << "fMLLR objf improvement for utterance " << key << " is "
196  << (objf_impr/(t+1.0e-10)) << " per frame over " << t
197  << " frames.";
198  tot_objf_impr += objf_impr;
199  tot_t_objf += t;
200  fmllr_writer.Write(feature_reader.Key(), fmllr_xforms);
201  }
202  }
203  KALDI_LOG << "Done " << num_done << " files, " << num_no_posterior
204  << " with no posteriors, " << num_other_error
205  << " with other errors.";
206  KALDI_LOG << "Overall objf improvement from MLLR is " << (tot_objf_impr/tot_t_objf)
207  << " per frame " << " over " << tot_t_objf << " frames.";
208  KALDI_LOG << "Overall acoustic likelihood was " << (tot_like/tot_t)
209  << " over " << tot_t << " frames.";
210  return 0;
211  } catch(const std::exception &e) {
212  std::cerr << e.what();
213  return -1;
214  }
215 }
216 
void Read(std::istream &in, bool binary, const AmDiagGmm &am)
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int main(int argc, char *argv[])
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
void Register(OptionsItf *opts)
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void Write(const std::string &key, const T &value) const
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
An FMLLR (feature-space MLLR) transformation, also called CMLLR (constrained MLLR) is an affine trans...
std::istream & Stream()
Definition: kaldi-io.cc:826
int32 NumBaseclasses() const
Accessors (const)
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
Configuration variables for FMLLR transforms.
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 regression tree is a clustering of Gaussian densities in an acoustic model, such that the group of ...
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).
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
Class for computing the accumulators needed for the maximum-likelihood estimate of FMLLR transforms f...
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
BaseFloat AccumulateForGmm(const RegressionTree &regtree, const AmDiagGmm &am, const VectorBase< BaseFloat > &data, size_t pdf_index, BaseFloat weight)
Accumulate stats for a single GMM in the model; returns log likelihood.
void Update(const RegressionTree &regtree, const RegtreeFmllrOptions &opts, RegtreeFmllrDiagGmm *out_fmllr, BaseFloat *auxf_impr, BaseFloat *tot_t) const
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
void Init(size_t num_bclass, size_t dim)
#define KALDI_LOG
Definition: kaldi-error.h:153
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147