All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
gmm-est-regtree-fmllr-ali.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 "transform/regtree-fmllr-diag-gmm.h"
Include dependency graph for gmm-est-regtree-fmllr-ali.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 31 of file gmm-est-regtree-fmllr-ali.cc.

References RegtreeFmllrDiagGmmAccs::AccumulateForGmm(), AmDiagGmm::Dim(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, RegtreeFmllrDiagGmmAccs::Init(), 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(), TransitionModel::TransitionIdToPdf(), RegtreeFmllrDiagGmmAccs::Update(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

31  {
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 }
void Read(std::istream &in, bool binary, const AmDiagGmm &am)
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
int32 TransitionIdToPdf(int32 trans_id) const
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
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 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