full-gmm-test.cc
10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// gmm/full-gmm-test.cc
// Copyright 2009-2011 Jan Silovsky; Saarland University;
// Microsoft Corporation
// See ../../COPYING for clarification regarding multiple authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
// WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
// MERCHANTABLITY OR NON-INFRINGEMENT.
// See the Apache 2 License for the specific language governing permissions and
// limitations under the License.
#include "gmm/full-gmm.h"
#include "gmm/diag-gmm.h"
#include "gmm/model-test-common.h"
#include "util/stl-utils.h"
#include "util/kaldi-io.h"
#include "gmm/full-gmm-normal.h"
#include "gmm/mle-full-gmm.h"
using namespace kaldi;
void RandPosdefSpMatrix(size_t dim, SpMatrix<BaseFloat> *matrix,
TpMatrix<BaseFloat> *matrix_sqrt = NULL,
BaseFloat *logdet = NULL) {
// generate random (non-singular) matrix
Matrix<BaseFloat> tmp(dim, dim);
while (1) {
tmp.SetRandn();
if (tmp.Cond() < 100) break;
std::cout << "Condition number of random matrix large "
<< static_cast<float>(tmp.Cond()) << ", trying again (this is normal)"
<< '\n';
}
// tmp * tmp^T will give positive definite matrix
matrix->AddMat2(1.0, tmp, kNoTrans, 0.0);
if (matrix_sqrt != NULL) matrix_sqrt->Cholesky(*matrix);
if (logdet != NULL) *logdet = matrix->LogPosDefDet();
if ((matrix_sqrt == NULL) && (logdet == NULL)) {
TpMatrix<BaseFloat> sqrt(dim);
sqrt.Cholesky(*matrix);
}
}
void init_rand_diag_gmm(DiagGmm *gmm) {
size_t num_comp = gmm->NumGauss(), dim = gmm->Dim();
Vector<BaseFloat> weights(num_comp);
Matrix<BaseFloat> means(num_comp, dim), vars(num_comp, dim);
BaseFloat tot_weight = 0.0;
for (size_t m = 0; m < num_comp; m++) {
weights(m) = kaldi::RandUniform();
for (size_t d= 0; d < dim; d++) {
means(m, d) = kaldi::RandGauss();
vars(m, d) = Exp(kaldi::RandGauss()) + 1e-5;
}
tot_weight += weights(m);
}
// normalize weights
for (size_t m = 0; m < num_comp; m++) {
weights(m) /= tot_weight;
}
vars.InvertElements();
gmm->SetWeights(weights);
gmm->SetInvVarsAndMeans(vars, means);
gmm->Perturb(0.5 * RandUniform());
gmm->ComputeGconsts(); // this is unnecassary; computed in Perturb
}
void UnitTestFullGmmEst() {
FullGmm fgmm;
int32 dim = 10 + Rand() % 10, num_comp = 1 + Rand() % 10;
unittest::InitRandFullGmm(dim, num_comp, &fgmm);
int32 num_frames = 5000;
Matrix<BaseFloat> feats(num_frames, dim);
FullGmmNormal fgmm_normal(fgmm);
fgmm_normal.Rand(&feats);
AccumFullGmm acc(fgmm, kGmmAll);
for (int32 t = 0; t < num_frames; t++)
acc.AccumulateFromFull(fgmm, feats.Row(t), 1.0);
BaseFloat objf_change, count;
MleFullGmmOptions opts;
MleFullGmmUpdate(opts, acc, kGmmAll, &fgmm, &objf_change, &count);
BaseFloat change = objf_change / count,
num_params = (num_comp * (dim + 1 + (dim*(dim+1)/2))),
predicted_change = 0.5 * num_params / num_frames; // Was there
KALDI_LOG << "Objf change per frame was " << change << " vs. predicted "
<< predicted_change;
KALDI_ASSERT(change < 2.0 * predicted_change && change > 0.0);
}
void
UnitTestFullGmm() {
// random dimension of the gmm
size_t dim = 1 + kaldi::RandInt(0, 9);
// random number of mixtures
size_t nMix = 1 + kaldi::RandInt(0, 9);
std::cout << "Testing NumGauss: " << nMix << ", " << "Dim: " << dim
<< '\n';
// generate random feature vector and
// random mean vectors and covariance matrices
Vector<BaseFloat> feat(dim);
Vector<BaseFloat> weights(nMix);
Vector<BaseFloat> loglikes(nMix);
Matrix<BaseFloat> means(nMix, dim);
std::vector<SpMatrix<BaseFloat> > invcovars(nMix);
for (size_t mix = 0; mix < nMix; mix++) {
invcovars[mix].Resize(dim);
}
Vector<BaseFloat> covars_logdet(nMix);
for (size_t d = 0; d < dim; d++) {
feat(d) = kaldi::RandGauss();
}
float tot_weight = 0.0;
for (size_t m = 0; m < nMix; m++) {
weights(m) = kaldi::RandUniform();
for (size_t d = 0; d < dim; d++) {
means(m, d) = kaldi::RandGauss();
}
SpMatrix<BaseFloat> covar(dim);
RandPosdefSpMatrix(dim, &covar, NULL, &covars_logdet(m));
invcovars[m].CopyFromSp(covar);
invcovars[m].InvertDouble();
tot_weight += weights(m);
}
// normalize weights and compute loglike for feature vector
for (size_t m = 0; m < nMix; m++) {
weights(m) /= tot_weight;
}
// compute loglike for feature vector
float loglike = 0.0;
for (size_t m = 0; m < nMix; m++) {
loglikes(m) += -0.5 * (M_LOG_2PI * dim
+ covars_logdet(m)
+ VecSpVec(means.Row(m), invcovars[m], means.Row(m))
+ VecSpVec(feat, invcovars[m], feat))
+ VecSpVec(means.Row(m), invcovars[m], feat);
loglikes(m) += Log(weights(m));
}
loglike = loglikes.LogSumExp();
// new GMM
FullGmm *gmm = new FullGmm();
gmm->Resize(nMix, dim);
gmm->SetWeights(weights);
gmm->SetInvCovarsAndMeans(invcovars, means);
gmm->ComputeGconsts();
Vector<BaseFloat> posterior1(nMix);
float loglike1 = gmm->ComponentPosteriors(feat, &posterior1);
// std::cout << "LogLike: " << loglike << '\n';
// std::cout << "LogLike1: " << loglike1 << '\n';
AssertEqual(loglike, loglike1, 0.01);
KALDI_ASSERT(fabs(1.0 - posterior1.Sum()) < 0.001);
{ // Test various accessors / mutators
Vector<BaseFloat> weights_bak(nMix);
Matrix<BaseFloat> means_bak(nMix, dim);
std::vector<SpMatrix<BaseFloat> > invcovars_bak(nMix);
for (size_t i = 0; i < nMix; i++) {
invcovars_bak[i].Resize(dim);
}
weights_bak.CopyFromVec(gmm->weights());
gmm->GetMeans(&means_bak);
gmm->GetCovars(&invcovars_bak);
for (size_t i = 0; i < nMix; i++) {
invcovars_bak[i].InvertDouble();
}
// set all params one-by-one to new model
FullGmm gmm2;
gmm2.Resize(gmm->NumGauss(), gmm->Dim());
gmm2.SetWeights(weights_bak);
gmm2.SetMeans(means_bak);
gmm2.SetInvCovars(invcovars_bak);
gmm2.ComputeGconsts();
BaseFloat loglike_gmm2 = gmm2.LogLikelihood(feat);
AssertEqual(loglike1, loglike_gmm2);
{
Vector<BaseFloat> loglikes;
gmm2.LogLikelihoods(feat, &loglikes);
AssertEqual(loglikes.LogSumExp(), loglike_gmm2);
}
{
std::vector<int32> indices;
for (int32 i = 0; i < gmm2.NumGauss(); i++)
indices.push_back(i);
Vector<BaseFloat> loglikes;
gmm2.LogLikelihoodsPreselect(feat, indices, &loglikes);
AssertEqual(loglikes.LogSumExp(), loglike_gmm2);
}
// single component mean accessor + mutator
FullGmm gmm3;
gmm3.Resize(gmm->NumGauss(), gmm->Dim());
gmm3.SetWeights(weights_bak);
means_bak.SetZero();
for (size_t i = 0; i < nMix; i++) {
SubVector<BaseFloat> tmp = means_bak.Row(i);
gmm->GetComponentMean(i, &tmp);
}
gmm3.SetMeans(means_bak);
gmm3.SetInvCovars(invcovars_bak);
gmm3.ComputeGconsts();
float loglike_gmm3 = gmm3.LogLikelihood(feat);
AssertEqual(loglike1, loglike_gmm3, 0.01);
// set all params one-by-one to new model
FullGmm gmm4;
gmm4.Resize(gmm->NumGauss(), gmm->Dim());
gmm4.SetWeights(weights_bak);
gmm->GetCovarsAndMeans(&invcovars_bak, &means_bak);
for (size_t i = 0; i < nMix; i++) {
invcovars_bak[i].InvertDouble();
}
gmm4.SetInvCovarsAndMeans(invcovars_bak, means_bak);
gmm4.ComputeGconsts();
BaseFloat loglike_gmm4 = gmm4.LogLikelihood(feat);
AssertEqual(loglike1, loglike_gmm4, 0.001);
} // Test various accessors / mutators end
// First, non-binary write
gmm->Write(Output("tmpf", false).Stream(), false);
{ // I/O tests
bool binary_in;
FullGmm *gmm2 = new FullGmm();
Input ki("tmpf", &binary_in);
gmm2->Read(ki.Stream(), binary_in);
float loglike3 = gmm2->ComponentPosteriors(feat, &posterior1);
AssertEqual(loglike, loglike3, 0.01);
// binary write
gmm2->Write(Output("tmpfb", true).Stream(), true);
delete gmm2;
// binary read
FullGmm *gmm3;
gmm3 = new FullGmm();
Input ki2("tmpfb", &binary_in);
gmm3->Read(ki2.Stream(), binary_in);
AssertEqual(loglike, loglike3, 0.01);
delete gmm3;
}
{ // CopyFromFullGmm
FullGmm gmm4;
gmm4.CopyFromFullGmm(*gmm);
float loglike5 = gmm4.ComponentPosteriors(feat, &posterior1);
AssertEqual(loglike, loglike5, 0.01);
}
{ // test copy from DiagGmm and back to DiagGmm
DiagGmm gmm_diag;
gmm_diag.Resize(nMix, dim);
init_rand_diag_gmm(&gmm_diag);
float loglike_diag = gmm_diag.LogLikelihood(feat);
FullGmm gmm_full;
gmm_full.CopyFromDiagGmm(gmm_diag);
float loglike_full = gmm_full.LogLikelihood(feat);
DiagGmm gmm_diag2;
gmm_diag2.CopyFromFullGmm(gmm_full);
float loglike_diag2 = gmm_diag2.LogLikelihood(feat);
AssertEqual(loglike_diag, loglike_full, 0.01);
AssertEqual(loglike_diag, loglike_diag2, 0.01);
}
{ // split and merge test for 1 component GMM (doesn't test the merge crit.)
FullGmm gmm1;
Vector<BaseFloat> weights1(1);
Matrix<BaseFloat> means1(1, dim);
std::vector<SpMatrix<BaseFloat> > invcovars1(1);
weights1(0) = 1.0;
means1.CopyFromMat(means.Range(0, 1, 0, dim));
invcovars1[0].Resize(dim);
invcovars1[0].CopyFromSp(invcovars[0]);
gmm1.Resize(1, dim);
gmm1.SetWeights(weights1);
gmm1.SetInvCovarsAndMeans(invcovars1, means1);
gmm1.ComputeGconsts();
FullGmm gmm2;
gmm2.CopyFromFullGmm(gmm1);
gmm2.Split(2, 0.001);
gmm2.Merge(1);
float loglike1 = gmm1.LogLikelihood(feat);
float loglike2 = gmm2.LogLikelihood(feat);
AssertEqual(loglike1, loglike2, 0.01);
}
delete gmm;
unlink("tmpf");
unlink("tmpfb");
}
int
main() {
// repeat the test ten times
for (int i = 0; i < 2; i++) {
UnitTestFullGmm();
UnitTestFullGmmEst();
}
std::cout << "Test OK.\n";
}