All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
transform-vec.cc File Reference
Include dependency graph for transform-vec.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 26 of file transform-vec.cc.

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

26  {
27  try {
28  using namespace kaldi;
29 
30  const char *usage =
31  "This program applies a linear or affine transform to individual vectors, e.g.\n"
32  "iVectors. It is transform-feats, except it works on vectors rather than matrices,\n"
33  "and expects a single transform matrix rather than possibly a table of matrices\n"
34  "\n"
35  "Usage: transform-vec [options] <transform-rxfilename> <feats-rspecifier> <feats-wspecifier>\n"
36  "See also: transform-feats, est-pca\n";
37 
38  ParseOptions po(usage);
39 
40  po.Read(argc, argv);
41 
42  if (po.NumArgs() != 3) {
43  po.PrintUsage();
44  exit(1);
45  }
46 
47  std::string transform_rxfilename = po.GetArg(1);
48  std::string vec_rspecifier = po.GetArg(2);
49  std::string vec_wspecifier = po.GetArg(3);
50 
51  SequentialBaseFloatVectorReader vec_reader(vec_rspecifier);
52  BaseFloatVectorWriter vec_writer(vec_wspecifier);
53 
54  Matrix<BaseFloat> transform;
55  ReadKaldiObject(transform_rxfilename, &transform);
56 
57  int32 num_done = 0;
58 
59  for (; !vec_reader.Done(); vec_reader.Next()) {
60  std::string key = vec_reader.Key();
61  const Vector<BaseFloat> &vec(vec_reader.Value());
62 
63  int32 transform_rows = transform.NumRows(),
64  transform_cols = transform.NumCols(),
65  vec_dim = vec.Dim();
66 
67  Vector<BaseFloat> vec_out(transform_rows);
68 
69  if (transform_cols == vec_dim) {
70  vec_out.AddMatVec(1.0, transform, kNoTrans, vec, 0.0);
71  } else {
72  if (transform_cols != vec_dim + 1) {
73  KALDI_ERR << "Dimension mismatch: input vector has dimension "
74  << vec.Dim() << " and transform has " << transform_cols
75  << " columns.";
76  }
77  vec_out.CopyColFromMat(transform, vec_dim);
78  vec_out.AddMatVec(1.0, transform.Range(0, transform.NumRows(),
79  0, vec_dim), kNoTrans, vec, 1.0);
80  }
81  vec_writer.Write(key, vec_out);
82  num_done++;
83  }
84 
85  KALDI_LOG << "Applied transform to " << num_done << " vectors.";
86 
87  return (num_done != 0 ? 0 : 1);
88  } catch(const std::exception &e) {
89  std::cerr << e.what();
90  return -1;
91  }
92 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:818
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
#define KALDI_ERR
Definition: kaldi-error.h:127
SubMatrix< Real > Range(const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Return a sub-part of matrix.
Definition: kaldi-matrix.h:196
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
#define KALDI_LOG
Definition: kaldi-error.h:133