transform-vec.cc
Go to the documentation of this file.
1 // bin/transform-vec.cc
2 
3 // Copyright 2009-2012 Microsoft Corporation
4 // 2012-2014 Johns Hopkins University (author: Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "matrix/kaldi-matrix.h"
24 
25 
26 int main(int argc, char *argv[]) {
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 }
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
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.
#define KALDI_ERR
Definition: kaldi-error.h:147
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
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).
int main(int argc, char *argv[])
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
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
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:202
#define KALDI_LOG
Definition: kaldi-error.h:153