iT邦幫忙

2021 iThome 鐵人賽

DAY 18
1
AI & Data

機器學習應用於語音相關服務系列 第 18

Day18 - 語音辨識神級工具-Kaldi part3

  • 分享至 

  • xImage
  •  

今天我們進入kaldi訓練神經網路模型的部分,程式的部分是在 local/chain/tuning/run_tdnn.sh 中。
程式主要分為以下幾個部分:

  • iVector extract (local/nnet3/run_ivector_common.sh): 包含
    • 資料擴充: 將資料集中音檔的語速調整為 0.9、1.1倍(可自行設定)來讓資料集增加為原本的2倍,透過 utils/data/perturb_data_dir_speed_3way.sh 來達成
    • 加入音量擾動 volume-perturbation: 這邊需要設定音量調整的最小、最大值(預設為0.125~2),會將每一筆音檔的音量隨機調整至此區間內,透過 utils/data/perturb_data_dir_volume.sh 來達成。經過此步驟後資料集總數不會改變
    • 語音特徵擷取: 擴充完資料集後需要重新擷取語音特徵,做法與Day16時提到的類似,不過這邊在擷取時會把MFCC的維度由原本的13調整為40,config檔則是 conf/mfcc_hires.conf
    • 取部分的訓練資料計算 PCA transform
    • 取部分的訓練資料訓練 diagonal UBM
    • 訓練 iVector (identity vector)
  • create neural network: 建立神經網路,以TDNN為範例
  • train (steps/nnet3/chain/train.py): 透過 steps/nnet3/chain/train.py 訓練神經網路,訓練完成後會在指定的目錄下產生模型檔 final.mdl
  • decode (steps/nnet3/decode.sh): 透過 steps/nnet3/decode.sh 進行解碼

參考程式如下

#!/bin/bash

set -e

# configs for 'chain'
affix=1d
stage=0
train_stage=-10
get_egs_stage=-10
dir=exp/chain/tdnn  # Note: _sp will get added to this
decode_iter=

# training options
num_epochs=6
initial_effective_lrate=0.00025
final_effective_lrate=0.000025
max_param_change=2.0
final_layer_normalize_target=0.5
num_jobs_initial=3
num_jobs_final=16
minibatch_size=64
frames_per_eg=150,110,90
remove_egs=false
common_egs_dir=
xent_regularize=0.1
dropout_schedule='0,0@0.20,0.5@0.50,0'

# End configuration section.
echo "$0 $@"  # Print the command line for logging

. ./cmd.sh
. ./path.sh
. ./utils/parse_options.sh

if ! cuda-compiled; then
  cat <<EOF && exit 1
This script is intended to be used with GPUs but you have not compiled Kaldi with CUDA
If you want to use GPUs (and have them), go to src/, and configure and make on a machine
where "nvcc" is installed.
EOF
fi

# The iVector-extraction and feature-dumping parts are the same as the standard
# nnet3 setup, and you can skip them by setting "--stage 8" if you have already
# run those things.

dir=${dir}${affix:+_$affix}_sp
train_set=train_sp
ali_dir=exp/tri5a_sp_ali
treedir=exp/chain/tri6a_tree_sp
lang=data/lang_chain


# if we are using the speed-perturbed data we need to generate
# alignments for it.
local/nnet3/run_ivector_common.sh --stage $stage --train-set train --gmm tri5a ${nnet3_affix:+ --nnet3-affix $nnet3_affix}

if [ $stage -le 7 ]; then
 # Get the alignments as lattices (gives the LF-MMI training more freedom).
 # use the same num-jobs as the alignments
 nj=$(cat $ali_dir/num_jobs) || exit 1;
 steps/align_fmllr_lats.sh --nj $nj --cmd "$train_cmd" data/$train_set \
   data/lang exp/tri5a exp/tri5a_sp_lats
 rm exp/tri5a_sp_lats/fsts.*.gz # save space
fi

