前言
OpenAI在開源了號稱其英文語音辨識能力已達到人類水準的Whisper項目,且它亦支持其它98種語言的自動語音辨識。Whisper所提供的自動語音識與翻譯任務(wù),它們能將各種語言的語音變成文本,也能將這些文本翻譯成英文。本項目主要的目的是為了對Whisper模型使用Lora進行微調(diào),目前開源了好幾個模型,具體可以在openai查看,下面列出了常用的幾個模型。另外項目最后還對語音識別加速推理,使用了CTranslate2加速推理,提示一下,加速推理支持直接使用Whisper原模型轉(zhuǎn)換,并不一定需要微調(diào)。
- openai/whisper-tiny
- openai/whisper-base
- openai/whisper-small
- openai/whisper-medium
- openai/whisper-large
- openai/whisper-large-v2
源碼地址:Whisper-Finetune
使用環(huán)境:
- Anaconda 3
- Python 3.8
- Pytorch 1.13.1
- Ubuntu 18.04
- GPU A100-PCIE-40GB*1
項目主要程序介紹
-
aishell.py
:制作AIShell訓(xùn)練數(shù)據(jù)。 -
finetune.py
:微調(diào)模型。 -
merge_lora.py
:合并Whisper和Lora的模型。 -
evaluation.py
:評估使用微調(diào)后的模型或者Whisper原模型。 -
infer_tfs.py
:使用transformers直接調(diào)用微調(diào)后的模型或者Whisper原模型預(yù)測,只適合推理短音頻。 -
infer_ct2.py
:使用轉(zhuǎn)換為CTranslate2的模型預(yù)測,主要參考這個程序用法。 -
infer_gui.py
:有GUI界面操作,使用轉(zhuǎn)換為CTranslate2的模型預(yù)測。 -
infer_server.py
:使用轉(zhuǎn)換為CTranslate2的模型部署到服務(wù)器端,提供給客戶端調(diào)用。
歡迎大家掃碼入知識星球討論,知識星球里面提供項目的模型文件和博主其他相關(guān)項目的模型文件,也包括其他一些資源。

