ivector-randomize.cc File Reference
Include dependency graph for ivector-randomize.cc:

Go to the source code of this file.

Functions

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

Function Documentation

◆ main()

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

Definition at line 26 of file ivector-randomize.cc.

References SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), KALDI_LOG, SequentialTableReader< Holder >::Key(), kaldi::kUndefined, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), kaldi::RandInt(), ParseOptions::Read(), ParseOptions::Register(), MatrixBase< Real >::Row(), SequentialTableReader< Holder >::Value(), kaldi::WithProb(), and TableWriter< Holder >::Write().

26  {
27  try {
28  using namespace kaldi;
29 
30  const char *usage =
31  "Copy matrices of online-estimated iVectors, but randomize them;\n"
32  "this is intended primarily for training the online nnet2 setup\n"
33  "with iVectors. For each input matrix, each row with index t is,\n"
34  "with probability given by the option --randomize-prob, replaced\n"
35  "with the contents an input row chosen randomly from the interval [t, T]\n"
36  "where T is the index of the last row of the matrix.\n"
37  "\n"
38  "Usage: ivector-randomize [options] <ivector-rspecifier> <ivector-wspecifier>\n"
39  " e.g.: ivector-randomize ark:- ark:-\n"
40  "See also: ivector-extract-online, ivector-extract-online2, subsample-feats\n";
41 
42  int32 srand_seed = 0;
43  BaseFloat randomize_prob = 0.5;
44 
45  ParseOptions po(usage);
46 
47  po.Register("srand", &srand_seed, "Seed for random number generator");
48  po.Register("randomize-prob", &randomize_prob, "For each row, replace it with a "
49  "random row with this probability.");
50 
51  po.Read(argc, argv);
52 
53  if (po.NumArgs() != 2) {
54  po.PrintUsage();
55  exit(1);
56  }
57 
58 
59  std::string ivector_rspecifier = po.GetArg(1),
60  ivector_wspecifier = po.GetArg(2);
61 
62  int num_done = 0;
63  SequentialBaseFloatMatrixReader reader(ivector_rspecifier);
64  BaseFloatMatrixWriter writer(ivector_wspecifier);
65 
66  for (; !reader.Done(); reader.Next(), num_done++) {
67  std::string utt = reader.Key();
68  const Matrix<BaseFloat> &ivectors_in = reader.Value();
69  int32 T = ivectors_in.NumRows(), dim = ivectors_in.NumCols();
70  Matrix<BaseFloat> ivectors_out(T, dim, kUndefined);
71  for (int32 t = 0; t < T; t++) {
72  int32 t_src;
73  if (WithProb(randomize_prob)) t_src = RandInt(t, T-1);
74  else t_src = t;
75  ivectors_out.Row(t).CopyFromVec(ivectors_in.Row(t_src));
76  }
77  writer.Write(utt, ivectors_out);
78  num_done++;
79  }
80  KALDI_LOG << "Randomized " << num_done << " iVectors.";
81  return (num_done != 0 ? 0 : 1);
82  } catch(const std::exception &e) {
83  std::cerr << e.what();
84  return -1;
85  }
86 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
bool WithProb(BaseFloat prob, struct RandomState *state)
Definition: kaldi-math.cc:72
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
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
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
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
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95