regtree-mllr-diag-gmm-test.cc File Reference
Include dependency graph for regtree-mllr-diag-gmm-test.cc:

Go to the source code of this file.

Functions

void TestMllrAccsIO (const kaldi::AmDiagGmm &am_gmm, const kaldi::RegressionTree &regtree, const RegtreeMllrDiagGmmAccs &accs, const kaldi::Matrix< BaseFloat > adapt_data)
 
void TestXformMean (const kaldi::AmDiagGmm &am_gmm, const kaldi::RegressionTree &regtree, const RegtreeMllrDiagGmmAccs &accs, const kaldi::Matrix< BaseFloat > adapt_data)
 
void UnitTestRegtreeMllrDiagGmm ()
 
int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 188 of file regtree-mllr-diag-gmm-test.cc.

References kaldi::g_kaldi_verbose_level, rnnlm::i, and UnitTestRegtreeMllrDiagGmm().

188  {
190  for (int i = 0; i <= 10; i++)
192  std::cout << "Test OK.\n";
193 }
void UnitTestRegtreeMllrDiagGmm()
int32 g_kaldi_verbose_level
This is set by util/parse-options.
Definition: kaldi-error.cc:46

◆ TestMllrAccsIO()

void TestMllrAccsIO ( const kaldi::AmDiagGmm am_gmm,
const kaldi::RegressionTree regtree,
const RegtreeMllrDiagGmmAccs accs,
const kaldi::Matrix< BaseFloat >  adapt_data 
)

Definition at line 33 of file regtree-mllr-diag-gmm-test.cc.

References kaldi::AssertEqual(), AmDiagGmm::CopyFromAmDiagGmm(), AmDiagGmm::Dim(), DiagGmm::Generate(), kaldi::unittest::InitRandDiagGmm(), rnnlm::j, KALDI_LOG, AmDiagGmm::LogLikelihood(), RegtreeMllrOptions::min_count, MatrixBase< Real >::NumRows(), kaldi::RandInt(), RegtreeMllrDiagGmmAccs::Read(), MatrixBase< Real >::Row(), Input::Stream(), RegtreeMllrDiagGmm::TransformModel(), RegtreeMllrDiagGmmAccs::Update(), RegtreeMllrOptions::use_regtree, and RegtreeMllrDiagGmmAccs::Write().

Referenced by UnitTestRegtreeMllrDiagGmm().

