ivector-normalize-length.cc File Reference
Include dependency graph for ivector-normalize-length.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 28 of file ivector-normalize-length.cc.

References VectorBase< Real >::Dim(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), KALDI_LOG, KALDI_VLOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), VectorBase< Real >::Norm(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::Register(), VectorBase< Real >::Scale(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

28  {
29  using namespace kaldi;
30  typedef kaldi::int32 int32;
31  try {
32  const char *usage =
33  "Normalize length of iVectors to equal sqrt(feature-dimension)\n"
34  "\n"
35  "Usage: ivector-normalize-length [options] <ivector-rspecifier> "
36  "<ivector-wspecifier>\n"
37  "e.g.: \n"
38  " ivector-normalize-length ark:ivectors.ark ark:normalized_ivectors.ark\n";
39 
40  ParseOptions po(usage);
41  bool normalize = true;
42 
43  po.Register("normalize", &normalize,
44  "Set this to false to disable normalization");
45 
46  bool scaleup = true;
47  po.Register("scaleup", &scaleup,
48  "If 'true', the normalized iVector is scaled-up by 'sqrt(dim)'");
49 
50  po.Read(argc, argv);
51 
52  if (po.NumArgs() != 2) {
53  po.PrintUsage();
54  exit(1);
55  }
56 
57  std::string ivector_rspecifier = po.GetArg(1),
58  ivector_wspecifier = po.GetArg(2);
59 
60 
61  int32 num_done = 0;
62 
63  double tot_ratio = 0.0, tot_ratio2 = 0.0;
64 
65  SequentialBaseFloatVectorReader ivector_reader(ivector_rspecifier);
66  BaseFloatVectorWriter ivector_writer(ivector_wspecifier);
67 
68 
69  for (; !ivector_reader.Done(); ivector_reader.Next()) {
70  std::string key = ivector_reader.Key();
71  Vector<BaseFloat> ivector = ivector_reader.Value();
72  BaseFloat norm = ivector.Norm(2.0);
73  BaseFloat ratio = norm / sqrt(ivector.Dim()); // how much larger it is
74  // than it would be, in
75  // expectation, if normally
76  if (!scaleup) ratio = norm;
77 
78  KALDI_VLOG(2) << "Ratio for key " << key << " is " << ratio;
79  if (ratio == 0.0) {
80  KALDI_WARN << "Zero iVector";
81  } else {
82  if (normalize) ivector.Scale(1.0 / ratio);
83  }
84  ivector_writer.Write(key, ivector);
85  tot_ratio += ratio;
86  tot_ratio2 += ratio * ratio;
87  num_done++;
88  }
89 
90  KALDI_LOG << "Processed " << num_done << " iVectors.";
91  if (num_done != 0) {
92  BaseFloat avg_ratio = tot_ratio / num_done,
93  ratio_stddev = sqrt(tot_ratio2 / num_done - avg_ratio * avg_ratio);
94  KALDI_LOG << "Average ratio of iVector to expected length was "
95  << avg_ratio << ", standard deviation was " << ratio_stddev;
96  }
97  return (num_done != 0 ? 0 : 1);
98  } catch(const std::exception &e) {
99  std::cerr << e.what();
100  return -1;
101  }
102 }
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
Real Norm(Real p) const
Compute the p-th norm of the vector.
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
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 Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
void Scale(Real alpha)
Multiplies all elements by this constant.
A class representing a vector.
Definition: kaldi-vector.h:406
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
#define KALDI_LOG
Definition: kaldi-error.h:153