~ubuntu-branches/ubuntu/raring/sphinxtrain/raring-proposed

« back to all changes in this revision

Viewing changes to scripts/20.ci_hmm/slave_convg.pl

  • Committer: Package Import Robot
  • Author(s): Samuel Thibault
  • Date: 2013-01-02 04:10:21 UTC
  • Revision ID: package-import@ubuntu.com-20130102041021-ynsizmz33fx02hea
Tags: upstream-1.0.8
ImportĀ upstreamĀ versionĀ 1.0.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
## ====================================================================
 
3
##
 
4
## Copyright (c) 1996-2000 Carnegie Mellon University.  All rights
 
5
## reserved.
 
6
##
 
7
## Redistribution and use in source and binary forms, with or without
 
8
## modification, are permitted provided that the following conditions
 
9
## are met:
 
10
##
 
11
## 1. Redistributions of source code must retain the above copyright
 
12
##    notice, this list of conditions and the following disclaimer.
 
13
##
 
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
 
17
##    distribution.
 
18
##
 
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.
 
22
##
 
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.
 
34
##
 
35
## ====================================================================
 
36
##
 
37
## Author: Ricky Houghton
 
38
## Author: David Huggins-Daines
 
39
##
 
40
 
 
41
use strict;
 
42
use File::Copy;
 
43
use File::Basename;
 
44
use File::Spec::Functions;
 
45
use File::Path;
 
46
 
 
47
use lib catdir(dirname($0), updir(), 'lib');
 
48
use SphinxTrain::Config;
 
49
use SphinxTrain::Util;
 
50
 
 
51
#***************************************************************************
 
52
# This script launches all the ci - continuous training jobs in the proper
 
53
# order. First it cleans up the directories, then launches the
 
54
# flat initialization, and the baum-welch and norm jobs for the required
 
55
# number of iterations. Within each iteration it launches as many baumwelch
 
56
# jobs as the number of parts we wish to split the training into.
 
57
#***************************************************************************
 
58
 
 
59
my ($iter, $n_parts, $n_gau) = @ARGV;
 
60
$iter = 1 unless defined $iter;
 
61
$n_parts = (defined($ST::CFG_NPART) ? $ST::CFG_NPART : 1) unless defined $n_parts;
 
62
$n_gau = $ST::CFG_INITIAL_NUM_DENSITIES unless defined $n_gau;
 
63
 
 
64
my $modeldir  = "$ST::CFG_BASE_DIR/model_parameters";
 
65
mkdir ($modeldir,0777);
 
66
 
 
67
$| = 1; # Turn on autoflushing
 
68
my $logdir = "$ST::CFG_LOG_DIR/20.ci_hmm";
 
69
my $return_value = 0;
 
70
 
 
71
use vars qw($MLLT_FILE);
 
72
$MLLT_FILE = catfile($ST::CFG_MODEL_DIR, "${ST::CFG_EXPTNAME}.mllt");
 
73
 
 
74
# We have to clean up and run flat initialize if it is the first iteration
 
75
if ($iter == 1 and $n_gau == $ST::CFG_INITIAL_NUM_DENSITIES) {
 
76
    Log("MODULE: 20 Training Context Independent models\n");
 
77
    Log("Phase 1: Cleaning up directories:");
 
78
    # Don't do this on a queue, because of NFS bugs
 
79
    unless ($ST::CFG_QUEUE_TYPE eq 'Queue::PBS') {
 
80
        LogProgress("\taccumulator...");
 
81
        rmtree ($ST::CFG_BWACCUM_DIR, 0, 1);
 
82
        mkdir ($ST::CFG_BWACCUM_DIR,0777);
 
83
    }
 
84
    LogProgress("logs...");
 
85
    rmtree($logdir, 0, 1);
 
86
    mkdir ($logdir,0777);
 
87
    LogProgress("qmanager...");
 
88
    rmtree ($ST::CFG_QMGR_DIR, 0, 1);
 
89
    mkdir ($ST::CFG_QMGR_DIR,0777);
 
90
    LogProgress("models...\n");
 
91
    rmtree("$modeldir/${ST::CFG_EXPTNAME}.ci_$ST::CFG_DIRLABEL", 0, 1);
 
92
    LogStatus('completed');
 
93
 
 
94
    # If we previously force aligned with single-Gaussian models, use
 
95
    # them for initialization to save some time.
 
96
    if (($ST::CFG_FORCEDALIGN eq 'yes' or $ST::CFG_VTLN eq 'yes')
 
97
        and $ST::CFG_FALIGN_CI_MGAU ne 'yes'
 
98
        and -e catfile($ST::CFG_FORCE_ALIGN_MODELDIR, 'means')) {
 
99
        $return_value = CopyInitialize();
 
100
        exit ($return_value) if ($return_value);
 
101
    }
 
102
    else {
 
103
        # For the first iteration Flat initialize models.
 
104
        $return_value = FlatInitialize();
 
105
        exit ($return_value) if ($return_value);
 
106
    }
 
107
    Log("Phase 3: Forward-Backward");
 
108
}
 
