gmm-est-fmllr-global.cc
Go to the documentation of this file.
1 // gmmbin/gmm-est-fmllr-global.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation; Saarland University
4 // 2013-2014 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 
35 
36 int main(int argc, char *argv[]) {
37  try {
38  typedef kaldi::int32 int32;
39  using namespace kaldi;
40  const char *usage =
41  "Estimate global fMLLR transforms, either per utterance or for the supplied\n"
42  "set of speakers (spk2utt option). This version is for when you have a single\n"
43  "global GMM, e.g. a UBM. Writes to a table of matrices.\n"
44  "Usage: gmm-est-fmllr-global [options] <gmm-in> <feature-rspecifier> "
45  "<transform-wspecifier>\n"
46  "e.g.: gmm-est-fmllr-global 1.ubm scp:feats.scp ark:trans.1\n";
47 
48  ParseOptions po(usage);
49  FmllrOptions fmllr_opts;
50  string spk2utt_rspecifier;
51  string gselect_rspecifier;
52  po.Register("spk2utt", &spk2utt_rspecifier, "rspecifier for speaker to "
53  "utterance-list map");
54  po.Register("gselect", &gselect_rspecifier, "rspecifier for "
55  "Gaussian-selection information");
56  fmllr_opts.Register(&po);
57 
58  po.Read(argc, argv);
59 
60  if (po.NumArgs() != 3) {
61  po.PrintUsage();
62  exit(1);
63  }
64 
65  string gmm_rxfilename = po.GetArg(1),
66  feature_rspecifier = po.GetArg(2),
67  trans_wspecifier = po.GetArg(3);
68 
69  DiagGmm gmm;
70  ReadKaldiObject(gmm_rxfilename, &gmm);
71 
72  double tot_impr = 0.0, tot_t = 0.0;
73 
74  BaseFloatMatrixWriter transform_writer(trans_wspecifier);
75  RandomAccessInt32VectorVectorReader gselect_reader(gselect_rspecifier);
76 
77  int32 num_done = 0, num_err = 0;
78  if (spk2utt_rspecifier != "") { // per-speaker adaptation
79  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
80  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
81 
82  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
83  FmllrDiagGmmAccs spk_stats(gmm.Dim(), fmllr_opts);
84  string spk = spk2utt_reader.Key();
85  const vector<string> &uttlist = spk2utt_reader.Value();
86  for (size_t i = 0; i < uttlist.size(); i++) {
87  std::string utt = uttlist[i];
88  if (!feature_reader.HasKey(utt)) {
89  KALDI_WARN << "Did not find features for utterance " << utt;
90  num_err++;
91  continue;
92  }
93  const Matrix<BaseFloat> &feats = feature_reader.Value(utt);
94 
95  if (gselect_rspecifier == "") {
96  for (size_t i = 0; i < feats.NumRows(); i++)
97  spk_stats.AccumulateForGmm(gmm, feats.Row(i), 1.0);
98  } else {
99  if (!gselect_reader.HasKey(utt) ||
100  gselect_reader.Value(utt).size() != feats.NumRows()) {
101  KALDI_LOG << "No gselect information for utterance " << utt
102  << " (or wrong size)";
103  num_err++;
104  continue;
105  }
106  const std::vector<std::vector<int32> > &gselect =
107  gselect_reader.Value(utt);
108  for (size_t i = 0; i < feats.NumRows(); i++)
109  spk_stats.AccumulateForGmmPreselect(gmm, gselect[i],
110  feats.Row(i), 1.0);
111  }
112  num_done++;
113  } // end looping over all utterances of the current speaker
114 
115  BaseFloat impr, spk_tot_t;
116  { // Compute the transform and write it out.
117  Matrix<BaseFloat> transform(gmm.Dim(), gmm.Dim()+1);
118  transform.SetUnit();
119  spk_stats.Update(fmllr_opts, &transform, &impr, &spk_tot_t);
120  transform_writer.Write(spk, transform);
121  }
122  KALDI_LOG << "For speaker " << spk << ", auxf-impr from fMLLR is "
123  << (impr/spk_tot_t) << ", over " << spk_tot_t << " frames.";
124  tot_impr += impr;
125  tot_t += spk_tot_t;
126  } // end looping over speakers
127  } else { // per-utterance adaptation
128  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
129  for (; !feature_reader.Done(); feature_reader.Next()) {
130  string utt = feature_reader.Key();
131  const Matrix<BaseFloat> &feats = feature_reader.Value();
132 
133  num_done++;
134 
135  FmllrDiagGmmAccs spk_stats(gmm.Dim(), fmllr_opts);
136 
137  if (gselect_rspecifier == "") {
138  for (size_t i = 0; i < feats.NumRows(); i++)
139  spk_stats.AccumulateForGmm(gmm, feats.Row(i), 1.0);
140  } else {
141  if (!gselect_reader.HasKey(utt) ||
142  gselect_reader.Value(utt).size() != feats.NumRows()) {
143  KALDI_LOG << "No gselect information for utterance " << utt
144  << " (or wrong size)";
145  num_err++;
146  continue;
147  }
148  const std::vector<std::vector<int32> > &gselect =
149  gselect_reader.Value(utt);
150  for (size_t i = 0; i < feats.NumRows(); i++)
151  spk_stats.AccumulateForGmmPreselect(gmm, gselect[i],
152  feats.Row(i), 1.0);
153  }
154  BaseFloat impr, utt_tot_t;
155  { // Compute the transform and write it out.
156  Matrix<BaseFloat> transform(gmm.Dim(), gmm.Dim()+1);
157  transform.SetUnit();
158  spk_stats.Update(fmllr_opts, &transform, &impr, &utt_tot_t);
159  transform_writer.Write(utt, transform);
160  }
161  KALDI_LOG << "For utterance " << utt << ", auxf-impr from fMLLR is "
162  << (impr/utt_tot_t) << ", over " << utt_tot_t << " frames.";
163  tot_impr += impr;
164  tot_t += utt_tot_t;
165  }
166  }
167  KALDI_LOG << "Done " << num_done << " files, "
168  << num_err << " with errors.";
169  KALDI_LOG << "Overall fMLLR auxf impr per frame is "
170  << (tot_impr / tot_t) << " over " << tot_t << " frames.";
171  return (num_done != 0 ? 0 : 1);
172  } catch(const std::exception &e) {
173  std::cerr << e.what();
174  return -1;
175  }
176 }
177 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: diag-gmm.h:74
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
This does not work with multiple feature transforms.
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 SetUnit()
Sets to zero, except ones along diagonal [for non-square matrices too].
int main(int argc, char *argv[])
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
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 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 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).
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
Definition for Gaussian Mixture Model with diagonal covariances.
Definition: diag-gmm.h:42
#define KALDI_LOG
Definition: kaldi-error.h:153
BaseFloat AccumulateForGmm(const DiagGmm &gmm, const VectorBase< BaseFloat > &data, BaseFloat weight)
Accumulate stats for a single GMM in the model; returns log likelihood.