Facebook 開源語音識別工具包wav2letter(附實現教程)

劉曉坤發表於2018-01-01
近日,Facebook AI 研究院開源了端到端語音識別系統 wav2letter,本文是該架構的論文實現,讀者可據此做語音轉錄。

GitHub 地址:https://github.com/facebookresearch/wav2letter

wav2letter

wav2letter 是 Facebook AI 研究院今天開源的簡單高效的端到端自動語音識別(ASR)系統。該實現的原作者包括 Ronan Collobert、Christian Puhrsch、Gabriel Synnaeve、Neil Zeghidour 和 Vitaliy Liptchinsky。

wav2letter 實現的是論文「Wav2Letter: an End-to-End ConvNet-based Speech Recognition System」以及「Letter-Based Speech Recognition with Gated ConvNets」中提出的架構(如果你使用了這個模型或預訓練模型,請引用以上兩篇論文之一)。

如果你想要立刻進行語音轉錄,我們提供了在 Librispeech 資料集上預訓練的模型。

預訓練模型:https://github.com/facebookresearch/wav2letter#pre-trained-models

Librispeech 資料集:http://www.openslr.org/12

安裝要求

  • MacOS 或 Linux 作業系統
  • Torch,我們在下文介紹了安裝教程
  • 在 CPU 上訓練:Intel MKL
  • 在 GPU 上訓練:NVIDIA CUDA Toolkit (cuDNN v5.1 for CUDA 8.0)
  • 讀取錄音檔案:Libsndfile(必須在任何標準發行版中可用)
  • 標準語音特徵:FFTW(必須在任何標準發行版中可用)

安裝

MKL

如果你打算在 CPU 上訓練,我們強烈推薦安裝 Intel MKL。

通過以下程式碼更新你的 .bashrc 檔案

  1. # We assume Torch will be installed in $HOME/usr.# Change according to your needs.export PATH=$HOME/usr/bin:$PATH# This is to detect MKL during compilation# but also to make sure it is found at runtime.

  2. INTEL_DIR=/opt/intel/lib/intel64

  3. MKL_DIR=/opt/intel/mkl/lib/intel64

  4. MKL_INC_DIR=/opt/intel/mkl/include

  5. if [ ! -d "$INTEL_DIR" ]; thenecho "$ warning: INTEL_DIR out of date"fiif [ ! -d "$MKL_DIR" ]; thenecho "$ warning: MKL_DIR out of date"fiif [ ! -d "$MKL_INC_DIR" ]; thenecho "$ warning: MKL_INC_DIR out of date"fi# Make sure MKL can be found by Torch.export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$INTEL_DIR:$MKL_DIRexport CMAKE_LIBRARY_PATH=$LD_LIBRARY_PATHexport CMAKE_INCLUDE_PATH=$CMAKE_INCLUDE_PATH:$MKL_INC_DIR

LuaJIT + LuaRocks

以下程式碼在本地的$HOME/usr 安裝了 LuaJIT 和 LuaRocks。如果你需要全系統的安裝,請刪除-DCMAKE_INSTALL_PREFIX=$HOME/usr 選項。

  1. git clone https://github.com/torch/luajit-rocks.gitcd luajit-rocks

  2. mkdir build; cd build

  3. cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/usr -DWITH_LUAJIT21=OFF

  4. make -j 4

  5. make installcd ../..

在下一部分,我們假定 LuaJIT 和 LuaRocks 被安裝在了路徑$PATH。如果不是,並假定你將它們安裝在了本地的$HOME/usr,你需要替換成執行~/usr/bin/luarocks 和 ~/usr/bin/luajit。

KenLM 語言模型工具包

執行 wav2letter 解碼器需要 KenLM 工具包,執行 KenLM 需要安裝 Boost 庫。

  1. # make sure boost is installed (with system/thread/test modules)# actual command might vary depending on your system

  2. sudo apt-get install libboost-dev libboost-system-dev libboost-thread-dev libboost-test-dev

