gmm-transform-means-global.cc
Go to the documentation of this file.
1 // gmmbin/gmm-transform-means-global.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 // 2012-2014 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/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  "This version for a single GMM, e.g. a UBM.\n"
36  "Useful when estimating MLLT/STC\n"
37  "Usage: gmm-transform-means-global <transform-matrix> <gmm-in> <gmm-out>\n"
38  "e.g.: gmm-transform-means-global 2.mat 2.dubm 3.dubm\n";
39 
40  bool binary = true; // write in binary if true.
41 
42  ParseOptions po(usage);
43  po.Register("binary", &binary, "Write output in binary mode");
44 
45  po.Read(argc, argv);
46 
47  if (po.NumArgs() != 3) {
48  po.PrintUsage();
49  exit(1);
50  }
51 
52  std::string mat_rxfilename = po.GetArg(1),
53  gmm_in_rxfilename = po.GetArg(2),
54  gmm_out_wxfilename = po.GetArg(3);
55 
57  ReadKaldiObject(mat_rxfilename, &mat);
58 
59  DiagGmm gmm;
60  ReadKaldiObject(gmm_in_rxfilename, &gmm);
61 
62  int32 dim = gmm.Dim();
63  if (mat.NumRows() != dim)
64  KALDI_ERR << "Transform matrix has " << mat.NumRows() << " rows but "
65  "model has dimension " << gmm.Dim();
66  if (mat.NumCols() != dim
67  && mat.NumCols() != dim+1)
68  KALDI_ERR << "Transform matrix has " << mat.NumCols() << " columns but "
69  "model has dimension " << gmm.Dim() << " (neither a linear nor an "
70  "affine transform";
71 
72  Matrix<BaseFloat> means;
73  gmm.GetMeans(&means);
74  Matrix<BaseFloat> new_means(means.NumRows(), means.NumCols());
75  if (mat.NumCols() == dim) { // linear case
76  // Right-multiply means by mat^T (equivalent to left-multiplying each
77  // row by mat).
78  new_means.AddMatMat(1.0, means, kNoTrans, mat, kTrans, 0.0);
79  } else { // affine case
80  Matrix<BaseFloat> means_ext(means.NumRows(), means.NumCols()+1);
81  means_ext.Set(1.0); // set all elems to 1.0
82  SubMatrix<BaseFloat> means_part(means_ext, 0, means.NumRows(),
83  0, means.NumCols());
84  means_part.CopyFromMat(means); // copy old part...
85  new_means.AddMatMat(1.0, means_ext, kNoTrans, mat, kTrans, 0.0);
86  }
87  gmm.SetMeans(new_means);
88  gmm.ComputeGconsts();
89 
90  WriteKaldiObject(gmm, gmm_out_wxfilename, binary);
91  KALDI_LOG << "Written model to " << gmm_out_wxfilename;
92  return 0;
93  } catch(const std::exception &e) {
94  std::cerr << e.what() << '\n';
95  return -1;
96  }
97 }
98 
99 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int32 Dim() const
Returns the dimensionality of the Gaussian mean vectors.
Definition: diag-gmm.h:74
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
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
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.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
Definition for Gaussian Mixture Model with diagonal covariances.
Definition: diag-gmm.h:42
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
int main(int argc, char *argv[])
#define KALDI_LOG
Definition: kaldi-error.h:153
Sub-matrix representation.
Definition: kaldi-matrix.h:988
void Set(Real)
Sets all elements to a specific value.