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

« back to all changes in this revision

Viewing changes to scripts/02.mllt_train/norm_and_launchbw.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
## Modified: Rita Singh, 27 Nov 2000
 
38
## Author: Ricky Houghton 
 
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
#*******************************************************************
 
53
$| = 1; # Turn on autoflushing
 
54
 
 
55
die "USAGE: $0 <iter> <n_parts>" if (@ARGV != 2);
 
56
 
 
57
my ($iter, $n_parts) = @ARGV;
 
58
 
 
59
my $processname="02.mllt_train";
 
60
 
 
61
my $logdir ="$ST::CFG_LOG_DIR/$processname";
 
62
mkdir ($logdir,0777);
 
63
my $log = "$logdir/${ST::CFG_EXPTNAME}.$iter.norm.log";
 
64
 
 
65
# Check the number and list of parts done. Compute avg likelihood per frame
 
66
my $num_done = 0; my $tot_lkhd = 0; my $tot_frms = 0;
 
67
my @done;
 
68
for (my $i=1;$i<=$n_parts;$i++){
 
69
    $done[$i] = 0;
 
70
    my $input_log = "${logdir}/${ST::CFG_EXPTNAME}.${iter}-${i}.bw.log";
 
71
    next if (! -s $input_log);
 
72
    open LOG,$input_log;
 
73
    while (<LOG>) {
 
74
        if (/.*(Counts saved to).*/) {
 
75
            $num_done++;
 
76
            $done[$i] = 1;
 
77
        }
 
78
        if (/.*(overall>).*/){
 
79
            my (undef, undef, $nfrms, undef, undef, $lkhd) = split(/ /);
 
80
            $tot_lkhd = $tot_lkhd + $lkhd;
 
81
            $tot_frms = $tot_frms + $nfrms;
 
82
        }
 
83
    }
 
84
    close LOG;
 
85
}
 
86
 
 
87
if ($num_done != $n_parts) {
 
88
    print OUTPUT "Only $num_done parts of $n_parts of Baum Welch were successfully completed\n";
 
89
    my $errmsg = "Parts ";
 
90
    for (my $i=1;$i<=$n_parts;$i++) {
 
91
        $errmsg .= "$i " if ($done[$i] == 0);
 
92
    }
 
93
    $errmsg .= "failed to run!\n";
 
94
    open OUTPUT,">$log";
 
95
    print OUTPUT "Only $num_done parts of $n_parts of Baum Welch were successfully completed\n";
 
96
    print OUTPUT $errmsg;
 
97
    close OUTPUT;
 
98
    LogError("Only $num_done parts of $n_parts of Baum Welch were successfully completed");
 
99
    LogError($errmsg);
 
100
    exit (0);
 
101
}
 
102
 
 
103
if ($tot_frms == 0) {
 
104
    open OUTPUT,">$log";
 
105
    print OUTPUT "Baum welch ran successfully for only 0 frames! Aborting..\n";
 
106
    close OUTPUT;
 
107
    exit (0);
 
108
}
 
109
 
 
110
my $lkhd_per_frame = $tot_lkhd/$tot_frms;
 
111
 
 
112
my $previter = $iter - 1;
 
113
my $prev_norm = "${logdir}/${ST::CFG_EXPTNAME}.${previter}.norm.log";
 
114
if (! -s $prev_norm) {
 
115
    # Either iter == 1 or we are starting from an intermediate iter value
 
116
    RunScript('norm.pl', $iter);
 
117
    open OUTPUT, ">> $log";
 
118
    print OUTPUT "Current Overall Likelihood Per Frame = $lkhd_per_frame\n";
 
119
    close OUTPUT;
 
120
    Launch_BW($iter, $iter + 1);
 
121
    exit (0);
 
122
}
 
123
 
 
124
# Find the likelihood from the previous iteration
 
125
open LOG, $prev_norm; my $prevlkhd = -99999999;
 
