gmm-transform-means.cc
Go to the documentation of this file.
1 // gmmbin/gmm-transform-means.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 // 2012 Johns Hopkins University (author: Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "gmm/am-diag-gmm.h"
24 #include "tree/context-dep.h"
25 #include "hmm/transition-model.h"
26 #include "transform/mllt.h"
27 
28 int main(int argc, char *argv[]) {
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 }
110 
111 
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
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
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 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 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)
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.
int32 Dim() const
Definition: am-diag-gmm.h:79
int32 NumPdfs() const
Definition: am-diag-gmm.h:82
int NumArgs() const
Number of positional parameters (c.f. argc-1).
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.
int main(int argc, char *argv[])