ivector-transform.cc File Reference
Include dependency graph for ivector-transform.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-transform.cc.

References VectorBase< Real >::AddMatVec(), VectorBase< Real >::CopyColFromMat(), VectorBase< Real >::Dim(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), KALDI_ASSERT, KALDI_LOG, SequentialTableReader< Holder >::Key(), kaldi::kNoTrans, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadKaldiObject(), SequentialTableReader< Holder >::Value(), kaldi::VecVec(), and TableWriter< Holder >::Write().

28  {
29  using namespace kaldi;
30  typedef kaldi::int32 int32;
31  try {
32  const char *usage =
33  "Multiplies iVectors (on the left) by a supplied transformation matrix\n"
34  "\n"
35  "Usage: ivector-transform [options] <matrix-in> <ivector-rspecifier>"
36  "<ivector-wspecifier>\n"
37  "e.g.: \n"
38  " ivector-transform transform.mat ark:ivectors.ark ark:transformed_ivectors.ark\n";
39 
40  ParseOptions po(usage);
41 
42  po.Read(argc, argv);
43 
44  if (po.NumArgs() != 3) {
45  po.PrintUsage();
46  exit(1);
47  }
48 
49  std::string matrix_rxfilename = po.GetArg(1),
50  ivector_rspecifier = po.GetArg(2),
51  ivector_wspecifier = po.GetArg(3);
52 
53 
54  Matrix<BaseFloat> transform;
55  ReadKaldiObject(matrix_rxfilename, &transform);
56 
57  int32 num_done = 0;
58 
59  // The following quantities will be needed if we're doing
60  // an affine transform (i.e. linear plus an offset)
61  SubMatrix<BaseFloat> linear_term(transform,
62  0, transform.NumRows(),
63  0, transform.NumCols() - 1);
64  Vector<BaseFloat> constant_term(transform.NumRows());
65  constant_term.CopyColFromMat(transform, transform.NumCols() - 1);
66 
67  Vector<double> sum(transform.NumRows());
68  double sumsq = 0.0;
69 
70  SequentialBaseFloatVectorReader ivector_reader(ivector_rspecifier);
71  BaseFloatVectorWriter ivector_writer(ivector_wspecifier);
72 
73  for (; !ivector_reader.Done(); ivector_reader.Next()) {
74  std::string key = ivector_reader.Key();
75  const Vector<BaseFloat> &ivector = ivector_reader.Value();
76 
77  Vector<BaseFloat> transformed_ivector(transform.NumRows());
78  if (ivector.Dim() == transform.NumCols()) {
79  transformed_ivector.AddMatVec(1.0, transform, kNoTrans, ivector, 0.0);
80  } else {
81  KALDI_ASSERT(ivector.Dim() == transform.NumCols() - 1);
82  transformed_ivector.CopyFromVec(constant_term);
83  transformed_ivector.AddMatVec(1.0, linear_term, kNoTrans, ivector, 1.0);
84  }
85  sum.AddVec(1.0, transformed_ivector);
86  sumsq += VecVec(transformed_ivector, transformed_ivector);
87  ivector_writer.Write(key, transformed_ivector);
88  num_done++;
89  }
90 
91  KALDI_LOG << "Processed " << num_done << " iVectors.";
92  if (num_done != 0) {
93  sum.Scale(1.0 / num_done);
94  sumsq /= num_done;
95  BaseFloat mean_length = sum.Norm(2.0),
96  variance = sumsq - VecVec(sum, sum),
97  avg_len = sqrt(variance),
98  norm_length = avg_len / sqrt(transform.NumRows());
99  KALDI_LOG << "Norm of mean was " << mean_length
100  << " (should be close to zero), length divided by sqrt(dim) was "
101  << norm_length << " (should probably be close to one)";
102  }
103  return (num_done != 0 ? 0 : 1);
104  } catch(const std::exception &e) {
105  std::cerr << e.what();
106  return -1;
107  }
108 }
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
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 ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
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
MatrixIndexT Dim() const
Returns the dimension of the vector.
Definition: kaldi-vector.h:64
void AddMatVec(const Real alpha, const MatrixBase< Real > &M, const MatrixTransposeType trans, const VectorBase< Real > &v, const Real beta)
Add matrix times vector : this <– beta*this + alpha*M*v.
Definition: kaldi-vector.cc:92
void CopyColFromMat(const MatrixBase< OtherReal > &M, MatrixIndexT col)
Extracts a column of the matrix M.
A class representing a vector.
Definition: kaldi-vector.h:406
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
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
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
Definition: kaldi-vector.cc:37
Sub-matrix representation.
Definition: kaldi-matrix.h:988