extend-transform-dim.cc
Go to the documentation of this file.
1 // featbin/extend-transform-dim.cc
2 
3 // Copyright 2012 Johns Hopkins University (author: 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 #include "base/kaldi-common.h"
21 #include "util/common-utils.h"
22 #include "matrix/kaldi-matrix.h"
24 
25 namespace kaldi {
26 void IncreaseTransformDimension(int32 new_dimension,
27  Matrix<BaseFloat> *mat) {
28  int32 d = mat->NumRows();
29  if (new_dimension < d)
30  KALDI_ERR << "--new-dimension argument invalid or not specified: "
31  << new_dimension << " < " << d;
32  if (mat->NumCols() == d) { // linear transform d->d
33  mat->Resize(new_dimension, new_dimension, kCopyData);
34  for (int32 i = d; i < new_dimension; i++)
35  (*mat)(i, i) = 1.0; // set new dims to unit matrix.
36  } else if (mat->NumCols() == d+1) { // affine transform d->d.
37  Vector<BaseFloat> offset(mat->NumRows());
38  offset.CopyColFromMat(*mat, d);
39  mat->Resize(d, d, kCopyData); // remove offset from mat->
40  mat->Resize(new_dimension, new_dimension+1, kCopyData); // extend with zeros.
41  for (int32 i = d; i < new_dimension; i++)
42  (*mat)(i, i) = 1.0; // set new dims to unit matrix.
43  for (int32 i = 0; i < d; i++) // and set offset [last column]
44  (*mat)(i, new_dimension) = offset(i);
45  } else {
46  KALDI_ERR << "Input matrix has unexpected dimension " << d
47  << " x " << mat->NumCols();
48  }
49 }
50 
51 } // end namespace kaldi
52 
53 
54 int main(int argc, char *argv[]) {
55  try {
56  using namespace kaldi;
57 
58  const char *usage =
59  "Read in transform from dimension d -> d (affine or linear), and output a transform\n"
60  "from dimension e -> e (with e >= d, and e controlled by option --new-dimension).\n"
61  "This new transform will leave the extra dimension unaffected, and transform the old\n"
62  "dimensions in the same way.\n"
63  "Usage: extend-transform-dim [options] (transform-A-rspecifier|transform-A-rxfilename) (transform-out-wspecifier|transform-out-wxfilename)\n"
64  "E.g.: extend-transform-dim --new-dimension=117 in.mat big.mat\n";
65 
66  bool binary = true;
67  int32 new_dimension = -1;
68  ParseOptions po(usage);
69 
70  po.Register("binary", &binary, "Write in binary mode (only relevant if output is a wxfilename)");
71  po.Register("new-dimension", &new_dimension,
72  "Larger dimension we are changing matrix to");
73 
74  po.Read(argc, argv);
75 
76  if (po.NumArgs() != 2) {
77  po.PrintUsage();
78  exit(1);
79  }
80 
81  std::string transform_in_fn = po.GetArg(1);
82  std::string transform_out_fn = po.GetArg(2);
83  // all these "fn"'s are either rspecifiers or filenames.
84 
85  bool in_is_rspecifier =
86  (ClassifyRspecifier(transform_in_fn, NULL, NULL)
87  != kNoRspecifier),
88  out_is_wspecifier =
89  (ClassifyWspecifier(transform_out_fn, NULL, NULL, NULL)
90  != kNoWspecifier);
91 
92  if (in_is_rspecifier != out_is_wspecifier)
93  KALDI_ERR << "Either none or both of the (input, output) must be a Table.";
94 
95  if (in_is_rspecifier) {
96  SequentialBaseFloatMatrixReader reader(transform_in_fn);
97  BaseFloatMatrixWriter writer(transform_out_fn);
98  int32 num_done = 0;
99  for (; !reader.Done(); reader.Next()) {
100  std::string key = reader.Key();
101  Matrix<BaseFloat> mat(reader.Value());
102  IncreaseTransformDimension(new_dimension, &mat);
103  writer.Write(key, mat);
104  num_done++;
105  }
106  KALDI_LOG << "Increased transform dim to " << new_dimension
107  << " for " << num_done << " matrices.";
108  return (num_done != 0 ? 0 : 1);
109  } else {
110  Matrix<BaseFloat> mat;
111  ReadKaldiObject(transform_in_fn, &mat);
112  int32 old_dim = mat.NumRows();
113  IncreaseTransformDimension(new_dimension, &mat);
114  WriteKaldiObject(mat, transform_out_fn, binary);
115  KALDI_LOG << "Increased transform dim from " << old_dim << " to "
116  << mat.NumRows() << " and wrote to " << transform_out_fn;
117  return 0;
118  }
119  } catch(const std::exception &e) {
120  std::cerr << e.what();
121  return -1;
122  }
123 }
124 
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
int main(int argc, char *argv[])
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
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.
WspecifierType ClassifyWspecifier(const std::string &wspecifier, std::string *archive_wxfilename, std::string *script_wxfilename, WspecifierOptions *opts)
Definition: kaldi-table.cc:135
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
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
void IncreaseTransformDimension(int32 new_dimension, Matrix< BaseFloat > *mat)
#define KALDI_LOG
Definition: kaldi-error.h:153