- 原始模型字錯率測試表。
使用模型 | 指定語言 | aishell_test | test_net | test_meeting | 模型獲取 |
---|---|---|---|---|---|
whisper-tiny | Chinese | 0.31898 | 0.40482 | 0.75332 | 加入知識星球獲取 |
whisper-base | Chinese | 0.22196 | 0.30404 | 0.50378 | 加入知識星球獲取 |
whisper-small | Chinese | 0.13897 | 0.18417 | 0.31154 | 加入知識星球獲取 |
whisper-medium | Chinese | 0.09538 | 0.13591 | 0.26669 | 加入知識星球獲取 |
whisper-large | Chinese | 0.08969 | 0.12933 | 0.23439 | 加入知識星球獲取 |
whisper-large-v2 | Chinese | 0.08817 | 0.12332 | 0.26547 | 加入知識星球獲取 |
- 微調(diào)數(shù)據(jù)集后字錯率測試表。
使用模型 | 指定語言 | 數(shù)據(jù)集 | aishell_test | test_net | test_meeting | 模型獲取 |
---|---|---|---|---|---|---|
whisper-tiny | Chinese | AIShell | 0.13043 | 0.4463 | 0.57728 | 加入知識星球獲取 |
whisper-base | Chinese | AIShell | 0.08999 | 0.33089 | 0.40713 | 加入知識星球獲取 |
whisper-small | Chinese | AIShell | 0.05452 | 0.19831 | 0.24229 | 加入知識星球獲取 |
whisper-medium | Chinese | AIShell | 0.03681 | 0.13073 | 0.16939 | 加入知識星球獲取 |
whisper-large-v2 | Chinese | AIShell | 0.03139 | 0.12201 | 0.15776 | 加入知識星球獲取 |
whisper-tiny | Chinese | WenetSpeech | 0.17711 | 0.24783 | 0.39226 | 加入知識星球獲取 |
whisper-large-v2 | Chinese | WenetSpeech | 0.05443 | 0.10087 | 0.19087 | 加入知識星球獲取 |
- 未加速和加速后的推理速度測試表,使用GPU為GTX3090(24G)。
使用模型 | 原生模型實時率(float16) | 轉(zhuǎn)換CTranslate2加速后實時率(float16) | 轉(zhuǎn)換CTranslate2加速后實時率(int8_float16) |
---|---|---|---|
whisper-tiny | 0.03 | 0.06 | 0.06 |
whisper-base | 0.04 | 0.06 | 0.06 |
whisper-small | 0.08 | 0.08 | 0.08 |
whisper-medium | 0.13 | 0.10 | 0.10 |
whisper-large-v2 | 0.19 | 0.12 | 0.12 |
- 經(jīng)過處理的數(shù)據(jù)列表。
數(shù)據(jù)列表處理方式 | AiShell | WenetSpeech |
---|---|---|
添加標點符號 | 加入知識星球獲取 | 加入知識星球獲取 |
添加標點符號和時間戳 | 加入知識星球獲取 | 加入知識星球獲取 |
重要說明:
- 在評估的時候移除模型輸出的標點符號,并把繁體中文轉(zhuǎn)成簡體中文。
-
aishell_test
為AIShell的測試集,test_net
和test_meeting
為WenetSpeech的測試集。 - RTF= 所有音頻總時間(單位秒) / ASR識別所有音頻處理時間(單位秒)。
- 測試速度的音頻為
dataset/test.wav
,時長為8秒。 - 訓(xùn)練數(shù)據(jù)使用的是帶標點符號的數(shù)據(jù),字錯率高一點。
- 微調(diào)AiShell數(shù)據(jù)不帶時間戳,微調(diào)WenetSpeech帶時間戳。
安裝環(huán)境
- 首先安裝的是Pytorch的GPU版本,如果已經(jīng)安裝過了,請?zhí)^。
conda install pytorch==1.13.1 torchvision==0.14.1 torchaudio==0.13.1 pytorch-cuda=11.6 -c pytorch -c nvidia
- 安裝所需的依賴庫。
python -m pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
準備數(shù)據(jù)
訓(xùn)練的數(shù)據(jù)集如下,是一個jsonlines的數(shù)據(jù)列表,也就是每一行都是一個JSON數(shù)據(jù),數(shù)據(jù)格式如下。Whisper是支持有標點符號的,所以訓(xùn)練的數(shù)據(jù)集中可以帶有標點符號。本項目提供了一個制作AIShell數(shù)據(jù)集的程序aishell.py
,執(zhí)行這個程序可以自動下載并生成如下列格式的訓(xùn)練集和測試集,注意: 這個程序可以通過指定AIShell的壓縮文件來跳過下載過程的,如果直接下載會非常慢,可以使用一些如迅雷等下載器下載該數(shù)據(jù)集,然后通過參數(shù)--filepath
指定下載的壓縮文件路徑,如/home/test/data_aishell.tgz
。如果不使用時間戳訓(xùn)練,可以不包含sentences
部分的數(shù)據(jù)。
{
"audio": {
"path": "dataset/0.wav"
},
"sentence": "近幾年,不但我用書給女兒壓歲,也勸說親朋不要給女兒壓歲錢,而改送壓歲書。",
"sentences": [
{
"start": 0,
"end": 1.4,
"text": "近幾年,"
},
{
"start": 1.42,
"end": 8.4,
"text": "不但我用書給女兒壓歲,也勸說親朋不要給女兒壓歲錢,而改送壓歲書。"
}
],
"duration": 7.37
}
微調(diào)模型
準備好數(shù)據(jù)之后,就可以開始微調(diào)模型了。訓(xùn)練最重要的兩個參數(shù)分別是,--base_model
指定微調(diào)的Whisper模型,這個參數(shù)值需要在HuggingFace存在的,這個不需要提前下載,啟動訓(xùn)練時可以自動下載,當然也可以提前下載,那么--base_model
指定就是路徑,同時--local_files_only
設(shè)置為True。第二個--output_path
是是訓(xùn)練時保存的Lora檢查點路徑,因為我們使用Lora來微調(diào)模型。如果想存足夠的話,最好將--use_8bit
設(shè)置為False,這樣訓(xùn)練速度快很多。其他更多的參數(shù)請查看這個程序。
單卡訓(xùn)練
單卡訓(xùn)練命令如下,Windows系統(tǒng)可以不添加CUDA_VISIBLE_DEVICES
參數(shù)。
CUDA_VISIBLE_DEVICES=0 python finetune.py --base_model=openai/whisper-tiny --output_dir=output/
多卡訓(xùn)練
多卡訓(xùn)練有兩種方法,分別是torchrun和accelerate,開發(fā)者可以根據(jù)自己的習(xí)慣使用對應(yīng)的方式。
- 使用torchrun啟動多卡訓(xùn)練,命令如下,通過
--nproc_per_node
指定使用的顯卡數(shù)量。
torchrun --nproc_per_node=2 finetune.py --base_model=openai/whisper-tiny --output_dir=output/
- 使用accelerate啟動多卡訓(xùn)練,如果是第一次使用accelerate,要配置訓(xùn)練參數(shù),方式如下。
首先配置訓(xùn)練參數(shù),過程是讓開發(fā)者回答幾個問題,基本都是默認就可以,但有幾個參數(shù)需要看實際情況設(shè)置。
accelerate config
大概過程就是這樣:
----------------------------------In which compute environment are you running?
This machine
----------------------------------Which type of machine are you using?
multi-GPU
How many different machines will you use (use more than 1 for multi-node training)? [1]:
Do you wish to optimize your script with torch dynamo?[yes/NO]:
Do you want to use DeepSpeed? [yes/NO]:
Do you want to use FullyShardedDataParallel? [yes/NO]:
Do you want to use Megatron-LM ? [yes/NO]:
How many GPU(s) should be used for distributed training? [1]:2
What GPU(s) (by id) should be used for training on this machine as a comma-seperated list? [all]:
----------------------------------Do you wish to use FP16 or BF16 (mixed precision)?
fp16
配置完成之后,可以使用以下命令查看配置。
accelerate env
開始訓(xùn)練命令如下。
accelerate launch finetune.py --base_model=openai/whisper-tiny --output_dir=output/
輸出日志如下:
{'loss': 0.9098, 'learning_rate': 0.000999046843662503, 'epoch': 0.01}
{'loss': 0.5898, 'learning_rate': 0.0009970611012927184, 'epoch': 0.01}
{'loss': 0.5583, 'learning_rate': 0.0009950753589229333, 'epoch': 0.02}
{'loss': 0.5469, 'learning_rate': 0.0009930896165531485, 'epoch': 0.02}
{'loss': 0.5959, 'learning_rate': 0.0009911038741833634, 'epoch': 0.03}
合并模型
微調(diào)完成之后會有兩個模型,第一個是Whisper基礎(chǔ)模型,第二個是Lora模型,需要把這兩個模型合并之后才能之后的操作。這個程序只需要傳遞兩個參數(shù),--lora_model
指定的是訓(xùn)練結(jié)束后保存的Lora模型路徑,注意如何不是最后的checkpoint-final
后面還有adapter_model
文件夾,第二個--output_dir
是合并后模型的保存目錄。
python merge_lora.py --lora_model=output/checkpoint-final --output_dir=models/
評估模型
執(zhí)行以下程序進行評估模型,最重要的兩個參數(shù)分別是。第一個--model_path
指定的是合并后的模型路徑,同時也支持直接使用Whisper原模型,例如直接指定openai/whisper-large-v2
,第二個是--metric
指定的是評估方法,例如有字錯率cer
和詞錯率wer
。提示: 沒有微調(diào)的模型,可能輸出帶有標點符號,影響準確率。其他更多的參數(shù)請查看這個程序。
python evaluation.py --model_path=models/whisper-tiny-finetune --metric=cer
預(yù)測
執(zhí)行以下程序進行語音識別,這個使用transformers直接調(diào)用微調(diào)后的模型或者Whisper原模型預(yù)測,只適合推理短音頻,長語音還是參考infer_ct2.py
的使用方式。第一個--audio_path
參數(shù)指定的是要預(yù)測的音頻路徑。第二個--model_path
指定的是合并后的模型路徑,同時也支持直接使用Whisper原模型,例如直接指定openai/whisper-large-v2
。其他更多的參數(shù)請查看這個程序。
python infer_tfs.py --audio_path=dataset/test.wav --model_path=models/whisper-tiny-finetune
加速預(yù)測
眾所周知,直接使用Whisper模型推理是比較慢的,所以這里提供了一個加速的方式,主要是使用了CTranslate2進行加速,首先要轉(zhuǎn)換模型,把合并后的模型轉(zhuǎn)換為CTranslate2模型。如下命令,--model
參數(shù)指定的是合并后的模型路徑,同時也支持直接使用Whisper原模型,例如直接指定openai/whisper-large-v2
。--output_dir
參數(shù)指定的是轉(zhuǎn)換后的CTranslate2模型路徑,--quantization
參數(shù)指定的是量化模型大小,不希望量化模型的可以直接去掉這個參數(shù)。
ct2-transformers-converter --model models/whisper-tiny-finetune --output_dir models/whisper-tiny-ct2 --copy_files tokenizer.json --quantization float16
執(zhí)行以下程序進行加速語音識別,--audio_path
參數(shù)指定的是要預(yù)測的音頻路徑。--model_path
指定的是轉(zhuǎn)換后的CTranslate2模型。其他更多的參數(shù)請查看這個程序。
python infer_ct2.py --audio_path=dataset/test.wav --model_path=models/whisper-tiny-ct2
輸出結(jié)果如下:
{
"language": "zh",
"duration": 8.39,
"results": [
{
"start": 0.0,
"end": 8.39,
"text": "近幾年不但我用書給女兒壓歲也勸說親朋友不要給女兒壓歲錢而改送壓歲書"
}
],
"text": "近幾年不但我用書給女兒壓歲也勸說親朋友不要給女兒壓歲錢而改送壓歲書"
}
GUI界面預(yù)測
這里同樣是使用了CTranslate2進行加速,轉(zhuǎn)換模型方式看上面文檔。--model_path
指定的是轉(zhuǎn)換后的CTranslate2模型。其他更多的參數(shù)請查看這個程序。
python infer_gui.py --model_path=models/whisper-tiny-ct2
啟動后界面如下:
Web部署
Web部署同樣是使用了CTranslate2進行加速,轉(zhuǎn)換模型方式看上面文檔。--host
指定服務(wù)啟動的地址,這里設(shè)置為0.0.0.0
,即任何地址都可以訪問。--port
指定使用的端口號。--model_path
指定的是轉(zhuǎn)換后的CTranslate2模型。--num_workers
指定是使用多少個線程并發(fā)推理,這在Web部署上很重要,當有多個并發(fā)訪問是可以同時推理。其他更多的參數(shù)請查看這個程序。
python infer_server.py --host=0.0.0.0 --port=5000 --model_path=models/whisper-tiny-ct2 --num_workers=2
接口文檔
目前提供兩個接口,普通的識別接口/recognition
和流式返回結(jié)果/recognition_stream
,注意這個流式是指流式返回識別結(jié)果,同樣是上傳完整的音頻,然后流式返回識別結(jié)果,這種方式針對長語音識別體驗非常好。他們的文檔接口是完全一致的,接口參數(shù)如下。
字段 | 是否必須 | 類型 | 默認值 | 說明 |
---|---|---|---|---|
audio | 是 | File | 要識別的音頻文件 | |
to_simple | 否 | int | 1 | 是否繁體轉(zhuǎn)簡體 |
remove_pun | 否 | int | 0 | 是否移除標點符號 |
task | 否 | String | transcribe | 識別任務(wù)類型,支持transcribe和translate |
為了方便理解,這里提供了調(diào)用Web接口的Python代碼,下面的是/recognition
的調(diào)用方式。
import requests
response = requests.post(url="http://127.0.0.1:5000/recognition",
files=[("audio", ("test.wav", open("dataset/test.wav", 'rb'), 'audio/wav'))],
json={"to_simple": 1, "remove_pun": 0, "task": "transcribe"}, timeout=20)
print(response.text)
下面的是/recognition_stream
的調(diào)用方式。
import json
import requests
response = requests.post(url="http://127.0.0.1:5000/recognition_stream",
files=[("audio", ("test.wav", open("dataset/test_long.wav", 'rb'), 'audio/wav'))],
json={"to_simple": 1, "remove_pun": 0, "task": "transcribe"}, stream=True, timeout=20)
for chunk in response.iter_lines(decode_unicode=False, delimiter=b"\0"):
if chunk:
result = json.loads(chunk.decode())
text = result["result"]
start = result["start"]
end = result["end"]
print(f"[{start} - {end}]:{text}")
提供的測試頁面如下:
首頁http://127.0.0.1:5000/
的頁面如下:
文檔頁面http://127.0.0.1:5000/docs
的頁面如下:
Android部署
安裝部署的源碼在AndroidDemo目錄下。
轉(zhuǎn)換模型
- 首先要克隆Whisper原生的源碼,因為需要它的一些文件,請在
Whisper-Finetune
項目根目錄下執(zhí)行下面命令。
git clone https://github.com/openai/whisper.git
- 然后開始轉(zhuǎn)換模型,請在
Whisper-Finetune
項目根目錄下執(zhí)行convert-ggml.py
程序,把模型轉(zhuǎn)換為Android項目所需的ggml格式的模型,需要轉(zhuǎn)換的模型可以是原始的Transformers模型,也可以是微調(diào)的模型。
python convert-ggml.py --model_dir=models/whisper-tiny-finetune/ --whisper_dir=whisper/ --output_path=models/ggml-model.bin
- 把模型放在Android項目的
app/src/main/assets/models
目錄下,然后就可以使用Android Studio打開項目了。
編譯說明
- 默認使用的NDK版本是
25.2.9519653
,如果下面修改其他版本,要修改app/build.gradle
里面的配置。 - 注意,在真正使用時,一定要發(fā)布
release
的APK包,這樣推理速度才快。 - 本項目已經(jīng)發(fā)布了
release
的APK包,請在Whisper-Finetune
項目主頁的最后掃碼下載。
效果圖




