gmm-est-basis-fmllr-gpost.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-gpost.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 GaussPost &gpost, 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 53 of file gmm-est-basis-fmllr-gpost.cc.

References kaldi::AccumulateForUtterance(), BasisFmllrEstimate::ComputeTransform(), 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().

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