ivector-randomize.cc
Go to the documentation of this file.
1 // online2bin/ivector-randomize.cc
2 
3 // Copyright 2014 Johns Hopkins University (author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #include "base/kaldi-common.h"
21 #include "util/common-utils.h"
22 #include "matrix/kaldi-matrix.h"
24 
25 
26 int main(int argc, char *argv[]) {
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 }
87 
88 
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
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
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
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
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
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
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
int main(int argc, char *argv[])
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95