36  {
37  // First, non-binary write
38  accs.Write(kaldi::Output("tmpf", false).Stream(), false);
39 
42  opts.min_count = 100;
43  opts.use_regtree = false;
44  accs.Update(regtree, opts, &mllr, NULL, NULL);
45  kaldi::AmDiagGmm am1;
46  am1.CopyFromAmDiagGmm(am_gmm);
47  mllr.TransformModel(regtree, &am1);
48 
49  BaseFloat loglike = 0;
50  int32 npoints = adapt_data.NumRows();
51  for (int32 j = 0; j < npoints; j++) {
52  loglike += am1.LogLikelihood(0, adapt_data.Row(j));
53  }
54  KALDI_LOG << "Per-frame loglike after adaptation = " << (loglike/npoints)
55  << " over " << npoints << " frames.";
56 
57  size_t num_comp2 = 1 + kaldi::RandInt(0, 9); // random number of mixtures
58  int32 dim = am_gmm.Dim();
59  kaldi::DiagGmm gmm2;
60  ut::InitRandDiagGmm(dim, num_comp2, &gmm2);
61  kaldi::Vector<BaseFloat> data(dim);
62  gmm2.Generate(&data);
63  BaseFloat loglike1 = am1.LogLikelihood(0, data);
64 // KALDI_LOG << "LL0 = " << loglike0 << "; LL1 = " << loglike1;
65 
66  KALDI_LOG << "Test ASCII IO.";
67  bool binary_in;
70  // Non-binary read
71  kaldi::Input ki1("tmpf", &binary_in);
72  accs1->Read(ki1.Stream(), binary_in, false);
73  accs1->Update(regtree, opts, &mllr1, NULL, NULL);
74  delete accs1;
75  kaldi::AmDiagGmm am2;
76  am2.CopyFromAmDiagGmm(am_gmm);
77  mllr.TransformModel(regtree, &am2);
78  BaseFloat loglike2 = am2.LogLikelihood(0, data);
79 // KALDI_LOG << "LL1 = " << loglike1 << "; LL2 = " << loglike2;
80  kaldi::AssertEqual(loglike1, loglike2, 1e-6);
81 
83  // Next, binary write
84  KALDI_LOG << "Test Binary IO.";
85  accs.Write(kaldi::Output("tmpfb", true).Stream(), true);
87  // Binary read
88  kaldi::Input ki2("tmpfb", &binary_in);
89  accs2->Read(ki2.Stream(), binary_in, false);
90  accs2->Update(regtree, opts, &mllr2, NULL, NULL);
91  delete accs2;
92  kaldi::AmDiagGmm am3;
93  am3.CopyFromAmDiagGmm(am_gmm);
94  mllr.TransformModel(regtree, &am3);
95  BaseFloat loglike3 = am3.LogLikelihood(0, data);
96 // KALDI_LOG << "LL1 = " << loglike1 << "; LL3 = " << loglike3;
97  kaldi::AssertEqual(loglike1, loglike3, 1e-6);
98 
99  unlink("tmpf");
100  unlink("tmpfb");
101 }
void CopyFromAmDiagGmm(const AmDiagGmm &other)
Copies the parameters from another model. Allocates necessary memory.
Definition: am-diag-gmm.cc:79
An MLLR mean transformation is an affine transformation of Gaussian means.
void TransformModel(const RegressionTree &regtree, AmDiagGmm *am)
Apply the transform(s) to all the Gaussian means in the model.
Configuration variables for FMLLR transforms.
bool use_regtree
If &#39;true&#39;, find transforms to generate using regression tree.
BaseFloat min_count
Minimum occupancy for computing a transform.
kaldi::int32 int32
void Read(std::istream &in_stream, bool binary, bool add)
float BaseFloat
Definition: kaldi-types.h:29
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)
int32 Dim() const
Definition: am-diag-gmm.h:79
Class for computing the maximum-likelihood estimates of the parameters of an acoustic model that uses...
A class representing a vector.
Definition: kaldi-vector.h:406
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 Generate(VectorBase< BaseFloat > *output)
Generates a random data-point from this distribution.
Definition: diag-gmm.cc:922
void Update(const RegressionTree &regtree, const RegtreeMllrOptions &opts, RegtreeMllrDiagGmm *out_mllr, BaseFloat *auxf_impr, BaseFloat *t) const
#define KALDI_LOG
Definition: kaldi-error.h:153
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95

◆ TestXformMean()

void TestXformMean ( const kaldi::AmDiagGmm am_gmm,
const kaldi::RegressionTree regtree,
const RegtreeMllrDiagGmmAccs accs,
const kaldi::Matrix< BaseFloat >  adapt_data 
)

Definition at line 103 of file regtree-mllr-diag-gmm-test.cc.

References kaldi::AssertEqual(), DiagGmm::ComputeGconsts(), AmDiagGmm::CopyFromAmDiagGmm(), DiagGmm::CopyFromDiagGmm(), AmDiagGmm::Dim(), AmDiagGmm::GetPdf(), RegtreeMllrDiagGmm::GetTransformedMeans(), DiagGmm::inv_vars(), rnnlm::j, KALDI_LOG, AmDiagGmm::LogLikelihood(), DiagGmm::LogLikelihood(), RegtreeMllrOptions::min_count, DiagGmm::NumGauss(), MatrixBase< Real >::NumRows(), MatrixBase< Real >::Row(), DiagGmm::SetInvVarsAndMeans(), RegtreeMllrDiagGmm::TransformModel(), RegtreeMllrDiagGmmAccs::Update(), and RegtreeMllrOptions::use_regtree.

Referenced by UnitTestRegtreeMllrDiagGmm().

