nnet-get-feature-transform.cc
Go to the documentation of this file.
1 // nnet2bin/nnet-get-feature-transform.cc
2 
3 // Copyright 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 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
24 
25 int main(int argc, char *argv[]) {
26  using namespace kaldi;
27  typedef kaldi::int32 int32;
28  try {
29  const char *usage =
30  "Get feature-projection transform using stats obtained with acc-lda.\n"
31  "See comments in the code of nnet2/get-feature-transform.h for more\n"
32  "information.\n"
33  "\n"
34  "Usage: nnet-get-feature-transform [options] <matrix-out> <lda-acc-1> <lda-acc-2> ...\n";
35 
36  bool binary = true;
38  std::string write_cholesky;
39  std::string write_within_covar;
40  ParseOptions po(usage);
41  po.Register("binary", &binary, "Write outputs in binary mode.");
42  po.Register("write-cholesky", &write_cholesky, "If supplied, write to this "
43  "wxfilename the Cholesky factor of the within-class covariance. "
44  "Can be used for perturbing features. E.g. "
45  "--write-cholesky=exp/nnet5/cholesky.tpmat");
46  po.Register("write-within-covar", &write_within_covar, "If supplied, write "
47  "to this wxfilename the within-class covariance (as a symmetric "
48  "matrix). E.g. --write-within-covar=exp/nnet5/within_covar.mat");
49  opts.Register(&po);
50  po.Read(argc, argv);
51 
52  if (po.NumArgs() < 2) {
53  po.PrintUsage();
54  exit(1);
55  }
56 
58  std::string projection_wxfilename = po.GetArg(1);
59 
60  for (int32 i = 2; i <= po.NumArgs(); i++) {
61  bool binary_in, add = true;
62  Input ki(po.GetArg(i), &binary_in);
63  fte.Read(ki.Stream(), binary_in, add);
64  }
65 
67  TpMatrix<BaseFloat> cholesky;
68  fte.Estimate(opts, &mat,
69  (write_cholesky != "" || write_within_covar != "" ?
70  &cholesky : NULL));
71  WriteKaldiObject(mat, projection_wxfilename, binary);
72  if (write_cholesky != "") {
73  WriteKaldiObject(cholesky, write_cholesky, binary);
74  }
75  if (write_within_covar != "") {
76  SpMatrix<BaseFloat> within_var(cholesky.NumRows());
77  within_var.AddTp2(1.0, cholesky, kNoTrans, 0.0);
78  WriteKaldiObject(within_var, write_within_covar, binary);
79  }
80  return 0;
81  } catch(const std::exception &e) {
82  std::cerr << e.what();
83  return -1;
84  }
85 }
86 
87 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Packed symetric matrix class.
Definition: matrix-common.h:62
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
void Estimate(const FeatureTransformEstimateOptions &opts, Matrix< BaseFloat > *M, TpMatrix< BaseFloat > *within_cholesky) const
Estimates the LDA transform matrix m.
kaldi::int32 int32
MatrixIndexT NumRows() const
This file is modified from transform/lda-estimate.h It contains a class intended to be used in precon...
void Register(const std::string &name, bool *ptr, const std::string &doc)
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
void Read(std::istream &in_stream, bool binary, bool add)
void AddTp2(const Real alpha, const TpMatrix< Real > &T, MatrixTransposeType transM, const Real beta=0.0)
The following function does: this <– beta*this + alpha * T * T^T.
Definition: sp-matrix.cc:1156
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
Packed symetric matrix class.
Definition: matrix-common.h:63
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).
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
int main(int argc, char *argv[])
Class for computing a feature transform used for preconditioning of the training data in neural-netwo...