gmm-transform-means.cc File Reference
Include dependency graph for gmm-transform-means.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 28 of file gmm-transform-means.cc.

References MatrixBase< Real >::AddMatMat(), DiagGmm::ComputeGconsts(), MatrixBase< Real >::CopyFromMat(), AmDiagGmm::Dim(), ParseOptions::GetArg(), DiagGmm::GetMeans(), AmDiagGmm::GetPdf(), rnnlm::i, KALDI_ERR, KALDI_LOG, kaldi::kNoTrans, kaldi::kTrans, ParseOptions::NumArgs(), MatrixBase< Real >::NumCols(), AmDiagGmm::NumPdfs(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), kaldi::ReadKaldiObject(), ParseOptions::Register(), MatrixBase< Real >::Set(), DiagGmm::SetMeans(), Output::Stream(), Input::Stream(), AmDiagGmm::Write(), and TransitionModel::Write().

28  {
29  try {
30  using namespace kaldi;
31  typedef kaldi::int32 int32;
32 
33  const char *usage =
34  "Transform GMM means with linear or affine transform\n"
35  "Usage: gmm-transform-means <transform-matrix> <model-in> <model-out>\n"
36  "e.g.: gmm-transform-means 2.mat 2.mdl 3.mdl\n";
37 
38  bool binary = true; // write in binary if true.
39 
40  ParseOptions po(usage);
41  po.Register("binary", &binary, "Write output in binary mode");
42 
43  po.Read(argc, argv);
44 
45  if (po.NumArgs() != 3) {
46  po.PrintUsage();
47  exit(1);
48  }
49 
50  std::string mat_rxfilename = po.GetArg(1),
51  model_in_rxfilename = po.GetArg(2),
52  model_out_wxfilename = po.GetArg(3);
53 
55  ReadKaldiObject(mat_rxfilename, &mat);
56 
57  AmDiagGmm am_gmm;
58  TransitionModel trans_model;
59  {
60  bool binary_read;
61  Input ki(model_in_rxfilename, &binary_read);
62  trans_model.Read(ki.Stream(), binary_read);
63  am_gmm.Read(ki.Stream(), binary_read);
64  }
65 
66  int32 dim = am_gmm.Dim();
67  if (mat.NumRows() != dim)
68  KALDI_ERR << "Transform matrix has " << mat.NumRows() << " rows but "
69  "model has dimension " << am_gmm.Dim();
70  if (mat.NumCols() != dim
71  && mat.NumCols() != dim+1)
72  KALDI_ERR << "Transform matrix has " << mat.NumCols() << " columns but "
73  "model has dimension " << am_gmm.Dim() << " (neither a linear nor an "
74  "affine transform";
75 
76  for (int32 i = 0; i < am_gmm.NumPdfs(); i++) {
77  DiagGmm &gmm = am_gmm.GetPdf(i);
78 
79  Matrix<BaseFloat> means;
80  gmm.GetMeans(&means);
81  Matrix<BaseFloat> new_means(means.NumRows(), means.NumCols());
82  if (mat.NumCols() == dim) { // linear case
83  // Right-multiply means by mat^T (equivalent to left-multiplying each
84  // row by mat).
85  new_means.AddMatMat(1.0, means, kNoTrans, mat, kTrans, 0.0);
86  } else { // affine case
87  Matrix<BaseFloat> means_ext(means.NumRows(), means.NumCols()+1);
88  means_ext.Set(1.0); // set all elems to 1.0
89  SubMatrix<BaseFloat> means_part(means_ext, 0, means.NumRows(),
90  0, means.NumCols());
91  means_part.CopyFromMat(means); // copy old part...
92  new_means.AddMatMat(1.0, means_ext, kNoTrans, mat, kTrans, 0.0);
93  }
94  gmm.SetMeans(new_means);
95  gmm.ComputeGconsts();
96  }
97 
98  {
99  Output ko(model_out_wxfilename, binary);
100  trans_model.Write(ko.Stream(), binary);
101  am_gmm.Write(ko.Stream(), binary);
102  }
103  KALDI_LOG << "Written model to " << model_out_wxfilename;
104  return 0;
105  } catch(const std::exception &e) {
106  std::cerr << e.what() << '\n';
107  return -1;
108  }
109 }
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
int32 ComputeGconsts()
Sets the gconsts.
Definition: diag-gmm.cc:114
kaldi::int32 int32
void SetMeans(const MatrixBase< Real > &m)
Use SetMeans to update only the Gaussian means (and not variances)
Definition: diag-gmm-inl.h:43
void CopyFromMat(const MatrixBase< OtherReal > &M, MatrixTransposeType trans=kNoTrans)
Copy given matrix. (no resize is done).
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 GetMeans(Matrix< Real > *m) const
Accessor for means.
Definition: diag-gmm-inl.h:123
void AddMatMat(const Real alpha, const MatrixBase< Real > &A, MatrixTransposeType transA, const MatrixBase< Real > &B, MatrixTransposeType transB, const Real beta)
#define KALDI_ERR
Definition: kaldi-error.h:147
int32 Dim() const
Definition: am-diag-gmm.h:79
int32 NumPdfs() const
Definition: am-diag-gmm.h:82
DiagGmm & GetPdf(int32 pdf_index)
Accessors.
Definition: am-diag-gmm.h:119
void Write(std::ostream &os, bool binary) const
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void Write(std::ostream &out_stream, bool binary) const
Definition: am-diag-gmm.cc:163
Definition for Gaussian Mixture Model with diagonal covariances.
Definition: diag-gmm.h:42
#define KALDI_LOG
Definition: kaldi-error.h:153
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147
Sub-matrix representation.
Definition: kaldi-matrix.h:988
void Set(Real)
Sets all elements to a specific value.