forked from kaldi-asr/kaldi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-tree-two-level.cc
More file actions
213 lines (178 loc) · 7.36 KB
/
build-tree-two-level.cc
File metadata and controls
213 lines (178 loc) · 7.36 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
// bin/build-tree-two-level.cc
// Copyright 2009-2011 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 "base/kaldi-common.h"
#include "util/common-utils.h"
#include "hmm/hmm-topology.h"
#include "tree/context-dep.h"
#include "tree/build-tree.h"
#include "tree/build-tree-utils.h"
#include "tree/context-dep.h"
#include "tree/clusterable-classes.h"
#include "util/text-utils.h"
namespace kaldi {
void GetSeenPhones(BuildTreeStatsType &stats, int P, std::vector<int32> *phones_out) {
// Get list of phones that we saw (in the central position P, although it
// shouldn't matter what position).
std::set<int32> phones_set;
for (size_t i = 0 ; i < stats.size(); i++) {
const EventType &evec = stats[i].first;
for (size_t j = 0; j < evec.size(); j++) {
if (evec[j].first == P) { // "key" is position P
KALDI_ASSERT(evec[j].second != 0);
phones_set.insert(evec[j].second); // insert "value" of this
// phone.
}
}
CopySetToVector(phones_set, phones_out);
}
}
}
int main(int argc, char *argv[]) {
using namespace kaldi;
try {
using namespace kaldi;
typedef kaldi::int32 int32;
const char *usage =
"Trains two-level decision tree. Outputs the larger tree, and a mapping from the\n"
"leaf-ids of the larger tree to those of the smaller tree. Useful, for instance,\n"
"in tied-mixture systems with multiple codebooks.\n"
"\n"
"Usage: build-tree-two-level [options] <tree-stats-in> <roots-file> <questions-file> <topo-file> <tree-out> <mapping-out>\n"
"e.g.: \n"
" build-tree-two-level treeacc roots.txt 1.qst topo tree tree.map\n";
bool binary = true;
int32 P = 1, N = 3;
bool cluster_leaves = true;
int32 max_leaves_first = 1000;
int32 max_leaves_second = 5000;
std::string occs_out_filename;
ParseOptions po(usage);
po.Register("binary", &binary, "Write output in binary mode");
po.Register("context-width", &N, "Context window size [must match "
"acc-tree-stats]");
po.Register("central-position", &P, "Central position in context window "
"[must match acc-tree-stats]");
po.Register("max-leaves-first", &max_leaves_first, "Maximum number of "
"leaves in first-level decision tree.");
po.Register("max-leaves-second", &max_leaves_second, "Maximum number of "
"leaves in second-level decision tree.");
po.Register("cluster-leaves", &cluster_leaves, "If true, do a post-clustering"
" of the leaves of the final decision tree.");
po.Read(argc, argv);
if (po.NumArgs() != 6) {
po.PrintUsage();
exit(1);
}
std::string stats_filename = po.GetArg(1),
roots_filename = po.GetArg(2),
questions_filename = po.GetArg(3),
topo_filename = po.GetArg(4),
tree_out_filename = po.GetArg(5),
map_out_filename = po.GetArg(6);
// Following 2 variables derived from roots file.
// phone_sets is sets of phones that share their roots.
// Just one phone each for normal systems.
std::vector<std::vector<int32> > phone_sets;
std::vector<bool> is_shared_root;
std::vector<bool> is_split_root;
{
Input ki(roots_filename.c_str());
ReadRootsFile(ki.Stream(), &phone_sets, &is_shared_root, &is_split_root);
}
HmmTopology topo;
ReadKaldiObject(topo_filename, &topo);
BuildTreeStatsType stats;
{
bool binary_in;
GaussClusterable gc; // dummy needed to provide type.
Input ki(stats_filename, &binary_in);
ReadBuildTreeStats(ki.Stream(), binary_in, gc, &stats);
}
std::cerr << "Number of separate statistics is " << stats.size() << '\n';
Questions qo;
{
bool binary_in;
try {
Input ki(questions_filename, &binary_in);
qo.Read(ki.Stream(), binary_in);
} catch (const std::exception &e) {
KALDI_ERR << "Error reading questions file "<<questions_filename<<", error is: " << e.what();
}
}
std::vector<int32> phone2num_pdf_classes;
topo.GetPhoneToNumPdfClasses(&phone2num_pdf_classes);
EventMap *to_pdf = NULL;
std::vector<int32> mapping;
//////// Build the tree. ////////////
to_pdf = BuildTreeTwoLevel(qo,
phone_sets,
phone2num_pdf_classes,
is_shared_root,
is_split_root,
stats,
max_leaves_first,
max_leaves_second,
cluster_leaves,
P,
&mapping);
ContextDependency ctx_dep(N, P, to_pdf); // takes ownership
// of pointer "to_pdf", so set it NULL.
to_pdf = NULL;
WriteKaldiObject(ctx_dep, tree_out_filename, binary);
{
Output ko(map_out_filename, binary);
WriteIntegerVector(ko.Stream(), binary, mapping);
}
{ // This block is just doing some checks.
std::vector<int32> all_phones;
for (size_t i = 0; i < phone_sets.size(); i++)
all_phones.insert(all_phones.end(),
phone_sets[i].begin(), phone_sets[i].end());
SortAndUniq(&all_phones);
if (all_phones != topo.GetPhones()) {
std::ostringstream ss;
WriteIntegerVector(ss, false, all_phones);
ss << " vs. ";
WriteIntegerVector(ss, false, topo.GetPhones());
KALDI_WARN << "Mismatch between phone sets provided in roots file, and those in topology: " << ss.str();
}
std::vector<int32> phones_vec; // phones we saw.
GetSeenPhones(stats, P, &phones_vec);
std::vector<int32> unseen_phones; // diagnostic.
for (size_t i = 0; i < all_phones.size(); i++)
if (!std::binary_search(phones_vec.begin(), phones_vec.end(), all_phones[i]))
unseen_phones.push_back(all_phones[i]);
for (size_t i = 0; i < phones_vec.size(); i++)
if (!std::binary_search(all_phones.begin(), all_phones.end(), phones_vec[i]))
KALDI_ERR << "Phone "<< (phones_vec[i]) << " appears in stats but is not listed in roots file.";
if (!unseen_phones.empty()) {
std::ostringstream ss;
for (size_t i = 0; i < unseen_phones.size(); i++)
ss << unseen_phones[i] << ' ';
// Note, unseen phones is just a warning as in certain kinds of
// systems, this can be OK (e.g. where phone encodes position and
// stress information).
KALDI_WARN << "Saw no stats for following phones: " << ss.str();
}
}
std::cerr << "Wrote tree and mapping\n";
DeleteBuildTreeStats(&stats);
} catch(const std::exception &e) {
std::cerr << e.what();
return -1;
}
}