Blame view

Scripts/steps/decode_basis_fmllr.sh 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
  #!/bin/bash
  
  # Copyright 2012   Carnegie Mellon University (Author: Yajie Miao)
  #                  Johns Hopkins University (Author: Daniel Povey)
  
  # Decoding script that does basis fMLLR.  This can be on top of delta+delta-delta,
  # or LDA+MLLT features.
  
  # There are 3 models involved potentially in this script,
  # and for a standard, speaker-independent system they will all be the same.
  # The "alignment model" is for the 1st-pass decoding and to get the 
  # Gaussian-level alignments for the "adaptation model" the first time we
  # do fMLLR.  The "adaptation model" is used to estimate fMLLR transforms
  # and to generate state-level lattices.  The lattices are then rescored
  # with the "final model".
  #
  # The following table explains where we get these 3 models from.
  # Note: $srcdir is one level up from the decoding directory.
  #
  #   Model              Default source:                 
  #
  #  "alignment model"   $srcdir/final.alimdl              --alignment-model <model>
  #                     (or $srcdir/final.mdl if alimdl absent)
  #  "adaptation model"  $srcdir/final.mdl                 --adapt-model <model>
  #  "final model"       $srcdir/final.mdl                 --final-model <model>
  
  
  # Begin configuration section
  first_beam=10.0 # Beam used in initial, speaker-indep. pass
  first_max_active=2000 # max-active used in initial pass.
  alignment_model=
  adapt_model=
  final_model=
  stage=0
  acwt=0.083333 # Acoustic weight used in getting fMLLR transforms, and also in 
                # lattice generation.
  
  # Parameters in alignment of training data
  scale_opts="--transition-scale=1.0 --acoustic-scale=0.1 --self-loop-scale=0.1"
  align_beam=10
  retry_beam=40
  
  max_active=7000
  beam=13.0
  lattice_beam=6.0
  nj=4
  silence_weight=0.01
  cmd=run.pl
  si_dir=
  # 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 [ $# != 3 ]; then
     echo "Usage: steps/decode_basis_fmllr.sh [options] <graph-dir> <data-dir> <decode-dir>"
     echo " e.g.: steps/decode_basis_fmllr.sh exp/tri2b/graph_tgpr data/train_si84 data/test_dev93 exp/tri2b/decode_dev93_tgpr"
     echo "main options (for others, see top of script file)"
     echo "  --config <config-file>                   # config containing options"
     echo "  --nj <nj>                                # number of parallel jobs"
     echo "  --cmd <cmd>                              # Command to run in parallel with"
     echo "  --adapt-model <adapt-mdl>                # Model to compute transforms with"
     echo "  --alignment-model <ali-mdl>              # Model to get Gaussian-level alignments for"
     echo "                                           # 1st pass of transform computation."
     echo "  --final-model <finald-mdl>               # Model to finally decode with"
     echo "  --si-dir <speaker-indep-decoding-dir>    # use this to skip 1st pass of decoding"
     echo "                                           # Caution-- must be with same tree"
     echo "  --acwt <acoustic-weight>                 # default 0.08333 ... used to get posteriors"
  
     exit 1;
  fi
  
  
  graphdir=$1
  data=$2
  dir=`echo $3 | sed 's:/$::g'` # remove any trailing slash.
  
  srcdir=`dirname $dir`; # Assume model directory one level up from decoding directory.
  sdata=$data/split$nj;
  
  mkdir -p $dir/log
  [[ -d $sdata && $data/feats.scp -ot $sdata ]] || split_data.sh $data $nj || exit 1;
  echo $nj > $dir/num_jobs
  splice_opts=`cat $srcdir/splice_opts 2>/dev/null` # frame-splicing options.
  
  silphonelist=`cat $graphdir/phones/silence.csl` || exit 1;
  
  # Some checks.  Note: we don't need $srcdir/tree but we expect
  # it should exist, given the current structure of the scripts.
  for f in $graphdir/HCLG.fst $data/feats.scp $srcdir/tree $srcdir/fmllr.basis; do
    [ ! -f $f ] && echo "$0: no such file $f" && exit 1;
  done
  
  ## 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;
  ##
  
  ## Do the speaker-independent decoding, if --si-dir option not present. ##
  if [ -z "$si_dir" ]; then # we need to do the speaker-independent decoding pass.
    si_dir=${dir}.si # Name it as our decoding dir, but with suffix ".si".
    if [ $stage -le 0 ]; then
      steps/decode.sh --acwt $acwt --nj $nj --cmd "$cmd" --beam $first_beam --model $alignment_model --max-active $first_max_active $graphdir $data $si_dir || exit 1;
    fi
  fi
  ##
  
  ## Some checks, and setting of defaults for variables.
  [ "$nj" -ne "`cat $si_dir/num_jobs`" ] && echo "Mismatch in #jobs with si-dir" && exit 1;
  [ ! -f "$si_dir/lat.1.gz" ] && echo "No such file $si_dir/lat.1.gz" && exit 1;
  [ -z "$adapt_model" ] && adapt_model=$srcdir/final.mdl
  [ -z "$final_model" ] && final_model=$srcdir/final.mdl
  for f in $adapt_model $final_model; do
    [ ! -f $f ] && echo "$0: no such file $f" && exit 1;
  done
  ##
  
  ## Set up the unadapted features "$sifeats" for testing set
  if [ -f $srcdir/final.mat ]; then feat_type=lda; else feat_type=delta; fi
  echo "$0: feature type is $feat_type";
  case $feat_type in
    delta) sifeats="ark,s,cs:apply-cmvn --norm-vars=false --utt2spk=ark:$sdata/JOB/utt2spk scp:$sdata/JOB/cmvn.scp scp:$sdata/JOB/feats.scp ark:- | add-deltas ark:- ark:- |";;
    lda) sifeats="ark,s,cs:apply-cmvn --norm-vars=false --utt2spk=ark:$sdata/JOB/utt2spk scp:$sdata/JOB/cmvn.scp scp:$sdata/JOB/feats.scp ark:- | splice-feats $splice_opts ark:- ark:- | transform-feats $srcdir/final.mat ark:- ark:- |";;
    *) echo "Invalid feature type $feat_type" && exit 1;
  esac
  ##
  
  ## Now get the first-pass fMLLR transforms.
  ## We give all the default parameters in gmm-est-basis-fmllr
  if [ $stage -le 1 ]; then
    echo "$0: getting first-pass fMLLR transforms."
    $cmd JOB=1:$nj $dir/log/fmllr_pass1.JOB.log \
      gunzip -c $si_dir/lat.JOB.gz \| \
      lattice-to-post --acoustic-scale=$acwt ark:- ark:- \| \
      weight-silence-post $silence_weight $silphonelist $alignment_model ark:- ark:- \| \
      gmm-post-to-gpost $alignment_model "$sifeats" ark:- ark:- \| \
      gmm-est-basis-fmllr-gpost --spk2utt=ark:$sdata/JOB/spk2utt \
      --fmllr-min-count=200  --num-iters=10 --size-scale=0.2 \
      --step-size-iters=3 --write-weights=ark:$dir/pre_wgt.JOB \
       $adapt_model $srcdir/fmllr.basis "$sifeats" ark,s,cs:- \
      ark:$dir/pre_trans.JOB || exit 1;
  fi
  ##
  
  pass1feats="$sifeats transform-feats --utt2spk=ark:$sdata/JOB/utt2spk ark:$dir/pre_trans.JOB ark:- ark:- |"
  
  ## Do the main lattice generation pass.  Note: we don't determinize the lattices at
  ## this stage, as we're going to use them in acoustic rescoring with the larger 
  ## model, and it's more correct to store the full state-level lattice for this purpose.
  if [ $stage -le 2 ]; then
    echo "$0: doing main lattice generation phase"
    $cmd JOB=1:$nj $dir/log/decode.JOB.log \
      gmm-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 \
      $adapt_model $graphdir/HCLG.fst "$pass1feats" "ark:|gzip -c > $dir/lat.tmp.JOB.gz" \
      || exit 1;
  fi
  ##
  
  ## Do a second pass of estimating the transform-- this time with the lattices
  ## generated from the alignment model.  Compose the transforms to get
  ## $dir/trans.1, etc.
  if [ $stage -le 3 ]; then
    echo "$0: estimating fMLLR transforms a second time."
    $cmd JOB=1:$nj $dir/log/fmllr_pass2.JOB.log \
      lattice-determinize-pruned --acoustic-scale=$acwt --beam=4.0 \
      "ark:gunzip -c $dir/lat.tmp.JOB.gz|" ark:- \| \
      lattice-to-post --acoustic-scale=$acwt ark:- ark:- \| \
      weight-silence-post $silence_weight $silphonelist $adapt_model ark:- ark:- \| \
      gmm-est-basis-fmllr --fmllr-min-count=200 \
      --spk2utt=ark:$sdata/JOB/spk2utt --write-weights=ark:$dir/trans_tmp_wgt.JOB \
      $adapt_model $srcdir/fmllr.basis "$pass1feats" ark,s,cs:- ark:$dir/trans_tmp.JOB '&&' \
      compose-transforms --b-is-affine=true ark:$dir/trans_tmp.JOB ark:$dir/pre_trans.JOB \
      ark:$dir/trans.JOB  || exit 1;
  fi
  ##
  
  feats="$sifeats transform-feats --utt2spk=ark:$sdata/JOB/utt2spk ark:$dir/trans.JOB ark:- ark:- |"
  
  # Rescore the state-level lattices with the final adapted features, and the final model
  # (which by default is $srcdir/final.mdl, but which may be specified on the command line,
  # useful in case of discriminatively trained systems).
  # At this point we prune and determinize the lattices and write them out, ready for 
  # language model rescoring.
  
  if [ $stage -le 4 ]; then
    echo "$0: doing a final pass of acoustic rescoring."
    $cmd JOB=1:$nj $dir/log/acoustic_rescore.JOB.log \
      gmm-rescore-lattice $final_model "ark:gunzip -c $dir/lat.tmp.JOB.gz|" "$feats" ark:- \| \
      lattice-determinize-pruned --acoustic-scale=$acwt --beam=$lattice_beam ark:- \
      "ark:|gzip -c > $dir/lat.JOB.gz" '&&' rm $dir/lat.tmp.JOB.gz || exit 1;
  fi
  
  [ ! -x local/score.sh ] && \
    echo "$0: not scoring because local/score.sh does not exist or not executable." && exit 1;
  local/score.sh --cmd "$cmd" $data $graphdir $dir
  
  rm $dir/{trans_tmp,pre_trans}.*
  
  exit 0;