gmm-est-lvtln-trans.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/lvtln.h"
#include "hmm/posterior.h"
Include dependency graph for gmm-est-lvtln-trans.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 AmDiagGmm &am_gmm, FmllrDiagGmmAccs *spk_stats)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

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

Definition at line 51 of file gmm-est-lvtln-trans.cc.

References kaldi::AccumulateForUtterance(), LinearVtln::ComputeTransform(), LinearVtln::Dim(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), ParseOptions::GetOptArg(), LinearVtln::GetWarp(), RandomAccessTableReader< Holder >::HasKey(), rnnlm::i, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), LinearVtln::NumClasses(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), kaldi::ReadKaldiObject(), ParseOptions::Register(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

51  {
52  try {
53  typedef kaldi::int32 int32;
54  using namespace kaldi;
55  const char *usage =
56  "Estimate linear-VTLN transforms, either per utterance or for "
57  "the supplied set of speakers (spk2utt option). Reads posteriors. \n"
58  "Usage: gmm-est-lvtln-trans [options] <model-in> <lvtln-in> "
59  "<feature-rspecifier> <gpost-rspecifier> <lvtln-trans-wspecifier> [<warp-wspecifier>]\n";
60 
61  ParseOptions po(usage);
62  string spk2utt_rspecifier;
63  BaseFloat logdet_scale = 1.0;
64  std::string norm_type = "offset";
65  po.Register("norm-type", &norm_type, "type of fMLLR applied (\"offset\"|\"none\"|\"diag\")");
66  po.Register("spk2utt", &spk2utt_rspecifier, "rspecifier for speaker to "
67  "utterance-list map");
68  po.Register("logdet-scale", &logdet_scale, "Scale on log-determinant term in auxiliary function");
69 
70  po.Read(argc, argv);
71 
72  if (po.NumArgs() < 5 || po.NumArgs() > 6) {
73  po.PrintUsage();
74  exit(1);
75  }
76 
77  string
78  model_rxfilename = po.GetArg(1),
79  lvtln_rxfilename = po.GetArg(2),
80  feature_rspecifier = po.GetArg(3),
81  gpost_rspecifier = po.GetArg(4),
82  trans_wspecifier = po.GetArg(5),
83  warp_wspecifier = po.GetOptArg(6);
84 
85  AmDiagGmm am_gmm;
86  {
87  bool binary;
88  Input ki(model_rxfilename, &binary);
89  TransitionModel trans_model;
90  trans_model.Read(ki.Stream(), binary);
91  am_gmm.Read(ki.Stream(), binary);
92  }
93  LinearVtln lvtln;
94  ReadKaldiObject(lvtln_rxfilename, &lvtln);
95 
96 
97  RandomAccessGaussPostReader gpost_reader(gpost_rspecifier);
98 
99  double tot_lvtln_impr = 0.0, tot_t = 0.0;
100 
101  BaseFloatMatrixWriter transform_writer(trans_wspecifier);
102 
103  BaseFloatWriter warp_writer(warp_wspecifier);
104 
105  std::vector<int32> class_counts(lvtln.NumClasses(), 0);
106  int32 num_done = 0, num_no_gpost = 0, num_other_error = 0;
107  if (spk2utt_rspecifier != "") { // per-speaker adaptation
108  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
109  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
110 
111  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
112  FmllrDiagGmmAccs spk_stats(lvtln.Dim());
113  string spk = spk2utt_reader.Key();
114  const vector<string> &uttlist = spk2utt_reader.Value();
115  for (size_t i = 0; i < uttlist.size(); i++) {
116  std::string utt = uttlist[i];
117  if (!feature_reader.HasKey(utt)) {
118  KALDI_WARN << "Did not find features for utterance " << utt;
119  continue;
120  }
121  if (!gpost_reader.HasKey(utt)) {
122  KALDI_WARN << "Did not find posteriors for utterance " << utt;
123  num_no_gpost++;
124  continue;
125  }
126  const Matrix<BaseFloat> &feats = feature_reader.Value(utt);
127  const GaussPost &gpost = gpost_reader.Value(utt);
128  if (static_cast<int32>(gpost.size()) != feats.NumRows()) {
129  KALDI_WARN << "GauPost vector has wrong size " << gpost.size()
130  << " vs. " << feats.NumRows();
131  num_other_error++;
132  continue;
133  }
134 
135  AccumulateForUtterance(feats, gpost, am_gmm, &spk_stats);
136 
137  num_done++;
138  } // end looping over all utterances of the current speaker
139 
140  BaseFloat impr, spk_tot_t;
141  { // Compute the transform and write it out.
142  Matrix<BaseFloat> transform(lvtln.Dim(), lvtln.Dim()+1);
143  int32 class_idx;
144  lvtln.ComputeTransform(spk_stats,
145  norm_type,
146  logdet_scale,
147  &transform,
148  &class_idx,
149  NULL,
150  &impr,
151  &spk_tot_t);
152  class_counts[class_idx]++;
153  transform_writer.Write(spk, transform);
154  if (warp_wspecifier != "")
155  warp_writer.Write(spk, lvtln.GetWarp(class_idx));
156  }
157  KALDI_LOG << "For speaker " << spk << ", auxf-impr from LVTLN is "
158  << (impr/spk_tot_t) << ", over " << spk_tot_t << " frames.";
159  tot_lvtln_impr += impr;
160  tot_t += spk_tot_t;
161  } // end looping over speakers
162  } else { // per-utterance adaptation
163  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
164  for (; !feature_reader.Done(); feature_reader.Next()) {
165  string utt = feature_reader.Key();
166  if (!gpost_reader.HasKey(utt)) {
167  KALDI_WARN << "Did not find gposts for utterance "
168  << utt;
169  num_no_gpost++;
170  continue;
171  }
172  const Matrix<BaseFloat> &feats = feature_reader.Value();
173  const GaussPost &gpost = gpost_reader.Value(utt);
174 
175  if (static_cast<int32>(gpost.size()) != feats.NumRows()) {
176  KALDI_WARN << "GauPost has wrong size " << gpost.size()
177  << " vs. " << feats.NumRows();
178  num_other_error++;
179  continue;
180  }
181  num_done++;
182 
183  FmllrDiagGmmAccs spk_stats(lvtln.Dim());
184 
185  AccumulateForUtterance(feats, gpost, am_gmm,
186  &spk_stats);
187  BaseFloat impr, utt_tot_t = spk_stats.beta_;
188  { // Compute the transform and write it out.
189  Matrix<BaseFloat> transform(lvtln.Dim(), lvtln.Dim()+1);
190  int32 class_idx;
191  lvtln.ComputeTransform(spk_stats,
192  norm_type,
193  logdet_scale,
194  &transform,
195  &class_idx,
196  NULL,
197  &impr,
198  &utt_tot_t);
199  class_counts[class_idx]++;
200  transform_writer.Write(utt, transform);
201  if (warp_wspecifier != "")
202  warp_writer.Write(utt, lvtln.GetWarp(class_idx));
203  }
204 
205  KALDI_LOG << "For utterance " << utt << ", auxf-impr from LVTLN is "
206  << (impr/utt_tot_t) << ", over " << utt_tot_t << " frames.";
207  tot_lvtln_impr += impr;
208  tot_t += utt_tot_t;
209  }
210  }
211 
212  {
213  std::ostringstream s;
214  for (size_t i = 0; i < class_counts.size(); i++)
215  s << ' ' << class_counts[i];
216  KALDI_LOG << "Distribution of classes is: " << s.str();
217  }
218 
219  KALDI_LOG << "Done " << num_done << " files, " << num_no_gpost
220  << " with no gposts, " << num_other_error << " with other errors.";
221  KALDI_LOG << "Overall LVTLN auxf impr per frame is "
222  << (tot_lvtln_impr / tot_t) << " over " << tot_t << " frames.";
223  return (num_done == 0 ? 1 : 0);
224  } catch(const std::exception &e) {
225  std::cerr << e.what();
226  return -1;
227  }
228 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
BaseFloat GetWarp(int32 i) const
Definition: lvtln.cc:180
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
int32 NumClasses() const
Definition: lvtln.h:78
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
int32 Dim() const
Definition: lvtln.h:77
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 ComputeTransform(const FmllrDiagGmmAccs &accs, std::string norm_type, BaseFloat logdet_scale, MatrixBase< BaseFloat > *Ws, int32 *class_idx, BaseFloat *logdet_out, BaseFloat *objf_impr=NULL, BaseFloat *count=NULL)
Compute the transform for the speaker.
Definition: lvtln.cc:97
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
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