forked from aalto-speech/AaltoASR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit_hmm.cc
230 lines (196 loc) · 6.19 KB
/
init_hmm.cc
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include "HmmSet.hh"
#include "str.hh"
#include "conf.hh"
#define MAXLINE 4096
void read_segmodels(HmmSet &model, const std::string &filename);
void read_kernels(HmmSet &model, const std::string &cb_base, float gwidth,
bool diag_cov, bool full_cov);
void make_transitions(HmmSet &model);
conf::Config config;
int main(int argc, char **argv)
{
std::string statebind, cb_base, outbase;
std::string outfile;
float gwidth;
int diag_cov = 0, full_cov = 0;
HmmSet model;
try {
config("usage: init_hmm [OPTION...]\n")
('h', "help", "", "", "display help")
('b', "bind=FILE", "arg must", "", "model state configuration and bindings")
('a', "cb-base=BASENAME", "arg must", "", "base filename for kernel input")
('o', "out=BASENAME", "arg must", "", "base filename for output models")
('w', "gausswidth=FLOAT", "arg", "1.0", "width of the Gaussian kernels")
('d', "diag", "", "", "use diagonal covariances")
('f', "full", "", "", "use full covariances")
;
config.default_parse(argc, argv);
statebind = config["bind"].get_str();
cb_base = config["cb-base"].get_str();
outbase = config["out"].get_str();
gwidth = config["gausswidth"].get_float();
diag_cov = config["diag"].specified;
full_cov = config["full"].specified;
if (diag_cov && full_cov)
throw std::string("Both -d and -f can't be defined at the same time");
read_segmodels(model, statebind);
read_kernels(model, cb_base, gwidth, diag_cov, full_cov);
make_transitions(model);
outfile = outbase + std::string(".gk");
model.write_gk(outfile);
outfile = outbase + std::string(".mc");
model.write_mc(outfile);
outfile = outbase + std::string(".ph");
model.write_ph(outfile);
}
catch (std::exception &e) {
fprintf(stderr, "exception: %s\n", e.what());
abort();
}
catch (std::string &str) {
fprintf(stderr, "exception: %s\n", str.c_str());
abort();
}
return (0);
}
void read_segmodels(HmmSet &model, const std::string &filename)
{
FILE *fp;
char line[MAXLINE], *token, *label;
int num_of_states, last_state=0;
if ((fp = fopen(filename.c_str(),"r")) == NULL) {
throw std::string("ERROR: cannot open file ") + filename +
std::string(" for reading.");
}
while (fgets(line,MAXLINE,fp)) {
if ((token = strtok(line," \t\n")) != NULL) {
label=token;
if ((token = strtok(NULL," \t\n")) == NULL) {
fclose(fp);
throw std::string("ERROR: state info missing for model '") +
std::string(label) + std::string("' in file ") + filename;
}
num_of_states=atoi(token);
Hmm &hmm = model.add_hmm(label,num_of_states);
for (int i=0; i<num_of_states; i++) {
if ((token = strtok(NULL," \t\n")) == NULL) {
throw str::fmt(1024, "ERROR: state index %d/%d missing for model '%s' in file %s",i+1,num_of_states,hmm.label.c_str()) + filename;
fclose(fp); exit(-1);
}
hmm.state(i) = atoi(token);
if (hmm.state(i) > last_state) {
last_state=hmm.state(i);
}
}
}
}
fclose(fp);
model.reserve_states(last_state + 1);
return;
}
void read_kernels(HmmSet &model, const std::string &cb_base, float gwidth,
bool diag_cov, bool full_cov)
{
std::string ifile;
int k, num_of_kernels, d;
int dim = 0;
HmmCovariance::Type type;
std::string line;
line.reserve(4096); // for efficiency
std::vector<std::string> line_fields;
bool ok = true;
if (diag_cov) {
model.set_covariance_type(HmmCovariance::DIAGONAL);
type=HmmCovariance::DIAGONAL;
}
else if (full_cov) {
model.set_covariance_type(HmmCovariance::FULL);
type=HmmCovariance::FULL;
}
else {
model.set_covariance_type(HmmCovariance::SINGLE);
type=HmmCovariance::SINGLE;
}
for (int s=0; s<model.num_states(); s++) {
ifile = cb_base + str::fmt(64, "_%d.cod", s);
int width, height;
std::ifstream in(ifile.c_str());
if (!in) {
throw std::string("ERROR: cannot open file ") + ifile +
std::string(" for reading.");
}
// Read header
std::getline(in, line);
str::split(&line, " ", false, &line_fields, 0);
dim=str::str2long(line_fields[0].c_str(), &ok);
if (!ok)
throw std::string("Invalid dim field in header of file ") + ifile;
width=str::str2long(line_fields[2].c_str(), &ok);
if (!ok)
throw std::string("Invalid width field in header of file ") + ifile;
height=str::str2long(line_fields[3].c_str(), &ok);
if (!ok)
throw std::string("Invalid height field in header of file ") + ifile;
// Set dimension
if (s == 0)
model.set_dim(dim);
else if (model.dim() != dim) {
throw str::fmt(256, "Conflicting dimensions %d %d",model.dim(),dim);
}
// Read kernel
num_of_kernels = width * height;
float weight = 1.0/num_of_kernels;
for (k=0; k<num_of_kernels; k++) {
int index = model.num_kernels();
HmmKernel &kernel=model.add_kernel();
HmmState &state = model.state(s);
state.add_weight(index, weight);
// Read center
std::getline(in, line);
str::split(&line, " ", false, &line_fields, 0);
double temp;
for (d=0; d<model.dim(); d++) {
temp = str::str2float(line_fields[d].c_str(), &ok);
if (!ok)
throw std::string("Invalid mean parameter field in file ") + ifile;
kernel.center[d] = temp;
}
// Set variance
if (type==HmmCovariance::DIAGONAL)
{
for (d=0; d<dim; d++)
kernel.cov.diag(d) = gwidth;
}
else if (type==HmmCovariance::FULL)
{
for (d=0; d<dim; d++)
kernel.cov.full(d,d) = gwidth;
}
else if (type==HmmCovariance::SINGLE)
{
kernel.cov.var() = gwidth;
}
}
}
}
void
make_transitions(HmmSet &model)
{
for (int h=0;h<model.num_hmms();h++) {
Hmm &hmm=model.hmm(h);
model.add_transition(h,-1,0,1.0, -1);
for (int s=0;s<hmm.num_states();s++) {
model.add_transition(h,s,s,0.8,-1);
if (s+1==hmm.num_states())
model.add_transition(h,s,-2,0.2,-1);
else
model.add_transition(h,s,s+1,0.2,-1);
}
}
}