~ubuntu-branches/ubuntu/saucy/sphinxtrain/saucy

« back to all changes in this revision

Viewing changes to scripts/01.lda_train/slave_lda.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
# 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");
 
56
    exit 0;
 
57
}
 
58
 
 
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");
 
62
    exit 0;
 
63
}
 
64
 
 
65
my $iter = 1;
 
66
if (@ARGV) {
 
67
    $iter = shift;
 
68
}
 
69
 
 
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
 
73
if (@ARGV) {
 
74
   $n_parts = shift;
 
75
}
 
76
 
 
77
$| = 1; # Turn on autoflushing
 
78
my $logdir = "$ST::CFG_LOG_DIR/01.lda_train";
 
79
 
 
80
if ($iter == 1) {
 
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);
 
88
    }
 
89
    LogProgress("logs...");
 
90
    rmtree($logdir, 0, 1);
 
91
    mkdir ($logdir,0777);
 
92
    LogProgress("qmanager...\n");
 
93
    rmtree ($ST::CFG_QMGR_DIR, 0, 1);
 
94
    mkdir ($ST::CFG_QMGR_DIR,0777);
 
95
    LogStatus('completed');
 
96
 
 
97
    # For the first iteration Flat initialize models.
 
98
    my $return_value = FlatInitialize();
 
99
    exit ($return_value) if ($return_value);
 
100
 
 
101
    Log("Phase 3: Forward-Backward");
 
102
}
 
103
 
 
104
if ($iter eq 'N') {
 
105
    Log("Phase 4: LDA transform estimation");
 
106
    my @deps;
 
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']);
 
109
    }
 
110
    LaunchScript("lda", "lda_train.pl", \@deps);
 
111
}
 
112
else {
 
113
    my @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']);
 
116
    }
 
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");
 
120
    if ($iter == 1) {
 
121
        # This is kind of a lousy way to do it, but oh well...
 
122
        local $SIG{CHLD} = sub { wait; };
 
123
        my $interval = 5;
 
124
        while (1) {
 
125
            # Look for an error
 
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");
 
132
                        exit 1;
 
133
                    }
 
134
                }
 
135
            }
 
136
            if (open LOG, "<$lda_log") {
 
137
                while (<LOG>) {
 
138
                    if (/failed/) {
 
139
                        LogError("LDA Training failed");
 
140
                        exit 1;
 
141
                    }
 
142
                    elsif (/complete/) {
 
143
                        Log("LDA Training completed", 'result');
 
144
                        exit 0;
 
145
                    }
 
146
                }
 
147
            }
 
148
            sleep $interval;
 
149
        }
 
150
    }
 
151
}
 
152
 
 
153
sub CopyInitialize {
 
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, $_))
 
161
            or return -1;
 
162
    }
 
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);
 
166
    return 0;
 
167
}
 
168
 
 
169
sub FlatInitialize
 
170
{
 
171
    Log("Phase 2: Flat initialize\n");
 
172
 
 
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
    #**************************************************************************
 
180
 
 
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);
 
187
    my $cmd ="";
 
188
 
 
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
 
192
    # following program.
 
193
    #-------------------------------------------------------------------------
 
194
 
 
195
    #$rawphonefile obtained from variables.def
 
196
    my $phonefile           = "$modarchdir/$ST::CFG_EXPTNAME.phonelist";
 
197
    my $ci_mdeffile         = "$modarchdir/$ST::CFG_EXPTNAME.ci.mdef";
 
198
 
 
199
    open PHONELIST, "<".$ST::CFG_RAWPHONEFILE;
 
200
    open PHONEFILE, ">".$phonefile;
 
201
    my $NUM_PHONES = 0;
 
202
    my @phonelist;
 
203
    while (defined(my $line = <PHONELIST>)) {
 
204
      $line = Trim($line);
 
205
      next if $line =~ m/^\s*$/;
 
206
      $line =~ s/$/ - - - /;
 
207
      push @phonelist, $line;
 
208
      $NUM_PHONES++;
 
209
    }
 
210
 
 
211
    # Make sure the CI phones are sorted, as PocketSphinx requires them to be
 
212
    foreach (sort @phonelist) {
 
213
        print PHONEFILE $_, "\n";
 
214
    }
 
215
#    system "sed 's+\$+ - - - +g' $ST::CFG_RAWPHONEFILE > $phonefile";
 
216
 
 
217
 
 
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
    #-------------------------------------------------------------------------
 
224
 
 
225
    #$statesperhmm obtained from variables.def
 
226
    my $topologyfile             = "$modarchdir/$ST::CFG_EXPTNAME.topology";
 
