gmm-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 "hmm/posterior.h"
Include dependency graph for gmm-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

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-fmllr.cc.

References kaldi::AccumulateForUtterance(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), FmllrOptions::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  "Estimate global fMLLR transforms, either per utterance or for the supplied\n"
61  "set of speakers (spk2utt option). Reads posteriors (on transition-ids). Writes\n"
62  "to a table of matrices.\n"
63  "Usage: gmm-est-fmllr [options] <model-in> "
64  "<feature-rspecifier> <post-rspecifier> <transform-wspecifier>\n";
65 
66  ParseOptions po(usage);
67  FmllrOptions fmllr_opts;
68  string spk2utt_rspecifier;
69  po.Register("spk2utt", &spk2utt_rspecifier, "rspecifier for speaker to "
70  "utterance-list map");
71  fmllr_opts.Register(&po);
72 
73  po.Read(argc, argv);
74 
75  if (po.NumArgs() != 4) {
76  po.PrintUsage();
77  exit(1);
78  }
79 
80  string
81  model_rxfilename = po.GetArg(1),
82  feature_rspecifier = po.GetArg(2),
83  post_rspecifier = po.GetArg(3),
84  trans_wspecifier = po.GetArg(4);
85 
86  TransitionModel trans_model;
87  AmDiagGmm am_gmm;
88  {
89  bool binary;
90  Input ki(model_rxfilename, &binary);
91  trans_model.Read(ki.Stream(), binary);
92  am_gmm.Read(ki.Stream(), binary);
93  }
94 
95  RandomAccessPosteriorReader post_reader(post_rspecifier);
96 
97  double tot_impr = 0.0, tot_t = 0.0;
98 
99  BaseFloatMatrixWriter transform_writer(trans_wspecifier);
100 
101  int32 num_done = 0, num_no_post = 0, num_other_error = 0;
102  if (spk2utt_rspecifier != "") { // per-speaker adaptation
103  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
104  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
105 
106  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
107  FmllrDiagGmmAccs spk_stats(am_gmm.Dim(), fmllr_opts);
108  string spk = spk2utt_reader.Key();
109  const vector<string> &uttlist = spk2utt_reader.Value();
110  for (size_t i = 0; i < uttlist.size(); i++) {
111  std::string utt = uttlist[i];
112  if (!feature_reader.HasKey(utt)) {
113  KALDI_WARN << "Did not find features for utterance " << utt;
114  num_other_error++;
115  continue;
116  }
117  if (!post_reader.HasKey(utt)) {
118  KALDI_WARN << "Did not find posteriors for utterance " << utt;
119  num_no_post++;
120  continue;
121  }
122  const Matrix<BaseFloat> &feats = feature_reader.Value(utt);
123  const Posterior &post = post_reader.Value(utt);
124  if (static_cast<int32>(post.size()) != feats.NumRows()) {
125  KALDI_WARN << "Posterior vector has wrong size " << (post.size())
126  << " vs. " << (feats.NumRows());
127  num_other_error++;
128  continue;
129  }
130 
131  AccumulateForUtterance(feats, post, trans_model, am_gmm, &spk_stats);
132 
133  num_done++;
134  } // end looping over all utterances of the current speaker
135 
136  BaseFloat impr, spk_tot_t;
137  { // Compute the transform and write it out.
138  Matrix<BaseFloat> transform(am_gmm.Dim(), am_gmm.Dim()+1);
139  transform.SetUnit();
140  spk_stats.Update(fmllr_opts, &transform, &impr, &spk_tot_t);
141  transform_writer.Write(spk, transform);
142  }
143  KALDI_LOG << "For speaker " << spk << ", auxf-impr from fMLLR is "
144  << (impr/spk_tot_t) << ", over " << spk_tot_t << " frames.";
145  tot_impr += impr;
146  tot_t += spk_tot_t;
147  } // end looping over speakers
148  } else { // per-utterance adaptation
149  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
150  for (; !feature_reader.Done(); feature_reader.Next()) {
151  string utt = feature_reader.Key();
152  if (!post_reader.HasKey(utt)) {
153  KALDI_WARN << "Did not find posts for utterance "
154  << utt;
155  num_no_post++;
156  continue;
157  }
158  const Matrix<BaseFloat> &feats = feature_reader.Value();
159  const Posterior &post = post_reader.Value(utt);
160 
161  if (static_cast<int32>(post.size()) != feats.NumRows()) {
162  KALDI_WARN << "Posterior has wrong size " << (post.size())
163  << " vs. " << (feats.NumRows());
164  num_other_error++;
165  continue;
166  }
167  num_done++;
168 
169  FmllrDiagGmmAccs spk_stats(am_gmm.Dim(), fmllr_opts);
170 
171  AccumulateForUtterance(feats, post, trans_model, am_gmm,
172  &spk_stats);
173 
174  BaseFloat impr, utt_tot_t;
175  { // Compute the transform and write it out.
176  Matrix<BaseFloat> transform(am_gmm.Dim(), am_gmm.Dim()+1);
177  transform.SetUnit();
178  spk_stats.Update(fmllr_opts, &transform, &impr, &utt_tot_t);
179  transform_writer.Write(utt, transform);
180  }
181  KALDI_LOG << "For utterance " << utt << ", auxf-impr from fMLLR is "
182  << (impr/utt_tot_t) << ", over " << utt_tot_t << " frames.";
183  tot_impr += impr;
184  tot_t += utt_tot_t;
185  }
186  }
187 
188  KALDI_LOG << "Done " << num_done << " files, " << num_no_post
189  << " with no posts, " << num_other_error << " with other errors.";
190  KALDI_LOG << "Overall fMLLR auxf impr per frame is "
191  << (tot_impr / tot_t) << " over " << tot_t << " frames.";
192  return (num_done != 0 ? 0 : 1);
193  } catch(const std::exception &e) {
194  std::cerr << e.what();
195  return -1;
196  }
197 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
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].
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)
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
void Register(OptionsItf *opts)
int32 Dim() const
Definition: am-diag-gmm.h:79
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