gmm-est-fmllr.cc
Go to the documentation of this file.
1 // gmmbin/gmm-est-fmllr.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation; Saarland University
4 // 2013 Johns Hopkins University (author: Daniel Povey)
5 // 2014 Guoguo Chen
6 
7 // See ../../COPYING for clarification regarding multiple authors
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
17 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
18 // MERCHANTABLITY OR NON-INFRINGEMENT.
19 // See the Apache 2 License for the specific language governing permissions and
20 // limitations under the License.
21 
22 #include <string>
23 using std::string;
24 #include <vector>
25 using std::vector;
26 
27 #include "base/kaldi-common.h"
28 #include "util/common-utils.h"
29 #include "gmm/am-diag-gmm.h"
30 #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  "Estimate global fMLLR transforms, either per utterance or for the supplied\n"
61  "set of speakers (spk2utt option). Reads posteriors (on transition-ids). Writes\n"
62  "to a table of matrices.\n"
63  "Usage: gmm-est-fmllr [options] <model-in> "
64  "<feature-rspecifier> <post-rspecifier> <transform-wspecifier>\n";
65 
66  ParseOptions po(usage);
67  FmllrOptions fmllr_opts;
68  string spk2utt_rspecifier;
69  po.Register("spk2utt", &spk2utt_rspecifier, "rspecifier for speaker to "
70  "utterance-list map");
71  fmllr_opts.Register(&po);
72 
73  po.Read(argc, argv);
74 
75  if (po.NumArgs() != 4) {
76  po.PrintUsage();
77  exit(1);
78  }
79 
80  string
81  model_rxfilename = po.GetArg(1),
82  feature_rspecifier = po.GetArg(2),
83  post_rspecifier = po.GetArg(3),
84  trans_wspecifier = po.GetArg(4);
85 
86  TransitionModel trans_model;
87  AmDiagGmm am_gmm;
88  {
89  bool binary;
90  Input ki(model_rxfilename, &binary);
91  trans_model.Read(ki.Stream(), binary);
92  am_gmm.Read(ki.Stream(), binary);
93  }
94 
95  RandomAccessPosteriorReader post_reader(post_rspecifier);
96 
97  double tot_impr = 0.0, tot_t = 0.0;
98 
99  BaseFloatMatrixWriter transform_writer(trans_wspecifier);
100 
101  int32 num_done = 0, num_no_post = 0, num_other_error = 0;
102  if (spk2utt_rspecifier != "") { // per-speaker adaptation
103  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
104  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
105 
106  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
107  FmllrDiagGmmAccs spk_stats(am_gmm.Dim(), fmllr_opts);
108  string spk = spk2utt_reader.Key();
109  const vector<string> &uttlist = spk2utt_reader.Value();
110  for (size_t i = 0; i < uttlist.size(); i++) {
111  std::string utt = uttlist[i];
112  if (!feature_reader.HasKey(utt)) {
113  KALDI_WARN << "Did not find features for utterance " << utt;
114  num_other_error++;
115  continue;
116  }
117  if (!post_reader.HasKey(utt)) {
118  KALDI_WARN << "Did not find posteriors for utterance " << utt;
119  num_no_post++;
120  continue;
121  }
122  const Matrix<BaseFloat> &feats = feature_reader.Value(utt);
123  const Posterior &post = post_reader.Value(utt);
124  if (static_cast<int32>(post.size()) != feats.NumRows()) {
125  KALDI_WARN << "Posterior vector has wrong size " << (post.size())
126  << " vs. " << (feats.NumRows());
127  num_other_error++;
128  continue;
129  }
130 
131  AccumulateForUtterance(feats, post, trans_model, am_gmm, &spk_stats);
132 
133  num_done++;
134  } // end looping over all utterances of the current speaker
135 
136  BaseFloat impr, spk_tot_t;
137  { // Compute the transform and write it out.
138  Matrix<BaseFloat> transform(am_gmm.Dim(), am_gmm.Dim()+1);
139  transform.SetUnit();
140  spk_stats.Update(fmllr_opts, &transform, &impr, &spk_tot_t);
141  transform_writer.Write(spk, transform);
142  }
143  KALDI_LOG << "For speaker " << spk << ", auxf-impr from fMLLR is "
144  << (impr/spk_tot_t) << ", over " << spk_tot_t << " frames.";
145  tot_impr += impr;
146  tot_t += spk_tot_t;
147  } // end looping over speakers
148  } else { // per-utterance adaptation
149  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
150  for (; !feature_reader.Done(); feature_reader.Next()) {
151  string utt = feature_reader.Key();
152  if (!post_reader.HasKey(utt)) {
153  KALDI_WARN << "Did not find posts for utterance "
154  << utt;
155  num_no_post++;
156  continue;
157  }
158  const Matrix<BaseFloat> &feats = feature_reader.Value();
159  const Posterior &post = post_reader.Value(utt);
160 
161  if (static_cast<int32>(post.size()) != feats.NumRows()) {
162  KALDI_WARN << "Posterior has wrong size " << (post.size())
163  << " vs. " << (feats.NumRows());
164  num_other_error++;
165  continue;
166  }
167  num_done++;
168 
169  FmllrDiagGmmAccs spk_stats(am_gmm.Dim(), fmllr_opts);
170 
171  AccumulateForUtterance(feats, post, trans_model, am_gmm,
172  &spk_stats);
173 
174  BaseFloat impr, utt_tot_t;
175  { // Compute the transform and write it out.
176  Matrix<BaseFloat> transform(am_gmm.Dim(), am_gmm.Dim()+1);
177  transform.SetUnit();
178  spk_stats.Update(fmllr_opts, &transform, &impr, &utt_tot_t);
179  transform_writer.Write(utt, transform);
180  }
181  KALDI_LOG << "For utterance " << utt << ", auxf-impr from fMLLR is "
182  << (impr/utt_tot_t) << ", over " << utt_tot_t << " frames.";
183  tot_impr += impr;
184  tot_t += utt_tot_t;
185  }
186  }
187 
188  KALDI_LOG << "Done " << num_done << " files, " << num_no_post
189  << " with no posts, " << num_other_error << " with other errors.";
190  KALDI_LOG << "Overall fMLLR auxf impr per frame is "
191  << (tot_impr / tot_t) << " over " << tot_t << " frames.";
192  return (num_done != 0 ? 0 : 1);
193  } catch(const std::exception &e) {
194  std::cerr << e.what();
195  return -1;
196  }
197 }
198 
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 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
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
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)
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)
void Register(OptionsItf *opts)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
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