ivector-subtract-global-mean.cc
Go to the documentation of this file.
1 // ivectorbin/ivector-subtract-global-mean.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 
24 
25 int main(int argc, char *argv[]) {
26  using namespace kaldi;
27  typedef kaldi::int32 int32;
28  try {
29  const char *usage =
30  "Copies a table of iVectors but subtracts the global mean as\n"
31  "it does so. The mean may be specified as the first argument; if not,\n"
32  "the sum of the input iVectors is used.\n"
33  "\n"
34  "Usage: ivector-subtract-global-mean <ivector-rspecifier> <ivector-wspecifier>\n"
35  "or: ivector-subtract-global-mean <mean-rxfliename> <ivector-rspecifier> <ivector-wspecifier>\n"
36  "e.g.: ivector-subtract-global-mean scp:ivectors.scp ark:-\n"
37  "or: ivector-subtract-global-mean mean.vec scp:ivectors.scp ark:-\n"
38  "See also: ivector-mean\n";
39 
40  ParseOptions po(usage);
41 
42  bool subtract_mean = true;
43  po.Register("subtract-mean", &subtract_mean,
44  "If true, subtract mean; if false, just copy the input.");
45 
46  po.Read(argc, argv);
47 
48  if (po.NumArgs() < 2 || po.NumArgs() > 3) {
49  po.PrintUsage();
50  exit(1);
51  }
52 
53  int64 num_done = 0;
54 
55  if (po.NumArgs() == 2) {
56  std::string ivector_rspecifier = po.GetArg(1),
57  ivector_wspecifier = po.GetArg(2);
58 
59  Vector<double> sum;
60 
61  std::vector<std::pair<std::string, Vector<BaseFloat>*> > ivectors;
62 
63  SequentialBaseFloatVectorReader ivector_reader(ivector_rspecifier);
64  BaseFloatVectorWriter ivector_writer(ivector_wspecifier);
65 
66 
67  for (; !ivector_reader.Done(); ivector_reader.Next()) {
68  std::string key = ivector_reader.Key();
69  const Vector<BaseFloat> &ivector = ivector_reader.Value();
70  if (sum.Dim() == 0) sum.Resize(ivector.Dim());
71  sum.AddVec(1.0, ivector);
72  num_done++;
73  ivectors.push_back(std::make_pair(key, new Vector<BaseFloat>(ivector)));
74  }
75 
76  KALDI_LOG << "Read " << num_done << " iVectors.";
77 
78  if (num_done != 0) {
79  KALDI_LOG << "Norm of iVector mean was " << (sum.Norm(2.0) / num_done);
80  for (size_t i = 0; i < ivectors.size(); i++) {
81  std::string key = ivectors[i].first;
82  Vector<BaseFloat> *ivector = ivectors[i].second;
83  if (subtract_mean)
84  ivector->AddVec(-1.0 / num_done, sum);
85  ivector_writer.Write(key, *ivector);
86  delete ivector;
87  ivectors[i].second = NULL;
88  }
89  }
90  } else {
91  // po.NumArgs() == 3
92  std::string mean_rxfilename = po.GetArg(1),
93  ivector_rspecifier = po.GetArg(2),
94  ivector_wspecifier = po.GetArg(3);
95  Vector<BaseFloat> mean;
96  ReadKaldiObject(mean_rxfilename, &mean);
97 
98  SequentialBaseFloatVectorReader ivector_reader(ivector_rspecifier);
99  BaseFloatVectorWriter ivector_writer(ivector_wspecifier);
100  for (; !ivector_reader.Done(); ivector_reader.Next()) {
101  std::string key = ivector_reader.Key();
102  Vector<BaseFloat> ivector = ivector_reader.Value();
103  ivector.AddVec(-1.0, mean);
104  ivector_writer.Write(key, ivector);
105  num_done++;
106  }
107  }
108  KALDI_LOG << "Wrote " << num_done << " mean-subtracted iVectors";
109  return (num_done != 0 ? 0 : 1);
110 
111  } catch(const std::exception &e) {
112  std::cerr << e.what();
113  return -1;
114  }
115 }
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
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
int main(int argc, char *argv[])
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)
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
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.
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
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_LOG
Definition: kaldi-error.h:153
void AddVec(const Real alpha, const VectorBase< OtherReal > &v)
Add vector : *this = *this + alpha * rv (with casting between floats and doubles) ...