gmm-est-fmllr-raw.cc
Go to the documentation of this file.
1 // gmmbin/gmm-est-fmllr-raw.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 Posterior &post,
34  const Matrix<BaseFloat> &feats,
35  FmllrRawAccs *accs) {
36  Posterior pdf_post;
37  ConvertPosteriorToPdfs(trans_model, post, &pdf_post);
38  for (size_t t = 0; t < post.size(); t++) {
39  for (size_t i = 0; i < pdf_post[t].size(); i++) {
40  int32 pdf = pdf_post[t][i].first;
41  BaseFloat weight = pdf_post[t][i].second;
42  accs->AccumulateForGmm(am_gmm.GetPdf(pdf),
43  feats.Row(t), weight);
44  }
45  }
46 }
47 
48 
49 }
50 
51 int main(int argc, char *argv[]) {
52  try {
53  typedef kaldi::int32 int32;
54  using namespace kaldi;
55  const char *usage =
56  "Estimate fMLLR transforms in the space before splicing and linear transforms\n"
57  "such as LDA+MLLT, but using models in the space transformed by these transforms\n"
58  "Requires the original spliced features, and the full LDA+MLLT (or similar) matrix\n"
59  "including the 'rejected' rows (see the program get-full-lda-mat)\n"
60  "Usage: gmm-est-fmllr-raw [options] <model-in> <full-lda-mat-in> "
61  "<feature-rspecifier> <post-rspecifier> <transform-wspecifier>\n";
62 
63 
64  int32 raw_feat_dim = 13;
65  ParseOptions po(usage);
66  FmllrRawOptions opts;
67  std::string spk2utt_rspecifier;
68  po.Register("spk2utt", &spk2utt_rspecifier, "rspecifier for speaker to "
69  "utterance-list map");
70  po.Register("raw-feat-dim", &raw_feat_dim, "Dimension of raw features "
71  "prior to splicing");
72  opts.Register(&po);
73 
74  po.Read(argc, argv);
75 
76  if (po.NumArgs() != 5) {
77  po.PrintUsage();
78  exit(1);
79  }
80 
81  std::string model_rxfilename = po.GetArg(1),
82  full_lda_mat_rxfilename = po.GetArg(2),
83  feature_rspecifier = po.GetArg(3),
84  post_rspecifier = po.GetArg(4),
85  transform_wspecifier = po.GetArg(5);
86 
87  AmDiagGmm am_gmm;
88  TransitionModel trans_model;
89  {
90  bool binary;
91  Input ki(model_rxfilename, &binary);
92  trans_model.Read(ki.Stream(), binary);
93  am_gmm.Read(ki.Stream(), binary);
94  }
95 
96  Matrix<BaseFloat> full_lda_mat;
97  ReadKaldiObject(full_lda_mat_rxfilename, &full_lda_mat);
98 
99  RandomAccessPosteriorReader post_reader(post_rspecifier);
100  BaseFloatMatrixWriter transform_writer(transform_wspecifier);
101 
102  double tot_auxf_impr = 0.0, tot_count = 0.0;
103 
104  int32 num_done = 0, num_err = 0;
105  if (!spk2utt_rspecifier.empty()) { // Adapting per speaker
106  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
107  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
108 
109  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
110  FmllrRawAccs accs(raw_feat_dim, am_gmm.Dim(), full_lda_mat);
111  std::string spk = spk2utt_reader.Key();
112  const std::vector<std::string> &uttlist = spk2utt_reader.Value();
113  for (size_t i = 0; i < uttlist.size(); i++) {
114  std::string utt = uttlist[i];
115  if (!feature_reader.HasKey(utt)) {
116  KALDI_WARN << "Features not found for utterance " << utt;
117  num_err++;
118  continue;
119  }
120  if (!post_reader.HasKey(utt)) {
121  KALDI_WARN << "Posteriors not found for utterance " << utt;
122  num_err++;
123  continue;
124  }
125  const Matrix<BaseFloat> &feats = feature_reader.Value(utt);
126  const Posterior &post = post_reader.Value(utt);
127  if (static_cast<int32>(post.size()) != feats.NumRows()) {
128  KALDI_WARN << "Size mismatch between posteriors " << post.size()
129  << " and features " << feats.NumRows();
130  num_err++;
131  continue;
132  }
133 
134  AccStatsForUtterance(trans_model, am_gmm, post, feats, &accs);
135  num_done++;
136  }
137 
138  BaseFloat auxf_impr, count;
139  {
140  Matrix<BaseFloat> transform(raw_feat_dim, raw_feat_dim + 1);
141  transform.SetUnit();
142  accs.Update(opts, &transform, &auxf_impr, &count);
143  transform_writer.Write(spk, transform);
144  }
145  KALDI_LOG << "For speaker " << spk << ", auxf-impr from raw fMLLR is "
146  << (auxf_impr/count) << " over " << count << " frames.";
147  tot_auxf_impr += auxf_impr;
148  tot_count += count;
149  }
150  } else { // --spk2utt option not given -> adapt per utterance.
151  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
152  for (; !feature_reader.Done(); feature_reader.Next()) {
153  std::string utt = feature_reader.Key();
154  if (!post_reader.HasKey(utt)) {
155  KALDI_WARN << "Posteriors not found for utterance " << utt;
156  num_err++;
157  continue;
158  }
159  const Matrix<BaseFloat> &feats = feature_reader.Value();
160  const Posterior &post = post_reader.Value(utt);
161 
162  if (static_cast<int32>(post.size()) != feats.NumRows()) {
163  KALDI_WARN << "Size mismatch between posteriors " << post.size()
164  << " and features " << feats.NumRows();
165  num_err++;
166  continue;
167  }
168 
169  FmllrRawAccs accs(raw_feat_dim, am_gmm.Dim(), full_lda_mat);
170 
171  AccStatsForUtterance(trans_model, am_gmm, post, feats, &accs);
172 
173  BaseFloat auxf_impr, count;
174  {
175  Matrix<BaseFloat> transform(raw_feat_dim, raw_feat_dim + 1);
176  transform.SetUnit();
177  accs.Update(opts, &transform, &auxf_impr, &count);
178  transform_writer.Write(utt, transform);
179  }
180  KALDI_LOG << "For utterance " << utt << ", auxf-impr from raw fMLLR is "
181  << (auxf_impr/count) << " over " << count << " frames.";
182  tot_auxf_impr += auxf_impr;
183  tot_count += count;
184  num_done++;
185  }
186  }
187 
188  KALDI_LOG << "Processed " << num_done << " utterances, "
189  << num_err << " had errors.";
190  KALDI_LOG << "Overall raw-fMLLR auxf impr per frame is "
191  << (tot_auxf_impr / tot_count) << " over " << tot_count
192  << " frames.";
193  return (num_done != 0 ? 0 : 1);
194  } catch(const std::exception &e) {
195  std::cerr << e.what();
196  return -1;
197  }
198 }
199 
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].
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].
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
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 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)
int main(int argc, char *argv[])
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
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)
BaseFloat AccumulateForGmm(const DiagGmm &gmm, const VectorBase< BaseFloat > &data, BaseFloat weight)
Accumulate stats for a single GMM in the model; returns log likelihood.
Definition: fmllr-raw.cc:107
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
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147