gmm-est-fmllr-raw-gpost.cc
Go to the documentation of this file.
1 // gmmbin/gmm-est-fmllr-raw-gpost.cc
2 
3 // Copyright 2013 Johns Hopkins University (author: Daniel Povey)
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 "base/kaldi-common.h"
22 #include "transform/fmllr-raw.h"
23 #include "gmm/am-diag-gmm.h"
24 #include "hmm/transition-model.h"
25 #include "util/common-utils.h"
26 #include "hmm/posterior.h"
27 
28 namespace kaldi {
29 
30 
31 void AccStatsForUtterance(const TransitionModel &trans_model,
32  const AmDiagGmm &am_gmm,
33  const GaussPost &gpost,
34  const Matrix<BaseFloat> &feats,
35  FmllrRawAccs *accs) {
36  for (size_t t = 0; t < gpost.size(); t++) {
37  for (size_t i = 0; i < gpost[t].size(); i++) {
38  int32 pdf = gpost[t][i].first;
39  const Vector<BaseFloat> &posterior(gpost[t][i].second);
40  accs->AccumulateFromPosteriors(am_gmm.GetPdf(pdf),
41  feats.Row(t), posterior);
42  }
43  }
44 }
45 
46 
47 }
48 
49 int main(int argc, char *argv[]) {
50  try {
51  typedef kaldi::int32 int32;
52  using namespace kaldi;
53  const char *usage =
54  "Estimate fMLLR transforms in the space before splicing and linear transforms\n"
55  "such as LDA+MLLT, but using models in the space transformed by these transforms\n"
56  "Requires the original spliced features, and the full LDA+MLLT (or similar) matrix\n"
57  "including the 'rejected' rows (see the program get-full-lda-mat). Reads in\n"
58  "Gaussian-level posteriors.\n"
59  "Usage: gmm-est-fmllr-raw-gpost [options] <model-in> <full-lda-mat-in> "
60  "<feature-rspecifier> <gpost-rspecifier> <transform-wspecifier>\n";
61 
62 
63  int32 raw_feat_dim = 13;
64  ParseOptions po(usage);
65  FmllrRawOptions opts;
66  std::string spk2utt_rspecifier;
67  po.Register("spk2utt", &spk2utt_rspecifier, "rspecifier for speaker to "
68  "utterance-list map");
69  po.Register("raw-feat-dim", &raw_feat_dim, "Dimension of raw features "
70  "prior to splicing");
71  opts.Register(&po);
72 
73  po.Read(argc, argv);
74 
75  if (po.NumArgs() != 5) {
76  po.PrintUsage();
77  exit(1);
78  }
79 
80  std::string model_rxfilename = po.GetArg(1),
81  full_lda_mat_rxfilename = po.GetArg(2),
82  feature_rspecifier = po.GetArg(3),
83  gpost_rspecifier = po.GetArg(4),
84  transform_wspecifier = po.GetArg(5);
85 
86  AmDiagGmm am_gmm;
87  TransitionModel trans_model;
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  Matrix<BaseFloat> full_lda_mat;
96  ReadKaldiObject(full_lda_mat_rxfilename, &full_lda_mat);
97 
98  RandomAccessGaussPostReader gpost_reader(gpost_rspecifier);
99  BaseFloatMatrixWriter transform_writer(transform_wspecifier);
100 
101  double tot_auxf_impr = 0.0, tot_count = 0.0;
102 
103  int32 num_done = 0, num_err = 0;
104  if (!spk2utt_rspecifier.empty()) { // Adapting per speaker
105  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
106  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
107 
108  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
109  FmllrRawAccs accs(raw_feat_dim, am_gmm.Dim(), full_lda_mat);
110  std::string spk = spk2utt_reader.Key();
111  const std::vector<std::string> &uttlist = spk2utt_reader.Value();
112  for (size_t i = 0; i < uttlist.size(); i++) {
113  std::string utt = uttlist[i];
114  if (!feature_reader.HasKey(utt)) {
115  KALDI_WARN << "Features not found for utterance " << utt;
116  num_err++;
117  continue;
118  }
119  if (!gpost_reader.HasKey(utt)) {
120  KALDI_WARN << "Gaussian-level posteriors not found for utterance " << utt;
121  num_err++;
122  continue;
123  }
124  const Matrix<BaseFloat> &feats = feature_reader.Value(utt);
125  const GaussPost &gpost = gpost_reader.Value(utt);
126  if (static_cast<int32>(gpost.size()) != feats.NumRows()) {
127  KALDI_WARN << "Size mismatch between gposteriors " << gpost.size()
128  << " and features " << feats.NumRows();
129  num_err++;
130  continue;
131  }
132 
133  AccStatsForUtterance(trans_model, am_gmm, gpost, feats, &accs);
134  num_done++;
135  }
136 
137  BaseFloat auxf_impr, count;
138  {
139  Matrix<BaseFloat> transform(raw_feat_dim, raw_feat_dim + 1);
140  transform.SetUnit();
141  accs.Update(opts, &transform, &auxf_impr, &count);
142  transform_writer.Write(spk, transform);
143  }
144  KALDI_LOG << "For speaker " << spk << ", auxf-impr from raw fMLLR is "
145  << (auxf_impr/count) << " over " << count << " frames.";
146  tot_auxf_impr += auxf_impr;
147  tot_count += count;
148  }
149  } else { // --spk2utt option not given -> adapt per utterance.
150  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
151  for (; !feature_reader.Done(); feature_reader.Next()) {
152  std::string utt = feature_reader.Key();
153  if (!gpost_reader.HasKey(utt)) {
154  KALDI_WARN << "Gaussian-level posteriors not found for utterance " << utt;
155  num_err++;
156  continue;
157  }
158  const Matrix<BaseFloat> &feats = feature_reader.Value();
159  const GaussPost &gpost = gpost_reader.Value(utt);
160 
161  if (static_cast<int32>(gpost.size()) != feats.NumRows()) {
162  KALDI_WARN << "Size mismatch between posteriors " << gpost.size()
163  << " and features " << feats.NumRows();
164  num_err++;
165  continue;
166  }
167 
168  FmllrRawAccs accs(raw_feat_dim, am_gmm.Dim(), full_lda_mat);
169 
170  AccStatsForUtterance(trans_model, am_gmm, gpost, feats, &accs);
171 
172  BaseFloat auxf_impr, count;
173  {
174  Matrix<BaseFloat> transform(raw_feat_dim, raw_feat_dim + 1);
175  transform.SetUnit();
176  accs.Update(opts, &transform, &auxf_impr, &count);
177  transform_writer.Write(utt, transform);
178  }
179  KALDI_LOG << "For utterance " << utt << ", auxf-impr from raw fMLLR is "
180  << (auxf_impr/count) << " over " << count << " frames.";
181  tot_auxf_impr += auxf_impr;
182  tot_count += count;
183  num_done++;
184  }
185  }
186 
187  KALDI_LOG << "Processed " << num_done << " utterances, "
188  << num_err << " had errors.";
189  KALDI_LOG << "Overall raw-fMLLR auxf impr per frame is "
190  << (tot_auxf_impr / tot_count) << " over " << tot_count
191  << " 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].
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 AccumulateFromPosteriors(const DiagGmm &gmm, const VectorBase< BaseFloat > &data, const VectorBase< BaseFloat > &posteriors)
Accumulate stats for a GMM, given supplied posteriors.
Definition: fmllr-raw.cc:246
void SetUnit()
Sets to zero, except ones along diagonal [for non-square matrices too].
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
const size_t count
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
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)
void Read(std::istream &is, bool binary)
void Register(OptionsItf *opts)
Definition: fmllr-raw.h:73
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).
DiagGmm & GetPdf(int32 pdf_index)
Accessors.
Definition: am-diag-gmm.h:119
A class representing a vector.
Definition: kaldi-vector.h:406
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void AccStatsForUtterance(const TransitionModel &trans_model, const AmDiagGmm &am_gmm, const GaussPost &gpost, const Matrix< BaseFloat > &feats, FmllrRawAccs *accs)
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
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147