sgmm2-project.cc File Reference
Include dependency graph for sgmm2-project.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 27 of file sgmm2-project.cc.

References MatrixBase< Real >::AddMatMat(), Sgmm2Project::ApplyProjection(), AmSgmm2::ComputeDerivedVars(), Sgmm2Project::ComputeProjection(), ParseOptions::GetArg(), KALDI_ASSERT, KALDI_LOG, kaldi::kNoTrans, ParseOptions::NumArgs(), MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), ParseOptions::Read(), TransitionModel::Read(), AmSgmm2::Read(), kaldi::ReadKaldiObject(), ParseOptions::Register(), Output::Stream(), Input::Stream(), kaldi::StringToSgmmWriteFlags(), TransitionModel::Write(), AmSgmm2::Write(), and kaldi::WriteKaldiObject().

27  {
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 }
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 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 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
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)
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