ivector-subtract-global-mean.cc File Reference
Include dependency graph for ivector-subtract-global-mean.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 25 of file ivector-subtract-global-mean.cc.

References VectorBase< Real >::AddVec(), VectorBase< Real >::Dim(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), rnnlm::i, KALDI_LOG, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), VectorBase< Real >::Norm(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadKaldiObject(), ParseOptions::Register(), Vector< Real >::Resize(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

25  {
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
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).
Real Norm(Real p) const
Compute the p-th norm of the vector.
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
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
#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) ...