/
read_htk_hmm.m
162 lines (137 loc) · 4.13 KB
/
read_htk_hmm.m
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
function hmms = read_htk_hmm(filename)
% hmm = read_htk_hmm(filename)
%
% Reads in an HTK HMM definition file. Only works on text files.
% At the moment this only works for Gaussian emissions with
% diagonal covariance.
%
% 2006-06-09 ronw@ee.columbia.edu
% Copyright (C) 2006-2007 Ron J. Weiss
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
% Read the M-file into a cell array of strings:
[fid, message] = fopen(filename, 'rt');
warning(message)
file = textscan(fid, '%s', 'delimiter', '\n', 'whitespace', '', 'bufSize', 16000);
fclose(fid);
file = file{1};
% Remove any empty lines
file = file(cellfun('length', file) > 0);
nhmms = 1;
lastlinewashmm = 0;
for x = 1:length(file)
% header stuff:
%tok = regexpi(file{x}, '<VECSIZE> %d', 'tokens');
%if ~isempty(tok)
% vecsize = tok{1}
%end
% is this a new HMM?
if ~lastlinewashmm
if length(file{x}) >= 2
if file{x}(1:2) == '~h' | ~isempty(strmatch(upper(file{x}), '<BEGINHMM>'))
hmms(nhmms) = readNextHMM(file, x);
nhmms = nhmms+1;
lastlinewashmm = 1;
end
end
else
lastlinewashmm = 0;
end
end
%%%%%%%%%%
function hmm = readNextHMM(file, linenum)
x = linenum;
if ~isempty(findstr(file{x}, '~h'))
c = strread(file{linenum}, '~h %q');
hmm.name = c{1};
x = x + 1;
else
hmm.name = 'matlabhmm';
end
x = x + 1;
% first and last state in
hmm.nstates = strread(upper(file{x}), '<NUMSTATES> %d')-2;
x = x + 1;
while isempty(findstr(upper(file{x}),'<TRANSP>'))
state = strread(upper(file{x}), '<STATE> %d') - 1;
x = x+1;
if ~isempty(findstr(upper(file{x}), '<NUMMIXES>'))
nmix = strread(upper(file{x}), '<NUMMIXES> %d');
x = x+1;
hmm.gmms(state).nmix = nmix;
hmm.gmms(state).priors(1:nmix) = -Inf;
else
nmix = 1;
end
for n = 1:nmix
if ~isempty(findstr(file{x}, '~s "'))
x = x+1;
break
end
if nmix > 1
if isempty(findstr(upper(file{x}), '<MIXTURE>'))
% sometimes HTK skips mixture components. If we make sure
% that is a prior of -Inf, then it won't be a problem.
% Luckilly this is take care of in the initialization above.
continue
end
[currmix, prior] = strread(upper(file{x}), '<MIXTURE> %d %f');
x = x+1;
end
ndim = strread(upper(file{x}), '<MEAN> %d');
x = x+1;
if n == 1 & nmix > 1
hmm.gmms(state).means(1:ndim,1:nmix) = 0;
hmm.gmms(state).covars(1:ndim,1:nmix) = 1;
end
mu = strread(file{x}, '%f', ndim);
x = x+1;
ndim = strread(upper(file{x}), '<VARIANCE> %d');
x = x+1;
covar = strread(file{x}, '%f', ndim);
x = x+1;
if ~isempty(findstr(upper(file{x}), '<GCONST>'))
gconst = strread(upper(file{x}), '<GCONST> %f');
x = x+1;
end
if nmix == 1
% Gaussian emissions
hmm.emission_type = 'gaussian';
hmm.means(:, state) = mu;
hmm.covars(:, state) = covar;
else
% GMM emissions
hmm.emission_type = 'GMM';
hmm.gmms(state).priors(currmix) = log(prior);
hmm.gmms(state).nmix = nmix;
hmm.gmms(state).means(:, currmix) = mu;
hmm.gmms(state).covars(:, currmix) = covar;
end
end
end
nstates = strread(upper(file{x}), '<TRANSP> %d');
x = x+1;
transmat = zeros(nstates);
for n = 1:nstates
transmat(n,:) = strread(file{x}, '%f', nstates);
x = x+1;
end
w = warning('query', 'MATLAB:log:logOfZero');
if strcmp(w.state, 'on')
warning('off', 'MATLAB:log:logOfZero');
end
hmm.start_prob = log(transmat(1,2:end-1));
hmm.transmat = log(transmat(2:end-1,2:end-1));
hmm.end_prob = log(transmat(2:end-1,end));
warning(w.state, w.identifier);