2
## ====================================================================
4
## Copyright (c) 1996-2000 Carnegie Mellon University. All rights
7
## Redistribution and use in source and binary forms, with or without
8
## modification, are permitted provided that the following conditions
11
## 1. Redistributions of source code must retain the above copyright
12
## notice, this list of conditions and the following disclaimer.
14
## 2. Redistributions in binary form must reproduce the above copyright
15
## notice, this list of conditions and the following disclaimer in
16
## the documentation and/or other materials provided with the
19
## This work was supported in part by funding from the Defense Advanced
20
## Research Projects Agency and the National Science Foundation of the
21
## United States of America, and the CMU Sphinx Speech Consortium.
23
## THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
24
## ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
25
## THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26
## PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
27
## NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28
## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29
## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30
## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31
## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32
## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33
## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
## ====================================================================
37
## Author: Ricky Houghton
38
## Author: David Huggins-Daines
44
use File::Spec::Functions;
47
use lib catdir(dirname($0), updir(), 'lib');
48
use SphinxTrain::Config;
49
use SphinxTrain::Util;
51
# LDA/MLLT doesn't really have sense with multistream
52
if ($ST::CFG_FEATURE eq "s2_4x") {
53
Log("Feature type is s2_4x which is 4 streams");
54
Log("LDA/MLLT only has sense for single stream features, for example 1s_c_d_dd");
55
Log("Skipping LDA training");
59
if ($ST::CFG_LDA_MLLT ne 'yes') {
60
Log("MODULE: 01 Train LDA transformation\n");
61
Log("Skipped (set \$CFG_LDA_MLLT = 'yes' to enable)\n");
70
my $n_parts = ($ST::CFG_NPART) ? $ST::CFG_NPART : 1;
71
# If the number of parts is given as command line argument, overwrite
72
# the number coming from the config file
77
$| = 1; # Turn on autoflushing
78
my $logdir = "$ST::CFG_LOG_DIR/01.lda_train";
81
Log("MODULE: 01 Train LDA transformation\n");
82
Log("Phase 1: Cleaning up directories:");
83
# Don't do this on a queue, because of NFS bugs
84
unless ($ST::CFG_QUEUE_TYPE eq 'Queue::PBS') {
85
LogProgress("\taccumulator...");
86
rmtree ($ST::CFG_BWACCUM_DIR, 0, 1);
87
mkdir ($ST::CFG_BWACCUM_DIR,0777);
89
LogProgress("logs...");
90
rmtree($logdir, 0, 1);
92
LogProgress("qmanager...\n");
93
rmtree ($ST::CFG_QMGR_DIR, 0, 1);
94
mkdir ($ST::CFG_QMGR_DIR,0777);
95
LogStatus('completed');
97
# For the first iteration Flat initialize models.
98
my $return_value = FlatInitialize();
99
exit ($return_value) if ($return_value);
101
Log("Phase 3: Forward-Backward");
105
Log("Phase 4: LDA transform estimation");
107
for (my $i=1; $i<=$n_parts; $i++) {
108
push @deps, LaunchScript("bw.lda.$iter.$i", ['baum_welch.pl', $iter, $i, $n_parts, 'yes']);
110
LaunchScript("lda", "lda_train.pl", \@deps);
114
for (my $i=1; $i<=$n_parts; $i++) {
115
push @deps, LaunchScript("bw.lda.$iter.$i", ['baum_welch.pl', $iter, $i, $n_parts, 'no']);
117
LaunchScript("norm.$iter", ['norm_and_launchbw.pl', $iter, $n_parts], \@deps);
118
# On the first iteration, wait for the LDA stuff to complete
119
my $lda_log = File::Spec->catfile($logdir, "$ST::CFG_EXPTNAME.lda_train.log");
121
# This is kind of a lousy way to do it, but oh well...
122
local $SIG{CHLD} = sub { wait; };
126
for ($iter = 1; $iter <= $ST::CFG_MAX_ITERATIONS; ++$iter) {
127
my $norm_log = File::Spec->catfile($logdir,
128
"$ST::CFG_EXPTNAME.$iter.norm.log");
129
if (open LOG, "<$norm_log") {
130
if (/failed/ or /Aborting/) {
131
LogError("Training failed in iteration $iter");
136
if (open LOG, "<$lda_log") {
139
LogError("LDA Training failed");
143
Log("LDA Training completed", 'result');
154
Log("Phase 2: Copy initialize from falign model\n");
155
my $hmmdir = catdir($ST::CFG_BASE_DIR, "model_parameters");
156
my $outhmm = catdir($hmmdir, "${ST::CFG_EXPTNAME}.ci_lda_flatinitial");
157
my $modarchdir = catdir($ST::CFG_BASE_DIR, "model_architecture");
158
mkdir ($outhmm,0777);
159
foreach (qw(means variances mixture_weights transition_matrices)) {
160
copy(catfile($ST::CFG_FORCE_ALIGN_MODELDIR, $_), catfile($outhmm, $_))
163
my $ci_mdeffile = catfile($modarchdir, "$ST::CFG_EXPTNAME.falign_ci.mdef");
164
my $out_mdeffile = catfile($modarchdir, "$ST::CFG_EXPTNAME.ci.mdef");
165
copy($ci_mdeffile, $out_mdeffile);
171
Log("Phase 2: Flat initialize\n");
173
#**************************************************************************
174
# this script given an mdef file and a codebook (means/vars in S3 format)
175
# produces flat mixture weights in a semicontinuos setting. From the models
176
# produced by this script we can restart normal baum-welch training
177
# Flat init might not be the best choice, specially if we have access to
178
# segmentation files for the whole training database.
179
#**************************************************************************
181
my $logdir = "$ST::CFG_LOG_DIR/01.lda_train";
182
my $modarchdir = "$ST::CFG_BASE_DIR/model_architecture";
183
my $hmmdir = "$ST::CFG_BASE_DIR/model_parameters";
184
mkdir ($logdir,0777);
185
mkdir ($modarchdir,0777);
186
mkdir ($hmmdir,0777);
189
#-------------------------------------------------------------------------
190
# Take the phone list. Put three hyphens after every phone. That is the
191
# required format. Then make a ci model definition file from it using the
193
#-------------------------------------------------------------------------
195
#$rawphonefile obtained from variables.def
196
my $phonefile = "$modarchdir/$ST::CFG_EXPTNAME.phonelist";
197
my $ci_mdeffile = "$modarchdir/$ST::CFG_EXPTNAME.ci.mdef";
199
open PHONELIST, "<".$ST::CFG_RAWPHONEFILE;
200
open PHONEFILE, ">".$phonefile;
203
while (defined(my $line = <PHONELIST>)) {
205
next if $line =~ m/^\s*$/;
206
$line =~ s/$/ - - - /;
207
push @phonelist, $line;
211
# Make sure the CI phones are sorted, as PocketSphinx requires them to be
212
foreach (sort @phonelist) {
213
print PHONEFILE $_, "\n";
215
# system "sed 's+\$+ - - - +g' $ST::CFG_RAWPHONEFILE > $phonefile";
218
my $logfile = "$logdir/${ST::CFG_EXPTNAME}.make_ci_mdef_fromphonelist.log";
219
#-------------------------------------------------------------------------
220
# Decide on what topology to use for the hmms: 3 state, 5 state, blah state
221
# or what, give it to the variable "statesperhmm" and use it to create
222
# the topology matrix in the topology file
223
#-------------------------------------------------------------------------
225
#$statesperhmm obtained from variables.def
226
my $topologyfile = "$modarchdir/$ST::CFG_EXPTNAME.topology";
227
RunScript("../prepare/maketopology.pl", $topologyfile);
230
if ($return_value = RunTool('mk_mdef_gen', $logfile, 0,
231
-phnlstfn => $phonefile,
232
-ocimdef => $ci_mdeffile,
233
-n_state_pm => $ST::CFG_STATESPERHMM)) {
234
return $return_value;
237
#-------------------------------------------------------------------------
238
# make the flat models using the above topology file and the mdef file
239
#------------------------------------------------------------------------
240
my $outhmm = "$hmmdir/${ST::CFG_EXPTNAME}.ci_lda_flatinitial";
241
mkdir ($outhmm,0777);
243
my $FLAT = "$ST::CFG_BIN_DIR/mk_flat";
245
$logfile = "$logdir/${ST::CFG_EXPTNAME}.makeflat_cihmm.log";
246
if ($return_value = RunTool('mk_flat', $logfile, 0,
247
-moddeffn => $ci_mdeffile,
248
-topo => $topologyfile,
249
-mixwfn => catfile($outhmm, 'mixture_weights'),
250
-tmatfn => catfile($outhmm, 'transition_matrices'),
251
-nstream => 1, # Always one stream!
252
-ndensity => 1 # Always one density!
254
return $return_value;
257
#-------------------------------------------------------------------------
258
# Accumulate the means from the training data
259
#------------------------------------------------------------------------
261
$logfile = "$logdir/${ST::CFG_EXPTNAME}.initmean_cihmm.log";
263
open LOG,">$logfile";
265
my $output_buffer_dir = "$ST::CFG_BWACCUM_DIR/${ST::CFG_EXPTNAME}_buff_1";
266
mkdir ($output_buffer_dir,0777);
268
if ($return_value = RunTool('init_gau', $logfile, 0,
269
-ctlfn => $ST::CFG_LISTOFFILES,
270
-part => 1, -npart => 1,
271
-cepdir => $ST::CFG_FEATFILES_DIR,
272
-cepext => $ST::CFG_FEATFILE_EXTENSION,
273
-accumdir => $output_buffer_dir,
274
-agc => $ST::CFG_AGC,
275
-cmn => $ST::CFG_CMN,
276
-varnorm => $ST::CFG_VARNORM,
277
-feat => $ST::CFG_FEATURE,
278
-ceplen => $ST::CFG_VECTOR_LENGTH,
280
return $return_value;
283
#-------------------------------------------------------------------------
284
# Normalize the means
285
#------------------------------------------------------------------------
287
$logfile = "$logdir/${ST::CFG_EXPTNAME}.normmean_cihmm.log";
289
if ($return_value = RunTool('norm', $logfile, 0,
290
-accumdir => $output_buffer_dir,
291
-meanfn => catfile($outhmm, "globalmean"),
293
return $return_value;
296
#-------------------------------------------------------------------------
297
# Accumulate the variances from the training data
298
#------------------------------------------------------------------------
300
$logfile = "$logdir/${ST::CFG_EXPTNAME}.initvar_cihmm.log";
302
if ($return_value = RunTool('init_gau', $logfile, 0,
303
-meanfn => catfile($outhmm, "globalmean"),
304
-ctlfn => $ST::CFG_LISTOFFILES,
305
-part => 1, -npart => 1,
306
-cepdir => $ST::CFG_FEATFILES_DIR,
307
-cepext => $ST::CFG_FEATFILE_EXTENSION,
308
-accumdir => $output_buffer_dir,
309
-agc => $ST::CFG_AGC,
310
-cmn => $ST::CFG_CMN,
311
-varnorm => $ST::CFG_VARNORM,
312
-feat => $ST::CFG_FEATURE,
313
-ceplen => $ST::CFG_VECTOR_LENGTH,
314
-fullvar => $ST::CFG_FULLVAR,
316
return $return_value;
319
#-------------------------------------------------------------------------
320
# Normalize the variances
321
#------------------------------------------------------------------------
323
$logfile = "$logdir/${ST::CFG_EXPTNAME}.normvar_cihmm.log";
325
if ($return_value = RunTool('norm', $logfile, 0,
326
-accumdir => $output_buffer_dir,
327
-fullvar => $ST::CFG_FULLVAR,
328
-varfn => catfile($outhmm, "globalvar"),
330
return $return_value;
334
#-------------------------------------------------------------------------
335
# Create the copy operation file, simply a map between states
336
#------------------------------------------------------------------------
338
my $NUM_CI_STATES = $NUM_PHONES * $ST::CFG_STATESPERHMM;
339
if (open CPFILE, ">$ST::CFG_CP_OPERATION") {
340
for (my $CI_STATE = 0; $CI_STATE < $NUM_CI_STATES; $CI_STATE++) {
341
print CPFILE "$CI_STATE\t0\n";
345
warn "Can't open $ST::CFG_CP_OPERATION\n";
348
#-------------------------------------------------------------------------
349
# Copy the means to all other states
350
#------------------------------------------------------------------------
352
$logfile = "$logdir/${ST::CFG_EXPTNAME}.cpmean_cihmm.log";
354
if ($return_value = RunTool('cp_parm', $logfile, 0,
355
-cpopsfn => $ST::CFG_CP_OPERATION,
356
-igaufn => catfile($outhmm, 'globalmean'),
357
-ncbout => $NUM_CI_STATES,
358
-ogaufn => catfile($outhmm, 'means')
360
return $return_value;
363
#-------------------------------------------------------------------------
364
# Copy the variances to all other states
365
#------------------------------------------------------------------------
367
$logfile = "$logdir/${ST::CFG_EXPTNAME}.cpvar_cihmm.log";
370
if ($ST::CFG_FULLVAR eq 'yes') {
371
@varcpy = (-ifullgaufn => catfile($outhmm, 'globalvar'),
372
-ofullgaufn => catfile($outhmm, 'variances'));
375
@varcpy = (-igaufn => catfile($outhmm, 'globalvar'),
376
-ogaufn => catfile($outhmm, 'variances'));
378
if ($return_value = RunTool('cp_parm', $logfile, 0,
379
-cpopsfn => $ST::CFG_CP_OPERATION,
380
-ncbout => $NUM_CI_STATES,
383
return $return_value;
386
unlink $ST::CFG_CP_OPERATION;
388
return $return_value;