nnet3-discriminative-get-egs.cc File Reference
#include <sstream>
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "hmm/transition-model.h"
#include "hmm/posterior.h"
#include "nnet3/nnet-discriminative-example.h"
#include "nnet3/discriminative-supervision.h"
#include "nnet3/nnet-example-utils.h"
#include "chain/chain-supervision.h"
Include dependency graph for nnet3-discriminative-get-egs.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:
 
 kaldi::nnet3
 

Functions

static bool ProcessFile (const discriminative::SplitDiscriminativeSupervisionOptions &config, const TransitionModel &tmodel, const MatrixBase< BaseFloat > &feats, const MatrixBase< BaseFloat > *ivector_feats, int32 ivector_period, const discriminative::DiscriminativeSupervision &supervision, const std::string &utt_id, bool compress, UtteranceSplitter *utt_splitter, NnetDiscriminativeExampleWriter *example_writer)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

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

Definition at line 155 of file nnet3-discriminative-get-egs.cc.

References ExampleGenerationConfig::ComputeDerived(), SequentialTableReader< Holder >::Done(), UtteranceSplitter::ExitStatus(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), DiscriminativeSupervision::Initialize(), KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), kaldi::nnet3::ProcessFile(), ParseOptions::Read(), TransitionModel::Read(), SplitDiscriminativeSupervisionOptions::Register(), ParseOptions::Register(), ExampleGenerationConfig::Register(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), and SequentialTableReader< Holder >::Value().

