mle-am-diag-gmm-test.cc
Go to the documentation of this file.
1 // gmm/mle-am-diag-gmm-test.cc
2 
3 // Copyright 2009-2012 Arnab Ghoshal
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 #include "gmm/model-test-common.h"
21 #include "gmm/am-diag-gmm.h"
22 #include "gmm/mle-am-diag-gmm.h"
23 #include "util/kaldi-io.h"
24 
25 using kaldi::AmDiagGmm;
27 using kaldi::int32;
28 using kaldi::BaseFloat;
29 namespace ut = kaldi::unittest;
30 using namespace kaldi;
31 
32 // Tests the Read() and Write() methods for the accumulators, in both binary
33 // and ASCII mode.
34 void TestAmDiagGmmAccsIO(const AmDiagGmm &am_gmm,
35  const Matrix<BaseFloat> &feats) {
37  AccumAmDiagGmm accs;
38  accs.Init(am_gmm, flags);
39  BaseFloat loglike = 0.0;
40  for (int32 i = 0; i < feats.NumRows(); i++) {
41  int32 state = RandInt(0, am_gmm.NumPdfs()-1);
42  loglike += accs.AccumulateForGmm(am_gmm, feats.Row(i), state, 1.0);
43  }
44  KALDI_LOG << "Data log-likelihood = " << loglike << " over "
45  << feats.NumRows() << " frames.";
46  KALDI_LOG << "Accumulated values: log-like = " << accs.TotLogLike()
47  << ", # frames = " << accs.TotCount();
48  AssertEqual(accs.TotLogLike(), loglike, 1e-5);
49  AssertEqual(accs.TotCount(), static_cast<BaseFloat>(feats.NumRows()), 1e-5);
50 
51  MleDiagGmmOptions config;
52  AmDiagGmm *am_gmm1 = new AmDiagGmm();
53  am_gmm1->CopyFromAmDiagGmm(am_gmm);
54  MleAmDiagGmmUpdate(config, accs, flags, am_gmm1, NULL, NULL);
55 
56  int32 check_pdf = RandInt(0, am_gmm.NumPdfs()-1),
57  check_frame = RandInt(0, feats.NumRows()-1);
58  BaseFloat loglike1 = am_gmm1->LogLikelihood(check_pdf, feats.Row(check_frame));
59  delete am_gmm1;
60 
61  // First, non-binary write
62  accs.Write(kaldi::Output("tmpf", false).Stream(), false);
63  bool binary_in;
64  AccumAmDiagGmm *accs1 = new AccumAmDiagGmm();
65  // Non-binary read
66  kaldi::Input ki1("tmpf", &binary_in);
67  accs1->Read(ki1.Stream(), binary_in, false);
68  AmDiagGmm *am_gmm2 = new AmDiagGmm();
69  am_gmm2->CopyFromAmDiagGmm(am_gmm);
70  MleAmDiagGmmUpdate(config, accs, flags, am_gmm2, NULL, NULL);
71  BaseFloat loglike2 = am_gmm2->LogLikelihood(check_pdf, feats.Row(check_frame));
72  kaldi::AssertEqual(loglike1, loglike2, 1e-6);
73  delete am_gmm2;
74  delete accs1;
75 
76  // Next, binary write
77  accs.Write(kaldi::Output("tmpfb", true).Stream(), true);
78  AccumAmDiagGmm *accs2 = new AccumAmDiagGmm();
79  // Binary read
80  kaldi::Input ki2("tmpfb", &binary_in);
81  accs2->Read(ki2.Stream(), binary_in, false);
82  AmDiagGmm *am_gmm3 = new AmDiagGmm();
83  am_gmm3->CopyFromAmDiagGmm(am_gmm);
84  MleAmDiagGmmUpdate(config, accs, flags, am_gmm3, NULL, NULL);
85  BaseFloat loglike3 = am_gmm3->LogLikelihood(check_pdf, feats.Row(check_frame));
86  kaldi::AssertEqual(loglike1, loglike3, 1e-6);
87  delete am_gmm3;
88  delete accs2;
89 
90  unlink("tmpf");
91  unlink("tmpfb");
92 }
93 
95  int32 dim = 1 + kaldi::RandInt(0, 9), // random dimension of the gmm
96  num_pdfs = 5 + kaldi::RandInt(0, 9); // random number of states
97 
98  AmDiagGmm am_gmm;
99  int32 total_num_comp = 0;
100  for (int32 i = 0; i < num_pdfs; i++) {
101  int32 num_comp = 1 + kaldi::RandInt(0, 9); // random mixture size
102  kaldi::DiagGmm gmm;
103  ut::InitRandDiagGmm(dim, num_comp, &gmm);
104  am_gmm.AddPdf(gmm);
105  total_num_comp += num_comp;
106  }
107 
109 
110  { // First, generate random means and variances
111  int32 num_feat_comp = total_num_comp + kaldi::RandInt(-total_num_comp/2,
112  total_num_comp/2);
113  kaldi::Matrix<BaseFloat> means(num_feat_comp, dim),
114  vars(num_feat_comp, dim);
115  for (int32 m = 0; m < num_feat_comp; m++) {
116  for (int32 d= 0; d < dim; d++) {
117  means(m, d) = kaldi::RandGauss();
118  vars(m, d) = Exp(kaldi::RandGauss()) + 1e-2;
119  }
120  }
121  // Now generate random features with those means and variances.
122  feats.Resize(num_feat_comp * 200, dim);
123  for (int32 m = 0; m < num_feat_comp; m++) {
124  kaldi::SubMatrix<BaseFloat> tmp(feats, m*200, 200, 0, dim);
125  ut::RandDiagGaussFeatures(200, means.Row(m), vars.Row(m), &tmp);
126  }
127  }
128  TestAmDiagGmmAccsIO(am_gmm, feats);
129 }
130 
131 
132 int main() {
133 // std::srand(time(NULL));
134  for (int i = 0; i < 10; i++)
136  std::cout << "Test OK.\n";
137  return 0;
138 }
void MleAmDiagGmmUpdate(const MleDiagGmmOptions &config, const AccumAmDiagGmm &am_diag_gmm_acc, GmmFlagsType flags, AmDiagGmm *am_gmm, BaseFloat *obj_change_out, BaseFloat *count_out)
for computing the maximum-likelihood estimates of the parameters of an acoustic model that uses diago...
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
double Exp(double x)
Definition: kaldi-math.h:83
void CopyFromAmDiagGmm(const AmDiagGmm &other)
Copies the parameters from another model. Allocates necessary memory.
Definition: am-diag-gmm.cc:79
void AddPdf(const DiagGmm &gmm)
Adds a GMM to the model, and increments the total number of PDFs.
Definition: am-diag-gmm.cc:57
BaseFloat AccumulateForGmm(const AmDiagGmm &model, const VectorBase< BaseFloat > &data, int32 gmm_index, BaseFloat weight)
Accumulate stats for a single GMM in the model; returns log likelihood.
float RandGauss(struct RandomState *state=NULL)
Definition: kaldi-math.h:155
kaldi::int32 int32
BaseFloat TotCount() const
uint16 GmmFlagsType
Bitwise OR of the above flags.
Definition: model-common.h:35
std::istream & Stream()
Definition: kaldi-io.cc:826
float BaseFloat
Definition: kaldi-types.h:29
BaseFloat TotLogLike() const
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
BaseFloat LogLikelihood(const int32 pdf_index, const VectorBase< BaseFloat > &data) const
Definition: am-diag-gmm.h:108
void InitRandDiagGmm(int32 dim, int32 num_comp, DiagGmm *gmm)
Configuration variables like variance floor, minimum occupancy, etc.
Definition: mle-diag-gmm.h:38
void Read(std::istream &in_stream, bool binary, bool add=false)
int32 NumPdfs() const
Definition: am-diag-gmm.h:82
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
static void AssertEqual(float a, float b, float relative_tolerance=0.001)
assert abs(a - b) <= relative_tolerance * (abs(a)+abs(b))
Definition: kaldi-math.h:276
Definition for Gaussian Mixture Model with diagonal covariances.
Definition: diag-gmm.h:42
void RandDiagGaussFeatures(int32 num_samples, const VectorBase< BaseFloat > &mean, const VectorBase< BaseFloat > &sqrt_var, MatrixBase< BaseFloat > *feats)
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
void TestAmDiagGmmAccsIO(const AmDiagGmm &am_gmm, const Matrix< BaseFloat > &feats)
int main()
#define KALDI_LOG
Definition: kaldi-error.h:153
void Init(const AmDiagGmm &model, GmmFlagsType flags)
Initializes accumulators for each GMM based on the number of components and dimension.
Sub-matrix representation.
Definition: kaldi-matrix.h:988
void UnitTestMleAmDiagGmm()
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95