Blame view

egs/mini_librispeech/s5/local/kws/search.sh 5.84 KB
8dcb6dfcb   Yannick Estève   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
  #!/bin/bash
  # Copyright 2012-2018  Johns Hopkins University (Author: Guoguo Chen, Yenda Trmal)
  # License: Apache 2.0
  
  
  help_message="$(basename $0): do keyword indexing and search.  data-dir is assumed to have
                   kws/ subdirectory that specifies the terms to search for.  Output is in
                   decode-dir/kws/
               Usage:
                   $(basename $0) <lang-dir> <data-dir> <decode-dir>"
  
  # Begin configuration section.
  min_lmwt=8
  max_lmwt=12
  cmd=run.pl
  model=
  skip_scoring=false
  skip_optimization=false # true can speed it up if #keywords is small.
  max_states=350000
  indices_dir=
  kwsout_dir=
  stage=0
  word_ins_penalty=0
  extraid=
  silence_word=  # specify this if you did to in kws_setup.sh, it's more accurate.
  strict=false
  duptime=0.6
  ntrue_scale=1.0
  frame_subsampling_factor=1
  nbest=-1
  max_silence_frames=50
  skip_indexing=false
  # End configuration section.
  
  echo "$0 $@"  # Print the command line for logging
  
  [ -f ./path.sh ] && . ./path.sh; # source the path.
  . parse_options.sh || exit 1;
  
  set -u
  set -e
  set -o pipefail
  
  
  if [[ "$#" -ne "3" ]] ; then
      echo -e "$0: FATAL: wrong number of script parameters!
  
  "
      printf "$help_message
  
  "
      exit 1;
  fi
  
  silence_opt=
  
  langdir=$1
  datadir=$2
  decodedir=$3
  
  if [ -z $extraid ] ; then
    kwsdatadir=$datadir/kws
  else
    kwsdatadir=$datadir/kwset_${extraid}
  fi
  
  if [ -z $extraid ] ; then
    kwsoutdir=$decodedir/kws
  else
    kwsoutdir=$decodedir/kwset_${extraid}
  fi
  
  
  if [ -z $indices_dir ]; then
    indices_dir=$kwsoutdir
  fi
  
  if [ ! -z "$model" ]; then
      model_flags="--model $model"
  else
      model_flags=
  fi
  
  mkdir -p $kwsoutdir
  for d in "$datadir" "$kwsdatadir" "$langdir" "$decodedir"; do
    if [ ! -d "$d" ]; then
      echo "$0: FATAL: expected directory $d to exist"
      exit 1;
    fi
  done
  
  echo "$0: Searching: $kwsdatadir"
  duration=$(cat $kwsdatadir/trials)
  echo "$0: Duration: $duration"
  
  
  frame_subsampling_factor=1
  if [ -f $decodedir/../frame_subsampling_factor ] ; then
    frame_subsampling_factor=$(cat $decodedir/../frame_subsampling_factor)
    echo "$0: Frame subsampling factor autodetected: $frame_subsampling_factor"
  elif [ -f $decodedir/../../frame_subsampling_factor ] ; then
    frame_subsampling_factor=$(cat $decodedir/../../frame_subsampling_factor)
    echo "$0: Frame subsampling factor autodetected: $frame_subsampling_factor"
  fi
  
  if [ $stage -le 0 ] ; then
    if [ ! -f $indices_dir/.done.index ] && ! $skip_indexing ; then
      [ ! -d $indices_dir ] && mkdir  $indices_dir
      for lmwt in $(seq $min_lmwt $max_lmwt) ; do
        indices=${indices_dir}_$lmwt
        mkdir -p $indices
  
        acwt=$(perl -e "print 1.0/$lmwt")
        [ ! -z $silence_word ] && silence_opt="--silence-word $silence_word"
        steps/make_index.sh $silence_opt --cmd "$cmd" --acwt $acwt $model_flags\
          --skip-optimization $skip_optimization --max-states $max_states \
          --word-ins-penalty $word_ins_penalty --max-silence-frames $max_silence_frames\
          --frame-subsampling-factor ${frame_subsampling_factor} \
          $kwsdatadir $langdir $decodedir $indices  || exit 1
      done
      touch $indices_dir/.done.index
    else
      echo "$0: Assuming indexing has been aready done. If you really need to re-run "
      echo "$0: the indexing again, delete the file $indices_dir/.done.index"
    fi
  fi
  
  keywords=$kwsdatadir/keywords.fsts
  if [ -f $keywords ] ; then
    echo "$0: Using ${keywords} for search"
    keywords="ark:$keywords"
  elif [ -f ${keywords}.gz ] ; then
    echo "$0: Using ${keywords}.gz for search"
    keywords="ark:gunzip -c ${keywords}.gz |"
  else
    echo "$0: The keyword file ${keywords}[.gz] does not exist"
  fi
  
  
  if [ $stage -le 1 ]; then
    for lmwt in $(seq $min_lmwt $max_lmwt) ; do
      kwsoutput=${kwsoutdir}_$lmwt
      indices=${indices_dir}_$lmwt
      nj=$(cat $indices/num_jobs)
  
  
      for f in $indices/index.1.gz ; do
        [ ! -f $f ] && echo "$0: no such file $f" && exit 1;
      done
  
      mkdir -p $kwsoutput/log
      $cmd JOB=1:$nj $kwsoutput/log/search.JOB.log \
        set -e  -o pipefail '&&' \
        kws-search --strict=$strict --negative-tolerance=-1 \
        --frame-subsampling-factor=${frame_subsampling_factor} \
        "ark:gzip -cdf $indices/index.JOB.gz|" "$keywords" \
        "ark,t:| sort -u | gzip -c > $kwsoutput/result.JOB.gz" \
        "ark,t:| sort -u | gzip -c > $kwsoutput/stats.JOB.gz" || exit 1;
    done
  fi
  
  if [ $stage -le 2 ]; then
    for lmwt in $(seq $min_lmwt $max_lmwt) ; do
      kwsoutput=${kwsoutdir}_$lmwt
      indices=${indices_dir}_$lmwt
      nj=$(cat $indices/num_jobs)
  
      # This is a memory-efficient way how to do the filtration
      # we do this in this way because the result.* files can be fairly big
      # and we do not want to run into troubles with memory
      files=""
      for job in $(seq 1 $nj); do
        if [ -f $kwsoutput/result.${job}.gz ] ; then
         files="$files <(gunzip -c $kwsoutput/result.${job}.gz)"
        elif [ -f $kwsoutput/result.${job} ] ; then
         files="$files $kwsoutput/result.${job}"
        else
          echo >&2 "The file $kwsoutput/result.${job}[.gz] does not exist"
          exit 1
        fi
      done
      # we have to call it using eval as we need the bash to interpret
      # the (possible) command substitution in case of gz files
      # bash -c would probably work as well, but would spawn another
      # shell instance
      eval "sort -m -u $files" |\
        local/kws/filter_kws_results.pl --likes --nbest $nbest > $kwsoutput/results || exit 1
    done
  fi
  
  if [ -z $extraid ] ; then
    extraid_flags=
  else
    extraid_flags="  --extraid ""$extraid"" "
  fi
  
  if [ $stage -le 4 ]; then
    if $skip_scoring ; then
      echo "$0: Not scoring, because --skip-scoring true was issued"
    elif [ ! -x local/kws/score.sh ] ; then
      echo "$0: Not scoring, because the file local/kws_score.sh is not present"
    else
      echo "$0: Scoring KWS results"
      local/kws/score.sh --cmd "$cmd" \
        --min-lmwt $min_lmwt --max-lmwt $max_lmwt $extraid_flags \
        $langdir $datadir ${kwsoutdir} || exit 1;
    fi
  fi
  
  echo "$0: Done"
  exit 0