ivector-normalize-length.cc
Go to the documentation of this file.
1 // ivectorbin/ivector-normalize-length.cc
2 
3 // Copyright 2013 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 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "gmm/am-diag-gmm.h"
25 #include "util/kaldi-thread.h"
26 
27 
28 int main(int argc, char *argv[]) {
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
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
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.
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
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.
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
void Scale(Real alpha)
Multiplies all elements by this constant.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
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
int main(int argc, char *argv[])