成功安裝了 Boost 之後,就可以安裝 KenLM:

  1. wget https://kheafield.com/code/kenlm.tar.gz

  2. tar xfvz kenlm.tar.gzcd kenlm

  3. mkdir build && cd build

  4. cmake .. -DCMAKE_INSTALL_PREFIX=$HOME/usr -DCMAKE_POSITION_INDEPENDENT_CODE=ON

  5. make -j 4

  6. make install

  7. cp -a lib/* ~/usr/lib # libs are not installed by default :(cd ../..

OpenMPI 和 TorchMPI

如果想使用多 CPU 或多 GPU 訓練(或多機器訓練),你需要安裝 OpenMPI 和 TorchMPI。

免責宣告:我們強烈推薦你自己重編譯 OpenMPI。OpenMPI 二進位制檔案的標準發行版的編譯標籤存在很大的方差。特定的標籤對於成功地編譯和執行 TorchMPI 很關鍵。

首先安裝OpenMPI:

  1. wget https://www.open-mpi.org/software/ompi/v2.1/downloads/openmpi-2.1.2.tar.bz2

  2. tar xfj openmpi-2.1.2.tar.bz2cd openmpi-2.1.2; mkdir build; cd build

  3. ./configure --prefix=$HOME/usr --enable-mpi-cxx --enable-shared --with-slurm --enable-mpi-thread-multiple --enable-mpi-ext=affinity,cuda --with-cuda=/public/apps/cuda/9.0

  4. make -j 20 all

  5. make install

注意:這裡也可以使用 openmpi-3.0.0.tar.bz2,但需要將—enable-mpi-thread-multiple 刪除。

現在可以安裝 TorchMPI 了:

  1. MPI_CXX_COMPILER=$HOME/usr/bin/mpicxx ~/usr/bin/luarocks install torchmpi

安裝 Torch 和其它的 Torch 包

  1. luarocks install torch

  2. luarocks install cudnn # for GPU support

  3. luarocks install cunn # for GPU support

安裝 wav2letter 包

  1. git clone https://github.com/facebookresearch/wav2letter.git

  2. cd wav2letter

  3. cd gtn && luarocks make rocks/gtn-scm-1.rockspec && cd ..

  4. cd speech && luarocks make rocks/speech-scm-1.rockspec && cd ..

  5. cd torchnet-optim && luarocks make rocks/torchnet-optim-scm-1.rockspec && cd ..

  6. cd wav2letter && luarocks make rocks/wav2letter-scm-1.rockspec && cd ..

  7. # Assuming here you got KenLM in $HOME/kenlm

  8. # And only if you plan to use the decoder:

  9. cd beamer && KENLM_INC=$HOME/kenlm luarocks make rocks/beamer-scm-1.rockspec && cd ..

訓練 wav2letter 模型

資料預處理

資料資料夾包含多個用於預處理多種資料集的指令碼。目前我們僅提供 LibriSpeech 和 TIMIT。

以下是預處理 LibriSpeech ASR 語料庫的例子:

  1. wget http://www.openslr.org/resources/12/dev-clean.tar.gz

  2. tar xfvz dev-clean.tar.gz# repeat for train-clean-100, train-clean-360, train-other-500, dev-other, test-clean, test-other

  3. luajit ~/wav2letter/data/librispeech/create.lua ~/LibriSpeech ~/librispeech-proc

  4. luajit ~/wav2letter/data/utils/create-sz.lua librispeech-proc/train-clean-100 librispeech-proc/train-clean-360 librispeech-proc/train-other-500 librispeech-proc/dev-clean librispeech-proc/dev-other librispeech-proc/test-clean librispeech-proc/test-other

訓練

  1. mkdir experiments

  2. luajit ~/wav2letter/train.lua --train -rundir ~/experiments -runname hello_librispeech -arch ~/wav2letter/arch/librispeech-glu-highdropout -lr 0.1 -lrcrit 0.0005 -gpu 1 -linseg 1 -linlr 0 -linlrcrit 0.005 -onorm target -nthread 6 -dictdir ~/librispeech-proc  -datadir ~/librispeech-proc -train train-clean-100+train-clean-360+train-other-500 -valid dev-clean+dev-other -test test-clean+test-other -gpu 1 -sqnorm -mfsc -melfloor 1 -surround "|" -replabel 2 -progress -wnorm -normclamp 0.2 -momentum 0.9 -weightdecay 1e-05

在多 GPU 上訓練

使用 OpenMPI 進行多 GPU 訓練:

  1. mpirun -n 2 --bind-to none  ~/TorchMPI/scripts/wrap.sh luajit ~/wav2letter/train.lua --train -mpi -gpu 1 ...

這裡,我們假定 mpirun 位於$PATH。

執行解碼器(推理)

執行解碼器之前,需要做些預處理步驟。

首先,創造一個字母詞典,裡面包含 wav2letter 中使用到的特殊重複字母

  1. cat ~/librispeech-proc/letters.lst >> ~/librispeech-proc/letters-rep.lst && echo "1" >> ~/librispeech-proc/letters-rep.lst && echo "2" >> ~/librispeech-proc/letters-rep.lst

然後用一個語言模型,做預處理。在這裡,我們使用的是基於 LibriSpeech 的預訓練語言模型,你們也可以使用 KenLM 訓練自己的語言模型。然後,把單詞預處理轉化為小寫字母,在 dict.lst 特定詞典中生成字母錄音文字(帶有重複字母)。該指令碼可能會提醒你哪個單詞轉錄錯誤,因為重複字母數量不對。在我們的案例中不存在這種情況,因為這種詞非常少。

  1. wget http://www.openslr.org/resources/11/3-gram.pruned.3e-7.arpa.gz luajit~/wav2letter/data/utils/convert-arpa.lua ~/3-gram.pruned.3e-7.arpa.gz ~/3-gram.pruned.3e-7.arpa ~/dict.lst -preprocess ~/wav2letter/data/librispeech/preprocess.lua -r 2 -letters letters-rep.lst

注意:也可以使用 4-gram 預訓練語言模型 4-gram.arpa.gz 作為替代,預處理可能花費的時間比較長。

可選項:用 KenLM 將其轉化為二進位制格式,後續載入語言模型,可加速訓練時間(我們在這裡假定 KenLM 位於你的$PATH)。

  1. build_binary 3-gram.pruned.3e-7.arpa 3-gram.pruned.3e-7.bin

我們現在可以生成特定訓練模型的 emissions,在資料集上執行 test.lua。該指令碼展示了字母錯誤率(LER)與詞錯率(WER),後者是在聲學模型沒有後處理的情況下計算的。

  1. luajit ~/wav2letter/test.lua ~/experiments/hello_librispeech/001_model_dev-clean.bin -progress -show -test dev-clean -save

一旦 emissions 儲存好,可執行解碼器計算通過用特定語言模型約束解碼獲得的詞錯率:

  1. luajit ~/wav2letter/decode.lua ~/experiments/hello_librispeech dev-clean -show -letters ~/librispeech-proc/letters-rep.lst  -words ~/dict.lst -lm ~/3-gram.pruned.3e-7.arpa -lmweight 3.1639 -beamsize 25000 -beamscore 40 -nthread 10 -smearing max -show

預訓練模型

我們提供了基於 LibriSpeech 的完整預訓練模型:

  1. wget https://s3.amazonaws.com/wav2letter/models/librispeech-glu-highdropout.bin

了使用該模型做語音轉錄,我們需要遵循該 github 專案中 README 的 requirements、installation 和 decoding 部分。

注意,該模型是 Facebook 基礎設施上的預訓練模型,所以你需要執行 test.lua 使用它,有略微不同的引數:

  1. luajit ~/wav2letter/test.lua ~/librispeech-glu-highdropout.bin -progress -show -test dev-clean -save -datadir ~/librispeech-proc/ -dictdir ~/librispeech-proc/ -gfsai

相關文章