init-ubm.cc
2.87 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
// sgmmbin/init-ubm.cc
// Copyright 2009-2011 Saarland University
// Author: Arnab Ghoshal
// 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 "base/kaldi-common.h"
#include "util/common-utils.h"
#include "util/kaldi-io.h"
#include "gmm/diag-gmm.h"
#include "gmm/full-gmm.h"
#include "gmm/am-diag-gmm.h"
#include "hmm/transition-model.h"
int main(int argc, char *argv[]) {
try {
typedef kaldi::int32 int32;
typedef kaldi::BaseFloat BaseFloat;
const char *usage =
"Cluster the Gaussians in a diagonal-GMM acoustic model\n"
"to a single full-covariance or diagonal-covariance GMM.\n"
"Usage: init-ubm [options] <model-file> <state-occs> <gmm-out>\n";
bool binary_write = true, fullcov_ubm = true;
kaldi::ParseOptions po(usage);
po.Register("binary", &binary_write, "Write output in binary mode");
po.Register("fullcov-ubm", &fullcov_ubm, "Write out full covariance UBM.");
kaldi::UbmClusteringOptions ubm_opts;
ubm_opts.Register(&po);
po.Read(argc, argv);
if (po.NumArgs() != 3) {
po.PrintUsage();
exit(1);
}
ubm_opts.Check();
std::string model_in_filename = po.GetArg(1),
occs_in_filename = po.GetArg(2),
gmm_out_filename = po.GetArg(3);
kaldi::AmDiagGmm am_gmm;
kaldi::TransitionModel trans_model;
{
bool binary_read;
kaldi::Input ki(model_in_filename, &binary_read);
trans_model.Read(ki.Stream(), binary_read);
am_gmm.Read(ki.Stream(), binary_read);
}
kaldi::Vector<BaseFloat> state_occs;
state_occs.Resize(am_gmm.NumPdfs());
{
bool binary_read;
kaldi::Input ki(occs_in_filename, &binary_read);
state_occs.Read(ki.Stream(), binary_read);
}
kaldi::DiagGmm ubm;
ClusterGaussiansToUbm(am_gmm, state_occs, ubm_opts, &ubm);
if (fullcov_ubm) {
kaldi::FullGmm full_ubm;
full_ubm.CopyFromDiagGmm(ubm);
kaldi::Output ko(gmm_out_filename, binary_write);
full_ubm.Write(ko.Stream(), binary_write);
} else {
kaldi::Output ko(gmm_out_filename, binary_write);
ubm.Write(ko.Stream(), binary_write);
}
KALDI_LOG << "Written UBM to " << gmm_out_filename;
} catch(const std::exception &e) {
std::cerr << e.what() << '\n';
return -1;
}
}