gmm-est-lvtln-trans.cc
Go to the documentation of this file.
1 // gmmbin/gmm-est-lvtln-trans.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation; Saarland University
4 // 2014 Johns Hopkins University (author: Daniel Povey)
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 <string>
22 using std::string;
23 #include <vector>
24 using std::vector;
25 
26 #include "base/kaldi-common.h"
27 #include "util/common-utils.h"
28 #include "gmm/am-diag-gmm.h"
29 #include "hmm/transition-model.h"
30 #include "transform/lvtln.h"
31 #include "hmm/posterior.h"
32 
33 namespace kaldi {
35  const GaussPost &gpost,
36  const AmDiagGmm &am_gmm,
37  FmllrDiagGmmAccs *spk_stats) {
38  for (size_t i = 0; i < gpost.size(); i++) {
39  for (size_t j = 0; j < gpost[i].size(); j++) {
40  int32 pdf_id = gpost[i][j].first;
41  const Vector<BaseFloat> &posterior(gpost[i][j].second);
42  spk_stats->AccumulateFromPosteriors(am_gmm.GetPdf(pdf_id),
43  feats.Row(i), posterior);
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 linear-VTLN transforms, either per utterance or for "
57  "the supplied set of speakers (spk2utt option). Reads posteriors. \n"
58  "Usage: gmm-est-lvtln-trans [options] <model-in> <lvtln-in> "
59  "<feature-rspecifier> <gpost-rspecifier> <lvtln-trans-wspecifier> [<warp-wspecifier>]\n";
60 
61  ParseOptions po(usage);
62  string spk2utt_rspecifier;
63  BaseFloat logdet_scale = 1.0;
64  std::string norm_type = "offset";
65  po.Register("norm-type", &norm_type, "type of fMLLR applied (\"offset\"|\"none\"|\"diag\")");
66  po.Register("spk2utt", &spk2utt_rspecifier, "rspecifier for speaker to "
67  "utterance-list map");
68  po.Register("logdet-scale", &logdet_scale, "Scale on log-determinant term in auxiliary function");
69 
70  po.Read(argc, argv);
71 
72  if (po.NumArgs() < 5 || po.NumArgs() > 6) {
73  po.PrintUsage();
74  exit(1);
75  }
76 
77  string
78  model_rxfilename = po.GetArg(1),
79  lvtln_rxfilename = po.GetArg(2),
80  feature_rspecifier = po.GetArg(3),
81  gpost_rspecifier = po.GetArg(4),
82  trans_wspecifier = po.GetArg(5),
83  warp_wspecifier = po.GetOptArg(6);
84 
85  AmDiagGmm am_gmm;
86  {
87  bool binary;
88  Input ki(model_rxfilename, &binary);
89  TransitionModel trans_model;
90  trans_model.Read(ki.Stream(), binary);
91  am_gmm.Read(ki.Stream(), binary);
92  }
93  LinearVtln lvtln;
94  ReadKaldiObject(lvtln_rxfilename, &lvtln);
95 
96 
97  RandomAccessGaussPostReader gpost_reader(gpost_rspecifier);
98 
99  double tot_lvtln_impr = 0.0, tot_t = 0.0;
100 
101  BaseFloatMatrixWriter transform_writer(trans_wspecifier);
102 
103  BaseFloatWriter warp_writer(warp_wspecifier);
104 
105  std::vector<int32> class_counts(lvtln.NumClasses(), 0);
106  int32 num_done = 0, num_no_gpost = 0, num_other_error = 0;
107  if (spk2utt_rspecifier != "") { // per-speaker adaptation
108  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
109  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
110 
111  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
112  FmllrDiagGmmAccs spk_stats(lvtln.Dim());
113  string spk = spk2utt_reader.Key();
114  const vector<string> &uttlist = spk2utt_reader.Value();
115  for (size_t i = 0; i < uttlist.size(); i++) {
116  std::string utt = uttlist[i];
117  if (!feature_reader.HasKey(utt)) {
118  KALDI_WARN << "Did not find features for utterance " << utt;
119  continue;
120  }
121  if (!gpost_reader.HasKey(utt)) {
122  KALDI_WARN << "Did not find posteriors for utterance " << utt;
123  num_no_gpost++;
124  continue;
125  }
126  const Matrix<BaseFloat> &feats = feature_reader.Value(utt);
127  const GaussPost &gpost = gpost_reader.Value(utt);
128  if (static_cast<int32>(gpost.size()) != feats.NumRows()) {
129  KALDI_WARN << "GauPost vector has wrong size " << gpost.size()
130  << " vs. " << feats.NumRows();
131  num_other_error++;
132  continue;
133  }
134 
135  AccumulateForUtterance(feats, gpost, am_gmm, &spk_stats);
136 
137  num_done++;
138  } // end looping over all utterances of the current speaker
139 
140  BaseFloat impr, spk_tot_t;
141  { // Compute the transform and write it out.
142  Matrix<BaseFloat> transform(lvtln.Dim(), lvtln.Dim()+1);
143  int32 class_idx;
144  lvtln.ComputeTransform(spk_stats,
145  norm_type,
146  logdet_scale,
147  &transform,
148  &class_idx,
149  NULL,
150  &impr,
151  &spk_tot_t);
152  class_counts[class_idx]++;
153  transform_writer.Write(spk, transform);
154  if (warp_wspecifier != "")
155  warp_writer.Write(spk, lvtln.GetWarp(class_idx));
156  }
157  KALDI_LOG << "For speaker " << spk << ", auxf-impr from LVTLN is "
158  << (impr/spk_tot_t) << ", over " << spk_tot_t << " frames.";
159  tot_lvtln_impr += impr;
160  tot_t += spk_tot_t;
161  } // end looping over speakers
162  } else { // per-utterance adaptation
163  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
164  for (; !feature_reader.Done(); feature_reader.Next()) {
165  string utt = feature_reader.Key();
166  if (!gpost_reader.HasKey(utt)) {
167  KALDI_WARN << "Did not find gposts for utterance "
168  << utt;
169  num_no_gpost++;
170  continue;
171  }
172  const Matrix<BaseFloat> &feats = feature_reader.Value();
173  const GaussPost &gpost = gpost_reader.Value(utt);
174 
175  if (static_cast<int32>(gpost.size()) != feats.NumRows()) {
176  KALDI_WARN << "GauPost has wrong size " << gpost.size()
177  << " vs. " << feats.NumRows();
178  num_other_error++;
179  continue;
180  }
181  num_done++;
182 
183  FmllrDiagGmmAccs spk_stats(lvtln.Dim());
184 
185  AccumulateForUtterance(feats, gpost, am_gmm,
186  &spk_stats);
187  BaseFloat impr, utt_tot_t = spk_stats.beta_;
188  { // Compute the transform and write it out.
189  Matrix<BaseFloat> transform(lvtln.Dim(), lvtln.Dim()+1);
190  int32 class_idx;
191  lvtln.ComputeTransform(spk_stats,
192  norm_type,
193  logdet_scale,
194  &transform,
195  &class_idx,
196  NULL,
197  &impr,
198  &utt_tot_t);
199  class_counts[class_idx]++;
200  transform_writer.Write(utt, transform);
201  if (warp_wspecifier != "")
202  warp_writer.Write(utt, lvtln.GetWarp(class_idx));
203  }
204 
205  KALDI_LOG << "For utterance " << utt << ", auxf-impr from LVTLN is "
206  << (impr/utt_tot_t) << ", over " << utt_tot_t << " frames.";
207  tot_lvtln_impr += impr;
208  tot_t += utt_tot_t;
209  }
210  }
211 
212  {
213  std::ostringstream s;
214  for (size_t i = 0; i < class_counts.size(); i++)
215  s << ' ' << class_counts[i];
216  KALDI_LOG << "Distribution of classes is: " << s.str();
217  }
218 
219  KALDI_LOG << "Done " << num_done << " files, " << num_no_gpost
220  << " with no gposts, " << num_other_error << " with other errors.";
221  KALDI_LOG << "Overall LVTLN auxf impr per frame is "
222  << (tot_lvtln_impr / tot_t) << " over " << tot_t << " frames.";
223  return (num_done == 0 ? 1 : 0);
224  } catch(const std::exception &e) {
225  std::cerr << e.what();
226  return -1;
227  }
228 }
229 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
BaseFloat GetWarp(int32 i) const
Definition: lvtln.cc:180
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
This does not work with multiple feature transforms.
void AccumulateFromPosteriors(const DiagGmm &gmm, const VectorBase< BaseFloat > &data, const VectorBase< BaseFloat > &posteriors)
Accumulate stats for a GMM, given supplied posteriors.
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)
int32 NumClasses() const
Definition: lvtln.h:78
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
int32 Dim() const
Definition: lvtln.h:77
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 ComputeTransform(const FmllrDiagGmmAccs &accs, std::string norm_type, BaseFloat logdet_scale, MatrixBase< BaseFloat > *Ws, int32 *class_idx, BaseFloat *logdet_out, BaseFloat *objf_impr=NULL, BaseFloat *count=NULL)
Compute the transform for the speaker.
Definition: lvtln.cc:97
void Read(std::istream &is, bool binary)
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)
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
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
std::string GetOptArg(int param) const
int main(int argc, char *argv[])