-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.m
282 lines (233 loc) · 8 KB
/
train.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
%%
clear;clc
%! Prepare GRAM and ORIGINAL folders first !%
root_dir = 'D:\Datasets\SVHN\all';
feat_name = 'cnn';
mix_num = 100;
vars = all_vars_func(root_dir, feat_name, mix_num);
write_config(vars);
%% ---------------------- GMM/HMM Training ------------------------
%% Init Train
% prepare wdnet
prepare_gram(vars);
% generate word list
generate_lists(vars);
% init HMM models
init_hmm_model(vars);
%% Embedded Training
hmm_dir = vars.flat_hmm_dir; % start with flat hmms
hmm_defs_path = fullfile(hmm_dir, vars.hmm_defs);
floor_path = fullfile(hmm_dir, vars.hmm_vfloors);
if ~exist(hmm_dir, 'dir'), mkdir(hmm_dir); end
% embedded training
cmd_emb = strjoin({'HERest.exe' ...
'-C' vars.htk_cfg_file...
'-H' hmm_defs_path ...
'-H' floor_path ...
'-S' vars.tr.samp_list ...
'-I' vars.tr.phone_mlf...
'-M' hmm_dir ...
vars.phone_list})
% decoding
cmd_decode = strjoin({'HVite.exe' vars.global_opt ...
'-C' vars.htk_cfg_file...
'-o SWC' ...
'-H' hmm_defs_path ...
'-H' floor_path ...
'-S' vars.te.samp_list ...
'-i' vars.te.rec_w_mlf ...
'-w' vars.wdnet_path ...
vars.dict_path ...
vars.phone_list});
% evaluation
cmd_eval = strjoin({'HResults.exe' vars.global_opt ...
'-f' ...
'-t' ...
'-I' vars.te.word_mlf ...
vars.phone_list ...
vars.te.rec_w_mlf});
cmd_rec = sprintf('%s & %s', cmd_decode, cmd_eval)
%% Forced Alignment
hmm_dir = vars.flat_hmm_dir; % start with flat hmms
hmm_defs_path = fullfile(hmm_dir, vars.hmm_defs);
floor_path = fullfile(hmm_dir, vars.hmm_vfloors);
% align train set
ds = vars.tr;
cmd_align_tr_s = strjoin({'HVite.exe' vars.global_opt ...
'-C' vars.htk_cfg_file...
'-a' ...
'-f' ...
'-o SW' ...
'-b SIL' ...
'-H' hmm_defs_path ...
'-H' floor_path ...
'-S' ds.samp_list ...
'-I' ds.word_mlf ...
'-i' ds.rec_s_mlf ...
vars.dict_path ...
vars.phone_list})
cmd_align_tr_w = strjoin({'HVite.exe' vars.global_opt ...
'-C' vars.htk_cfg_file...
'-a' ...
'-o SWC' ...
'-b SIL' ...
'-H' hmm_defs_path ...
'-H' floor_path ...
'-S' ds.samp_list ...
'-I' ds.word_mlf ...
'-i' ds.rec_w_mlf ...
vars.dict_path ...
vars.phone_list})
% align test set
ds = vars.te;
cmd_align_te_s = strjoin({'HVite.exe' vars.global_opt ...
'-C' vars.htk_cfg_file...
'-a' ...
'-f' ...
'-o SW' ...
'-b SIL' ...
'-H' hmm_defs_path ...
'-H' floor_path ...
'-S' ds.samp_list ...
'-I' ds.word_mlf ...
'-i' ds.rec_s_mlf ...
vars.dict_path ...
vars.phone_list})
cmd_align_te_w = strjoin({'HVite.exe' vars.global_opt ...
'-C' vars.htk_cfg_file...
'-a' ...
'-o SWC' ...
'-b SIL' ...
'-H' hmm_defs_path ...
'-H' floor_path ...
'-S' ds.samp_list ...
'-I' ds.word_mlf ...
'-i' ds.rec_w_mlf ...
vars.dict_path ...
vars.phone_list})
%% ----------------- ConvNet Training -----------------------------
% change OPTIONS.CFG and STATE2ID_OPTIONS.INI
%% State Tying
last_hmm_dir = vars.flat_hmm_dir;
new_hmm_dir = vars.tie_hmm_dir;
state_tying(last_hmm_dir, new_hmm_dir, vars);
%% make dataset, need 'tr_rec_state_mlf'
% first time '-make-train', '-make-test'
cmd_tr = strjoin({'python.exe' 'python/htkdata.py' '--make-train'})
cmd_te = strjoin({'python.exe' 'python/htkdata.py' '--make-test'})
%% save sample snapshot
% training samples
cmd_tr = strjoin({'python.exe' 'python/htkshowsamples.py' '--train --save' vars.convnet.tr.snapshot_dir})
% test samples
cmd_te = strjoin({'python.exe' 'python/htkshowsamples.py' '--test --save' vars.convnet.te.snapshot_dir})
%% Convnet Training
% !! before training, modify layer.output in `layers-htk.cfg`, test-range, train-range
cmd_conv_tr = strjoin({'python.exe' 'python/htktrain.py' ...
['--data-path=' vars.convnet.tr.batches_dir] ...
['--save-path=' vars.convnet.model_dir]...
'--layer-def=layer/definition.cfg' ...
'--layer-params=layer/params.cfg' ...
'--data-provider=svhn' ...
'--test-freq=10'...
'--train-range=1-4' ...
'--test-range=5'})
%% Change labels and continuous training
% get model file pathlast_hmm_dir
model_path = get_model_path(vars.convnet.model_dir)
% use forced alignment result to change labels of training data
cmd_chlb = strjoin({'python.exe' 'python/htkdata.py' '--change-labels'})
% continuous train convnet
cmd_train = strjoin({'python.exe' 'python/htktrain.py' ['--save-path=' ''] '-f' model_path})
%% Show train data Prediction
cmd_show = strjoin({'python.exe' 'python/htkshow.py' '-f' model_path '--show-cost=logprob --cost-idx=1'})
cmd_flts = strjoin({'python.exe' 'python/htkshow.py' '-f' model_path '--show-filters=conv1'})
%% --------------------------- Hybrid Training ---------------------------
% Change GMM mean to SenoneID
% Note: use ONLY ONCE
hybrid_assign_senone_ids(vars.senone_cfg_path);
%% Write TRAIN data Prediction probs
cmd_write = strjoin({'python.exe' 'python/htkwritefeatures.py' ...
'-f' model_path ...
'--write-features=probs' ...
['--feature-path=' vars.convnet.tr.results_dir]...
['--test-data-path=' vars.convnet.tr.batches_dir]})
% write recognition frame probs for training data
cmd_prob = strjoin({'python.exe' 'python/htkframeprobs.py' '-tr'})
%% Write TEST data Prediction probs
cmd_write = strjoin({'python.exe' 'python/htkwritefeatures.py' ...
'-f' model_path ...
'--write-features=probs' ...
['--feature-path=' vars.convnet.te.results_dir] ...
['--test-data-path=' vars.convnet.te.batches_dir]})
% write recognition frame probs for testing data
cmd_prob = strjoin({'python.exe' 'python/htkframeprobs.py' '-te'})
%% Hybrid Training and Decoding
new_hmm_dir = vars.senone_hmm_dir; %fullfile(vars.hmm_dir, 'hmm_senone');
hmm_defs_path = fullfile(new_hmm_dir, vars.hmm_defs);
floor_path = fullfile(new_hmm_dir, vars.hmm_vfloors);
% embedded training
cmd_train = strjoin({'emb_dnn.exe' ...
'-Y' 'train' ...
'-Z' vars.convnet.cfg_file ...
'-C' vars.htk_cfg_file ...
'-H' hmm_defs_path ...
'-H' floor_path ...
'-S' vars.tr.samp_list ...
'-I' vars.tr.phone_mlf...
'-M' new_hmm_dir ...
vars.phone_list})
% decoding
cmd_decode = strjoin({'hvite_dnn.exe' vars.global_opt ...
'-Y' 'test' ...
'-Z' vars.convnet.cfg_file ...
'-C' vars.htk_cfg_file ...
'-o SWC' ...
'-H' hmm_defs_path ...
'-H' floor_path ...
'-S' vars.te.samp_list ...
'-i' vars.te.rec_w_mlf ...
'-w' vars.wdnet_path ...
vars.dict_path ...
vars.phone_list});
% evaluation
cmd_eval = strjoin({'HResults.exe' vars.global_opt ...
'-f' ...
'-t' ...
'-I' vars.te.word_mlf ...
vars.phone_list ...
vars.te.rec_w_mlf});
cmd_rec = sprintf('%s & %s', cmd_decode, cmd_eval)
%% Hybrid Forced Alignment
hmm_defs_path = fullfile(new_hmm_dir, vars.hmm_defs);
floor_path = fullfile(new_hmm_dir, vars.hmm_vfloors);
% align train set
ds = vars.tr;
cmd_align_s = strjoin({'hvite_dnn.exe' vars.global_opt ...
'-Y' 'train' ...
'-Z' vars.convnet.cfg_file ...
'-C' vars.htk_cfg_file...
'-a' ...
'-f' ...
'-o SW' ...
'-b SIL' ...
'-H' hmm_defs_path ...
'-H' floor_path ...
'-S' ds.samp_list ...
'-I' ds.word_mlf ...
'-i' ds.rec_s_mlf ...
vars.dict_path ...
vars.phone_list})
cmd_align_w = strjoin({'hvite_dnn.exe' vars.global_opt ...
'-Y' 'train' ...
'-Z' vars.convnet.cfg_file ...
'-C' vars.htk_cfg_file...
'-a' ...
'-o SWC' ...
'-b SIL' ...
'-H' hmm_defs_path ...
'-H' floor_path ...
'-S' ds.samp_list ...
'-I' ds.word_mlf ...
'-i' ds.rec_w_mlf ...
vars.dict_path ...
vars.phone_list})