155  {
156  try {
157  using namespace kaldi;
158  using namespace kaldi::nnet3;
159  typedef kaldi::int32 int32;
160  typedef kaldi::int64 int64;
161 
162  const char *usage =
163  "Get frame-by-frame examples of data for nnet3+sequence neural network\n"
164  "training. This involves breaking up utterances into pieces of sizes\n"
165  "determined by the --num-frames option.\n"
166  "\n"
167  "Usage: nnet3-discriminative-get-egs [options] <model> <features-rspecifier> "
168  "<denominator-lattice-rspecifier> <numerator-alignment-rspecifier> <egs-wspecifier>\n"
169  "\n"
170  "An example [where $feats expands to the actual features]:\n"
171  " nnet3-discriminative-get-egs --left-context=25 --right-context=9 --num-frames=150,100,90 \\\n"
172  " \"$feats\" \"ark,s,cs:gunzip -c lat.1.gz\" scp:ali.scp ark:degs.1.ark\n";
173 
174  bool compress = true;
175  int32 length_tolerance = 100, online_ivector_period = 1;
176 
177  std::string online_ivector_rspecifier;
178 
179  ExampleGenerationConfig eg_config; // controls num-frames,
180  // left/right-context, etc.
182 
183  ParseOptions po(usage);
184 
185  eg_config.Register(&po);
186  po.Register("compress", &compress, "If true, write egs in "
187  "compressed format (recommended)");
188  po.Register("ivectors", &online_ivector_rspecifier, "Alias for --online-ivectors "
189  "option, for back compatibility");
190  po.Register("online-ivectors", &online_ivector_rspecifier, "Rspecifier of ivector "
191  "features, as a matrix.");
192  po.Register("online-ivector-period", &online_ivector_period, "Number of frames "
193  "between iVectors in matrices supplied to the --online-ivectors "
194  "option");
195  po.Register("length-tolerance", &length_tolerance, "Tolerance for "
196  "difference in num-frames between feat and ivector matrices");
197 
198  splitter_config.Register(&po);
199 
200  po.Read(argc, argv);
201 
202  if (po.NumArgs() != 5) {
203  po.PrintUsage();
204  exit(1);
205  }
206 
207  eg_config.ComputeDerived();
208  UtteranceSplitter utt_splitter(eg_config);
209 
210  std::string model_wxfilename = po.GetArg(1),
211  feature_rspecifier = po.GetArg(2),
212  den_lat_rspecifier = po.GetArg(3),
213  num_ali_rspecifier = po.GetArg(4),
214  examples_wspecifier = po.GetArg(5);
215 
216 
217  TransitionModel tmodel;
218  {
219  bool binary;
220  Input ki(model_wxfilename, &binary);
221  tmodel.Read(ki.Stream(), binary);
222  }
223 
224  SequentialBaseFloatMatrixReader feat_reader(feature_rspecifier);
225  RandomAccessLatticeReader den_lat_reader(den_lat_rspecifier);
226  RandomAccessInt32VectorReader ali_reader(num_ali_rspecifier);
227  NnetDiscriminativeExampleWriter example_writer(examples_wspecifier);
228  RandomAccessBaseFloatMatrixReader online_ivector_reader(
229  online_ivector_rspecifier);
230 
231  int32 num_err = 0;
232 
233  for (; !feat_reader.Done(); feat_reader.Next()) {
234  std::string key = feat_reader.Key();
235  const Matrix<BaseFloat> &feats = feat_reader.Value();
236  if (!den_lat_reader.HasKey(key)) {
237  KALDI_WARN << "No denominator lattice for key " << key;
238  num_err++;
239  } else if (!ali_reader.HasKey(key)) {
240  KALDI_WARN << "No numerator alignment for key " << key;
241  num_err++;
242  } else {
244  if (!supervision.Initialize(ali_reader.Value(key),
245  den_lat_reader.Value(key),
246  1.0)) {
247  KALDI_WARN << "Failed to convert lattice to supervision "
248  << "for utterance " << key;
249  num_err++;
250  continue;
251  }
252 
253  const Matrix<BaseFloat> *online_ivector_feats = NULL;
254  if (!online_ivector_rspecifier.empty()) {
255  if (!online_ivector_reader.HasKey(key)) {
256  KALDI_WARN << "No iVectors for utterance " << key;
257  num_err++;
258  continue;
259  } else {
260  // this address will be valid until we call HasKey() or Value()
261  // again.
262  online_ivector_feats = &(online_ivector_reader.Value(key));
263  }
264  }
265  if (online_ivector_feats != NULL &&
266  (abs(feats.NumRows() - (online_ivector_feats->NumRows() *
267  online_ivector_period)) > length_tolerance
268  || online_ivector_feats->NumRows() == 0)) {
269  KALDI_WARN << "Length difference between feats " << feats.NumRows()
270  << " and iVectors " << online_ivector_feats->NumRows()
271  << "exceeds tolerance " << length_tolerance;
272  num_err++;
273  continue;
274  }
275  if (!ProcessFile(splitter_config, tmodel,
276  feats, online_ivector_feats, online_ivector_period,
277  supervision, key, compress,
278  &utt_splitter, &example_writer))
279  num_err++;
280  }
281  }
282  if (num_err > 0)
283  KALDI_WARN << num_err << " utterances had errors and could "
284  "not be processed.";
285  // utt_splitter prints diagnostics.
286  return utt_splitter.ExitStatus();
287  } catch(const std::exception &e) {
288  std::cerr << e.what() << '\n';
289  return -1;
290  }
291 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
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
static bool ProcessFile(const discriminative::SplitDiscriminativeSupervisionOptions &config, const TransitionModel &tmodel, const MatrixBase< BaseFloat > &feats, const MatrixBase< BaseFloat > *ivector_feats, int32 ivector_period, const discriminative::DiscriminativeSupervision &supervision, const std::string &utt_id, bool compress, UtteranceSplitter *utt_splitter, NnetDiscriminativeExampleWriter *example_writer)
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
bool Initialize(const std::vector< int32 > &alignment, const Lattice &lat, BaseFloat weight)
void ComputeDerived()
This function decodes &#39;num_frames_str&#39; into &#39;num_frames&#39;, and ensures that the members of &#39;num_frames...