gmm-global-est-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 "transform/fmllr-diag-gmm.h"
Include dependency graph for gmm-global-est-fmllr.cc:

Go to the source code of this file.

Namespaces

 kaldi
 This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for mispronunciations detection tasks, the reference:
 

Functions

bool AccumulateForUtterance (const Matrix< BaseFloat > &feats, const DiagGmm &gmm, const std::string &key, RandomAccessBaseFloatVectorReader *weights_reader, RandomAccessInt32VectorVectorReader *gselect_reader, AccumFullGmm *fullcov_stats)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

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

Definition at line 83 of file gmm-global-est-fmllr.cc.

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

83  {
84  try {
85  typedef kaldi::int32 int32;
86  using namespace kaldi;
87  const char *usage =
88  "Estimate global fMLLR transforms, either per utterance or for the supplied\n"
89  "set of speakers (spk2utt option). Reads features, and (with --weights option)\n"
90  "weights for each frame (also see --gselect option)\n"
91  "Usage: gmm-global-est-fmllr [options] <gmm-in> <feature-rspecifier> <transform-wspecifier>\n";
92 
93  ParseOptions po(usage);
94  FmllrOptions fmllr_opts;
95  string spk2utt_rspecifier, gselect_rspecifier, weights_rspecifier,
96  alignment_model;
97 
98 
99  po.Register("spk2utt", &spk2utt_rspecifier, "rspecifier for speaker to "
100  "utterance-list map");
101  po.Register("gselect", &gselect_rspecifier, "rspecifier for gselect objects "
102  "to limit the #Gaussians accessed on each frame.");
103  po.Register("weights", &weights_rspecifier, "rspecifier for a vector of floats "
104  "for each utterance, that's a per-frame weight.");
105  po.Register("align-model", &alignment_model, "rxfilename for a model in the "
106  "speaker-independent space, to get Gaussian alignments from");
107 
108  fmllr_opts.Register(&po);
109 
110  po.Read(argc, argv);
111 
112  if (po.NumArgs() != 3) {
113  po.PrintUsage();
114  exit(1);
115  }
116 
117  string gmm_rxfilename = po.GetArg(1),
118  feature_rspecifier = po.GetArg(2),
119  trans_wspecifier = po.GetArg(3);
120 
121  DiagGmm gmm;
122  ReadKaldiObject(gmm_rxfilename, &gmm);
123  DiagGmm ali_gmm_read;
124  if (alignment_model != "") {
125  bool binary;
126  Input ki(gmm_rxfilename, &binary);
127  ali_gmm_read.Read(ki.Stream(), binary);
128  }
129  DiagGmm &ali_gmm = (alignment_model != "" ? ali_gmm_read : gmm);
130 
131  RandomAccessBaseFloatVectorReader weights_reader(weights_rspecifier);
132  RandomAccessInt32VectorVectorReader gselect_reader(gselect_rspecifier);
133 
134  double tot_impr = 0.0, tot_t = 0.0;
135 
136  BaseFloatMatrixWriter transform_writer(trans_wspecifier);
137 
138  int32 num_done = 0, num_err = 0;
139 
140  if (spk2utt_rspecifier != "") { // per-speaker adaptation
141  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
142  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
143 
144  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
145  AccumFullGmm fullcov_stats(gmm.NumGauss(), gmm.Dim(), kGmmAll);
146  string spk = spk2utt_reader.Key();
147  const vector<string> &uttlist = spk2utt_reader.Value();
148  for (size_t i = 0; i < uttlist.size(); i++) {
149  std::string utt = uttlist[i];
150  if (!feature_reader.HasKey(utt)) {
151  KALDI_WARN << "Did not find features for utterance " << utt;
152  continue;
153  }
154  const Matrix<BaseFloat> &feats = feature_reader.Value(utt);
155 
156  if (AccumulateForUtterance(feats, ali_gmm, utt, &weights_reader,
157  &gselect_reader, &fullcov_stats)) num_done++;
158  else num_err++;
159  } // end looping over all utterances of the current speaker
160 
161  BaseFloat impr, spk_tot_t;
162  { // Compute the transform and write it out.
163  Matrix<BaseFloat> transform(gmm.Dim(), gmm.Dim()+1);
164  transform.SetUnit();
165  FmllrDiagGmmAccs spk_stats(gmm, fullcov_stats);
166  spk_stats.Update(fmllr_opts, &transform, &impr, &spk_tot_t);
167  transform_writer.Write(spk, transform);
168  }
169  KALDI_LOG << "For speaker " << spk << ", auxf-impr from fMLLR is "
170  << (impr/spk_tot_t) << ", over " << spk_tot_t << " frames.";
171  tot_impr += impr;
172  tot_t += spk_tot_t;
173  } // end looping over speakers
174  } else { // per-utterance adaptation
175  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
176  for (; !feature_reader.Done(); feature_reader.Next()) {
177  string utt = feature_reader.Key();
178 
179  const Matrix<BaseFloat> &feats = feature_reader.Value();
180 
181  AccumFullGmm fullcov_stats(gmm.NumGauss(), gmm.Dim(), kGmmAll);
182 
183  if (AccumulateForUtterance(feats, ali_gmm, utt, &weights_reader,
184  &gselect_reader, &fullcov_stats)) {
185  BaseFloat impr, utt_tot_t;
186  { // Compute the transform and write it out.
187  Matrix<BaseFloat> transform(gmm.Dim(), gmm.Dim()+1);
188  transform.SetUnit();
189  FmllrDiagGmmAccs spk_stats(gmm, fullcov_stats);
190  spk_stats.Update(fmllr_opts, &transform, &impr, &utt_tot_t);
191  transform_writer.Write(utt, transform);
192  }
193  KALDI_LOG << "For utterance " << utt << ", auxf-impr from fMLLR is "
194  << (impr/utt_tot_t) << ", over " << utt_tot_t << " frames.";
195  tot_impr += impr;
196  tot_t += utt_tot_t;
197  num_done++;
198  } else num_err++;
199 
200  }
201  }
202 
203  KALDI_LOG << "Done " << num_done << " files, " << num_err
204  << " with errors.";
205  KALDI_LOG << "Overall fMLLR auxf impr per frame is "
206  << (tot_impr / tot_t) << " over " << tot_t << " frames.";
207  return (num_done != 0 ? 0 : 1);
208  } catch(const std::exception &e) {
209  std::cerr << e.what();
210  return -1;
211  }
212 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: diag-gmm.h:74
This does not work with multiple feature transforms.
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 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:832
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
Class for computing the maximum-likelihood estimates of the parameters of a Gaussian mixture model...
Definition: mle-full-gmm.h:74
#define KALDI_WARN
Definition: kaldi-error.h:150
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: diag-gmm.h:72
void Register(OptionsItf *opts)
void Read(std::istream &in, bool binary)
Definition: diag-gmm.cc:728
Definition for Gaussian Mixture Model with diagonal covariances.
Definition: diag-gmm.h:42
#define KALDI_LOG
Definition: kaldi-error.h:153