109
 
 
110
if (defined($ST::CFG_PHSEG_DIR) and ! -d $ST::CFG_PHSEG_DIR) {
 
111
    # Build phone segmentation dirs
 
112
    open INPUT,"${ST::CFG_LISTOFFILES}" or die "Failed to open $ST::CFG_LISTOFFILES: $!";
 
113
    my %dirs;
 
114
    while (<INPUT>) {
 
115
        chomp;
 
116
        my @fields = split;
 
117
        my $uttid = pop @fields;
 
118
        my $basedir = dirname($uttid);
 
119
        next if $basedir eq ".";
 
120
        unless ($dirs{$basedir}) {
 
121
            $dirs{$basedir}++;
 
122
            mkpath(catdir($ST::CFG_PHSEG_DIR, $basedir), 0, 0777);
 
123
        }
 
124
    }
 
125
    close INPUT;
 
126
}
 
127
 
 
128
 
 
129
# Call baum_welch with iter part and n_parts,
 
130
# once done call norm_and_lauchbw.pl
 
131
my @deps;
 
132
for (my $i=1; $i<=$n_parts; $i++)
 
133
{
 
134
    push @deps, LaunchScript("bw.$iter.$i", ['baum_welch.pl', $iter, $i, $n_parts, $n_gau])
 
135
}
 
136
LaunchScript("norm.$iter", ['norm_and_launchbw.pl', $iter, $n_parts, $n_gau], \@deps);
 
137
# For the first iteration (i.e. the one that was called from the
 
138
# command line or a parent script), wait until completion or error
 
139
if ($iter == 1 && $n_gau == $ST::CFG_INITIAL_NUM_DENSITIES) {
 
140
    if ($ST::CFG_CI_MGAU eq 'yes') {
 
141
        $return_value = TiedWaitForConvergence($logdir);
 
142
    }
 
143
    else {
 
144
        $return_value = WaitForConvergence($logdir);
 
145
    }
 
146
}
 
147
exit $return_value;
 
148
 
 
149
sub CopyInitialize {
 
150
    Log("Phase 2: Copy initialize from falign model\n");
 
151
    my $hmmdir = catdir($ST::CFG_BASE_DIR, "model_parameters");
 
152
    my $outhmm = catdir($hmmdir, "${ST::CFG_EXPTNAME}.ci_${ST::CFG_DIRLABEL}_flatinitial");
 
153
    my $modarchdir = catdir($ST::CFG_BASE_DIR, "model_architecture");
 
154
    mkdir ($outhmm,0777);
 
155
    foreach (qw(means variances mixture_weights transition_matrices)) {
 
156
        copy(catfile($ST::CFG_FORCE_ALIGN_MODELDIR, $_), catfile($outhmm, $_))
 
157
            or return -1;
 
158
    }
 
159
    my $ci_mdeffile = catfile($modarchdir, "$ST::CFG_EXPTNAME.falign_ci.mdef");
 
160
    my $out_mdeffile = catfile($modarchdir, "$ST::CFG_EXPTNAME.ci.mdef");
 
161
    copy($ci_mdeffile, $out_mdeffile);
 
162
    return 0;
 
163
}
 
164
 
 
165
sub FlatInitialize
 
