estimate-am-sgmm2-test.cc File Reference
Include dependency graph for estimate-am-sgmm2-test.cc:

Go to the source code of this file.

Functions

void TestSgmm2AccsIO (const AmSgmm2 &sgmm, const kaldi::Matrix< BaseFloat > &feats)
 
void UnitTestEstimateSgmm2 ()
 
int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 162 of file estimate-am-sgmm2-test.cc.

References rnnlm::i, and UnitTestEstimateSgmm2().

162  {
163  for (int i = 0; i < 10; i++)
165  std::cout << "Test OK.\n";
166  return 0;
167 }
void UnitTestEstimateSgmm2()

◆ TestSgmm2AccsIO()

void TestSgmm2AccsIO ( const AmSgmm2 sgmm,
const kaldi::Matrix< BaseFloat > &  feats 
)

Definition at line 38 of file estimate-am-sgmm2-test.cc.

References kaldi::AssertEqual(), MleAmSgmm2Accs::Check(), AmSgmm2::ComputeDerivedVars(), AmSgmm2::ComputePerFrameVars(), AmSgmm2::CopyFromSgmm2(), AmSgmm2::FeatureDim(), MleAmSgmm2Options::full_col_cov, Sgmm2GselectConfig::full_gmm_nbest, MleAmSgmm2Options::full_row_cov, AmSgmm2::GaussianSelection(), rnnlm::i, kaldi::kSgmmAll, kaldi::kSgmmSpeakerWeightProjections, AmSgmm2::LogLikelihood(), AmSgmm2::NumGauss(), AmSgmm2::NumGroups(), AmSgmm2::NumPdfs(), MatrixBase< Real >::NumRows(), AmSgmm2::PhoneSpaceDim(), kaldi::RandUniform(), MleAmSgmm2Accs::Read(), Sgmm2PerFrameDerivedVars::Resize(), MatrixBase< Real >::Row(), Input::Stream(), MleAmSgmm2Options::tau_map_M, MleAmSgmm2Updater::Update(), MleAmSgmm2Accs::Write(), and AmSgmm2::Write().

Referenced by UnitTestEstimateSgmm2().

39  {
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 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
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 Read(std::istream &in_stream, bool binary, bool add)
kaldi::int32 int32
int32 PhoneSpaceDim() const
Definition: am-sgmm2.h:361
bool full_row_cov
Estimate row covariance instead of using I.
int32 FeatureDim() const
Definition: am-sgmm2.h:363
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
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
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
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 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
u [ for SSGMM ]
Definition: model-common.h:56

◆ UnitTestEstimateSgmm2()

void UnitTestEstimateSgmm2 ( )

Definition at line 126 of file estimate-am-sgmm2-test.cc.

References AmSgmm2::ComputeDerivedVars(), AmSgmm2::ComputeNormalizers(), rnnlm::d, kaldi::Exp(), AmSgmm2::InitializeFromFullGmm(), kaldi::unittest::InitRandFullGmm(), kaldi::unittest::RandDiagGaussFeatures(), kaldi::RandGauss(), kaldi::RandInt(), Matrix< Real >::Resize(), MatrixBase< Real >::Row(), and TestSgmm2AccsIO().

Referenced by main().

126  {
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 }
double Exp(double x)
Definition: kaldi-math.h:83
Class for definition of the subspace Gmm acoustic model.
Definition: am-sgmm2.h:231
Definition for Gaussian Mixture Model with full covariances.
Definition: full-gmm.h:40
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)
void InitRandFullGmm(int32 dim, int32 num_comp, FullGmm *gmm)
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
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).
Sub-matrix representation.
Definition: kaldi-matrix.h:988
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95