Blame view

Scripts/steps/tandem/.svn/text-base/decode_sgmm.sh.svn-base 12.9 KB
ec85f8892   bigot benjamin   first commit
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
  #!/bin/bash
  
  # Copyright 2012  Johns Hopkins University (Author: Daniel Povey).  Apache 2.0.
  #                 Korbinian Riedhammer
  
  # This script does decoding with an SGMM system, with speaker vectors. 
  # If the SGMM system was
  # built on top of fMLLR transforms from a conventional system, you should
  # provide the --transform-dir option.
  
  # Begin configuration section.
  stage=1
  alignment_model=
  transform_dir=    # dir to find fMLLR transforms.
  nj=4 # number of decoding jobs.
  acwt=0.1  # Just a default value, used for adaptation and beam-pruning..
  cmd=run.pl
  beam=15.0
  gselect=15  # Number of Gaussian-selection indices for SGMMs.  [Note:
              # the first_pass_gselect variable is used for the 1st pass of
              # decoding and can be tighter.
  first_pass_gselect=3 # Use a smaller number of Gaussian-selection indices in 
              # the 1st pass of decoding (lattice generation).
  max_active=7000
  
  #WARNING: This option is renamed lat_beam (it was renamed to follow the naming 
  #         in the other scripts
  lattice_beam=8.0 # Beam we use in lattice generation.
  vecs_beam=4.0 # Beam we use to prune lattices while getting posteriors for 
      # speaker-vector computation.  Can be quite tight (actually we could
      # probably just do best-path.
  use_fmllr=false
  fmllr_iters=10
  fmllr_min_count=1000
  
  # End configuration section.
  
  echo "$0 $@"  # Print the command line for logging
  
  [ -f ./path.sh ] && . ./path.sh; # source the path.
  . parse_options.sh || exit 1;
  
  if [ $# -ne 4 ]; then
    echo "Usage: steps/tandem/decode_sgmm.sh [options] <graph-dir> <data1-dir> <data2-dir> <decode-dir>"
    echo " e.g.: steps/tandem/decode_sgmm.sh --transform-dir exp/tri3b/decode_dev93_tgpr \\"
    echo "      exp/sgmm3a/graph_tgpr {mfcc,bottleneck}/data/test_dev93 exp/sgmm3a/decode_dev93_tgpr"
    echo "main options (for others, see top of script file)"
    echo "  --transform-dir <decoding-dir>           # directory of previous decoding"
    echo "                                           # where we can find transforms for SAT systems."
    echo "  --alignment-model <ali-mdl>              # Model for the first-pass decoding."
    echo "  --config <config-file>                   # config containing options"
    echo "  --nj <nj>                                # number of parallel jobs"
    echo "  --cmd <cmd>                              # Command to run in parallel with"
    echo "  --beam <beam>                            # Decoding beam; default 13.0"
    exit 1;
  fi
  
  graphdir=$1
  data1=$2
  data2=$3
  dir=$4
  srcdir=`dirname $dir`; # Assume model directory one level up from decoding directory.
  
  for f in $graphdir/HCLG.fst $data1/feats.scp $data2/feats.scp $srcdir/final.mdl; do
    [ ! -f $f ] && echo "$0: no such file $f" && exit 1;
  done
  
  silphonelist=`cat $graphdir/phones/silence.csl` || exit 1
  gselect_opt="--gselect=ark:gunzip -c $dir/gselect.JOB.gz|"
  gselect_opt_1stpass="$gselect_opt copy-gselect --n=$first_pass_gselect ark:- ark:- |"
  
  mkdir -p $dir/log
  echo $nj > $dir/num_jobs
  
  sdata1=$data1/split$nj;
  sdata2=$data2/split$nj;
  [[ -d $sdata1 && $data1/feats.scp -ot $sdata1 ]] || split_data.sh $data1 $nj || exit 1;
  [[ -d $sdata2 && $data2/feats.scp -ot $sdata2 ]] || split_data.sh $data2 $nj || exit 1;
  
  
  ## Set up features.
  
  splice_opts=`cat $srcdir/splice_opts 2>/dev/null` # frame-splicing options.
  normft2=`cat $srcdir/normft2 2>/dev/null`
  
  if [ -f $srcdir/final.mat ]; then feat_type=lda; else feat_type=delta; fi
  
  case $feat_type in
    delta) 
    	echo "$0: feature type is $feat_type"
    	;;
    lda) 
    	echo "$0: feature type is $feat_type"
      cp $srcdir/{lda,final}.mat $dir/   
      ;;
    *) echo "$0: invalid feature type $feat_type" && exit 1;
  esac
  
  # set up feature stream 1;  this are usually spectral features, so we will add
  # deltas or splice them
  feats1="ark,s,cs:apply-cmvn --norm-vars=false --utt2spk=ark:$sdata1/JOB/utt2spk scp:$sdata1/JOB/cmvn.scp scp:$sdata1/JOB/feats.scp ark:- |"
  
  if [ "$feat_type" == "delta" ]; then
    feats1="$feats1 add-deltas ark:- ark:- |"
  elif [ "$feat_type" == "lda" ]; then
    feats1="$feats1 splice-feats $splice_opts ark:- ark:- | transform-feats $dir/lda.mat ark:- ark:- |"
  fi
  
  # set up feature stream 2;  this are usually bottleneck or posterior features, 
  # which may be normalized if desired
  feats2="scp:$sdata2/JOB/feats.scp"
  
  if [ "$normft2" == "true" ]; then
    echo "Using cmvn for feats2"
    feats2="ark,s,cs:apply-cmvn --norm-vars=false --utt2spk=ark:$sdata2/JOB/utt2spk scp:$sdata2/JOB/cmvn.scp $feats2 ark:- |"
  fi
  
  # assemble tandem features
  feats="ark,s,cs:paste-feats '$feats1' '$feats2' ark:- |"
  
  # add transformation, if applicable
  if [ "$feat_type" == "lda" ]; then
    feats="$feats transform-feats $dir/final.mat ark:- ark:- |"
  fi
  
  # splicing/normalization options
  cp $srcdir/{splice_opts,normft2,tandem} $dir 2>/dev/null
  
  if [ ! -z "$transform_dir" ]; then
    echo "$0: using transforms from $transform_dir"
    [ ! -f $transform_dir/trans.1 ] && echo "$0: no such file $transform_dir/trans.1" && exit 1;
    [ "$nj" -ne "`cat $transform_dir/num_jobs`" ] \
      && echo "$0: #jobs mismatch with transform-dir." && exit 1;
    feats="$feats transform-feats --utt2spk=ark:$sdata1/JOB/utt2spk ark,s,cs:$transform_dir/trans.JOB ark:- ark:- |"
  elif grep 'transform-feats --utt2spk' $srcdir/log/acc.0.1.log 2>/dev/null; then
    echo "$0: **WARNING**: you seem to be using an SGMM system trained with transforms,"
    echo "  but you are not providing the --transform-dir option in test time."
  fi
  ##
  
  
  ## Calculate FMLLR pre-transforms if needed. We are doing this here since this
  ## step is requried by models both with and without speaker vectors
  if $use_fmllr; then
    if [ ! -f $srcdir/final.fmllr_mdl ] || [ $srcdir/final.fmllr_mdl -ot $srcdir/final.mdl ]; then
      echo "$0: computing pre-transform for fMLLR computation."
      sgmm-comp-prexform $srcdir/final.mdl $srcdir/final.occs $srcdir/final.fmllr_mdl || exit 1;
    fi
  fi
  
  ## Save Gaussian-selection info to disk.
  # Note: we can use final.mdl regardless of whether there is an alignment model--
  # they use the same UBM.
  if [ $stage -le 1 ]; then
    $cmd JOB=1:$nj $dir/log/gselect.JOB.log \
      sgmm-gselect --full-gmm-nbest=$gselect $srcdir/final.mdl \
      "$feats" "ark:|gzip -c >$dir/gselect.JOB.gz" || exit 1;
  fi
  
  ## Work out name of alignment model. ##
  if [ -z "$alignment_model" ]; then
    if [ -f "$srcdir/final.alimdl" ]; then alignment_model=$srcdir/final.alimdl;
    else alignment_model=$srcdir/final.mdl; fi
  fi
  [ ! -f "$alignment_model" ] && echo "$0: no alignment model $alignment_model " && exit 1;
  
  # Generate state-level lattice which we can rescore.  This is done with the 
  # alignment model and no speaker-vectors.
  if [ $stage -le 2 ]; then
    $cmd JOB=1:$nj $dir/log/decode_pass1.JOB.log \
      sgmm-latgen-faster --max-active=$max_active --beam=$beam --lattice-beam=$lattice_beam \
      --acoustic-scale=$acwt --determinize-lattice=false --allow-partial=true \
      --word-symbol-table=$graphdir/words.txt "$gselect_opt_1stpass" $alignment_model \
      $graphdir/HCLG.fst "$feats" "ark:|gzip -c > $dir/pre_lat.JOB.gz" || exit 1;
  fi
  
  ## Check if the model has speaker vectors
  spkdim=`sgmm-info $srcdir/final.mdl | grep 'speaker vector' | awk '{print $NF}'`
  
  if [ $spkdim -gt 0 ]; then  ### For models with speaker vectors:
  
  # Estimate speaker vectors (1st pass).  Prune before determinizing
  # because determinization can take a while on un-pruned lattices.
  # Note: the sgmm-post-to-gpost stage is necessary because we have
  # a separate alignment-model and final model, otherwise we'd skip it 
  # and use sgmm-est-spkvecs.
    if [ $stage -le 3 ]; then
      $cmd JOB=1:$nj $dir/log/vecs_pass1.JOB.log \
        gunzip -c $dir/pre_lat.JOB.gz \| \
        lattice-prune --acoustic-scale=$acwt --beam=$vecs_beam ark:- ark:- \| \
        lattice-determinize-pruned --acoustic-scale=$acwt --beam=$vecs_beam ark:- ark:- \| \
        lattice-to-post --acoustic-scale=$acwt ark:- ark:- \| \
        weight-silence-post 0.0 $silphonelist $alignment_model ark:- ark:- \| \
        sgmm-post-to-gpost "$gselect_opt" $alignment_model "$feats" ark:- ark:- \| \
        sgmm-est-spkvecs-gpost --spk2utt=ark:$sdata1/JOB/spk2utt \
        $srcdir/final.mdl "$feats" ark,s,cs:- "ark:$dir/pre_vecs.JOB" || exit 1;
    fi
  
  # Estimate speaker vectors (2nd pass).  Since we already have spk vectors,
  # at this point we need to rescore the lattice to get the correct posteriors.
    if [ $stage -le 4 ]; then
      $cmd JOB=1:$nj $dir/log/vecs_pass2.JOB.log \
        gunzip -c $dir/pre_lat.JOB.gz \| \
        sgmm-rescore-lattice --spk-vecs=ark:$dir/pre_vecs.JOB --utt2spk=ark:$sdata1/JOB/utt2spk \
        "$gselect_opt" $srcdir/final.mdl ark:- "$feats" ark:- \| \
        lattice-prune --acoustic-scale=$acwt --beam=$vecs_beam ark:- ark:- \| \
        lattice-determinize-pruned --acoustic-scale=$acwt --beam=$vecs_beam ark:- ark:- \| \
        lattice-to-post --acoustic-scale=$acwt ark:- ark:- \| \
        weight-silence-post 0.0 $silphonelist $srcdir/final.mdl ark:- ark:- \| \
        sgmm-est-spkvecs --spk2utt=ark:$sdata1/JOB/spk2utt "$gselect_opt" --spk-vecs=ark:$dir/pre_vecs.JOB \
        $srcdir/final.mdl "$feats" ark,s,cs:- "ark:$dir/vecs.JOB" || exit 1;
    fi
    rm $dir/pre_vecs.*
  
    if $use_fmllr; then
    # Estimate fMLLR transforms (note: these may be on top of any
    # fMLLR transforms estimated with the baseline GMM system.
      if [ $stage -le 5 ]; then # compute fMLLR transforms.
        echo "$0: computing fMLLR transforms."
        $cmd JOB=1:$nj $dir/log/fmllr.JOB.log \
  	gunzip -c $dir/pre_lat.JOB.gz \| \
  	sgmm-rescore-lattice --spk-vecs=ark:$dir/vecs.JOB --utt2spk=ark:$sdata1/JOB/utt2spk \
  	"$gselect_opt" $srcdir/final.mdl ark:- "$feats" ark:- \| \
  	lattice-prune --acoustic-scale=$acwt --beam=$vecs_beam ark:- ark:- \| \
  	lattice-determinize-pruned --acoustic-scale=$acwt --beam=$vecs_beam ark:- ark:- \| \
  	lattice-to-post --acoustic-scale=$acwt ark:- ark:- \| \
  	weight-silence-post 0.0 $silphonelist $srcdir/final.mdl ark:- ark:- \| \
  	sgmm-est-fmllr --spk2utt=ark:$sdata1/JOB/spk2utt "$gselect_opt" --spk-vecs=ark:$dir/vecs.JOB \
  	--fmllr-iters=$fmllr_iters --fmllr-min-count=$fmllr_min_count \
  	$srcdir/final.fmllr_mdl "$feats" ark,s,cs:- "ark:$dir/trans.JOB" || exit 1;
      fi
      feats="$feats transform-feats --utt2spk=ark:$sdata1/JOB/utt2spk ark,s,cs:$dir/trans.JOB ark:- ark:- |"  
    fi
  
  # Now rescore the state-level lattices with the adapted features and the
  # corresponding model.  Prune and determinize the lattices to limit
  # their size.
    if [ $stage -le 6 ]; then
      $cmd JOB=1:$nj $dir/log/rescore.JOB.log \
        sgmm-rescore-lattice "$gselect_opt" --utt2spk=ark:$sdata1/JOB/utt2spk --spk-vecs=ark:$dir/vecs.JOB \
        $srcdir/final.mdl "ark:gunzip -c $dir/pre_lat.JOB.gz|" "$feats" ark:- \| \
        lattice-determinize-pruned --acoustic-scale=$acwt --beam=$lattice_beam ark:- \
        "ark:|gzip -c > $dir/lat.JOB.gz" || exit 1;
    fi
    rm $dir/pre_lat.*.gz
  
  else  ### For models without speaker vectors:
  
    if $use_fmllr; then
    # Estimate fMLLR transforms (note: these may be on top of any
    # fMLLR transforms estimated with the baseline GMM system.
      if [ $stage -le 5 ]; then # compute fMLLR transforms.
        echo "$0: computing fMLLR transforms."
        $cmd JOB=1:$nj $dir/log/fmllr.JOB.log \
  	gunzip -c $dir/pre_lat.JOB.gz \| \
  	sgmm-rescore-lattice --utt2spk=ark:$sdata1/JOB/utt2spk \
  	"$gselect_opt" $srcdir/final.mdl ark:- "$feats" ark:- \| \
  	lattice-prune --acoustic-scale=$acwt --beam=$vecs_beam ark:- ark:- \| \
  	lattice-determinize-pruned --acoustic-scale=$acwt --beam=$vecs_beam ark:- ark:- \| \
  	lattice-to-post --acoustic-scale=$acwt ark:- ark:- \| \
  	weight-silence-post 0.0 $silphonelist $srcdir/final.mdl ark:- ark:- \| \
  	sgmm-est-fmllr --spk2utt=ark:$sdata1/JOB/spk2utt "$gselect_opt" \
  	--fmllr-iters=$fmllr_iters --fmllr-min-count=$fmllr_min_count \
  	$srcdir/final.fmllr_mdl "$feats" ark,s,cs:- "ark:$dir/trans.JOB" || exit 1;
      fi
      feats="$feats transform-feats --utt2spk=ark:$sdata1/JOB/utt2spk ark,s,cs:$dir/trans.JOB ark:- ark:- |"  
    fi
  
  # Now rescore the state-level lattices with the adapted features and the
  # corresponding model.  Prune and determinize the lattices to limit
  # their size.
    if [ $stage -le 6 ] && $use_fmllr; then
      $cmd JOB=1:$nj $dir/log/rescore.JOB.log \
        sgmm-rescore-lattice "$gselect_opt" --utt2spk=ark:$sdata1/JOB/utt2spk \
        $srcdir/final.mdl "ark:gunzip -c $dir/pre_lat.JOB.gz|" "$feats" ark:- \| \
        lattice-determinize-pruned --acoustic-scale=$acwt --beam=$lattice_beam ark:- \
        "ark:|gzip -c > $dir/lat.JOB.gz" || exit 1;
      rm $dir/pre_lat.*.gz
    else  # Already done with decoding if no adaptation needed.
      for n in `seq 1 $nj`; do
        mv $dir/pre_lat.${n}.gz $dir/lat.${n}.gz
      done
    fi
  
  fi
  
  # The output of this script is the files "lat.*.gz"-- we'll rescore this at 
  # different acoustic scales to get the final output.
  
  
  if [ $stage -le 7 ]; then
    [ ! -x local/score.sh ] && \
      echo "Not scoring because local/score.sh does not exist or not executable." && exit 1;
    echo "score best paths"
    local/score.sh --cmd "$cmd" $data $graphdir $dir
    echo "score confidence and timing with sclite"
    #local/score_sclite_conf.sh --cmd "$cmd" --language turkish $data $graphdir $dir
  fi
  echo "Decoding done."
  exit 0;