All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
gmm-est-fmllr-global.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/fmllr-diag-gmm.h"
#include "hmm/posterior.h"
Include dependency graph for gmm-est-fmllr-global.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 36 of file gmm-est-fmllr-global.cc.

References FmllrDiagGmmAccs::AccumulateForGmm(), DiagGmm::Dim(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadKaldiObject(), FmllrOptions::Register(), ParseOptions::Register(), MatrixBase< Real >::Row(), MatrixBase< Real >::SetUnit(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

36  {
37  try {
38  typedef kaldi::int32 int32;
39  using namespace kaldi;
40  const char *usage =
41  "Estimate global fMLLR transforms, either per utterance or for the supplied\n"
42  "set of speakers (spk2utt option). This version is for when you have a single\n"
43  "global GMM, e.g. a UBM. Writes to a table of matrices.\n"
44  "Usage: gmm-est-fmllr-global [options] <gmm-in> <feature-rspecifier> "
45  "<transform-wspecifier>\n"
46  "e.g.: gmm-est-fmllr-global 1.ubm scp:feats.scp ark:trans.1\n";
47 
48  ParseOptions po(usage);
49  FmllrOptions fmllr_opts;
50  string spk2utt_rspecifier;
51  string gselect_rspecifier;
52  po.Register("spk2utt", &spk2utt_rspecifier, "rspecifier for speaker to "
53  "utterance-list map");
54  po.Register("gselect", &gselect_rspecifier, "rspecifier for "
55  "Gaussian-selection information");
56  fmllr_opts.Register(&po);
57 
58  po.Read(argc, argv);
59 
60  if (po.NumArgs() != 3) {
61  po.PrintUsage();
62  exit(1);
63  }
64 
65  string gmm_rxfilename = po.GetArg(1),
66  feature_rspecifier = po.GetArg(2),
67  trans_wspecifier = po.GetArg(3);
68 
69  DiagGmm gmm;
70  ReadKaldiObject(gmm_rxfilename, &gmm);
71 
72  double tot_impr = 0.0, tot_t = 0.0;
73 
74  BaseFloatMatrixWriter transform_writer(trans_wspecifier);
75  RandomAccessInt32VectorVectorReader gselect_reader(gselect_rspecifier);
76 
77  int32 num_done = 0, num_err = 0;
78  if (spk2utt_rspecifier != "") { // per-speaker adaptation
79  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
80  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
81 
82  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
83  FmllrDiagGmmAccs spk_stats(gmm.Dim(), fmllr_opts);
84  string spk = spk2utt_reader.Key();
85  const vector<string> &uttlist = spk2utt_reader.Value();
86  for (size_t i = 0; i < uttlist.size(); i++) {
87  std::string utt = uttlist[i];
88  if (!feature_reader.HasKey(utt)) {
89  KALDI_WARN << "Did not find features for utterance " << utt;
90  num_err++;
91  continue;
92  }
93  const Matrix<BaseFloat> &feats = feature_reader.Value(utt);
94 
95  if (gselect_rspecifier == "") {
96  for (size_t i = 0; i < feats.NumRows(); i++)
97  spk_stats.AccumulateForGmm(gmm, feats.Row(i), 1.0);
98  } else {
99  if (!gselect_reader.HasKey(utt) ||
100  gselect_reader.Value(utt).size() != feats.NumRows()) {
101  KALDI_LOG << "No gselect information for utterance " << utt
102  << " (or wrong size)";
103  num_err++;
104  continue;
105  }
106  const std::vector<std::vector<int32> > &gselect =
107  gselect_reader.Value(utt);
108  for (size_t i = 0; i < feats.NumRows(); i++)
109  spk_stats.AccumulateForGmmPreselect(gmm, gselect[i],
110  feats.Row(i), 1.0);
111  }
112  num_done++;
113  } // end looping over all utterances of the current speaker
114 
115  BaseFloat impr, spk_tot_t;
116  { // Compute the transform and write it out.
117  Matrix<BaseFloat> transform(gmm.Dim(), gmm.Dim()+1);
118  transform.SetUnit();
119  spk_stats.Update(fmllr_opts, &transform, &impr, &spk_tot_t);
120  transform_writer.Write(spk, transform);
121  }
122  KALDI_LOG << "For speaker " << spk << ", auxf-impr from fMLLR is "
123  << (impr/spk_tot_t) << ", over " << spk_tot_t << " frames.";
124  tot_impr += impr;
125  tot_t += spk_tot_t;
126  } // end looping over speakers
127  } else { // per-utterance adaptation
128  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
129  for (; !feature_reader.Done(); feature_reader.Next()) {
130  string utt = feature_reader.Key();
131  const Matrix<BaseFloat> &feats = feature_reader.Value();
132 
133  num_done++;
134 
135  FmllrDiagGmmAccs spk_stats(gmm.Dim(), fmllr_opts);
136 
137  if (gselect_rspecifier == "") {
138  for (size_t i = 0; i < feats.NumRows(); i++)
139  spk_stats.AccumulateForGmm(gmm, feats.Row(i), 1.0);
140  } else {
141  if (!gselect_reader.HasKey(utt) ||
142  gselect_reader.Value(utt).size() != feats.NumRows()) {
143  KALDI_LOG << "No gselect information for utterance " << utt
144  << " (or wrong size)";
145  num_err++;
146  continue;
147  }
148  const std::vector<std::vector<int32> > &gselect =
149  gselect_reader.Value(utt);
150  for (size_t i = 0; i < feats.NumRows(); i++)
151  spk_stats.AccumulateForGmmPreselect(gmm, gselect[i],
152  feats.Row(i), 1.0);
153  }
154  BaseFloat impr, utt_tot_t;
155  { // Compute the transform and write it out.
156  Matrix<BaseFloat> transform(gmm.Dim(), gmm.Dim()+1);
157  transform.SetUnit();
158  spk_stats.Update(fmllr_opts, &transform, &impr, &utt_tot_t);
159  transform_writer.Write(utt, transform);
160  }
161  KALDI_LOG << "For utterance " << utt << ", auxf-impr from fMLLR is "
162  << (impr/utt_tot_t) << ", over " << utt_tot_t << " frames.";
163  tot_impr += impr;
164  tot_t += utt_tot_t;
165  }
166  }
167  KALDI_LOG << "Done " << num_done << " files, "
168  << num_err << " with errors.";
169  KALDI_LOG << "Overall fMLLR auxf impr per frame is "
170  << (tot_impr / tot_t) << " over " << tot_t << " frames.";
171  return (num_done != 0 ? 0 : 1);
172  } catch(const std::exception &e) {
173  std::cerr << e.what();
174  return -1;
175  }
176 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
This does not work with multiple feature transforms.
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
void SetUnit()
Sets to zero, except ones along diagonal [for non-square matrices too].
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:818
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
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
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: diag-gmm.h:74
#define KALDI_WARN
Definition: kaldi-error.h:130
void Register(OptionsItf *opts)
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
Definition for Gaussian Mixture Model with diagonal covariances.
Definition: diag-gmm.h:42
#define KALDI_LOG
Definition: kaldi-error.h:133
BaseFloat AccumulateForGmm(const DiagGmm &gmm, const VectorBase< BaseFloat > &data, BaseFloat weight)
Accumulate stats for a single GMM in the model; returns log likelihood.