nnet3-get-egs-dense-targets.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-example.h"
#include "nnet3/nnet-example-utils.h"
Include dependency graph for nnet3-get-egs-dense-targets.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 GeneralMatrix &feats, const MatrixBase< BaseFloat > *ivector_feats, int32 ivector_period, const MatrixBase< BaseFloat > &targets, const std::string &utt_id, bool compress, int32 num_targets, int32 length_tolerance, UtteranceSplitter *utt_splitter, NnetExampleWriter *example_writer)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

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

Definition at line 148 of file nnet3-get-egs-dense-targets.cc.

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

148  {
149  try {
150  using namespace kaldi;
151  using namespace kaldi::nnet3;
152  typedef kaldi::int32 int32;
153  typedef kaldi::int64 int64;
154 
155  const char *usage =
156  "Get frame-by-frame examples of data for nnet3 neural network training.\n"
157  "This program is similar to nnet3-get-egs, but the targets here are "
158  "dense matrices instead of posteriors (sparse matrices).\n"
159  "This is useful when you want the targets to be continuous real-valued "
160  "with the neural network possibly trained with a quadratic objective\n"
161  "\n"
162  "Usage: nnet3-get-egs-dense-targets --num-targets=<n> [options] "
163  "<features-rspecifier> <targets-rspecifier> <egs-out>\n"
164  "\n"
165  "An example [where $feats expands to the actual features]:\n"
166  "nnet-get-egs-dense-targets --num-targets=26 --left-context=12 \\\n"
167  "--right-context=9 --num-frames=8 \"$feats\" \\\n"
168  "\"ark:copy-matrix ark:exp/snrs/snr.1.ark ark:- |\"\n"
169  " ark:- \n";
170 
171 
172  bool compress = true;
173  int32 num_targets = -1, length_tolerance = 100,
174  targets_length_tolerance = 2,
175  online_ivector_period = 1;
176 
177  ExampleGenerationConfig eg_config; // controls num-frames,
178  // left/right-context, etc.
179 
180  std::string online_ivector_rspecifier;
181 
182  ParseOptions po(usage);
183 
184  po.Register("compress", &compress, "If true, write egs with input features "
185  "in compressed format (recommended). This is "
186  "only relevant if the features being read are un-compressed; "
187  "if already compressed, we keep we same compressed format when "
188  "dumping egs.");
189  po.Register("num-targets", &num_targets, "Output dimension in egs, "
190  "only used to check targets have correct dim if supplied.");
191  po.Register("ivectors", &online_ivector_rspecifier, "Alias for "
192  "--online-ivectors option, for back compatibility");
193  po.Register("online-ivectors", &online_ivector_rspecifier, "Rspecifier of "
194  "ivector features, as a matrix.");
195  po.Register("online-ivector-period", &online_ivector_period, "Number of "
196  "frames between iVectors in matrices supplied to the "
197  "--online-ivectors option");
198  po.Register("length-tolerance", &length_tolerance, "Tolerance for "
199  "difference in num-frames between feat and ivector matrices");
200  po.Register("targets-length-tolerance", &targets_length_tolerance,
201  "Tolerance for "
202  "difference in num-frames (after subsampling) between "
203  "feature and target matrices");
204  eg_config.Register(&po);
205 
206  po.Read(argc, argv);
207 
208  if (po.NumArgs() != 3) {
209  po.PrintUsage();
210  exit(1);
211  }
212 
213  eg_config.ComputeDerived();
214  UtteranceSplitter utt_splitter(eg_config);
215 
216  std::string feature_rspecifier = po.GetArg(1),
217  matrix_rspecifier = po.GetArg(2),
218  examples_wspecifier = po.GetArg(3);
219 
220  // SequentialGeneralMatrixReader can read either a Matrix or
221  // CompressedMatrix (or SparseMatrix, but not as relevant here),
222  // and it retains the type. This way, we can generate parts of
223  // the feature matrices without uncompressing and re-compressing.
224  SequentialGeneralMatrixReader feat_reader(feature_rspecifier);
225  RandomAccessBaseFloatMatrixReader matrix_reader(matrix_rspecifier);
226  NnetExampleWriter example_writer(examples_wspecifier);
227  RandomAccessBaseFloatMatrixReader online_ivector_reader(
228  online_ivector_rspecifier);
229 
230  int32 num_err = 0;
231 
232  for (; !feat_reader.Done(); feat_reader.Next()) {
233  std::string key = feat_reader.Key();
234  const GeneralMatrix &feats = feat_reader.Value();
235  if (!matrix_reader.HasKey(key)) {
236  KALDI_WARN << "No target matrix for key " << key;
237  num_err++;
238  } else {
239  const Matrix<BaseFloat> &target_matrix = matrix_reader.Value(key);
240  const Matrix<BaseFloat> *online_ivector_feats = NULL;
241  if (!online_ivector_rspecifier.empty()) {
242  if (!online_ivector_reader.HasKey(key)) {
243  KALDI_WARN << "No iVectors for utterance " << key;
244  num_err++;
245  continue;
246  } else {
247  // this address will be valid until we call HasKey() or Value()
248  // again.
249  online_ivector_feats = &(online_ivector_reader.Value(key));
250  }
251  }
252 
253  if (online_ivector_feats != NULL &&
254  (abs(feats.NumRows() - (online_ivector_feats->NumRows() *
255  online_ivector_period)) > length_tolerance
256  || online_ivector_feats->NumRows() == 0)) {
257  KALDI_WARN << "Length difference between feats " << feats.NumRows()
258  << " and iVectors " << online_ivector_feats->NumRows()
259  << "exceeds tolerance " << length_tolerance;
260  num_err++;
261  continue;
262  }
263 
264  if (!ProcessFile(feats, online_ivector_feats, online_ivector_period,
265  target_matrix, key, compress, num_targets,
266  targets_length_tolerance,
267  &utt_splitter, &example_writer))
268  num_err++;
269  }
270  }
271  if (num_err > 0)
272  KALDI_WARN << num_err << " utterances had errors and could "
273  "not be processed.";
274  // utt_splitter prints stats in its destructor.
275  return utt_splitter.ExitStatus();
276  } catch(const std::exception &e) {
277  std::cerr << e.what() << '\n';
278  return -1;
279  }
280 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
This class is a wrapper that enables you to store a matrix in one of three forms: either as a Matrix<...
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)
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
MatrixIndexT NumRows() const
void ComputeDerived()
This function decodes &#39;num_frames_str&#39; into &#39;num_frames&#39;, and ensures that the members of &#39;num_frames...