if [ $stage -le 8 ]; then
 # Create a version of the lang/ directory that has one state per phone in the
 # topo file. [note, it really has two states.. the first one is only repeated
 # once, the second one has zero or more repeats.]
 rm -rf $lang
 cp -r data/lang $lang
 silphonelist=$(cat $lang/phones/silence.csl) || exit 1;
 nonsilphonelist=$(cat $lang/phones/nonsilence.csl) || exit 1;
 # Use our special topology... note that later on may have to tune this
 # topology.
 steps/nnet3/chain/gen_topo.py $nonsilphonelist $silphonelist >$lang/topo
fi

if [ $stage -le 9 ]; then
 # Build a tree using our new topology. This is the critically different
 # step compared with other recipes.
 steps/nnet3/chain/build_tree.sh --frame-subsampling-factor 3 \
     --context-opts "--context-width=2 --central-position=1" \
     --cmd "$train_cmd" 7000 data/$train_set $lang $ali_dir $treedir
fi

if [ $stage -le 10 ]; then
  echo "$0: creating neural net configs using the xconfig parser";

  num_targets=$(tree-info $treedir/tree |grep num-pdfs|awk '{print $2}')
  learning_rate_factor=$(echo "print (0.5/$xent_regularize)" | python)
  affine_opts="l2-regularize=0.01 dropout-proportion=0.0 dropout-per-dim=true dropout-per-dim-continuous=true"
  tdnnf_opts="l2-regularize=0.01 dropout-proportion=0.0 bypass-scale=0.66"
  linear_opts="l2-regularize=0.01 orthonormal-constraint=-1.0"
  prefinal_opts="l2-regularize=0.01"
  output_opts="l2-regularize=0.002"

  mkdir -p $dir/configs
  cat <<EOF > $dir/configs/network.xconfig
  input dim=100 name=ivector
  input dim=43 name=input

  # please note that it is important to have input layer with the name=input
  # as the layer immediately preceding the fixed-affine-layer to enable
  # the use of short notation for the descriptor
  fixed-affine-layer name=lda input=Append(-1,0,1,ReplaceIndex(ivector, t, 0)) affine-transform-file=$dir/configs/lda.mat
  # the first splicing is moved before the lda layer, so no splicing here
  relu-batchnorm-dropout-layer name=tdnn1 $affine_opts dim=1536
  tdnnf-layer name=tdnnf2 $tdnnf_opts dim=1536 bottleneck-dim=160 time-stride=1
  tdnnf-layer name=tdnnf3 $tdnnf_opts dim=1536 bottleneck-dim=160 time-stride=1
  tdnnf-layer name=tdnnf4 $tdnnf_opts dim=1536 bottleneck-dim=160 time-stride=1
  tdnnf-layer name=tdnnf5 $tdnnf_opts dim=1536 bottleneck-dim=160 time-stride=0
  tdnnf-layer name=tdnnf6 $tdnnf_opts dim=1536 bottleneck-dim=160 time-stride=3
  tdnnf-layer name=tdnnf7 $tdnnf_opts dim=1536 bottleneck-dim=160 time-stride=3
  tdnnf-layer name=tdnnf8 $tdnnf_opts dim=1536 bottleneck-dim=160 time-stride=3
  tdnnf-layer name=tdnnf9 $tdnnf_opts dim=1536 bottleneck-dim=160 time-stride=3
  tdnnf-layer name=tdnnf10 $tdnnf_opts dim=1536 bottleneck-dim=160 time-stride=3
  tdnnf-layer name=tdnnf11 $tdnnf_opts dim=1536 bottleneck-dim=160 time-stride=3
  tdnnf-layer name=tdnnf12 $tdnnf_opts dim=1536 bottleneck-dim=160 time-stride=3
  tdnnf-layer name=tdnnf13 $tdnnf_opts dim=1536 bottleneck-dim=160 time-stride=3
  tdnnf-layer name=tdnnf14 $tdnnf_opts dim=1536 bottleneck-dim=160 time-stride=3
  tdnnf-layer name=tdnnf15 $tdnnf_opts dim=1536 bottleneck-dim=160 time-stride=3
  linear-component name=prefinal-l dim=256 $linear_opts
  prefinal-layer name=prefinal-chain input=prefinal-l $prefinal_opts big-dim=1536 small-dim=256
  output-layer name=output include-log-softmax=false dim=$num_targets $output_opts
  prefinal-layer name=prefinal-xent input=prefinal-l $prefinal_opts big-dim=1536 small-dim=256
  output-layer name=output-xent dim=$num_targets learning-rate-factor=$learning_rate_factor $output_opts
