sgmm2-project.cc
Go to the documentation of this file.
1 // sgmm2bin/sgmm2-project.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 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "util/kaldi-thread.h"
24 #include "hmm/transition-model.h"
25 #include "sgmm2/am-sgmm2-project.h"
26 
27 int main(int argc, char *argv[]) {
28  try {
29  using namespace kaldi;
30  typedef kaldi::int32 int32;
31  const char *usage =
32  "Compute SGMM model projection that only models a part of a pre-LDA space.\n"
33  "Used in predictive SGMMs. Takes as input an LDA+MLLT transform,\n"
34  "and outputs a transform from the pre-LDA+MLLT space to the space that\n"
35  "we want to model\n"
36  "Usage: sgmm2-project [options] <model-in> <lda-mllt-mat-in> <model-out> <new-projection-out>\n"
37  "e.g.: sgmm2-project --start-dim=0 --end-dim=52 final.mdl final.inv_full_mat final_proj1.mdl proj1.mat\n";
38 
39  std::string write_flags_str = "gsnu";
40 
41  bool binary_write = false;
42  int32 start_dim = 0;
43  int32 end_dim = 0;
44 
45  ParseOptions po(usage);
46  po.Register("binary", &binary_write, "Write output in binary mode");
47  po.Register("start-dim", &start_dim, "Starting dimension to keep in "
48  "pre-LDA-MLLT space.");
49  po.Register("end-dim", &end_dim, "Ending dimension to keep in "
50  "pre-LDA-MLLT space (equals last retained dimension plus one)");
51 
52  po.Read(argc, argv);
53  if (po.NumArgs() != 4) {
54  po.PrintUsage();
55  exit(1);
56  }
57  std::string model_rxfilename = po.GetArg(1),
58  lda_mllt_rxfilename = po.GetArg(2),
59  model_wxfilename = po.GetArg(3),
60  proj_wxfilename = po.GetArg(4);
61 
62  kaldi::SgmmWriteFlagsType write_flags =
63  StringToSgmmWriteFlags(write_flags_str);
64 
65  AmSgmm2 am_sgmm;
66  TransitionModel trans_model;
67  {
68  bool binary;
69  Input ki(model_rxfilename, &binary);
70  trans_model.Read(ki.Stream(), binary);
71  am_sgmm.Read(ki.Stream(), binary);
72  }
73 
74 
75  Matrix<BaseFloat> lda_mllt_mat;
76  ReadKaldiObject(lda_mllt_rxfilename, &lda_mllt_mat);
77 
78  // Need the full LDA+MLLT matrix, including the extra rows.
79  // See featbin/extend-transform.cc
80  KALDI_ASSERT(lda_mllt_mat.NumRows() == lda_mllt_mat.NumCols());
81 
82  Matrix<BaseFloat> inv_lda_mllt_mat(lda_mllt_mat);
83  inv_lda_mllt_mat.Invert();
84 
85  Matrix<BaseFloat> projection;
86  Sgmm2Project sgmm_project;
87  sgmm_project.ComputeProjection(am_sgmm, inv_lda_mllt_mat, start_dim, end_dim,
88  &projection);
89 
90  Matrix<BaseFloat> total_projection(projection.NumRows(), projection.NumCols());
91  total_projection.AddMatMat(1.0, projection, kNoTrans,
92  inv_lda_mllt_mat, kNoTrans, 0.0);
93 
94  sgmm_project.ApplyProjection(total_projection, &am_sgmm);
95 
96  am_sgmm.ComputeDerivedVars(); // recompute normalizers, and possibly
97  // weights.
98 
99  {
100  Output ko(model_wxfilename, binary_write);
101  trans_model.Write(ko.Stream(), binary_write);
102  am_sgmm.Write(ko.Stream(), binary_write, write_flags);
103  }
104  KALDI_LOG << "Wrote model to " << model_wxfilename;
105 
106  WriteKaldiObject(projection, proj_wxfilename, binary_write);
107  KALDI_LOG << "Wrote projection matrix to " << proj_wxfilename;
108 
109  return 0;
110  } catch(const std::exception &e) {
111  std::cerr << e.what();
112  return -1;
113  }
114 }
115 
116 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
uint16 SgmmWriteFlagsType
Bitwise OR of the above flags.
Definition: model-common.h:70
void Write(std::ostream &os, bool binary, SgmmWriteFlagsType write_params) const
Definition: am-sgmm2.cc:203
Class for definition of the subspace Gmm acoustic model.
Definition: am-sgmm2.h:231
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].
void Read(std::istream &is, bool binary)
Definition: am-sgmm2.cc:89
void ComputeProjection(const AmSgmm2 &sgmm, const Matrix< BaseFloat > &inv_lda_mllt, int32 begin_dim, int32 end_dim, Matrix< BaseFloat > *projection)
kaldi::int32 int32
void ApplyProjection(const Matrix< BaseFloat > &total_projection, AmSgmm2 *sgmm)
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
std::istream & Stream()
Definition: kaldi-io.cc:826
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
std::ostream & Stream()
Definition: kaldi-io.cc:701
void Read(std::istream &is, bool binary)
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int main(int argc, char *argv[])
int NumArgs() const
Number of positional parameters (c.f. argc-1).
void Write(std::ostream &os, bool binary) const
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void ComputeDerivedVars()
Computes (and initializes if necessary) derived vars...
Definition: am-sgmm2.cc:810
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
SgmmUpdateFlagsType StringToSgmmWriteFlags(std::string str)
Definition: model-common.cc:86
#define KALDI_LOG
Definition: kaldi-error.h:153