get-full-lda-mat.cc
Go to the documentation of this file.
1 // featbin/get-full-lda-mat.cc
2 
3 // Copyright 2012-2013 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)(d, i) = 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  "This is a special-purpose program to be used in \"predictive SGMMs\".\n"
60  "It takes in an LDA+MLLT matrix, and the original \"full\" LDA matrix\n"
61  "as output by the --write-full-matrix option of est-lda; and it writes\n"
62  "out a \"full\" LDA+MLLT matrix formed by the LDA+MLLT matrix plus the\n"
63  "remaining rows of the \"full\" LDA matrix; and also writes out its inverse\n"
64  "Usage: get-full-lda-mat [options] <lda-mllt-rxfilename> <full-lda-rxfilename> "
65  "<full-lda-mllt-wxfilename> [<inv-full-lda-mllt-wxfilename>]\n"
66  "E.g.: get-full-lda-mat final.mat full.mat full_lda_mllt.mat full_lda_mllt_inv.mat\n";
67 
68  bool binary = true;
69  ParseOptions po(usage);
70 
71  po.Register("binary", &binary, "Write in binary mode (only relevant if output is a wxfilename)");
72 
73  po.Read(argc, argv);
74 
75  if (po.NumArgs() < 3 || po.NumArgs() > 4) {
76  po.PrintUsage();
77  exit(1);
78  }
79 
80  std::string lda_mllt_rxfilename = po.GetArg(1),
81  full_lda_rxfilename = po.GetArg(2),
82  full_lda_mllt_wxfilename = po.GetArg(3),
83  inv_full_lda_mllt_wxfilename = po.GetOptArg(4);
84 
85  Matrix<BaseFloat> lda_mllt;
86  ReadKaldiObject(lda_mllt_rxfilename, &lda_mllt);
87  Matrix<BaseFloat> full_lda;
88  ReadKaldiObject(full_lda_rxfilename, &full_lda);
89 
90  KALDI_ASSERT(full_lda.NumCols() == lda_mllt.NumCols());
91  KALDI_ASSERT(full_lda.NumRows() == full_lda.NumCols());
92 
93  Matrix<BaseFloat> full_lda_mllt(full_lda);
94  full_lda_mllt.Range(0, lda_mllt.NumRows(),
95  0, lda_mllt.NumCols()).CopyFromMat(lda_mllt);
96 
97  WriteKaldiObject(full_lda_mllt, full_lda_mllt_wxfilename, binary);
98 
99  if (po.NumArgs() != 3) {
100  full_lda_mllt.Invert();
101  WriteKaldiObject(full_lda_mllt, inv_full_lda_mllt_wxfilename, binary);
102  }
103  return 0;
104  } catch(const std::exception &e) {
105  std::cerr << e.what();
106  return -1;
107  }
108 }
109 
110 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
kaldi::int32 int32
void Register(const std::string &name, bool *ptr, const std::string &doc)
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
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.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
void IncreaseTransformDimension(int32 new_dimension, Matrix< BaseFloat > *mat)
int main(int argc, char *argv[])
std::string GetOptArg(int param) const