ivector-transform.cc
Go to the documentation of this file.
1 // ivectorbin/ivector-transform.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  "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
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 Write(const std::string &key, const T &value) const
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
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
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
int NumArgs() const
Number of positional parameters (c.f. argc-1).
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
int main(int argc, char *argv[])
#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