106  {
109  opts.min_count = 100;
110  opts.use_regtree = false;
111  accs.Update(regtree, opts, &mllr, NULL, NULL);
112 
113  kaldi::AmDiagGmm am1;
114  am1.CopyFromAmDiagGmm(am_gmm);
115  mllr.TransformModel(regtree, &am1);
116 
117  kaldi::DiagGmm tmp_pdf;
118  tmp_pdf.CopyFromDiagGmm(am_gmm.GetPdf(0));
119  kaldi::Matrix<BaseFloat> tmp_means(am_gmm.GetPdf(0).NumGauss(), am_gmm.Dim());
120  mllr.GetTransformedMeans(regtree, am_gmm, 0, &tmp_means);
121  tmp_pdf.SetInvVarsAndMeans(tmp_pdf.inv_vars(), tmp_means);
122  tmp_pdf.ComputeGconsts();
123 
124  BaseFloat loglike0 = 0, loglike = 0;
125  int32 npoints = adapt_data.NumRows();
126  for (int32 j = 0; j < npoints; j++) {
127  loglike0 += am1.LogLikelihood(0, adapt_data.Row(j));
128  loglike += tmp_pdf.LogLikelihood(adapt_data.Row(j));
129  }
130  KALDI_LOG << "Per-frame loglike after adaptation = " << (loglike0/npoints)
131  << " over " << npoints << " frames.";
132 // KALDI_LOG << "LL0 = " << loglike0 << "; LL = " << loglike;
133  kaldi::AssertEqual(loglike0, loglike, 1e-6);
134 
135  kaldi::Matrix<BaseFloat> tmp_means2(am_gmm.GetPdf(0).NumGauss(), am_gmm.Dim());
136  mllr.GetTransformedMeans(regtree, am_gmm, 0, &tmp_means2);
137  tmp_pdf.SetInvVarsAndMeans(tmp_pdf.inv_vars(), tmp_means2);
138  tmp_pdf.ComputeGconsts();
139 
140  BaseFloat loglike1 = 0;
141  for (int32 j = 0; j < npoints; j++) {
142  loglike1 += tmp_pdf.LogLikelihood(adapt_data.Row(j));
143  }
144 // KALDI_LOG << "LL = " << loglike << "; LL1 = " << loglike1;
145  kaldi::AssertEqual(loglike, loglike1, 1e-6);
146 }
void CopyFromAmDiagGmm(const AmDiagGmm &other)
Copies the parameters from another model. Allocates necessary memory.
Definition: am-diag-gmm.cc:79
void CopyFromDiagGmm(const DiagGmm &diaggmm)
Copies from given DiagGmm.
Definition: diag-gmm.cc:83
void SetInvVarsAndMeans(const MatrixBase< Real > &invvars, const MatrixBase< Real > &means)
Use SetInvVarsAndMeans if updating both means and (inverse) variances.
Definition: diag-gmm-inl.h:63
An MLLR mean transformation is an affine transformation of Gaussian means.
void TransformModel(const RegressionTree &regtree, AmDiagGmm *am)
Apply the transform(s) to all the Gaussian means in the model.
Configuration variables for FMLLR transforms.
bool use_regtree
If &#39;true&#39;, find transforms to generate using regression tree.
BaseFloat min_count
Minimum occupancy for computing a transform.
int32 ComputeGconsts()
Sets the gconsts.
Definition: diag-gmm.cc:114
kaldi::int32 int32
float BaseFloat
Definition: kaldi-types.h:29
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
BaseFloat LogLikelihood(const VectorBase< BaseFloat > &data) const
Returns the log-likelihood of a data point (vector) given the GMM.
Definition: diag-gmm.cc:517
int32 NumGauss() const
Returns the number of mixture components in the GMM.
Definition: diag-gmm.h:72
int32 Dim() const
Definition: am-diag-gmm.h:79
void GetTransformedMeans(const RegressionTree &regtree, const AmDiagGmm &am, int32 pdf_index, MatrixBase< BaseFloat > *out) const
Get all the transformed means for a given pdf.
DiagGmm & GetPdf(int32 pdf_index)
Accessors.
Definition: am-diag-gmm.h:119
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
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 Update(const RegressionTree &regtree, const RegtreeMllrOptions &opts, RegtreeMllrDiagGmm *out_mllr, BaseFloat *auxf_impr, BaseFloat *t) const
#define KALDI_LOG
Definition: kaldi-error.h:153
const Matrix< BaseFloat > & inv_vars() const
Definition: diag-gmm.h:180

◆ UnitTestRegtreeMllrDiagGmm()

void UnitTestRegtreeMllrDiagGmm ( )

Definition at line 149 of file regtree-mllr-diag-gmm-test.cc.