126
while (<LOG>) {
 
127
   if (/.*(Current Overall Likelihood Per Frame).*/){
 
128
      (undef,undef,undef,undef,undef,undef,$prevlkhd) = split(/ /);
 
129
   }
 
130
}
 
131
close LOG;
 
132
 
 
133
if ($prevlkhd == -99999999) {
 
134
    # Some error with previous norm.log. Continue Baum Welch
 
135
    RunScript('norm.pl', $iter);
 
136
    open OUTPUT, ">> $log";
 
137
    print OUTPUT "Current Overall Likelihood Per Frame = $lkhd_per_frame\n";
 
138
    close OUTPUT;
 
139
    Launch_BW($iter, $iter + 1);
 
140
    exit (0);
 
141
}
 
142
 
 
143
my $convg_ratio;
 
144
if ($prevlkhd == 0) {
 
145
    $convg_ratio = 0;
 
146
    $convg_ratio = 1 if ($lkhd_per_frame > 0);
 
147
    $convg_ratio = -1 if ($lkhd_per_frame < 0);
 
148
}
 
149
else {
 
150
    $convg_ratio = $lkhd_per_frame - $prevlkhd;
 
151
}
 
152
RunScript('norm.pl', $iter);
 
153
 
 
154
open OUTPUT, ">> $log";
 
155
print OUTPUT "Current Overall Likelihood Per Frame = $lkhd_per_frame\n";
 
156
if (defined($convg_ratio)) {
 
157
    print OUTPUT "Convergence ratio = $convg_ratio\n";
 
158
}
 
159
 
 
160
if ($convg_ratio < 0) {
 
161
    print OUTPUT "*WARNING*: NEGATIVE CONVERGENCE RATIO! CHECK YOUR DATA AND TRASNCRIPTS\n";
 
162
    LogWarning("*WARNING*: NEGATIVE CONVERGENCE RATIO AT ITER ${iter}! CHECK BW AND NORM LOGFILES");
 
163
}
 
164
 
 
165
if ($convg_ratio > $ST::CFG_CONVERGENCE_RATIO && $iter >= $ST::CFG_MAX_ITERATIONS) {
 
166
    open OUTPUT, ">> $log";
 
167
    print OUTPUT "Maximum desired iterations $ST::CFG_MAX_ITERATIONS performed. Terminating training iteration\n";
 
168
    my $date = localtime;
 
169
    print OUTPUT "$date\n";
 
170
    Log("Maximum desired iterations $ST::CFG_MAX_ITERATIONS performed. Terminating CI training for MLLT",
 
171
        'result');
 
172
    close OUTPUT;
 
173
    # Do the last iteration
 
174
    Launch_BW($iter, 'N');
 
175
    exit (0);
 
176
}
 
177
 
 
178
if ($convg_ratio > $ST::CFG_CONVERGENCE_RATIO or $iter < $ST::CFG_MIN_ITERATIONS) {
 
179
    Launch_BW($iter, $iter + 1);
 
180
    exit (0);
 
181
}
 
182
else {
 
183
    Log("Current Overall Likelihood Per Frame = $lkhd_per_frame", 'result');
 
184
    open OUTPUT, ">> $log";
 
185
    print OUTPUT "Likelihoods have converged! CI training for MLLT completed\!\n";
 
186
    my $date = localtime;
 
187
    print OUTPUT "$date\n";
 
188
    close OUTPUT;
 
189
    # Do the last iteration
 
190
    Launch_BW($iter, 'N');
 
191
    exit (0);
 
192
}
 
193
 
 
194
sub Launch_BW {
 
195
    my ($iter, $newiter) = @_;
 
196
    Log("Current Overall Likelihood Per Frame = $lkhd_per_frame", 'result');
 
197
    Log("Convergence Ratio = $convg_ratio", 'result') if defined $convg_ratio;
 
198
    RunScript('slave_mllt.pl', $newiter, $n_parts);
 
199
}