gmm-est-fmllr-gpost.cc
Go to the documentation of this file.
1 // gmmbin/gmm-est-fmllr-gpost.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 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  "Estimate global fMLLR transforms, either per utterance or for the supplied\n"
59  "set of speakers (spk2utt option). Reads Gaussian-level posteriors. Writes\n"
60  "to a table of matrices.\n"
61  "Usage: gmm-est-fmllr-gpost [options] <model-in> "
62  "<feature-rspecifier> <gpost-rspecifier> <transform-wspecifier>\n";
63 
64  ParseOptions po(usage);
65  FmllrOptions fmllr_opts;
66  string spk2utt_rspecifier;
67  po.Register("spk2utt", &spk2utt_rspecifier, "rspecifier for speaker to "
68  "utterance-list map");
69  fmllr_opts.Register(&po);
70 
71  po.Read(argc, argv);
72 
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  trans_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 
95  double tot_impr = 0.0, tot_t = 0.0;
96 
97  BaseFloatMatrixWriter transform_writer(trans_wspecifier);
98 
99  int32 num_done = 0, num_no_gpost = 0, num_other_error = 0;
100  if (spk2utt_rspecifier != "") { // per-speaker adaptation
101  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
102  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
103 
104  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
105  FmllrDiagGmmAccs spk_stats(am_gmm.Dim());
106  string spk = spk2utt_reader.Key();
107  const vector<string> &uttlist = spk2utt_reader.Value();
108  for (size_t i = 0; i < uttlist.size(); i++) {
109  std::string utt = uttlist[i];
110  if (!feature_reader.HasKey(utt)) {
111  KALDI_WARN << "Did not find features for utterance " << utt;
112  num_other_error++;
113  continue;
114  }
115  if (!gpost_reader.HasKey(utt)) {
116  KALDI_WARN << "Did not find posteriors for utterance " << utt;
117  num_no_gpost++;
118  continue;
119  }
120  const Matrix<BaseFloat> &feats = feature_reader.Value(utt);
121  const GaussPost &gpost = gpost_reader.Value(utt);
122  if (static_cast<int32>(gpost.size()) != feats.NumRows()) {
123  KALDI_WARN << "GaussPost vector has wrong size " << (gpost.size())
124  << " vs. " << (feats.NumRows());
125  num_other_error++;
126  continue;
127  }
128 
129  AccumulateForUtterance(feats, gpost, trans_model, am_gmm, &spk_stats);
130 
131  num_done++;
132  } // end looping over all utterances of the current speaker
133 
134  BaseFloat impr, spk_tot_t;
135  { // Compute the transform and write it out.
136  Matrix<BaseFloat> transform(am_gmm.Dim(), am_gmm.Dim()+1);
137  transform.SetUnit();
138  spk_stats.Update(fmllr_opts, &transform, &impr, &spk_tot_t);
139  transform_writer.Write(spk, transform);
140  }
141  KALDI_LOG << "For speaker " << spk << ", auxf-impr from fMLLR is "
142  << (impr/spk_tot_t) << ", over " << spk_tot_t << " frames.\n";
143  tot_impr += impr;
144  tot_t += spk_tot_t;
145  } // end looping over speakers
146  } else { // per-utterance adaptation
147  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
148  for (; !feature_reader.Done(); feature_reader.Next()) {
149  string utt = feature_reader.Key();
150  if (!gpost_reader.HasKey(utt)) {
151  KALDI_WARN << "Did not find gposts for utterance "
152  << utt;
153  num_no_gpost++;
154  continue;
155  }
156  const Matrix<BaseFloat> &feats = feature_reader.Value();
157  const GaussPost &gpost = gpost_reader.Value(utt);
158 
159  if (static_cast<int32>(gpost.size()) != feats.NumRows()) {
160  KALDI_WARN << "GaussPost has wrong size " << (gpost.size())
161  << " vs. " << (feats.NumRows());
162  num_other_error++;
163  continue;
164  }
165  num_done++;
166 
167  FmllrDiagGmmAccs spk_stats(am_gmm.Dim());
168 
169  AccumulateForUtterance(feats, gpost, trans_model, am_gmm,
170  &spk_stats);
171 
172  BaseFloat impr, utt_tot_t;
173  { // Compute the transform and write it out.
174  Matrix<BaseFloat> transform(am_gmm.Dim(), am_gmm.Dim()+1);
175  transform.SetUnit();
176  spk_stats.Update(fmllr_opts, &transform, &impr, &utt_tot_t);
177  transform_writer.Write(utt, transform);
178  }
179  KALDI_LOG << "For utterancer " << utt << ", auxf-impr from fMLLR is "
180  << (impr/utt_tot_t) << ", over " << utt_tot_t << " frames.";
181  tot_impr += impr;
182  tot_t += utt_tot_t;
183  }
184  }
185 
186  KALDI_LOG << "Done " << num_done << " files, " << num_no_gpost
187  << " with no gposts, " << num_other_error << " with other errors.";
188  KALDI_LOG << "Overall fMLLR auxf impr per frame is "
189  << (tot_impr / tot_t) << " over " << tot_t << " frames.";
190  return (num_done != 0 ? 0 : 1);
191  } catch(const std::exception &e) {
192  std::cerr << e.what();
193  return -1;
194  }
195 }
196 
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].
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
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
int main(int argc, char *argv[])
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).
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