acc-tree-stats.cc
4.46 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
// bin/acc-tree-stats.cc
// Copyright 2009-2011 Microsoft Corporation, GoVivace Inc.
// 2013 Johns Hopkins University (author: Daniel Povey)
// 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 "tree/context-dep.h"
#include "tree/build-tree-utils.h"
#include "hmm/transition-model.h"
#include "hmm/tree-accu.h"
/** @brief Accumulate tree statistics for decision tree training. The
program reads in a feature archive, and the corresponding alignments,
and generates the sufficient statistics for the decision tree
creation. Context width and central phone position are used to
identify the contexts.Transition model is used as an input to identify
the PDF's and the phones. */
int main(int argc, char *argv[]) {
using namespace kaldi;
typedef kaldi::int32 int32;
try {
const char *usage =
"Accumulate statistics for phonetic-context tree building.\n"
"Usage: acc-tree-stats [options] <model-in> <features-rspecifier> <alignments-rspecifier> <tree-accs-out>\n"
"e.g.: \n"
" acc-tree-stats 1.mdl scp:train.scp ark:1.ali 1.tacc\n";
bool binary = true;
AccumulateTreeStatsOptions opts;
ParseOptions po(usage);
po.Register("binary", &binary, "Write output in binary mode");
opts.Register(&po);
po.Read(argc, argv);
if (po.NumArgs() != 4) {
po.PrintUsage();
exit(1);
}
std::string model_filename = po.GetArg(1),
feature_rspecifier = po.GetArg(2),
alignment_rspecifier = po.GetArg(3),
accs_out_wxfilename = po.GetOptArg(4);
AccumulateTreeStatsInfo acc_tree_stats_info(opts);
TransitionModel trans_model;
{
bool binary;
Input ki(model_filename, &binary);
trans_model.Read(ki.Stream(), binary);
// There is more in this file but we don't need it.
}
SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
RandomAccessInt32VectorReader alignment_reader(alignment_rspecifier);
std::map<EventType, GaussClusterable*> tree_stats;
int num_done = 0, num_no_alignment = 0, num_other_error = 0;
for (; !feature_reader.Done(); feature_reader.Next()) {
std::string key = feature_reader.Key();
if (!alignment_reader.HasKey(key)) {
num_no_alignment++;
} else {
const Matrix<BaseFloat> &mat = feature_reader.Value();
const std::vector<int32> &alignment = alignment_reader.Value(key);
if (alignment.size() != mat.NumRows()) {
KALDI_WARN << "Alignments has wrong size "<< (alignment.size())<<" vs. "<< (mat.NumRows());
num_other_error++;
continue;
}
AccumulateTreeStats(trans_model,
acc_tree_stats_info,
alignment,
mat,
&tree_stats);
num_done++;
if (num_done % 1000 == 0)
KALDI_LOG << "Processed " << num_done << " utterances.";
}
}
BuildTreeStatsType stats; // vectorized form.
for (std::map<EventType, GaussClusterable*>::const_iterator iter = tree_stats.begin();
iter != tree_stats.end();
++iter) {
stats.push_back(std::make_pair(iter->first, iter->second));
}
tree_stats.clear();
{
Output ko(accs_out_wxfilename, binary);
WriteBuildTreeStats(ko.Stream(), binary, stats);
}
KALDI_LOG << "Accumulated stats for " << num_done << " files, "
<< num_no_alignment << " failed due to no alignment, "
<< num_other_error << " failed for other reasons.";
KALDI_LOG << "Number of separate stats (context-dependent states) is "
<< stats.size();
DeleteBuildTreeStats(&stats);
if (num_done != 0) return 0;
else return 1;
} catch(const std::exception &e) {
std::cerr << e.what();
return -1;
}
}