estimate-am-sgmm2-test.cc
Go to the documentation of this file.
1 // sgmm2/estimate-am-sgmm2-test.cc
2 
3 // Copyright 2009-2011 Saarland University (author: Arnab Ghoshal)
4 // 2012-2013 Johns Hopkins University (author: Daniel Povey)
5 // Arnab Ghoshal
6 
7 // See ../../COPYING for clarification regarding multiple authors
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
17 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
18 // MERCHANTABLITY OR NON-INFRINGEMENT.
19 // See the Apache 2 License for the specific language governing permissions and
20 // limitations under the License.
21 
22 #include "base/kaldi-math.h"
23 #include "gmm/model-test-common.h"
24 #include "sgmm2/am-sgmm2.h"
26 #include "util/kaldi-io.h"
27 
28 using kaldi::AmSgmm2;
30 using kaldi::int32;
31 using kaldi::BaseFloat;
32 using kaldi::Exp;
33 
34 namespace ut = kaldi::unittest;
35 
36 // Tests the Read() and Write() methods for the accumulators, in both binary
37 // and ASCII mode, as well as Check().
38 void TestSgmm2AccsIO(const AmSgmm2 &sgmm,
39  const kaldi::Matrix<BaseFloat> &feats) {
40  using namespace kaldi;
44  frame_vars.Resize(sgmm.NumGauss(), sgmm.FeatureDim(),
45  sgmm.PhoneSpaceDim());
46  kaldi::Sgmm2GselectConfig sgmm_config;
47  sgmm_config.full_gmm_nbest = std::min(sgmm_config.full_gmm_nbest,
48  sgmm.NumGauss());
49  MleAmSgmm2Accs accs(sgmm, flags, true);
50  BaseFloat loglike = 0.0;
51 
52  for (int32 i = 0; i < feats.NumRows(); i++) {
53  std::vector<int32> gselect;
54  sgmm.GaussianSelection(sgmm_config, feats.Row(i), &gselect);
55  sgmm.ComputePerFrameVars(feats.Row(i), gselect, empty, &frame_vars);
56  loglike += accs.Accumulate(sgmm, frame_vars, 0, 1.0, &empty);
57  }
58  accs.CommitStatsForSpk(sgmm, empty);
59 
60  kaldi::MleAmSgmm2Options update_opts;
61  AmSgmm2 *sgmm1 = new AmSgmm2();
62  sgmm1->CopyFromSgmm2(sgmm, false, false);
63  kaldi::MleAmSgmm2Updater updater(update_opts);
64  updater.Update(accs, sgmm1, flags);
65  sgmm1->ComputeDerivedVars();
66  std::vector<int32> gselect;
67  Sgmm2LikelihoodCache like_cache(sgmm.NumGroups(), sgmm.NumPdfs());
68 
69  sgmm1->GaussianSelection(sgmm_config, feats.Row(0), &gselect);
70  sgmm1->ComputePerFrameVars(feats.Row(0), gselect, empty, &frame_vars);
71  BaseFloat loglike1 = sgmm1->LogLikelihood(frame_vars, 0, &like_cache, &empty);
72  delete sgmm1;
73 
74  // First, non-binary write
75  accs.Write(kaldi::Output("tmpf", false).Stream(), false);
76  bool binary_in;
77  MleAmSgmm2Accs *accs1 = new MleAmSgmm2Accs();
78  // Non-binary read
79  kaldi::Input ki1("tmpf", &binary_in);
80  accs1->Read(ki1.Stream(), binary_in, false);
81  accs1->Check(sgmm, true);
82  AmSgmm2 *sgmm2 = new AmSgmm2();
83  sgmm2->CopyFromSgmm2(sgmm, false, false);
84  updater.Update(*accs1, sgmm2, flags);
85  sgmm2->ComputeDerivedVars();
86  sgmm2->GaussianSelection(sgmm_config, feats.Row(0), &gselect);
87  sgmm2->ComputePerFrameVars(feats.Row(0), gselect, empty, &frame_vars);
88  Sgmm2LikelihoodCache like_cache2(sgmm2->NumGroups(), sgmm2->NumPdfs());
89  BaseFloat loglike2 = sgmm2->LogLikelihood(frame_vars, 0, &like_cache2, &empty);
90  kaldi::AssertEqual(loglike1, loglike2, 1e-4);
91  delete accs1;
92 
93  // Next, binary write
94  accs.Write(kaldi::Output("tmpfb", true).Stream(), true);
95  MleAmSgmm2Accs *accs2 = new MleAmSgmm2Accs();
96  // Binary read
97  kaldi::Input ki2("tmpfb", &binary_in);
98  accs2->Read(ki2.Stream(), binary_in, false);
99  accs2->Check(sgmm, true);
100  AmSgmm2 *sgmm3 = new AmSgmm2();
101  sgmm3->CopyFromSgmm2(sgmm, false, false);
102  updater.Update(*accs2, sgmm3, flags);
103  sgmm3->ComputeDerivedVars();
104  sgmm3->GaussianSelection(sgmm_config, feats.Row(0), &gselect);
105  sgmm3->ComputePerFrameVars(feats.Row(0), gselect, empty, &frame_vars);
106  Sgmm2LikelihoodCache like_cache3(sgmm3->NumGroups(), sgmm3->NumPdfs());
107  BaseFloat loglike3 = sgmm3->LogLikelihood(frame_vars, 0, &like_cache3, &empty);
108  kaldi::AssertEqual(loglike1, loglike3, 1e-6);
109 
110  // Testing the MAP update of M
111  update_opts.tau_map_M = 10;
112  update_opts.full_col_cov = (RandUniform() > 0.5)? true : false;
113  update_opts.full_row_cov = (RandUniform() > 0.5)? true : false;
114  kaldi::MleAmSgmm2Updater updater_map(update_opts);
115  sgmm3->CopyFromSgmm2(sgmm, false, false);
116  updater_map.Update(*accs2, sgmm3, flags);
117 
118  delete accs2;
119  delete sgmm2;
120  delete sgmm3;
121 
122  unlink("tmpf");
123  unlink("tmpfb");
124 }
125 
127  int32 dim = 1 + kaldi::RandInt(0, 9); // random dimension of the gmm
128  int32 num_comp = 2 + kaldi::RandInt(0, 9); // random mixture size
129  kaldi::FullGmm full_gmm;
130  ut::InitRandFullGmm(dim, num_comp, &full_gmm);
131 
132  AmSgmm2 sgmm;
134  std::vector<int32> pdf2group;
135  pdf2group.push_back(0);
136  sgmm.InitializeFromFullGmm(full_gmm, pdf2group, dim+1, dim, false, 0.9); // TODO-- make this true!
137  sgmm.ComputeNormalizers();
138 
140 
141  { // First, generate random means and variances
142  int32 num_feat_comp = num_comp + kaldi::RandInt(-num_comp/2, num_comp/2);
143  kaldi::Matrix<BaseFloat> means(num_feat_comp, dim),
144  vars(num_feat_comp, dim);
145  for (int32 m = 0; m < num_feat_comp; m++) {
146  for (int32 d= 0; d < dim; d++) {
147  means(m, d) = kaldi::RandGauss();
148  vars(m, d) = Exp(kaldi::RandGauss()) + 1e-2;
149  }
150  }
151  // Now generate random features with those means and variances.
152  feats.Resize(num_feat_comp * 200, dim);
153  for (int32 m = 0; m < num_feat_comp; m++) {
154  kaldi::SubMatrix<BaseFloat> tmp(feats, m*200, 200, 0, dim);
155  ut::RandDiagGaussFeatures(200, means.Row(m), vars.Row(m), &tmp);
156  }
157  }
158  sgmm.ComputeDerivedVars();
159  TestSgmm2AccsIO(sgmm, feats);
160 }
161 
162 int main() {
163  for (int i = 0; i < 10; i++)
165  std::cout << "Test OK.\n";
166  return 0;
167 }
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 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
void CopyFromSgmm2(const AmSgmm2 &other, bool copy_normalizers, bool copy_weights)
Used to copy models (useful in update)
Definition: am-sgmm2.cc:415
float RandUniform(struct RandomState *state=NULL)
Returns a random number strictly between 0 and 1.
Definition: kaldi-math.h:151
void UnitTestEstimateSgmm2()
Definition for Gaussian Mixture Model with full covariances.
Definition: full-gmm.h:40
void Read(std::istream &in_stream, bool binary, bool add)
void InitializeFromFullGmm(const FullGmm &gmm, const std::vector< int32 > &pdf2group, int32 phn_subspace_dim, int32 spk_subspace_dim, bool speaker_dependent_weights, BaseFloat self_weight)
Initializes the SGMM parameters from a full-covariance UBM.
Definition: am-sgmm2.cc:381
float RandGauss(struct RandomState *state=NULL)
Definition: kaldi-math.h:155
kaldi::int32 int32
void TestSgmm2AccsIO(const AmSgmm2 &sgmm, const kaldi::Matrix< BaseFloat > &feats)
int32 PhoneSpaceDim() const
Definition: am-sgmm2.h:361
bool full_row_cov
Estimate row covariance instead of using I.
void Update(const MleAmSgmm2Accs &accs, AmSgmm2 *model, SgmmUpdateFlagsType flags)
int32 FeatureDim() const
Definition: am-sgmm2.h:363
std::istream & Stream()
Definition: kaldi-io.cc:826
int32 NumGroups() const
Definition: am-sgmm2.h:351
t .. not really part of SGMM.
Definition: model-common.h:55
float BaseFloat
Definition: kaldi-types.h:29
BaseFloat LogLikelihood(const Sgmm2PerFrameDerivedVars &per_frame_vars, int32 j2, Sgmm2LikelihoodCache *cache, Sgmm2PerSpkDerivedVars *spk_vars, BaseFloat log_prune=0.0) const
This does a likelihood computation for a given state using the pre-selected Gaussian components (in p...
Definition: am-sgmm2.cc:517
uint16 SgmmUpdateFlagsType
Bitwise OR of the above flags.
Definition: model-common.h:59
bool full_col_cov
Estimate col covariance instead of using I.
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
int main()
BaseFloat GaussianSelection(const Sgmm2GselectConfig &config, const VectorBase< BaseFloat > &data, std::vector< int32 > *gselect) const
Computes the top-scoring Gaussian indices (used for pruning of later stages of computation).
Definition: am-sgmm2.cc:1406
int32 NumPdfs() const
Various model dimensions.
Definition: am-sgmm2.h:350
int32 full_gmm_nbest
Number of highest-scoring full-covariance Gaussians per frame.
Definition: am-sgmm2.h:120
void ComputePerFrameVars(const VectorBase< BaseFloat > &data, const std::vector< int32 > &gselect, const Sgmm2PerSpkDerivedVars &spk_vars, Sgmm2PerFrameDerivedVars *per_frame_vars) const
This needs to be called with each new frame of data, prior to accumulation or likelihood evaluation: ...
Definition: am-sgmm2.cc:442
void Resize(int32 ngauss, int32 feat_dim, int32 phn_dim)
Definition: am-sgmm2.h:151
int32 NumGauss() const
Definition: am-sgmm2.h:360
void Write(std::ostream &out_stream, bool binary) const
void InitRandFullGmm(int32 dim, int32 num_comp, FullGmm *gmm)
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 ComputeNormalizers()
Computes the data-independent terms in the log-likelihood computation for each Gaussian component and...
Definition: am-sgmm2.cc:857
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
Sgmm2LikelihoodCache caches SGMM likelihoods at two levels: the final pdf likelihoods, and the sub-state level likelihoods, which means that with the SCTM system we can avoid redundant computation.
Definition: am-sgmm2.h:199
BaseFloat tau_map_M
For MAP update of the phonetic subspace M.
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 Check(const AmSgmm2 &model, bool show_properties=true) const
Checks the various accumulators for correct sizes given a model.
Configuration variables needed in the SGMM estimation process.
Class for the accumulators associated with the phonetic-subspace model parameters.
Holds the per-frame precomputed quantities x(t), x_{i}(t), z_{i}(t), and n_{i}(t) (cf...
Definition: am-sgmm2.h:142
Sub-matrix representation.
Definition: kaldi-matrix.h:988
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95
u [ for SSGMM ]
Definition: model-common.h:56