kaldi的編譯安裝與報錯解決方法

ybdesire發表於2019-06-03

引入

kaldi是語音識別領域,最常用的一個工具。

它自帶了很多特徵提取模組,能提取MFCC/ivector/xvector等語音特徵;也自帶了很多語音模型程式碼,可以直接使用或重新訓練GMM-HMM等模型;它還支援GPU進行訓練。可以說是功能很強大了。

更厲害的是,你只需要簡單的SHELL程式設計,就能使用kaldi。kaldi作為一個工具,不需要像庫一樣進行大量程式設計,所以使用門檻其實不高。

但是,它並沒有做到像tensorflow/keras這樣非常容易的安裝配置。kaldi是需要自己在不同平臺上重新編譯,才能使用的。這就需要我們配置編譯環境,以及各種編譯依賴。

雖然有一個python版本的pykaldi,但它只是一個wrapper,底層還是得自己重新編譯安裝kaldi才能使用。並且筆者和pykaldi的研發人員交流過,它目前並不支援kaldi的所有功能。感興趣的讀者可以檢視這裡(https://github.com/pykaldi/pykaldi/issues/101)。

相信很多新手在編譯kaldi時,都遇到過很多問題。網上有一些資料(雖然有的比較老,與目前的編譯方式不一樣了),也能搜到一些解決方法,但筆者還是遇到了不少需要自己解決的問題。

所以把自己編譯安裝kaldi的過程,以及遇到問題的解決方法記錄在此,供參考。

編譯安裝kaldi

我的系統是Linux Ubuntu 16.04。具體過程如下

  1. 安裝一些依賴工具與第三方庫
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install git
sudo apt-get install bc
sudo apt-get install g++
sudo apt-get install zlib1g-dev make automake autoconf bzip2 libtool subversion
sudo apt-get install libatlas3-base
  1. 下載kaldi,目前kaldi最新版本的程式碼都是在github(早期的老版本是在svn)
git clone https://github.com/kaldi-asr/kaldi.git
cd kaldi/tools
  1. 用kaldi自帶的指令碼check_dependencies.sh來檢測是否安裝完所有必須的依賴工具
extras/check_dependencies.sh

根據提示安裝必須的依賴。我這裡提示缺少MKL,也提示了需要用tools目錄下自帶的指令碼extras/install_mkl.sh來安裝MKL。

  1. 安裝MKL

執行指令碼extras/install_mkl.sh

extras/install_mkl.sh

安裝過程中報錯如下:

AppStream cache update completed, but some metadata was ignored due to errors.
Reading package lists... Done
E: Failed to fetch http://security.ubuntu.com/ubuntu/dists/xenial-security/multiverse/dep11/Components-amd64.yml  Could not open file /var/lib/apt/lists/parti
al/security.ubuntu.com_ubuntu_dists_xenial-security_multiverse_dep11_Components-amd64.yml.xz - open (13: Permission denied) [IP: 91.xxx.88.xxx 80]
E: Some index files failed to download. They have been ignored, or old ones used instead.
extras/install_mkl.sh: MKL package intel-mkl-64bit-2019.2-057 installation FAILED.

Please open an issue with us at https://github.com/kaldi-asr/kaldi/ if you
believe this is a bug.

用ll命令,檢查報錯的檔案,發現是個連結,如下所示:

kaldi/tools# ll /var/lib/apt/lists/partial/security.ubuntu.com_ubuntu_dists_xenial-security_multiverse_dep11_Components-amd64.yml.xz

lrwxrwxrwx 1 root root 108 Nov 22 01:31 /var/lib/apt/lists/partial/security.ubuntu.com_ubuntu_dists_xenial-security_multiverse_dep11_Components-amd64.yml.xz -> /var/lib/apt/lists/security.ubuntu.com_ubuntu_dists_xenial-security_multiverse_dep11_Components-amd64.yml.gz

檢視連結到的目標檔案許可權,發現它沒有寫許可權。這就是問題所在了,上面報錯是Permission denied,正好是許可權不夠。

kaldi/tools# ll /var/lib/apt/lists/security.ubuntu.com_ubuntu_dists_xenial-security_multiverse_dep11_Components-amd64.yml.gz

-rw-r--r-- 1 root root 158 Feb  2  2017 /var/lib/apt/lists/security.ubuntu.com_ubuntu_dists_xenial-security_multiverse_dep11_Components-amd64.yml.gz

改動該檔案的許可權,讓其可寫,如下所示

kaldi/tools# chmod 666  /var/lib/apt/lists/security.ubuntu.com_ubuntu_dists_xenial-security_multiverse_dep11_Components-amd64.yml.gz

然後,重新安裝mkl

extras/install_mkl.sh

得到如下提示,說明MKL安裝成功

extras/install_mkl.sh: Configuring ld runtime bindings
+ echo '/opt/intel/lib/intel64
/opt/intel/mkl/lib/intel64'
+ ldconfig
extras/install_mkl.sh: MKL package intel-mkl-64bit-2019.2-057 was successfully installed

在執行check_dependencies.sh,檢查依賴,得到如下提示,則說明所有依賴都安裝成功。

# extras/check_dependencies.sh

extras/check_dependencies.sh: all OK.
  1. 編譯tools

依賴安裝成功,說明編譯所需的工具和環境都配置好了,接下來就可以編譯tools。

kaldi/tools#  make -j 8
  1. 編譯src

tools編譯成功後,就可以到src目錄下,編譯src


kaldi/src#  ./configure --shared

kaldi/src#  make depend -j

kaldi/src#  make -j

這個步驟比較花時間,編譯成功後,提示如下

Running matrix-lib-test ... 3s... SUCCESS matrix-lib-test
Running sparse-matrix-test ... 0s... SUCCESS sparse-matrix-test
make[1]: Leaving directory '/home/rootuser/speech_2019/kaldi/src/matrix'
echo Done
Done

接下來,可以跑一下最簡單的例子,來驗證kaldi是否安裝成功。

跑yesno例子來驗證安裝成功

執行自帶的yesno例子,跑./run.sh即可執行:

kaldi/egs/yesno/s5# ls

conf  input  local  path.sh  run.sh  steps  utils

kaldi/egs/yesno/s5# ./run.sh

如果沒有報錯,那恭喜您,到這裡,kaldi編譯安裝成功!

附錄:執行yesno例子的完整輸出

kaldi/egs/yesno/s5# ./run.sh

steps/train_mono.sh: Pass 25
steps/train_mono.sh: Pass 26
steps/train_mono.sh: Aligning data
steps/train_mono.sh: Pass 27
steps/train_mono.sh: Pass 28
steps/train_mono.sh: Pass 29
steps/train_mono.sh: Aligning data
steps/train_mono.sh: Pass 30
steps/train_mono.sh: Pass 31
steps/train_mono.sh: Pass 32
steps/train_mono.sh: Aligning data
steps/train_mono.sh: Pass 33
steps/train_mono.sh: Pass 34
steps/train_mono.sh: Pass 35
steps/train_mono.sh: Aligning data
steps/train_mono.sh: Pass 36
steps/train_mono.sh: Pass 37
steps/train_mono.sh: Pass 38
steps/train_mono.sh: Aligning data
steps/train_mono.sh: Pass 39
steps/diagnostic/analyze_alignments.sh --cmd utils/run.pl data/lang exp/mono0a
steps/diagnostic/analyze_alignments.sh: see stats in exp/mono0a/log/analyze_alignments.log
1 warnings in exp/mono0a/log/update.*.log
6 warnings in exp/mono0a/log/init.log
exp/mono0a: nj=1 align prob=-81.88 over 0.05h [retry=0.0%, fail=0.0%] states=11 gauss=371
steps/train_mono.sh: Done training monophone system in exp/mono0a
tree-info exp/mono0a/tree
tree-info exp/mono0a/tree
fstpushspecial
fstminimizeencoded
fsttablecompose data/lang_test_tg/L_disambig.fst data/lang_test_tg/G.fst
fstdeterminizestar --use-log=true
fstisstochastic data/lang_test_tg/tmp/LG.fst
0.534295 0.533859
[info]: LG not stochastic.
fstcomposecontext --context-size=1 --central-position=0 --read-disambig-syms=data/lang_test_tg/phones/disambig.int --write-disambig-syms=data/lang_test_tg/tmp
/disambig_ilabels_1_0.int data/lang_test_tg/tmp/ilabels_1_0.21306 data/lang_test_tg/tmp/LG.fst
fstisstochastic data/lang_test_tg/tmp/CLG_1_0.fst
0.534295 0.533859
[info]: CLG not stochastic.
make-h-transducer --disambig-syms-out=exp/mono0a/graph_tgpr/disambig_tid.int --transition-scale=1.0 data/lang_test_tg/tmp/ilabels_1_0 exp/mono0a/tree exp/mono
0a/final.mdl
fsttablecompose exp/mono0a/graph_tgpr/Ha.fst data/lang_test_tg/tmp/CLG_1_0.fst
fstdeterminizestar --use-log=true
fstminimizeencoded
fstrmsymbols exp/mono0a/graph_tgpr/disambig_tid.int
fstrmepslocal
fstisstochastic exp/mono0a/graph_tgpr/HCLGa.fst
0.5342 -0.000299216
HCLGa is not stochastic
add-self-loops --self-loop-scale=0.1 --reorder=true exp/mono0a/final.mdl exp/mono0a/graph_tgpr/HCLGa.fst
steps/decode.sh --nj 1 --cmd utils/run.pl exp/mono0a/graph_tgpr data/test_yesno exp/mono0a/decode_test_yesno
decode.sh: feature type is delta
steps/diagnostic/analyze_lats.sh --cmd utils/run.pl exp/mono0a/graph_tgpr exp/mono0a/decode_test_yesno
steps/diagnostic/analyze_lats.sh: see stats in exp/mono0a/decode_test_yesno/log/analyze_alignments.log
Overall, lattice depth (10,50,90-percentile)=(1,1,2) and mean=1.2
steps/diagnostic/analyze_lats.sh: see stats in exp/mono0a/decode_test_yesno/log/analyze_lattice_depth_stats.log
local/score.sh --cmd utils/run.pl data/test_yesno exp/mono0a/graph_tgpr exp/mono0a/decode_test_yesno
local/score.sh: scoring with word insertion penalty=0.0,0.5,1.0
%WER 0.00 [ 0 / 232, 0 ins, 0 del, 0 sub ] exp/mono0a/decode_test_yesno/wer_10_0.0

參考

相關文章