WeNet:面向工業(yè)落地的E2E語音識別工具
一、WeNet語音識別平臺搭建
1、參考資料
- wenet-e2e/wenet
- Mozilla DeepSpeech
- yeyupiaoling/PaddlePaddle-DeepSpeech
2、快速搭建WeNet平臺
參考 WeNet中文文檔
下載官方提供的預(yù)訓(xùn)練模型,并啟動 docker 服務(wù),加載模型,提供 websocket 協(xié)議的語音識別服務(wù)。
wget https://wenet-1256283475.cos.ap-shanghai.myqcloud.com/models/aishell2/20210618_u2pp_conformer_libtorch.tar.gz
tar -xf 20210618_u2pp_conformer_libtorch.tar.gz
model_dir=$PWD/20210618_u2pp_conformer_libtorch
docker run --rm -it -p 10086:10086 -v $model_dir:/home/wenet/model wenetorg/wenet-mini:latest bash /home/run.sh
Note:
-
這里的
$PWD = "/home/wenet/model"
。 -
一定要保證預(yù)訓(xùn)練模型文件的存儲位置要正確,即解壓在
$PWD
下,執(zhí)行如下命令model_dir=$PWD/20210618_u2pp_conformer_libtorch
進行變量賦值,否則會報:
實時識別
使用瀏覽器打開文件**index.html,在 WebSocket URL
中填入 ws://127.0.0.1:10086
(若在windows下通過wsl2**運行docker, 則使用ws://localhost:10086
) , 允許瀏覽器彈出的請求使用麥克風(fēng),即可通過麥克風(fēng)進行實時語音識別。
這里使用wsl2下的docker進行演示:如果靠近麥克風(fēng),誤檢率比較低。
二、WeNet實現(xiàn)推理(暫時無法使用onnx cpu版本進行推理)
Note:
-
如果僅使用wenet/bin/recognize.py,使用libTorch模型進行推理,可以在windows中搭建環(huán)境,具體搭建過程參考WeNet官網(wǎng)
-
如果要使用wenet/bin/recognize_onnx.py進行推理,需要先下載ctc_encoder,這里要注意pypi上的ctc_encoder只有2020的版本(WeNet1.0),和當(dāng)前的WeNet3.0版本不一致,因此需要到https://github.com/Slyne/ctc_decoder下載并編譯。由于編譯swig_encoder過程中需要用到bash命令,所以嘗試在linux系統(tǒng)中運行,這里使用WSL + ubuntu作為解決方案。
其實windows安裝git就可以執(zhí)行bash命令,只不過這里在安裝好
wget.exe
,swig.exe
,git clone
相應(yīng)的package(kenlm,ThreadPool)之后,對于下載的openfst-1.6.3,即使在VC中補全了.h
文件,無法編譯成功。
1、搭建WeNet環(huán)境
這里由于要嘗試使用onnx推理模型,因此使用WSL + ubuntu作為解決方案
WSL + Docker Desktop 的使用教程參考 WSL Ubuntu + Docker Desktop搭建python環(huán)境
在完成好WSL和Docker Desktop安裝之后,WeNet環(huán)境配置步驟如下:
-
實例化anaconda容器
docker run -it --name="anaconda" -p 8888:8888 continuumio/anaconda3 /bin/bash
如果退出了,可以重啟anaconda容器
#重啟 docker start anaconda docker exec -it anaconda /bin/bash
-
在base環(huán)境下配置wenet環(huán)境(不要創(chuàng)建虛擬環(huán)境,方便之后打包成鏡像,供pycharm使用)
將WSL中的wenet項目拷貝到docker容器中(假設(shè)在WSL的
/home/usr
下有wenet項目)docker cp /home/usr/wenet/requirements.txt 9cf7b3c196f3:/home/ #9cf7b3c196f3為anaconda容器id
進入anaconda容器內(nèi),在
/home/
使用pip安裝所有包(conda源修改參考 ubuntu更換conda源 )pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple conda install pytorch=1.10.0 torchvision torchaudio=0.10.0 cudatoolkit=11.1 -c pytorch -c conda-forge
-
下載ctc_encoder項目(讓conformer進行語音識別時能夠使用beam_search方法)
ctc_encoder官網(wǎng)如下:https://github.com/Slyne/ctc_decoder.
由于
github clone
在ubuntu中可能不好使,所以在windows中進入swig/setup.sh
:#!/usr/bin/env bash if [ ! -d kenlm ]; then git clone https://github.com/kpu/kenlm.git echo -e "\n" fi if [ ! -d openfst-1.6.3 ]; then echo "Download and extract openfst ..." wget http://www.openfst.org/twiki/pub/FST/FstDownload/openfst-1.6.3.tar.gz --no-check-certificate tar -xzvf openfst-1.6.3.tar.gz echo -e "\n" fi if [ ! -d ThreadPool ]; then git clone https://github.com/progschj/ThreadPool.git echo -e "\n" fi echo "Install decoders ..." # python3 setup.py install --num_processes 10 python3 setup.py install --user --num_processes 10
將必要的包安裝好后(在
git bash
中使用setup.sh中的命令,wget,swig直接安裝exe即可),整體文件結(jié)構(gòu)如下(多了這四個文件):再把完整的ctc_encoder復(fù)制到anaconda容器中,直接進行編譯即可。
-
編譯ctc_encoder:
假設(shè)現(xiàn)在在anaconda容器中,ctc_encoder項目在
/home
目錄下,進入swig文件夾后,運行bash setup.sh
即可完成編譯(需要先apt install gcc
,apt install g++
) -
配置onnx,onnxruntime環(huán)境
pip install onnx==1.9.0 -i https://pypi.tuna.tsinghua.edu.cn/simple pip install onnxruntime==1.9.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
-
將Docker運行時容器打包成鏡像
將anaconda容器運行時的環(huán)境打包成鏡像,給pycharm專業(yè)版調(diào)用,參考Pycharm使用docker容器內(nèi)的環(huán)境開發(fā)
#OPTIONS說明: # -a :提交的鏡像作者; # -c :使用Dockerfile指令來創(chuàng)建鏡像; # -m :提交時的說明文字; # -p :在commit時,將容器暫停。 #2b1ad7022d19為anaconda容器運行時的id docker commit -a "wangxiaoxi" -m "wenet_env" 2b1ad7022d19 wenet_env:v1
2、模型訓(xùn)練
參考Tutorial on AIShell
3、基于libTorch模型的推理
下載aishell2 sample數(shù)據(jù)集進行wenet模型的推理,官網(wǎng)如下:希爾貝殼
下載WeNet的預(yù)訓(xùn)練模型(下載Checkpoint model - conformer)
將測試數(shù)據(jù)集和預(yù)訓(xùn)練模型放在項目路徑下,比如:
修改train.yaml
中的cmvn_file
的位置(如果使用docker容器中的python環(huán)境,建議使用相對路徑)
cmvn_file: ../../test/aishell2/global_cmvn #這里使用相對路徑
將aishell2數(shù)據(jù)集修改成wenet數(shù)據(jù)格式
{"key": "D4_753", "wav": "../../test/aishell2/test_data/D4_750.wav", "txt": ""}
{"key": "D4_754", "wav": "../../test/aishell2/test_data/D4_751.wav", "txt": ""}
{"key": "D4_755", "wav": "../../test/aishell2/test_data/D4_752.wav", "txt": ""}
{"key": "D4_753", "wav": "../../test/aishell2/test_data/D4_753.wav", "txt": ""}
{"key": "D4_754", "wav": "../../test/aishell2/test_data/D4_754.wav", "txt": ""}
{"key": "D4_755", "wav": "../../test/aishell2/test_data/D4_755.wav", "txt": ""}
{"key": "D4_756", "wav": "../../test/aishell2/test_data/D4_756.wav", "txt": ""}
使用wenet/bin/recognize.py,輸入如下命令
python recognize
--config=../../test/aishell2/train.yaml \
--dict=../../test/aishell2/units.txt \
--checkpoint=../../test/aishell2/final.pt \
--result_file=../../test/aishell2/att_res_result.txt \
--test_data=../../test/aishell2/test_data/data.list \
輸出結(jié)果如下:
Namespace(batch_size=16, beam_size=10, bpe_model=None, checkpoint='../../test/aishell2/final.pt', config='../../test/aishell2/train.yaml', connect_symbol='', ctc_weight=0.0, data_type='raw', decoding_chunk_size=-1, dict='../../test/aishell2/units.txt', gpu=-1, mode='attention', non_lang_syms=None, num_decoding_left_chunks=-1, override_config=[], penalty=0.0, result_file='../../test/aishell2/att_res_result.txt', reverse_weight=0.0, simulate_streaming=False, test_data='../../test/aishell2/test_data/data1.list')
2022-07-04 15:54:22,441 INFO Checkpoint: loading from checkpoint ../../test/aishell2/final.pt for CPU
F:\ASR\wenet\wenet\transformer\asr_model.py:266: UserWarning: __floordiv__ is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor').
best_hyps_index = best_k_index // beam_size
2022-07-04 15:54:27,189 INFO D4_753 中國人民保險集團股份有限公司閩寧營銷服務(wù)部
2022-07-04 15:54:27,189 INFO D4_755 中國電信閩寧鎮(zhèn)合作營業(yè)廳
2022-07-04 15:54:27,189 INFO D4_754 閩寧鎮(zhèn)衛(wèi)生院
2022-07-04 15:54:27,189 INFO D4_756 閩寧鎮(zhèn)客運站
2022-07-04 15:54:27,189 INFO D4_753 第六十一集
2022-07-04 15:54:27,189 INFO D4_755 第六十三集
2022-07-04 15:54:27,189 INFO D4_754 第六十二集
4、WeNet導(dǎo)出onnx模型
參考 ONNX backend on WeNet
這里先下載WeNet的預(yù)訓(xùn)練模型(下載Checkpoint model - conformer),接著使用wenet/bin/export_onnx_cpu.py
, 設(shè)置如下參數(shù),即可將libtorch的pt文件轉(zhuǎn)換成onnx文件
python export_onnx_cpu.py
--config F:/ASR/model/20210618_u2pp_conformer_libtorch_aishell2/train.yaml \
--checkpoint F:/ASR/model/20210618_u2pp_conformer_libtorch_aishell2/final.pt \
--chunk_size 16 \
--output_dir F:/ASR/model/20210618_u2pp_conformer_libtorch_aishell2/onnx_dir \
--num_decoding_left_chunks -1
如果onnx導(dǎo)出成功,會在輸出文件夾中生成如下3個文件:encoder.onnx,ctc.onnx, decoder.onnx
。
5、使用recognize_onnx
進行推理(未解決)
參考 https://github.com/wenet-e2e/wenet/pull/761.
先下載conformer模型的權(quán)重文件(checkpoint model),https://wenet.org.cn/wenet/pretrained_models.html
解壓好權(quán)重文件之后,文件夾目錄如下
修改train.yaml
中的cmvn_file
的位置
#cmvn_file: F:/ASR/model/20210618_u2pp_conformer_libtorch_aishell2/global_cmvn
cmvn_file: ../../test/aishell2/global_cmvn #這里使用相對路徑
轉(zhuǎn)換成wenet的json數(shù)據(jù)格式:假設(shè)現(xiàn)在有音頻文件D4_750.wav
,通過格式轉(zhuǎn)換成如下json格式, 參考https://wenet.org.cn/wenet/tutorial_librispeech.html?highlight=test_data#stage-0-prepare-training-data
{"key": "D4_753", "wav": "../../test/aishell2/test_data/D4_750.wav", "txt": "而對樓市成交抑制作用最大的限購"}
接著運行:
python3 wenet/bin/recognize_onnx.py --config=20210618_u2pp_conformer_exp/train.yaml --test_data=raw_wav/test/data.list --gpu=0 --dict=20210618_u2pp_conformer_exp/words.txt --mode=attention_rescoring --reverse_weight=0.4 --ctc_weight=0.1 --result_file=./att_res_result.txt --encoder_onnx=onnx_model/encoder.onnx --decoder_onnx=onnx_model/decoder.onnx
注意這里最好使用相對路徑,因為使用的是docker里的python環(huán)境,如果在讀取文件時使用windows下的絕對路徑,會導(dǎo)致如下錯誤。解決思路參考https://github.com/microsoft/onnxruntime/issues/8735(反正我解決不了)
{FileNotFoundError}[Errno 2] No such file or directory: 'F:/ASR/model/20210618_u2pp_conformer_libtorch_aishell2/train.yaml'
這里使用export_onnx_cpu
導(dǎo)出的onnx模型,使用recognize_onnx
進行推理
encoder_ort_session=onnxruntime.InferenceSession(encoder_outpath, providers=['CPUExecutionProvider']);
ort_inputs = {
encoder_ort_session.get_inputs()[0].name: feats.astype('float32'),
encoder_ort_session.get_inputs()[1].name: feats_lengths.astype('int64'),
encoder_ort_session.get_inputs()[2].name: np.zeros((12,4,0,128)).astype('float32'),
encoder_ort_session.get_inputs()[3].name: np.zeros((12,1,256,7)).astype('float32')
}
encoder_ort_session.run(None, ort_inputs)
會拋出錯誤
{Fail}[ONNXRuntimeError] : 1 : FAIL : Non-zero status code returned while running Slice node. Name:'Slice_49' Status Message: slice.cc:153 FillVectorsFromInput Starts must be a 1-D array
應(yīng)該是cuda和onnxruntime版本不一致導(dǎo)致的,參考 OnnxRunTime遇到FAIL : Non-zero status code returned while running BatchNormalization node.
后來發(fā)現(xiàn)recognize_onnx
是對export_onnx_gpu.py
導(dǎo)出的模型進行推理,而不是export_onnx_cpu.py
。要使用export_onnx_gpu.py
還得 安裝nividia_docker
和onnxruntime_gpu
,否則會報錯:文章來源:http://www.zghlxwxcb.cn/news/detail-458231.html
/opt/conda/lib/python3.9/site-packages/onnxruntime/capi/onnxruntime_inference_collection.py:53: UserWarning: Specified provider 'CUDAExecutionProvider' is not in available provider names.Available providers: 'CPUExecutionProvider'
warnings.warn("Specified provider '{}' is not in available provider names."
Traceback (most recent call last):
File "/opt/project/wenet/bin/export_onnx_gpu.py", line 574, in <module>
onnx_config = export_enc_func(model, configs, args, logger, encoder_onnx_path)
File "/opt/project/wenet/bin/export_onnx_gpu.py", line 334, in export_offline_encoder
test(to_numpy([o0, o1, o2, o3, o4]), ort_outs)
NameError: name 'test' is not defined
這里就不費這個力了,等wenet項目完善吧。文章來源地址http://www.zghlxwxcb.cn/news/detail-458231.html
到了這里,關(guān)于【語音識別】WeNet:面向工業(yè)落地的E2E語音識別工具的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!