]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - processor-sdk/kaldi.git/blob - egs/wsj/s5/steps/nnet3/decode.sh
Merge pull request #875 from vijayaditya/lsr_aspire
[processor-sdk/kaldi.git] / egs / wsj / s5 / steps / nnet3 / decode.sh
1 #!/bin/bash
3 # Copyright 2012-2015  Johns Hopkins University (Author: Daniel Povey).
4 # Apache 2.0.
6 # This script does decoding with a neural-net.  If the neural net was built on
7 # top of fMLLR transforms from a conventional system, you should provide the
8 # --transform-dir option.
10 # Begin configuration section.
11 stage=1
12 transform_dir=    # dir to find fMLLR transforms.
13 nj=4 # number of decoding jobs.  If --transform-dir set, must match that number!
14 acwt=0.1  # Just a default value, used for adaptation and beam-pruning..
15 post_decode_acwt=1.0  # can be used in 'chain' systems to scale acoustics by 10 so the
16                       # regular scoring script works.
17 cmd=run.pl
18 beam=15.0
19 frames_per_chunk=50
20 max_active=7000
21 min_active=200
22 ivector_scale=1.0
23 lattice_beam=8.0 # Beam we use in lattice generation.
24 iter=final
25 num_threads=1 # if >1, will use gmm-latgen-faster-parallel
26 parallel_opts=  # ignored now.
27 scoring_opts=
28 skip_diagnostics=false
29 skip_scoring=false
30 extra_left_context=0
31 extra_right_context=0
32 extra_left_context_initial=-1
33 extra_right_context_final=-1
34 feat_type=
35 online_ivector_dir=
36 minimize=false
37 # End configuration section.
39 echo "$0 $@"  # Print the command line for logging
41 [ -f ./path.sh ] && . ./path.sh; # source the path.
42 . parse_options.sh || exit 1;
44 if [ $# -ne 3 ]; then
45   echo "Usage: $0 [options] <graph-dir> <data-dir> <decode-dir>"
46   echo "e.g.:   steps/nnet3/decode.sh --nj 8 \\"
47   echo "--online-ivector-dir exp/nnet2_online/ivectors_test_eval92 \\"
48   echo "    exp/tri4b/graph_bg data/test_eval92_hires $dir/decode_bg_eval92"
49   echo "main options (for others, see top of script file)"
50   echo "  --transform-dir <decoding-dir>           # directory of previous decoding"
51   echo "                                           # where we can find transforms for SAT systems."
52   echo "  --config <config-file>                   # config containing options"
53   echo "  --nj <nj>                                # number of parallel jobs"
54   echo "  --cmd <cmd>                              # Command to run in parallel with"
55   echo "  --beam <beam>                            # Decoding beam; default 15.0"
56   echo "  --iter <iter>                            # Iteration of model to decode; default is final."
57   echo "  --scoring-opts <string>                  # options to local/score.sh"
58   echo "  --num-threads <n>                        # number of threads to use, default 1."
59   echo "  --parallel-opts <opts>                   # e.g. '--num-threads 4' if you supply --num-threads 4"
60   exit 1;
61 fi
63 graphdir=$1
64 data=$2
65 dir=$3
66 srcdir=`dirname $dir`; # Assume model directory one level up from decoding directory.
67 model=$srcdir/$iter.mdl
70 [ ! -z "$online_ivector_dir" ] && \
71   extra_files="$online_ivector_dir/ivector_online.scp $online_ivector_dir/ivector_period"
73 for f in $graphdir/HCLG.fst $data/feats.scp $model $extra_files; do
74   [ ! -f $f ] && echo "$0: no such file $f" && exit 1;
75 done
77 sdata=$data/split${nj}utt;
78 cmvn_opts=`cat $srcdir/cmvn_opts` || exit 1;
79 thread_string=
80 [ $num_threads -gt 1 ] && thread_string="-parallel --num-threads=$num_threads"
82 mkdir -p $dir/log
83 [[ -d $sdata && $data/feats.scp -ot $sdata ]] || split_data.sh --per-utt $data $nj || exit 1;
84 echo $nj > $dir/num_jobs
87 ## Set up features.
88 if [ -z "$feat_type" ]; then
89   if [ -f $srcdir/final.mat ]; then feat_type=lda; else feat_type=raw; fi
90   echo "$0: feature type is $feat_type"
91 fi
93 splice_opts=`cat $srcdir/splice_opts 2>/dev/null`
95 case $feat_type in
96   raw) feats="ark,s,cs:apply-cmvn $cmvn_opts --utt2spk=ark:$sdata/JOB/utt2spk scp:$sdata/JOB/cmvn.scp scp:$sdata/JOB/feats.scp ark:- |";;
97   lda) feats="ark,s,cs:apply-cmvn $cmvn_opts --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:- |"
98     ;;
99   *) echo "$0: invalid feature type $feat_type" && exit 1;
100 esac
101 if [ ! -z "$transform_dir" ]; then
102   echo "$0: using transforms from $transform_dir"
103   [ ! -s $transform_dir/num_jobs ] && \
104     echo "$0: expected $transform_dir/num_jobs to contain the number of jobs." && exit 1;
105   nj_orig=$(cat $transform_dir/num_jobs)
107   if [ $feat_type == "raw" ]; then trans=raw_trans;
108   else trans=trans; fi
109   if [ $feat_type == "lda" ] && \
110     ! cmp $transform_dir/../final.mat $srcdir/final.mat && \
111     ! cmp $transform_dir/final.mat $srcdir/final.mat; then
112     echo "$0: LDA transforms differ between $srcdir and $transform_dir"
113     exit 1;
114   fi
115   if [ ! -f $transform_dir/$trans.1 ]; then
116     echo "$0: expected $transform_dir/$trans.1 to exist (--transform-dir option)"
117     exit 1;
118   fi
119   if [ $nj -ne $nj_orig ]; then
120     # Copy the transforms into an archive with an index.
121     for n in $(seq $nj_orig); do cat $transform_dir/$trans.$n; done | \
122        copy-feats ark:- ark,scp:$dir/$trans.ark,$dir/$trans.scp || exit 1;
123     feats="$feats transform-feats --utt2spk=ark:$sdata/JOB/utt2spk scp:$dir/$trans.scp ark:- ark:- |"
124   else
125     # number of jobs matches with alignment dir.
126     feats="$feats transform-feats --utt2spk=ark:$sdata/JOB/utt2spk ark:$transform_dir/$trans.JOB ark:- ark:- |"
127   fi
128 elif grep 'transform-feats --utt2spk' $srcdir/log/train.1.log >&/dev/null; then
129   echo "$0: **WARNING**: you seem to be using a neural net system trained with transforms,"
130   echo "  but you are not providing the --transform-dir option in test time."
131 fi
132 ##
134 if [ ! -z "$online_ivector_dir" ]; then
135   ivector_period=$(cat $online_ivector_dir/ivector_period) || exit 1;
136   ivector_opts="--online-ivectors=scp:$online_ivector_dir/ivector_online.scp --online-ivector-period=$ivector_period"
137 fi
139 if [ "$post_decode_acwt" == 1.0 ]; then
140   lat_wspecifier="ark:|gzip -c >$dir/lat.JOB.gz"
141 else
142   lat_wspecifier="ark:|lattice-scale --acoustic-scale=$post_decode_acwt ark:- ark:- | gzip -c >$dir/lat.JOB.gz"
143 fi
145 frame_subsampling_opt=
146 if [ -f $srcdir/frame_subsampling_factor ]; then
147   # e.g. for 'chain' systems
148   frame_subsampling_opt="--frame-subsampling-factor=$(cat $srcdir/frame_subsampling_factor)"
149 fi
151 if [ $stage -le 1 ]; then
152   $cmd --num-threads $num_threads JOB=1:$nj $dir/log/decode.JOB.log \
153     nnet3-latgen-faster$thread_string $ivector_opts $frame_subsampling_opt \
154      --frames-per-chunk=$frames_per_chunk \
155      --extra-left-context=$extra_left_context \
156      --extra-right-context=$extra_right_context \
157      --extra-left-context-initial=$extra_left_context_initial \
158      --extra-right-context-final=$extra_right_context_final \
159      --minimize=$minimize --max-active=$max_active --min-active=$min_active --beam=$beam \
160      --lattice-beam=$lattice_beam --acoustic-scale=$acwt --allow-partial=true \
161      --word-symbol-table=$graphdir/words.txt "$model" \
162      $graphdir/HCLG.fst "$feats" "$lat_wspecifier" || exit 1;
163 fi
166 if [ $stage -le 2 ]; then
167   if ! $skip_diagnostics ; then
168     [ ! -z $iter ] && iter_opt="--iter $iter"
169     steps/diagnostic/analyze_lats.sh --cmd "$cmd" $iter_opt $graphdir $dir
170   fi
171 fi
174 # The output of this script is the files "lat.*.gz"-- we'll rescore this at
175 # different acoustic scales to get the final output.
176 if [ $stage -le 3 ]; then
177   if ! $skip_scoring ; then
178     [ ! -x local/score.sh ] && \
179       echo "Not scoring because local/score.sh does not exist or not executable." && exit 1;
180     echo "score best paths"
181     [ "$iter" != "final" ] && iter_opt="--iter $iter"
182     local/score.sh $iter_opt $scoring_opts --cmd "$cmd" $data $graphdir $dir
183     echo "score confidence and timing with sclite"
184   fi
185 fi
186 echo "Decoding done."
187 exit 0;