gmm-est-regtree-fmllr-ali.cc
Go to the documentation of this file.
1 // gmmbin/gmm-est-regtree-fmllr-ali.cc
2 
3 // Copyright 2009-2011 Saarland University; Microsoft Corporation
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #include <string>
21 using std::string;
22 #include <vector>
23 using std::vector;
24 
25 #include "base/kaldi-common.h"
26 #include "util/common-utils.h"
27 #include "gmm/am-diag-gmm.h"
28 #include "hmm/transition-model.h"
30 
31 int main(int argc, char *argv[]) {
32  try {
33  typedef kaldi::int32 int32;
34  using namespace kaldi;
35  const char *usage =
36  "Compute FMLLR transforms per-utterance (default) or per-speaker for "
37  "the supplied set of speakers (spk2utt option). Note: writes RegtreeFmllrDiagGmm objects\n"
38  "Usage: gmm-est-regtree-fmllr-ali [options] <model-in> <feature-rspecifier> "
39  "<alignments-rspecifier> <regression-tree> <transforms-wspecifier>\n";
40 
41  ParseOptions po(usage);
42  string spk2utt_rspecifier;
43  bool binary = true;
44  po.Register("spk2utt", &spk2utt_rspecifier, "rspecifier for speaker to "
45  "utterance-list map");
46  po.Register("binary", &binary, "Write output in binary mode");
47  // register other modules
49  opts.Register(&po);
50 
51  po.Read(argc, argv);
52 
53  if (po.NumArgs() != 5) {
54  po.PrintUsage();
55  exit(1);
56  }
57 
58  string model_filename = po.GetArg(1),
59  feature_rspecifier = po.GetArg(2),
60  alignments_rspecifier = po.GetArg(3),
61  regtree_filename = po.GetArg(4),
62  xforms_wspecifier = po.GetArg(5);
63 
64  RandomAccessInt32VectorReader alignments_reader(alignments_rspecifier);
65  RegtreeFmllrDiagGmmWriter fmllr_writer(xforms_wspecifier);
66 
67  AmDiagGmm am_gmm;
68  TransitionModel trans_model;
69  {
70  bool binary;
71  Input ki(model_filename, &binary);
72  trans_model.Read(ki.Stream(), binary);
73  am_gmm.Read(ki.Stream(), binary);
74  }
75  RegressionTree regtree;
76  {
77  bool binary;
78  Input in(regtree_filename, &binary);
79  regtree.Read(in.Stream(), binary, am_gmm);
80  }
81 
82  RegtreeFmllrDiagGmm fmllr_xforms;
83  RegtreeFmllrDiagGmmAccs fmllr_accs;
84  fmllr_accs.Init(regtree.NumBaseclasses(), am_gmm.Dim());
85 
86  double tot_like = 0.0;
87  kaldi::int64 tot_t = 0;
88 
89  int32 num_done = 0, num_no_alignment = 0, num_other_error = 0;
90  double tot_objf_impr = 0.0, tot_t_objf = 0.0;
91  if (spk2utt_rspecifier != "") { // per-speaker adaptation
92  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
93  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
94  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
95  string spk = spk2utt_reader.Key();
96  fmllr_accs.SetZero();
97  const vector<string> &uttlist = spk2utt_reader.Value();
98  for (vector<string>::const_iterator utt_itr = uttlist.begin(),
99  itr_end = uttlist.end(); utt_itr != itr_end; ++utt_itr) {
100  if (!feature_reader.HasKey(*utt_itr)) {
101  KALDI_WARN << "Did not find features for utterance " << *utt_itr;
102  continue;
103  }
104  if (!alignments_reader.HasKey(*utt_itr)) {
105  KALDI_WARN << "Did not find aligned transcription for utterance "
106  << *utt_itr;
107  num_no_alignment++;
108  continue;
109  }
110  const Matrix<BaseFloat> &feats = feature_reader.Value(*utt_itr);
111  const vector<int32> &alignment = alignments_reader.Value(*utt_itr);
112  if (static_cast<int32>(alignment.size()) != feats.NumRows()) {
113  KALDI_WARN << "Alignments has wrong size " << (alignment.size())
114  << " vs. " << (feats.NumRows());
115  num_other_error++;
116  continue;
117  }
118 
119  BaseFloat file_like = 0.0;
120  for (size_t i = 0; i < alignment.size(); i++) {
121  int32 pdf_id = trans_model.TransitionIdToPdf(alignment[i]);
122  file_like += fmllr_accs.AccumulateForGmm(regtree, am_gmm,
123  feats.Row(i), pdf_id, 1.0);
124  }
125  KALDI_VLOG(2) << "Average like for this file is " << (file_like
126  / alignment.size()) << " over " << alignment.size()
127  << " frames.\n";
128  tot_like += file_like;
129  tot_t += alignment.size();
130  num_done++;
131  if (num_done % 10 == 0) KALDI_VLOG(1)
132  << "Avg like per frame so far is " << (tot_like / tot_t) << '\n';
133  } // end looping over all utterances of the current speaker
134  BaseFloat objf_impr, t;
135  fmllr_accs.Update(regtree, opts, &fmllr_xforms, &objf_impr, &t);
136  KALDI_LOG << "fMLLR objf improvement for speaker " << spk << " is "
137  << (objf_impr/(t+1.0e-10)) << " per frame over " << t
138  << " frames.";
139  tot_objf_impr += objf_impr;
140  tot_t_objf += t;
141  fmllr_writer.Write(spk, fmllr_xforms);
142  } // end looping over speakers
143  } else { // per-utterance adaptation
144  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
145  for (; !feature_reader.Done(); feature_reader.Next()) {
146  string key = feature_reader.Key();
147  if (!alignments_reader.HasKey(key)) {
148  KALDI_WARN << "Did not find aligned transcription for utterance "
149  << key;
150  num_no_alignment++;
151  continue;
152  }
153  const Matrix<BaseFloat> &feats = feature_reader.Value();
154  const vector<int32> &alignment = alignments_reader.Value(key);
155 
156  if (static_cast<int32>(alignment.size()) != feats.NumRows()) {
157  KALDI_WARN << "Alignments has wrong size " << (alignment.size())
158  << " vs. " << (feats.NumRows());
159  num_other_error++;
160  continue;
161  }
162 
163  num_done++;
164  BaseFloat file_like = 0.0;
165  fmllr_accs.SetZero();
166  for (size_t i = 0; i < alignment.size(); i++) {
167  int32 pdf_id = trans_model.TransitionIdToPdf(alignment[i]);
168  file_like += fmllr_accs.AccumulateForGmm(regtree, am_gmm,
169  feats.Row(i), pdf_id, 1.0);
170  }
171  KALDI_VLOG(2) << "Average like for this file is " << (file_like
172  / alignment.size()) << " over " << alignment.size() << " frames.";
173  tot_like += file_like;
174  tot_t += alignment.size();
175  if (num_done % 10 == 0) KALDI_VLOG(1)
176  << "Avg like per frame so far is " << (tot_like / tot_t);
177  BaseFloat objf_impr, t;
178  fmllr_accs.Update(regtree, opts, &fmllr_xforms, &objf_impr, &t);
179  KALDI_LOG << "fMLLR objf improvement for utterance " << key << " is "
180  << (objf_impr/(t+1.0e-10)) << " per frame over " << t
181  << " frames.";
182  tot_objf_impr += objf_impr;
183  tot_t_objf += t;
184  fmllr_writer.Write(feature_reader.Key(), fmllr_xforms);
185  }
186  }
187 
188  KALDI_LOG << "Overall objf improvement from fMLLR is "
189  << (tot_objf_impr/tot_t_objf)
190  << " per frame over " << tot_t_objf << " frames.";
191  KALDI_LOG << "Done " << num_done << " files, " << num_no_alignment
192  << " with no alignments, " << num_other_error
193  << " with other errors.";
194  KALDI_LOG << "Overall acoustic like per frame = " << (tot_like / tot_t)
195  << " over " << tot_t << " frames.";
196  return 0;
197  } catch(const std::exception &e) {
198  std::cerr << e.what();
199  return -1;
200  }
201 }
202 
void Read(std::istream &in, bool binary, const AmDiagGmm &am)
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 Register(OptionsItf *opts)
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
int32 TransitionIdToPdf(int32 trans_id) const
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
An FMLLR (feature-space MLLR) transformation, also called CMLLR (constrained MLLR) is an affine trans...
std::istream & Stream()
Definition: kaldi-io.cc:826
int32 NumBaseclasses() const
Accessors (const)
float BaseFloat
Definition: kaldi-types.h:29
int main(int argc, char *argv[])
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
Configuration variables for FMLLR transforms.
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 regression tree is a clustering of Gaussian densities in an acoustic model, such that the group of ...
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)
int32 Dim() const
Definition: am-diag-gmm.h:79
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
Class for computing the accumulators needed for the maximum-likelihood estimate of FMLLR transforms f...
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
BaseFloat AccumulateForGmm(const RegressionTree &regtree, const AmDiagGmm &am, const VectorBase< BaseFloat > &data, size_t pdf_index, BaseFloat weight)
Accumulate stats for a single GMM in the model; returns log likelihood.
void Update(const RegressionTree &regtree, const RegtreeFmllrOptions &opts, RegtreeFmllrDiagGmm *out_fmllr, BaseFloat *auxf_impr, BaseFloat *tot_t) const
void Init(size_t num_bclass, size_t dim)
#define KALDI_LOG
Definition: kaldi-error.h:153
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147