References RegtreeMllrDiagGmmAccs::AccumulateForGmm(), RegressionTree::BuildTree(), DiagGmm::Generate(), AmDiagGmm::Init(), RegtreeMllrDiagGmmAccs::Init(), kaldi::unittest::InitRandDiagGmm(), rnnlm::j, KALDI_LOG, RegressionTree::NumBaseclasses(), kaldi::RandInt(), TestMllrAccsIO(), and TestXformMean().

Referenced by main().

149  {
150  size_t dim = 1 + kaldi::RandInt(1, 9); // random dimension of the gmm
151  size_t num_comp = 1 + kaldi::RandInt(0, 5); // random number of mixtures
152  kaldi::DiagGmm gmm;
153  ut::InitRandDiagGmm(dim, num_comp, &gmm);
154  kaldi::AmDiagGmm am_gmm;
155  am_gmm.Init(gmm, 1);
156 
157  size_t num_comp2 = 1 + kaldi::RandInt(0, 5); // random number of mixtures
158  kaldi::DiagGmm gmm2;
159  ut::InitRandDiagGmm(dim, num_comp2, &gmm2);
160  int32 npoints = dim*(dim+1)*10 + 500;
161  kaldi::Matrix<BaseFloat> adapt_data(npoints, dim);
162  for (int32 j = 0; j < npoints; j++) {
163  kaldi::SubVector<BaseFloat> row(adapt_data, j);
164  gmm2.Generate(&row);
165  }
166 
167  kaldi::RegressionTree regtree;
168  std::vector<int32> sil_indices;
169  kaldi::Vector<BaseFloat> state_occs(1);
170  state_occs(0) = npoints;
171  regtree.BuildTree(state_occs, sil_indices, am_gmm, 1);
172  int32 num_bclass = regtree.NumBaseclasses();
173 
175  BaseFloat loglike = 0;
176  accs.Init(num_bclass, dim);
177  for (int32 j = 0; j < npoints; j++) {
178  loglike += accs.AccumulateForGmm(regtree, am_gmm, adapt_data.Row(j),
179  0, 1.0);
180  }
181  KALDI_LOG << "Per-frame loglike during accumulations = " << (loglike/npoints)
182  << " over " << npoints << " frames.";
183 
184  TestMllrAccsIO(am_gmm, regtree, accs, adapt_data);
185  TestXformMean(am_gmm, regtree, accs, adapt_data);
186 }
kaldi::int32 int32
void TestXformMean(const kaldi::AmDiagGmm &am_gmm, const kaldi::RegressionTree &regtree, const RegtreeMllrDiagGmmAccs &accs, const kaldi::Matrix< BaseFloat > adapt_data)
void Init(int32 num_bclass, int32 dim)
int32 NumBaseclasses() const
Accessors (const)
float BaseFloat
Definition: kaldi-types.h:29
void BuildTree(const Vector< BaseFloat > &state_occs, const std::vector< int32 > &sil_indices, const AmDiagGmm &am, int32 max_clusters)
Top-down clustering of the Gaussians in a model based on their means.
A regression tree is a clustering of Gaussian densities in an acoustic model, such that the group of ...
void InitRandDiagGmm(int32 dim, int32 num_comp, DiagGmm *gmm)
Class for computing the maximum-likelihood estimates of the parameters of an acoustic model that uses...
A class representing a vector.
Definition: kaldi-vector.h:406
Definition for Gaussian Mixture Model with diagonal covariances.
Definition: diag-gmm.h:42
void Generate(VectorBase< BaseFloat > *output)
Generates a random data-point from this distribution.
Definition: diag-gmm.cc:922
BaseFloat AccumulateForGmm(const RegressionTree &regtree, const AmDiagGmm &am, const VectorBase< BaseFloat > &data, int32 pdf_index, BaseFloat weight)
Accumulate stats for a single GMM in the model; returns log likelihood.
#define KALDI_LOG
Definition: kaldi-error.h:153
void Init(const DiagGmm &proto, int32 num_pdfs)
Initializes with a single "prototype" GMM.
Definition: am-diag-gmm.cc:38
Represents a non-allocating general vector which can be defined as a sub-vector of higher-level vecto...
Definition: kaldi-vector.h:501
void TestMllrAccsIO(const kaldi::AmDiagGmm &am_gmm, const kaldi::RegressionTree &regtree, const RegtreeMllrDiagGmmAccs &accs, const kaldi::Matrix< BaseFloat > adapt_data)
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95