Windows桌面應(yīng)用
程序在WhisperDesktop目錄下。該程序是使用Whisper翻譯得的,源碼可以前面該項目查看。該程序使用的模型格式是GGML格式,跟Android部署的一樣,所以需要轉(zhuǎn)換模型格式才能使用。文章來源:http://www.zghlxwxcb.cn/news/detail-468678.html
轉(zhuǎn)換模型
- 首先要克隆Whisper原生的源碼,因為需要它的一些文件,請在
Whisper-Finetune
項目根目錄下執(zhí)行下面命令。
git clone https://github.com/openai/whisper.git
- 然后開始轉(zhuǎn)換模型,請在
Whisper-Finetune
項目根目錄下執(zhí)行convert-ggml.py
程序,把模型轉(zhuǎn)換為Android項目所需的ggml格式的模型,需要轉(zhuǎn)換的模型可以是原始的Transformers模型,也可以是微調(diào)的模型。
python convert-ggml.py --model_dir=models/whisper-tiny-finetune/ --whisper_dir=whisper/ --output_path=models/whisper-tiny-finetune-ggml.bin
效果圖
效果圖如下:
文章來源地址http://www.zghlxwxcb.cn/news/detail-468678.html

圖1:加載模型頁面

圖2:選擇音頻文件轉(zhuǎn)錄

圖3:錄音轉(zhuǎn)錄
參考資料
- https://github.com/huggingface/peft
- https://github.com/guillaumekln/faster-whisper
- https://github.com/ggerganov/whisper.cpp
- https://github.com/Const-me/Whisper
到了這里,關(guān)于微調(diào)Whisper語音識別模型和加速推理的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!