166
{
 
167
    Log("Phase 2: Flat initialize\n");
 
168
 
 
169
    #**************************************************************************
 
170
    # this script given an mdef file and a  codebook (means/vars in S3 format)
 
171
    # produces flat mixture weights in a semicontinuos setting. From the models
 
172
    # produced by this script we can restart normal baum-welch training
 
173
    # Flat init might not be the best choice, specially if we have access to
 
174
    # segmentation files for the whole training database.
 
175
    #**************************************************************************
 
176
 
 
177
    my $logdir              = "$ST::CFG_LOG_DIR/20.ci_hmm";
 
178
    my $modarchdir          = "$ST::CFG_BASE_DIR/model_architecture";
 
179
    my $hmmdir              = "$ST::CFG_BASE_DIR/model_parameters";
 
180
    mkdir ($logdir,0777);
 
181
    mkdir ($modarchdir,0777);
 
182
    mkdir ($hmmdir,0777);
 
183
    my $cmd ="";
 
184
 
 
185
    #-------------------------------------------------------------------------
 
186
    # Take the phone list. Put three hyphens after every phone. That is the
 
187
    # required format. Then make a ci model definition file from it using the
 
188
    # following program.
 
189
    #-------------------------------------------------------------------------
 
190
 
 
191
    #$rawphonefile obtained from variables.def
 
192
    my $phonefile           = "$modarchdir/$ST::CFG_EXPTNAME.phonelist";
 
193
    my $ci_mdeffile         = "$modarchdir/$ST::CFG_EXPTNAME.ci.mdef";
 
194
 
 
195
    open PHONELIST, "<".$ST::CFG_RAWPHONEFILE;
 
196
    open PHONEFILE, ">".$phonefile;
 
197
    my $NUM_PHONES = 0;
 
198
    my @phonelist;
 
199
    while (defined(my $line = <PHONELIST>)) {
 
200
      $line = Trim($line);
 
201
      next if $line =~ m/^\s*$/;
 
202
      $line =~ s/$/ - - - /;
 
203
      push @phonelist, $line;
 
204
      $NUM_PHONES++;
 
205
    }
 
206
 
 
207
    # Make sure the CI phones are sorted, as PocketSphinx requires them to be
 
208
    foreach (sort @phonelist) {
 
209
        print PHONEFILE $_, "\n";
 
210
    }
 
211
#    system "sed 's+\$+ - - - +g' $ST::CFG_RAWPHONEFILE > $phonefile";
 
212
 
 
213
 
 
214
    my $logfile = "$logdir/${ST::CFG_EXPTNAME}.make_ci_mdef_fromphonelist.log";
 
215
    #-------------------------------------------------------------------------
 
216
    # Decide on what topology to use for the hmms: 3 state, 5 state, blah state
 
217
    # or what, give it to the variable "statesperhmm" and use it to create
 
218
    # the topology matrix in the topology file
 
219
    #-------------------------------------------------------------------------
 
220
 
 
221
    #$statesperhmm obtained from variables.def
 
222
    my $topologyfile             = "$modarchdir/$ST::CFG_EXPTNAME.topology";
 
223
    RunScript("../prepare/maketopology.pl", $topologyfile);
 
224
 
 
225
    if ($return_value = RunTool('mk_mdef_gen', $logfile, 0,
 
226
                                -phnlstfn => $phonefile,
 
227
                                -ocimdef => $ci_mdeffile,
 
228
                                -n_state_pm => $ST::CFG_STATESPERHMM)) {
 
229
      return $return_value;
 
230
    }
 
231
 
 
232
    #-------------------------------------------------------------------------
 
233
    # make the flat models using the above topology file and the mdef file
 
234
    #------------------------------------------------------------------------
 
235
    my $outhmm               = "$hmmdir/${ST::CFG_EXPTNAME}.ci_${ST::CFG_DIRLABEL}_flatinitial";
 
236
    mkdir ($outhmm,0777);
 
237
 
 
238
    my $FLAT = "$ST::CFG_BIN_DIR/mk_flat";
 
239
 
 
240
    $logfile = "$logdir/${ST::CFG_EXPTNAME}.makeflat_cihmm.log";
 
241
    if ($return_value = RunTool('mk_flat', $logfile, 0,
 
242
                                -moddeffn => $ci_mdeffile,
 
243
                                -topo => $topologyfile,
 
244
                                -mixwfn => catfile($outhmm, 'mixture_weights'),
 
245
                                -tmatfn => catfile($outhmm, 'transition_matrices'),
 
246
                                -nstream => $ST::CFG_NUM_STREAMS,
 
247
                                -ndensity => $ST::CFG_INITIAL_NUM_DENSITIES,
 
248
                               )) {
 
249
      return $return_value;
 
250
    }
 
251
 
 
252
    # For semi-continuous, PTM, or generalized tied mixtures,
 
253
    # duplicate the codebook in-place to produce the initial model
 
254
    # (this does nothing for SC but is necessary for the others)
 
255
    if ($ST::CFG_HMM_TYPE ne ".cont.") {
 
256
        $logfile = "$logdir/${ST::CFG_EXPTNAME}.duplicate_codebook.log";
 
257
        if ($return_value = RunTool('init_mixw', $logfile, 0,
 
258
                                    # Flat K-means init always gives us one codebook
 
259
                                    -src_ts2cbfn => '.semi.',
 
260
                                    # There might be multiple codebooks here though
 
261
                                    -dest_ts2cbfn => $ST::CFG_HMM_TYPE,
 
262
                                    -fullvar => $ST::CFG_FULLVAR,
 
263
                                    -src_moddeffn => $ci_mdeffile,
 
264
                                    -dest_moddeffn => $ci_mdeffile,
 
265
                                    -src_mixwfn => catfile($outhmm, 'mixture_weights'),
 
266
                                    -dest_mixwfn => catfile($outhmm, 'mixture_weights'),
 
267
                                    -src_tmatfn => catfile($outhmm, 'transition_matrices'),
 
268
                                    -dest_tmatfn => catfile($outhmm, 'transition_matrices'),
 
269
                                    -src_meanfn=> catfile($outhmm, 'means'),
 
270
                                    -dest_meanfn => catfile($outhmm, 'means'),
 
271
                                    -src_varfn=> catfile($outhmm, 'variances'),
 
272
                                    -dest_varfn => catfile($outhmm, 'variances'),
 
273
            )) {
 
274
            return $return_value;
 
275
        }
 
276
      return (0);
 
277
    }
 
278
 
 
279
    #-------------------------------------------------------------------------
 
280
    # Accumulate the means from the training data
 
281
    #------------------------------------------------------------------------
 
282
 
 
283
    $logfile = "$logdir/${ST::CFG_EXPTNAME}.initmean_cihmm.log";
 
284
 
 
285
    open LOG,">$logfile";
 
286
 
 
287
    my $output_buffer_dir = "$ST::CFG_BWACCUM_DIR/${ST::CFG_EXPTNAME}_buff_1";
 
288
    mkdir ($output_buffer_dir,0777);
 
289
 
 
290
    # if there is an MLLT transformation, use it
 
291
    my @feat_args;
 
292
    if (defined($ST::CFG_SVSPEC)) {
 
293
        push(@feat_args, -svspec =>$ST::CFG_SVSPEC);
 
294
    }
 
295
    if (-r $MLLT_FILE) {
 
296
        push(@feat_args,
 
297
             -lda => $MLLT_FILE,
 
298
             -ldadim => $ST::CFG_LDA_DIMENSION);
 
299
    }
 
300
    if ($return_value = RunTool('init_gau', $logfile, 0,
 
301
                                -ctlfn => $ST::CFG_LISTOFFILES,
 
302
                                -part => 1, -npart => 1,
 
303
                                -cepdir => $ST::CFG_FEATFILES_DIR,
 
304
                                -cepext => $ST::CFG_FEATFILE_EXTENSION,
 
305
                                -accumdir => $output_buffer_dir,
 
306
                                -agc => $ST::CFG_AGC,
 
307
                                -cmn => $ST::CFG_CMN,
 
308
                                -varnorm => $ST::CFG_VARNORM,
 
309
                                -feat => $ST::CFG_FEATURE,
 
310
                                -ceplen => $ST::CFG_VECTOR_LENGTH,
 
311
                                @feat_args
 
312
                               )) {
 
313
      return $return_value;
 
314
    }
 
315
 
 
316
    #-------------------------------------------------------------------------
 
317
    # Normalize the means
 
318
    #------------------------------------------------------------------------
 
319
 
 
320
    $logfile = "$logdir/${ST::CFG_EXPTNAME}.normmean_cihmm.log";
 
321
 
 
322
    if ($return_value = RunTool('norm', $logfile, 0,
 
323
                                -accumdir => $output_buffer_dir,
 
324
                                -meanfn => catfile($outhmm, "globalmean"),
 
325
                               )) {
 
326
      return $return_value;
 
327
    }
 
328
 
 
329
    #-------------------------------------------------------------------------
 
330
    # Accumulate the variances from the training data
 
331
    #------------------------------------------------------------------------
 
332
 
 
333
    $logfile = "$logdir/${ST::CFG_EXPTNAME}.initvar_cihmm.log";
 
334
 
 
335
    if ($return_value = RunTool('init_gau', $logfile, 0,
 
336
                                -meanfn => catfile($outhmm, "globalmean"),
 
337
                                -ctlfn => $ST::CFG_LISTOFFILES,
 
338
                                -part => 1,  -npart => 1,
 
339
                                -cepdir => $ST::CFG_FEATFILES_DIR,
 
340
                                -cepext => $ST::CFG_FEATFILE_EXTENSION,
 
341
                                -accumdir => $output_buffer_dir,
 
342
                                -agc => $ST::CFG_AGC,
 
343
                                -cmn => $ST::CFG_CMN,
 
344
                                -varnorm => $ST::CFG_VARNORM,
 
345
                                -feat => $ST::CFG_FEATURE,
 
346
                                -ceplen => $ST::CFG_VECTOR_LENGTH,
 
347
                                -fullvar => $ST::CFG_FULLVAR,
 
348
                                @feat_args
 
349
                               )) {
 
350
      return $return_value;
 
351
    }
 
352
 
 
353
    #-------------------------------------------------------------------------
 
354
    # Normalize the variances
 
355
    #------------------------------------------------------------------------
 
356
 
 
357
    $logfile = "$logdir/${ST::CFG_EXPTNAME}.normvar_cihmm.log";
 
358
 
 
359
    if ($return_value = RunTool('norm', $logfile, 0,
 
360
                                -accumdir => $output_buffer_dir,
 
361
                                -fullvar => $ST::CFG_FULLVAR,
 
362
                                -varfn => catfile($outhmm, "globalvar"),
 
363
                               )) {
 
364
      return $return_value;
 
365
    }
 
366
 
 
367
 
 
368
    #-------------------------------------------------------------------------
 
369
    # Create the copy operation file, simply a map between states
 
370
    #------------------------------------------------------------------------
 
371
 
 
372
    my $NUM_CI_STATES = $NUM_PHONES * $ST::CFG_STATESPERHMM;
 
373
    if (open CPFILE, ">$ST::CFG_CP_OPERATION") {
 
374
      for (my $CI_STATE = 0; $CI_STATE < $NUM_CI_STATES; $CI_STATE++) {
 
375
        print CPFILE "$CI_STATE\t0\n";
 
376
      }
 
377
      close(CPFILE);
 
378
    } else {
 
379
      warn "Can't open $ST::CFG_CP_OPERATION\n";
 
380
    }
 
381
 
 
382
    #-------------------------------------------------------------------------
 
383
    # Copy the means to all other states
 
384
    #------------------------------------------------------------------------
 
385
 
 
386
    $logfile = "$logdir/${ST::CFG_EXPTNAME}.cpmean_cihmm.log";
 
387
 
 
388
    if ($return_value = RunTool('cp_parm', $logfile, 0,
 
389
                                -cpopsfn => $ST::CFG_CP_OPERATION,
 
390
                                -igaufn => catfile($outhmm, 'globalmean'),
 
391
                                -ncbout => $NUM_CI_STATES,
 
392
                                -ogaufn => catfile($outhmm, 'means')
 
393
                               )) {
 
394
      return $return_value;
 
395
    }
 
396
 
 
397
    #-------------------------------------------------------------------------
 
398
    # Copy the variances to all other states
 
399
    #------------------------------------------------------------------------
 
400
 
 
401
    $logfile = "$logdir/${ST::CFG_EXPTNAME}.cpvar_cihmm.log";
 
402
 
 
403
    my @varcpy;
 
404
    if ($ST::CFG_FULLVAR eq 'yes') {
 
405
        @varcpy = (-ifullgaufn => catfile($outhmm, 'globalvar'),
 
406
                   -ofullgaufn => catfile($outhmm, 'variances'));
 
407
    }
 
408
    else {
 
409
        @varcpy = (-igaufn => catfile($outhmm, 'globalvar'),
 
410
                   -ogaufn => catfile($outhmm, 'variances'));
 
411
    }
 
412
    if ($return_value = RunTool('cp_parm', $logfile, 0,
 
413
                                -cpopsfn => $ST::CFG_CP_OPERATION,
 
414
                                -ncbout => $NUM_CI_STATES,
 
415
                                @varcpy
 
416
                               )) {
 
417
        return $return_value;
 
418
    }
 
419
 
 
420
    unlink $ST::CFG_CP_OPERATION;
 
421
 
 
422
    return $return_value;
 
423
}