gmm-est-basis-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 "transform/basis-fmllr-diag-gmm.h"
#include "hmm/posterior.h"
Include dependency graph for gmm-est-basis-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

void AccumulateForUtterance (const Matrix< BaseFloat > &feats, const Posterior &post, const TransitionModel &trans_model, const AmDiagGmm &am_gmm, FmllrDiagGmmAccs *spk_stats)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

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

Definition at line 55 of file gmm-est-basis-fmllr.cc.

References kaldi::AccumulateForUtterance(), BasisFmllrEstimate::ComputeTransform(), VectorBase< Real >::Dim(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), TableWriter< Holder >::Open(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadKaldiObject(), BasisFmllrOptions::Register(), ParseOptions::Register(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

55  {
56  try {
57  typedef kaldi::int32 int32;
58  using namespace kaldi;
59  const char *usage =
60  "Perform basis fMLLR adaptation in testing stage, either per utterance or\n"
61  "for the supplied set of speakers (spk2utt option). Reads posterior to\n"
62  "accumulate fMLLR stats for each speaker/utterance. Writes to a table of\n"
63  "matrices.\n"
64  "Usage: gmm-est-basis-fmllr [options] <model-in> <basis-rspecifier> <feature-rspecifier> "
65  "<post-rspecifier> <transform-wspecifier>\n";
66 
67  ParseOptions po(usage);
68  BasisFmllrOptions basis_fmllr_opts;
69  string spk2utt_rspecifier;
70  string weights_out_filename;
71 
72  po.Register("spk2utt", &spk2utt_rspecifier, "Rspecifier for speaker to "
73  "utterance-list map");
74  po.Register("write-weights", &weights_out_filename, "File to write base "
75  "weights to.");
76 
77  basis_fmllr_opts.Register(&po);
78 
79  po.Read(argc, argv);
80  if (po.NumArgs() != 5) {
81  po.PrintUsage();
82  exit(1);
83  }
84 
85  string
86  model_rxfilename = po.GetArg(1),
87  basis_rspecifier = po.GetArg(2),
88  feature_rspecifier = po.GetArg(3),
89  post_rspecifier = po.GetArg(4),
90  trans_wspecifier = po.GetArg(5);
91 
92  TransitionModel trans_model;
93  AmDiagGmm am_gmm;
94  {
95  bool binary;
96  Input ki(model_rxfilename, &binary);
97  trans_model.Read(ki.Stream(), binary);
98  am_gmm.Read(ki.Stream(), binary);
99  }
100 
101  BasisFmllrEstimate basis_est;
102  ReadKaldiObject(basis_rspecifier, &basis_est);
103 
104  RandomAccessPosteriorReader post_reader(post_rspecifier);
105 
106  double tot_impr = 0.0, tot_t = 0.0;
107 
108  BaseFloatMatrixWriter transform_writer(trans_wspecifier);
109  BaseFloatVectorWriter weights_writer;
110  if (!weights_out_filename.empty()) {
111  weights_writer.Open(weights_out_filename);
112  }
113 
114  int32 num_done = 0, num_no_post = 0, num_other_error = 0;
115  if (spk2utt_rspecifier != "") { // per-speaker adaptation
116  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
117  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
118 
119  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
120  FmllrDiagGmmAccs spk_stats(am_gmm.Dim());
121  string spk = spk2utt_reader.Key();
122  const vector<string> &uttlist = spk2utt_reader.Value();
123  for (size_t i = 0; i < uttlist.size(); i++) {
124  std::string utt = uttlist[i];
125  if (!feature_reader.HasKey(utt)) {
126  KALDI_WARN << "Did not find features for utterance " << utt;
127  num_other_error++;
128  continue;
129  }
130  if (!post_reader.HasKey(utt)) {
131  KALDI_WARN << "Did not find posteriors for utterance " << utt;
132  num_no_post++;
133  continue;
134  }
135  const Matrix<BaseFloat> &feats = feature_reader.Value(utt);
136  const Posterior &post = post_reader.Value(utt);
137  if (static_cast<int32>(post.size()) != feats.NumRows()) {
138  KALDI_WARN << "Posterior vector has wrong size " << (post.size())
139  << " vs. " << (feats.NumRows());
140  num_other_error++;
141  continue;
142  }
143 
144  AccumulateForUtterance(feats, post, trans_model, am_gmm, &spk_stats);
145  num_done++;
146  } // end looping over all utterances of the current speaker
147 
148  double impr, spk_tot_t; int32 wgt_size;
149  {
150  // Compute the transform and write it out.
151  Matrix<BaseFloat> transform(am_gmm.Dim(), am_gmm.Dim() + 1);
152  transform.SetUnit();
153  Vector<BaseFloat> weights; // size will be adjusted
154  impr = basis_est.ComputeTransform(spk_stats, &transform,
155  &weights, basis_fmllr_opts);
156  spk_tot_t = spk_stats.beta_;
157  wgt_size = weights.Dim();
158  transform_writer.Write(spk, transform);
159  // Optionally write out the base weights
160  if (!weights_out_filename.empty() && weights.Dim() > 0)
161  weights_writer.Write(spk, weights);
162  }
163  KALDI_LOG << "For speaker " << spk << ", auxf-impr from Basis fMLLR is "
164  << (impr / spk_tot_t) << ", over " << spk_tot_t << " frames, "
165  << "the top " << wgt_size << " basis elements have been used";
166  tot_impr += impr;
167  tot_t += spk_tot_t;
168  } // end looping over speakers
169  } else { // per-utterance adaptation
170  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
171  for (; !feature_reader.Done(); feature_reader.Next()) {
172  string utt = feature_reader.Key();
173  if (!post_reader.HasKey(utt)) {
174  KALDI_WARN << "Did not find posts for utterance " << utt;
175  num_no_post++;
176  continue;
177  }
178  const Matrix<BaseFloat> &feats = feature_reader.Value();
179  const Posterior &post = post_reader.Value(utt);
180 
181  if (static_cast<int32>(post.size()) != feats.NumRows()) {
182  KALDI_WARN << "Posterior has wrong size " << (post.size())
183  << " vs. " << (feats.NumRows());
184  num_other_error++;
185  continue;
186  }
187 
188  FmllrDiagGmmAccs spk_stats(am_gmm.Dim());
189  AccumulateForUtterance(feats, post, trans_model, am_gmm, &spk_stats);
190  num_done++;
191 
192  BaseFloat impr, utt_tot_t; int32 wgt_size;
193  { // Compute the transform and write it out.
194  Matrix<BaseFloat> transform(am_gmm.Dim(), am_gmm.Dim()+1);
195  transform.SetUnit();
196  Vector<BaseFloat> weights(am_gmm.Dim() * (am_gmm.Dim() + 1)); // size will be adjusted
197  impr = basis_est.ComputeTransform(spk_stats, &transform,
198  &weights, basis_fmllr_opts);
199  utt_tot_t = spk_stats.beta_;
200  wgt_size = weights.Dim();
201  transform_writer.Write(utt, transform);
202  // Optionally write out the base weights
203  if (!weights_out_filename.empty() && weights.Dim() > 0)
204  weights_writer.Write(utt, weights);
205  }
206  KALDI_LOG << "For utterance " << utt << ", auxf-impr from Basis fMLLR is "
207  << (impr / utt_tot_t) << ", over " << utt_tot_t << " frames, "
208  << "the top " << wgt_size << " basis elements have been used";
209  tot_impr += impr;
210  tot_t += utt_tot_t;
211  } // end looping over all the utterances
212  }
213 
214  KALDI_LOG << "Done " << num_done << " files, " << num_no_post
215  << " with no posts, " << num_other_error << " with other errors.";
216  KALDI_LOG << "Overall fMLLR auxf-impr per frame is "
217  << (tot_impr / tot_t) << " over " << tot_t << " frames.";
218  return (num_done != 0 ? 0 : 1);
219  } catch(const std::exception& e) {
220  std::cerr << e.what();
221  return -1;
222  }
223 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool Open(const std::string &wspecifier)
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].
double ComputeTransform(const AffineXformStats &spk_stats, Matrix< BaseFloat > *out_xform, Vector< BaseFloat > *coefficients, BasisFmllrOptions options) const
This function performs speaker adaptation, computing the fMLLR matrix based on speaker statistics...
void Write(const std::string &key, const T &value) const
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
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:42
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
void Read(std::istream &is, bool binary)
void Register(OptionsItf *opts)
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:150
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
int32 Dim() const
Definition: am-diag-gmm.h:79
A class representing a vector.
Definition: kaldi-vector.h:406
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
#define KALDI_LOG
Definition: kaldi-error.h:153
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147
Estimation functions for basis fMLLR.