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_scoring=false
29 extra_left_context=0
30 extra_right_context=0
31 extra_left_context_initial=-1
32 extra_right_context_final=-1
33 feat_type=
34 online_ivector_dir=
35 minimize=false
36 # End configuration section.
38 echo "$0 $@" # Print the command line for logging
40 [ -f ./path.sh ] && . ./path.sh; # source the path.
41 . parse_options.sh || exit 1;
43 if [ $# -ne 3 ]; then
44 echo "Usage: $0 [options] <graph-dir> <data-dir> <decode-dir>"
45 echo "e.g.: steps/nnet3/decode.sh --nj 8 \\"
46 echo "--online-ivector-dir exp/nnet2_online/ivectors_test_eval92 \\"
47 echo " exp/tri4b/graph_bg data/test_eval92_hires $dir/decode_bg_eval92"
48 echo "main options (for others, see top of script file)"
49 echo " --transform-dir <decoding-dir> # directory of previous decoding"
50 echo " # where we can find transforms for SAT systems."
51 echo " --config <config-file> # config containing options"
52 echo " --nj <nj> # number of parallel jobs"
53 echo " --cmd <cmd> # Command to run in parallel with"
54 echo " --beam <beam> # Decoding beam; default 15.0"
55 echo " --iter <iter> # Iteration of model to decode; default is final."
56 echo " --scoring-opts <string> # options to local/score.sh"
57 echo " --num-threads <n> # number of threads to use, default 1."
58 echo " --parallel-opts <opts> # e.g. '--num-threads 4' if you supply --num-threads 4"
59 exit 1;
60 fi
62 graphdir=$1
63 data=$2
64 dir=$3
65 srcdir=`dirname $dir`; # Assume model directory one level up from decoding directory.
66 model=$srcdir/$iter.mdl
69 [ ! -z "$online_ivector_dir" ] && \
70 extra_files="$online_ivector_dir/ivector_online.scp $online_ivector_dir/ivector_period"
72 for f in $graphdir/HCLG.fst $data/feats.scp $model $extra_files; do
73 [ ! -f $f ] && echo "$0: no such file $f" && exit 1;
74 done
76 sdata=$data/split$nj;
77 cmvn_opts=`cat $srcdir/cmvn_opts` || exit 1;
78 thread_string=
79 [ $num_threads -gt 1 ] && thread_string="-parallel --num-threads=$num_threads"
81 mkdir -p $dir/log
82 [[ -d $sdata && $data/feats.scp -ot $sdata ]] || split_data.sh $data $nj || exit 1;
83 echo $nj > $dir/num_jobs
86 ## Set up features.
87 if [ -z "$feat_type" ]; then
88 if [ -f $srcdir/final.mat ]; then feat_type=lda; else feat_type=raw; fi
89 echo "$0: feature type is $feat_type"
90 fi
92 splice_opts=`cat $srcdir/splice_opts 2>/dev/null`
94 case $feat_type in
95 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:- |";;
96 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:- |"
97 ;;
98 *) echo "$0: invalid feature type $feat_type" && exit 1;
99 esac
100 if [ ! -z "$transform_dir" ]; then
101 echo "$0: using transforms from $transform_dir"
102 [ ! -s $transform_dir/num_jobs ] && \
103 echo "$0: expected $transform_dir/num_jobs to contain the number of jobs." && exit 1;
104 nj_orig=$(cat $transform_dir/num_jobs)
106 if [ $feat_type == "raw" ]; then trans=raw_trans;
107 else trans=trans; fi
108 if [ $feat_type == "lda" ] && \
109 ! cmp $transform_dir/../final.mat $srcdir/final.mat && \
110 ! cmp $transform_dir/final.mat $srcdir/final.mat; then
111 echo "$0: LDA transforms differ between $srcdir and $transform_dir"
112 exit 1;
113 fi
114 if [ ! -f $transform_dir/$trans.1 ]; then
115 echo "$0: expected $transform_dir/$trans.1 to exist (--transform-dir option)"
116 exit 1;
117 fi
118 if [ $nj -ne $nj_orig ]; then
119 # Copy the transforms into an archive with an index.
120 for n in $(seq $nj_orig); do cat $transform_dir/$trans.$n; done | \
121 copy-feats ark:- ark,scp:$dir/$trans.ark,$dir/$trans.scp || exit 1;
122 feats="$feats transform-feats --utt2spk=ark:$sdata/JOB/utt2spk scp:$dir/$trans.scp ark:- ark:- |"
123 else
124 # number of jobs matches with alignment dir.
125 feats="$feats transform-feats --utt2spk=ark:$sdata/JOB/utt2spk ark:$transform_dir/$trans.JOB ark:- ark:- |"
126 fi
127 elif grep 'transform-feats --utt2spk' $srcdir/log/train.1.log >&/dev/null; then
128 echo "$0: **WARNING**: you seem to be using a neural net system trained with transforms,"
129 echo " but you are not providing the --transform-dir option in test time."
130 fi
131 ##
133 if [ ! -z "$online_ivector_dir" ]; then
134 ivector_period=$(cat $online_ivector_dir/ivector_period) || exit 1;
135 ivector_opts="--online-ivectors=scp:$online_ivector_dir/ivector_online.scp --online-ivector-period=$ivector_period"
136 fi
138 if [ "$post_decode_acwt" == 1.0 ]; then
139 lat_wspecifier="ark:|gzip -c >$dir/lat.JOB.gz"
140 else
141 lat_wspecifier="ark:|lattice-scale --acoustic-scale=$post_decode_acwt ark:- ark:- | gzip -c >$dir/lat.JOB.gz"
142 fi
144 if [ -f $srcdir/frame_subsampling_factor ]; then
145 # e.g. for 'chain' systems
146 frame_subsampling_opt="--frame-subsampling-factor=$(cat $srcdir/frame_subsampling_factor)"
147 fi
149 if [ $stage -le 1 ]; then
150 $cmd --num-threads $num_threads JOB=1:$nj $dir/log/decode.JOB.log \
151 nnet3-latgen-faster$thread_string $ivector_opts $frame_subsampling_opt \
152 --frames-per-chunk=$frames_per_chunk \
153 --extra-left-context=$extra_left_context \
154 --extra-right-context=$extra_right_context \
155 --extra-left-context-initial=$extra_left_context_initial \
156 --extra-right-context-final=$extra_right_context_final \
157 --minimize=$minimize --max-active=$max_active --min-active=$min_active --beam=$beam \
158 --lattice-beam=$lattice_beam --acoustic-scale=$acwt --allow-partial=true \
159 --word-symbol-table=$graphdir/words.txt "$model" \
160 $graphdir/HCLG.fst "$feats" "$lat_wspecifier" || exit 1;
161 fi
163 # The output of this script is the files "lat.*.gz"-- we'll rescore this at
164 # different acoustic scales to get the final output.
167 if [ $stage -le 2 ]; then
168 if ! $skip_scoring ; then
169 [ ! -x local/score.sh ] && \
170 echo "Not scoring because local/score.sh does not exist or not executable." && exit 1;
171 echo "score best paths"
172 local/score.sh $scoring_opts --cmd "$cmd" $data $graphdir $dir
173 echo "score confidence and timing with sclite"
174 fi
175 fi
176 echo "Decoding done."
177 exit 0;