Blame view

egs/wsj/s5/steps/cleanup/make_segmentation_graph.sh 4.44 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
  #!/bin/bash
  
  # Copyright 2014  Guoguo Chen
  # Apache 2.0
  
  # Begin configuration section.
  nj=4
  cmd=run.pl
  tscale=1.0      # transition scale.
  loopscale=0.1   # scale for self-loops.
  cleanup=true
  ngram_order=1
  srilm_options="-wbdiscount"   # By default, use Witten-Bell discounting in SRILM
  # End configuration section.
  
  set -e
  
  echo "$0 $@"
  
  [ -f ./path.sh ] && . ./path.sh
  . parse_options.sh || exit 1;
  
  if [ $# -ne 4 ]; then
    echo "This script builds one decoding graph for each truncated utterance in"
    echo "segmentation. It first calls steps/cleanup/make_utterance_graph.sh to"
    echo "build one decoding graph for each original utterance, which will be"
    echo "shared by the truncated utterances from the same original utterance."
    echo "We assign the decoding graph to each truncated utterance using the scp"
    echo "file so that we can avoid duplicating the graphs on the disk."
    echo ""
    echo "Usage: $0 [options] <data-dir> <lang-dir> <model-dir> <graph-dir>"
    echo " e.g.: $0 data/train_si284_split/ \\"
    echo "                data/lang exp/tri2b/ exp/tri2b/graph_train_si284_split"
    echo ""
    echo "Options:"
    echo "    --ngram-order           # order of n-gram language model"
    echo "    --srilm-options         # options for ngram-count in SRILM tool"
    echo "    --tscale                # transition scale"
    echo "    --loopscale             # scale for self-loops"
    echo "    --cleanup               # if true, removes the intermediate files"
    exit 1;
  fi
  
  data=$1
  lang=$2
  model_dir=$3
  graph_dir=$4
  
  for f in $data/text.orig $data/orig2utt $lang/L_disambig.fst \
    $lang/words.txt $lang/oov.int $model_dir/final.mdl $model_dir/tree; do
    if [ ! -f $f ]; then
      echo "$0: expected $f to exist"
      exit 1;
    fi
  done
  
  utils/lang/check_phones_compatible.sh $lang/phones.txt $model_dir/phones.txt
  
  # If --ngram-order is larger than 1, we will have to use SRILM
  if [ $ngram_order -gt 1 ]; then
    ngram_count=`which ngram-count` || true
    if [ -z $ngram_count ]; then
      if uname -a | grep 64 >/dev/null; then # some kind of 64 bit...
        sdir=$KALDI_ROOT/tools/srilm/bin/i686-m64
      else
        sdir=$KALDI_ROOT/tools/srilm/bin/i686
      fi
      if [ -f $sdir/ngram-count ]; then
        echo Using SRILM tools from $sdir
        export PATH=$PATH:$sdir
      else
        echo You appear to not have SRILM tools installed, either on your path,
        echo or installed in $sdir.  See tools/install_srilm.sh for installation
        echo instructions.
        exit 1
      fi
    fi
  fi
  
  # Creates one graph for each transcript. We parallelize the process a little
  # bit.
  num_lines=`cat $data/text.orig | wc -l`
  if [ $nj -gt $num_lines ]; then
    nj=$num_lines
    echo "$0: Too many number of jobs, using $nj instead"
  fi
  
  mkdir -p $graph_dir/split$nj
  mkdir -p $graph_dir/log
   
  split_texts=""
  for n in $(seq $nj); do
    mkdir -p $graph_dir/split$nj/$n
    split_texts="$split_texts $graph_dir/split$nj/$n/text"
  done
  utils/split_scp.pl $data/text.orig $split_texts
  
  $cmd JOB=1:$nj $graph_dir/log/make_utterance_graph.JOB.log \
    steps/cleanup/make_utterance_graph.sh --cleanup $cleanup \
    --tscale $tscale --loopscale $loopscale \
    --ngram-order $ngram_order --srilm-options "$srilm_options" \
    $graph_dir/split$nj/JOB/text $lang \
    $model_dir $graph_dir/split$nj/JOB || exit 1;
  
  # Copies files from lang directory.
  mkdir -p $graph_dir
  cp -r $lang/* $graph_dir
  
  am-info --print-args=false $model_dir/final.mdl |\
   grep pdfs | awk '{print $NF}' > $graph_dir/num_pdfs
  
  # Creates the graph table.
  cat $graph_dir/split$nj/*/HCLG.fsts.scp > $graph_dir/split$nj/HCLG.fsts.scp
  fstcopy scp:$graph_dir/split$nj/HCLG.fsts.scp \
    "ark,scp:$graph_dir/HCLG.fsts,$graph_dir/tmp.HCLG.fsts.scp"
  
  # The graphs we created above were indexed by the old utterance id. We have to
  # duplicate them for the new utterance id. We do this in the scp file so we do
  # not have to store the duplicated graphs on the disk.
  cat $graph_dir/tmp.HCLG.fsts.scp | perl -e '
    open(O2U, "<$ARGV[0]") || die "Error: fail to open $ARGV[0]
  ";
    while (<STDIN>) {
      chomp;
      @col = split;
      @col == 2 || die "Error: bad line $_
  ";
      $scp{$col[0]} = $col[1];
    }
    while (<O2U>) {
      chomp;
      @col = split;
      @col >= 2 || die "Error: bad line $_
  ";
      defined($scp{$col[0]}) ||
        die "Error: $col[0] not defined in original scp file
  ";
      for ($i = 1; $i < @col; $i += 1) {
        print "$col[$i] $scp{$col[0]}
  "
      }
    }' $data/orig2utt > $graph_dir/HCLG.fsts.scp
  rm $graph_dir/tmp.HCLG.fsts.scp
  
  if $cleanup; then
    rm -r $graph_dir/split$nj
  fi
  
  exit 0;