227
    RunScript("../prepare/maketopology.pl", $topologyfile);
 
228
 
 
229
    my $return_value;
 
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;
 
235
    }
 
236
 
 
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);
 
242
 
 
243
    my $FLAT = "$ST::CFG_BIN_DIR/mk_flat";
 
244
 
 
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!
 
253
                               )) {
 
254
      return $return_value;
 
255
    }
 
256
 
 
257
    #-------------------------------------------------------------------------
 
258
    # Accumulate the means from the training data
 
259
    #------------------------------------------------------------------------
 
260
 
 
261
    $logfile = "$logdir/${ST::CFG_EXPTNAME}.initmean_cihmm.log";
 
262
 
 
263
    open LOG,">$logfile";
 
264
 
 
265
    my $output_buffer_dir = "$ST::CFG_BWACCUM_DIR/${ST::CFG_EXPTNAME}_buff_1";
 
266
    mkdir ($output_buffer_dir,0777);
 
267
 
 
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,
 
279
                               )) {
 
280
      return $return_value;
 
281
    }
 
282
 
 
283
    #-------------------------------------------------------------------------
 
284
    # Normalize the means
 
285
    #------------------------------------------------------------------------
 
286
 
 
287
    $logfile = "$logdir/${ST::CFG_EXPTNAME}.normmean_cihmm.log";
 
288
 
 
289
    if ($return_value = RunTool('norm', $logfile, 0,
 
290
                                -accumdir => $output_buffer_dir,
 
291
                                -meanfn => catfile($outhmm, "globalmean"),
 
292
                               )) {
 
293
      return $return_value;
 
294
    }
 
295
 
 
296
    #-------------------------------------------------------------------------
 
297
    # Accumulate the variances from the training data
 
298
    #------------------------------------------------------------------------
 
299
 
 
300
    $logfile = "$logdir/${ST::CFG_EXPTNAME}.initvar_cihmm.log";
 
301
 
 
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,
 
315
                               )) {
 
316
      return $return_value;
 
317
    }
 
318
 
 
319
    #-------------------------------------------------------------------------
 
320
    # Normalize the variances
 
321
    #------------------------------------------------------------------------
 
322
 
 
323
    $logfile = "$logdir/${ST::CFG_EXPTNAME}.normvar_cihmm.log";
 
324
 
 
325
    if ($return_value = RunTool('norm', $logfile, 0,
 
326
                                -accumdir => $output_buffer_dir,
 
327
                                -fullvar => $ST::CFG_FULLVAR,
 
328
                                -varfn => catfile($outhmm, "globalvar"),
 
329
                               )) {
 
330
      return $return_value;
 
331
    }
 
332
 
 
333
 
 
334
    #-------------------------------------------------------------------------
 
335
    # Create the copy operation file, simply a map between states
 
336
    #------------------------------------------------------------------------
 
337
 
 
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";
 
342
      }
 
343
      close(CPFILE);
 
344
    } else {
 
345
      warn "Can't open $ST::CFG_CP_OPERATION\n";
 
346
    }
 
347
 
 
348
    #-------------------------------------------------------------------------
 
349
    # Copy the means to all other states
 
350
    #------------------------------------------------------------------------
 
351
 
 
352
    $logfile = "$logdir/${ST::CFG_EXPTNAME}.cpmean_cihmm.log";
 
353
 
 
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')
 
359
                               )) {
 
360
      return $return_value;
 
361
    }
 
362
 
 
363
    #-------------------------------------------------------------------------
 
364
    # Copy the variances to all other states
 
365
    #------------------------------------------------------------------------
 
366
 
 
367
    $logfile = "$logdir/${ST::CFG_EXPTNAME}.cpvar_cihmm.log";
 
368
 
 
369
    my @varcpy;
 
370
    if ($ST::CFG_FULLVAR eq 'yes') {
 
371
        @varcpy = (-ifullgaufn => catfile($outhmm, 'globalvar'),
 
372
                   -ofullgaufn => catfile($outhmm, 'variances'));
 
373
    }
 
374
    else {
 
375
        @varcpy = (-igaufn => catfile($outhmm, 'globalvar'),
 
376
                   -ogaufn => catfile($outhmm, 'variances'));
 
377
    }
 
378
    if ($return_value = RunTool('cp_parm', $logfile, 0,
 
379
                                -cpopsfn => $ST::CFG_CP_OPERATION,
 
380
                                -ncbout => $NUM_CI_STATES,
 
381
                                @varcpy
 
382
                               )) {
 
383
        return $return_value;
 
384
    }
 
385
 
 
386
    unlink $ST::CFG_CP_OPERATION;
 
387
 
 
388
    return $return_value;
 
389
}