gmm-basis-fmllr-accs-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-basis-fmllr-accs-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-basis-fmllr-accs-gpost.cc.

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

53  {
54  try {
55  typedef kaldi::int32 int32;
56  using namespace kaldi;
57  const char *usage =
58  "Accumulate gradient scatter from training set, 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  "gradient scatter matrix.\n"
62  "Usage: gmm-basis-fmllr-accs-gpost [options] <model-in> <feature-rspecifier>"
63  "<post-rspecifier> <accs-wspecifier>\n";
64 
65  bool binary_write = true;
66  string spk2utt_rspecifier;
67  ParseOptions po(usage);
68  po.Register("binary", &binary_write, "Write output in binary mode");
69  po.Register("spk2utt", &spk2utt_rspecifier, "rspecifier for speaker to "
70  "utterance-list map");
71 
72  po.Read(argc, argv);
73  if (po.NumArgs() != 4) {
74  po.PrintUsage();
75  exit(1);
76  }
77 
78  string
79  model_rxfilename = po.GetArg(1),
80  feature_rspecifier = po.GetArg(2),
81  gpost_rspecifier = po.GetArg(3),
82  accs_wspecifier = po.GetArg(4);
83 
84  TransitionModel trans_model;
85  AmDiagGmm am_gmm;
86  {
87  bool binary;
88  Input ki(model_rxfilename, &binary);
89  trans_model.Read(ki.Stream(), binary);
90  am_gmm.Read(ki.Stream(), binary);
91  }
92 
93  RandomAccessGaussPostReader gpost_reader(gpost_rspecifier);
94  BasisFmllrAccus basis_accs(am_gmm.Dim());
95 
96  int32 num_done = 0, num_no_post = 0, num_other_error = 0;
97  if (spk2utt_rspecifier != "") { // per-speaker mode
98  SequentialTokenVectorReader spk2utt_reader(spk2utt_rspecifier);
99  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
100 
101  int32 num_spk = 0;
102  for (; !spk2utt_reader.Done(); spk2utt_reader.Next()) {
103  FmllrDiagGmmAccs spk_stats(am_gmm.Dim());
104  string spk = spk2utt_reader.Key();
105  const vector<string> &uttlist = spk2utt_reader.Value();
106  for (size_t i = 0; i < uttlist.size(); i++) {
107  std::string utt = uttlist[i];
108  if (!feature_reader.HasKey(utt)) {
109  KALDI_WARN << "Did not find features for utterance " << utt;
110  num_other_error++;
111  continue;
112  }
113  if (!gpost_reader.HasKey(utt)) {
114  KALDI_WARN << "Did not find posteriors for utterance " << utt;
115  num_no_post++;
116  continue;
117  }
118  const Matrix<BaseFloat> &feats = feature_reader.Value(utt);
119  const GaussPost &gpost = gpost_reader.Value(utt);
120  if (static_cast<int32>(gpost.size()) != feats.NumRows()) {
121  KALDI_WARN << "GaussPost has wrong size " << (gpost.size())
122  << " vs. " << (feats.NumRows());
123  num_other_error++;
124  continue;
125  }
126 
127  AccumulateForUtterance(feats, gpost, trans_model, am_gmm, &spk_stats);
128 
129  num_done++;
130  } // end looping over all utterances of this speaker
131  basis_accs.AccuGradientScatter(spk_stats);
132  num_spk++;
133  } // end looping over speakers
134  KALDI_LOG << "Accumulate statistics from " << num_spk << " speakers";
135 
136  } else { // per-utterance mode
137  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
138  for (; !feature_reader.Done(); feature_reader.Next()) {
139  string utt = feature_reader.Key();
140  if (!gpost_reader.HasKey(utt)) {
141  KALDI_WARN << "Did not find posts for utterance "
142  << utt;
143  num_no_post++;
144  continue;
145  }
146  const Matrix<BaseFloat> &feats = feature_reader.Value();
147  const GaussPost &gpost = gpost_reader.Value(utt);
148 
149  if (static_cast<int32>(gpost.size()) != feats.NumRows()) {
150  KALDI_WARN << "GaussPost has wrong size " << (gpost.size())
151  << " vs. " << (feats.NumRows());
152  num_other_error++;
153  continue;
154  }
155  // Accumulate stats for this utterance
156  FmllrDiagGmmAccs utt_stats(am_gmm.Dim());
157  AccumulateForUtterance(feats, gpost, trans_model, am_gmm, &utt_stats);
158  num_done++;
159 
160  basis_accs.AccuGradientScatter(utt_stats);
161  } // end looping over all utterances
162  }
163  // Write out accumulations
164  {
165  Output ko(accs_wspecifier, binary_write);
166  basis_accs.Write(ko.Stream(), binary_write);
167  }
168  KALDI_LOG << "Done " << num_done << " files, " << num_no_post
169  << " with no posts, " << num_other_error << " with other errors.";
170  KALDI_LOG << "Written gradient scatter to " << accs_wspecifier;
171  return (num_done != 0 ? 0 : 1);
172  } catch(const std::exception& e) {
173  std::cerr << e.what();
174  return -1;
175  }
176 }
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)
kaldi::int32 int32
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
Stats for fMLLR subspace estimation.
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
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
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