All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
gmm-est-regtree-fmllr.cc File Reference
#include <string>
#include <vector>
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "gmm/am-diag-gmm.h"
#include "hmm/transition-model.h"
#include "hmm/posterior.h"
#include "transform/regtree-fmllr-diag-gmm.h"
Include dependency graph for gmm-est-regtree-fmllr.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 33 of file gmm-est-regtree-fmllr.cc.

References RegtreeFmllrDiagGmmAccs::AccumulateForGmm(), kaldi::ConvertPosteriorToPdfs(), AmDiagGmm::Dim(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, RegtreeFmllrDiagGmmAccs::Init(), rnnlm::j, KALDI_LOG, KALDI_VLOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), RegressionTree::NumBaseclasses(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), RegressionTree::Read(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), RegtreeFmllrOptions::Register(), ParseOptions::Register(), MatrixBase< Real >::Row(), RegtreeFmllrDiagGmmAccs::SetZero(), Input::Stream(), RegtreeFmllrDiagGmmAccs::Update(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

33  {
34  try {
35  typedef kaldi::int32 int32;
36  using namespace kaldi;
37  const char *usage =
38  "Compute FMLLR transforms per-utterance (default) or per-speaker for "
39  "the supplied set of speakers (spk2utt option). Note: writes RegtreeFmllrDiagGmm objects\n"
40  "Usage: gmm-est-regtree-fmllr [options] <model-in> <feature-rspecifier> "
41  "<posteriors-rspecifier> <regression-tree> <transforms-wspecifier>\n";
42 
43  ParseOptions po(usage);
44  string spk2utt_rspecifier;
45  bool binary = true;
46  po.Register("spk2utt", &spk2utt_rspecifier, "rspecifier for speaker to "
47  "utterance-list map");
48  po.Register("binary", &binary, "Write output in binary mode");
49  // register other modules
51  opts.Register(&po);
52 
53  po.Read(argc, argv);
54 
55  if (po.NumArgs() != 5) {
56  po.PrintUsage();
57  exit(1);
58  }
59 
60  string model_filename = po.GetArg(1),
61  feature_rspecifier = po.GetArg(2),
62  posteriors_rspecifier = po.GetArg(3),
63  regtree_filename = po.GetArg(4),
64  xforms_wspecifier = po.GetArg(5);
65 
66  RandomAccessPosteriorReader posteriors_reader(posteriors_rspecifier);
67  RegtreeFmllrDiagGmmWriter fmllr_writer(xforms_wspecifier);
68 
69  AmDiagGmm am_gmm;
70  TransitionModel trans_model;
71  {
72  bool binary;
73  Input ki(model_filename, &binary);
74  trans_model.Read(ki.Stream(), binary);
75  am_gmm.Read(ki.Stream(), binary);
76  }
77  RegressionTree regtree;
78  {
79  bool binary;
80  Input in(regtree_filename, &binary);
81  regtree.Read(in.Stream(), binary, am_gmm);
82  }
83 
84  RegtreeFmllrDiagGmm fmllr_xforms;
85  RegtreeFmllrDiagGmmAccs fmllr_accs;
86  fmllr_accs.Init(regtree.NumBaseclasses(), am_gmm.Dim());
87 
88  double tot_like = 0.0, tot_t = 0;
89 
90  int32 num_done = 0, num_no_posterior = 0, num_other_error = 0;
91  double tot_objf_impr = 0.0, tot_t_objf = 0.0;
92  if (spk2utt_rspecifier != "") { // per-speaker adaptation
93  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
94  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
95  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
96  string spk = spk2utt_reader.Key();
97  fmllr_accs.SetZero();
98  const vector<string> &uttlist = spk2utt_reader.Value();
99  for (vector<string>::const_iterator utt_itr = uttlist.begin(),
100  itr_end = uttlist.end(); utt_itr != itr_end; ++utt_itr) {
101  if (!feature_reader.HasKey(*utt_itr)) {
102  KALDI_WARN << "Did not find features for utterance " << *utt_itr;
103  continue;
104  }
105  if (!posteriors_reader.HasKey(*utt_itr)) {
106  KALDI_WARN << "Did not find posteriors for utterance "
107  << *utt_itr;
108  num_no_posterior++;
109  continue;
110  }
111  const Matrix<BaseFloat> &feats = feature_reader.Value(*utt_itr);
112  const Posterior &posterior = posteriors_reader.Value(*utt_itr);
113  if (static_cast<int32>(posterior.size()) != feats.NumRows()) {
114  KALDI_WARN << "Posteriors has wrong size " << (posterior.size())
115  << " vs. " << (feats.NumRows());
116  num_other_error++;
117  continue;
118  }
119 
120  BaseFloat file_like = 0.0, file_t = 0.0;
121  Posterior pdf_posterior;
122  ConvertPosteriorToPdfs(trans_model, posterior, &pdf_posterior);
123  for (size_t i = 0; i < posterior.size(); i++) {
124  for (size_t j = 0; j < pdf_posterior[i].size(); j++) {
125  int32 pdf_id = pdf_posterior[i][j].first;
126  BaseFloat prob = pdf_posterior[i][j].second;
127  file_like += fmllr_accs.AccumulateForGmm(regtree, am_gmm,
128  feats.Row(i), pdf_id,
129  prob);
130  file_t += prob;
131  }
132  }
133  KALDI_VLOG(2) << "Average like for this file is " << (file_like/file_t)
134  << " over " << file_t << " frames.";
135  tot_like += file_like;
136  tot_t += file_t;
137  num_done++;
138  if (num_done % 10 == 0)
139  KALDI_VLOG(1) << "Avg like per frame so far is "
140  << (tot_like / tot_t);
141  } // end looping over all utterances of the current speaker
142  BaseFloat objf_impr, t;
143  fmllr_accs.Update(regtree, opts, &fmllr_xforms, &objf_impr, &t);
144  KALDI_LOG << "fMLLR objf improvement for speaker " << spk << " is "
145  << (objf_impr/(t+1.0e-10)) << " per frame over " << t
146  << " frames.";
147  tot_objf_impr += objf_impr;
148  tot_t_objf += t;
149  fmllr_writer.Write(spk, fmllr_xforms);
150  } // end looping over speakers
151  } else { // per-utterance adaptation
152  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
153  for (; !feature_reader.Done(); feature_reader.Next()) {
154  string key = feature_reader.Key();
155  if (!posteriors_reader.HasKey(key)) {
156  KALDI_WARN << "Did not find posteriors for utterance "
157  << key;
158  num_no_posterior++;
159  continue;
160  }
161  const Matrix<BaseFloat> &feats = feature_reader.Value();
162  const Posterior &posterior = posteriors_reader.Value(key);
163 
164  if (static_cast<int32>(posterior.size()) != feats.NumRows()) {
165  KALDI_WARN << "Posteriors has wrong size " << (posterior.size())
166  << " vs. " << (feats.NumRows());
167  num_other_error++;
168  continue;
169  }
170 
171  num_done++;
172  BaseFloat file_like = 0.0, file_t = 0.0;
173  fmllr_accs.SetZero();
174  Posterior pdf_posterior;
175  ConvertPosteriorToPdfs(trans_model, posterior, &pdf_posterior);
176  for (size_t i = 0; i < posterior.size(); i++) {
177  for (size_t j = 0; j < pdf_posterior[i].size(); j++) {
178  int32 pdf_id = pdf_posterior[i][j].first;
179  BaseFloat prob = pdf_posterior[i][j].second;
180  file_like += fmllr_accs.AccumulateForGmm(regtree, am_gmm,
181  feats.Row(i), pdf_id,
182  prob);
183  file_t += prob;
184  }
185  }
186  KALDI_VLOG(2) << "Average like for this file is " << (file_like/file_t)
187  << " over " << file_t << " frames.";
188  tot_like += file_like;
189  tot_t += file_t;
190  if (num_done % 10 == 0)
191  KALDI_VLOG(1) << "Avg like per frame so far is "
192  << (tot_like / tot_t);
193  BaseFloat objf_impr, t;
194  fmllr_accs.Update(regtree, opts, &fmllr_xforms, &objf_impr, &t);
195  KALDI_LOG << "fMLLR objf improvement for utterance " << key << " is "
196  << (objf_impr/(t+1.0e-10)) << " per frame over " << t
197  << " frames.";
198  tot_objf_impr += objf_impr;
199  tot_t_objf += t;
200  fmllr_writer.Write(feature_reader.Key(), fmllr_xforms);
201  }
202  }
203  KALDI_LOG << "Done " << num_done << " files, " << num_no_posterior
204  << " with no posteriors, " << num_other_error
205  << " with other errors.";
206  KALDI_LOG << "Overall objf improvement from MLLR is " << (tot_objf_impr/tot_t_objf)
207  << " per frame " << " over " << tot_t_objf << " frames.";
208  KALDI_LOG << "Overall acoustic likelihood was " << (tot_like/tot_t)
209  << " over " << tot_t << " frames.";
210  return 0;
211  } catch(const std::exception &e) {
212  std::cerr << e.what();
213  return -1;
214  }
215 }
void Read(std::istream &in, bool binary, const AmDiagGmm &am)
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
void Register(OptionsItf *opts)
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
int32 NumBaseclasses() const
Accessors (const)
An FMLLR (feature-space MLLR) transformation, also called CMLLR (constrained MLLR) is an affine trans...
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:43
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.
int32 Dim() const
Definition: am-diag-gmm.h:79
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)
void Update(const RegressionTree &regtree, const RegtreeFmllrOptions &opts, RegtreeFmllrDiagGmm *out_fmllr, BaseFloat *auxf_impr, BaseFloat *tot_t) const
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_WARN
Definition: kaldi-error.h:130
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
Class for computing the accumulators needed for the maximum-likelihood estimate of FMLLR transforms f...
#define KALDI_VLOG(v)
Definition: kaldi-error.h:136
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 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
void Init(size_t num_bclass, size_t dim)
#define KALDI_LOG
Definition: kaldi-error.h:133
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147