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