EOF
  steps/nnet3/xconfig_to_configs.py --xconfig-file $dir/configs/network.xconfig --config-dir $dir/configs/
fi

if [ $stage -le 11 ]; then
  steps/nnet3/chain/train.py --stage $train_stage \
    --cmd "$decode_cmd" \
    --feat.online-ivector-dir exp/nnet3$nnet3_affix/ivectors_${train_set} \
    --feat.cmvn-opts "--norm-means=false --norm-vars=false" \
    --chain.xent-regularize $xent_regularize \
    --chain.leaky-hmm-coefficient 0.1 \
    --chain.l2-regularize 0.0 \
    --chain.apply-deriv-weights false \
    --chain.lm-opts="--num-extra-lm-states=2000" \
    --trainer.dropout-schedule $dropout_schedule \
    --trainer.add-option="--optimization.memory-compression-level=2" \
    --egs.dir "$common_egs_dir" \
    --egs.stage $get_egs_stage \
    --egs.opts "--frames-overlap-per-eg 0 --constrained false" \
    --egs.chunk-width $frames_per_eg \
    --trainer.num-chunk-per-minibatch $minibatch_size \
    --trainer.frames-per-iter 1500000 \
    --trainer.num-epochs $num_epochs \
    --trainer.optimization.num-jobs-initial $num_jobs_initial \
    --trainer.optimization.num-jobs-final $num_jobs_final \
    --trainer.optimization.initial-effective-lrate $initial_effective_lrate \
    --trainer.optimization.final-effective-lrate $final_effective_lrate \
    --trainer.max-param-change $max_param_change \
    --cleanup.remove-egs $remove_egs \
    --feat-dir data/${train_set}_hires \
    --tree-dir $treedir \
    --lat-dir exp/tri5a_sp_lats \
    --use-gpu wait \
    --dir $dir  || exit 1;
fi

if [ $stage -le 12 ]; then
  # Note: it might appear that this $lang directory is mismatched, and it is as
  # far as the 'topo' is concerned, but this script doesn't read the 'topo' from
  # the lang directory.
  utils/mkgraph.sh --self-loop-scale 1.0 data/lang_test $dir $dir/graph
fi

graph_dir=$dir/graph
if [ $stage -le 13 ]; then
  for test_set in test ; do
    steps/nnet3/decode.sh --acwt 1.0 --post-decode-acwt 10.0 \
      --nj 10 --cmd "$decode_cmd" \
      --online-ivector-dir exp/nnet3${nnet3_affix:+_$nnet3_affix}/ivectors_$test_set \
      $graph_dir data/${test_set}_hires $dir/decode_${test_set} || exit 1;
  done
  wait;
fi

exit 0;

Kaldi 初步的介紹就到這邊了,明天開始就要進入語音情緒辨識的範圍了。

參考資料:

https://www.cs.cmu.edu/~elaw/papers/pca.pdf
https://www.danielpovey.com/files/jhu_lecture2.pdf
https://www.sciencedirect.com/science/article/pii/S1877050918314042


上一篇
Day17 - 語音辨識神級工具-Kaldi part2
下一篇
Day 19 - 語音情緒辨識簡介
系列文
機器學習應用於語音相關服務30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言