🚀 基於IEMOCAP的wav2vec2情感識別
本倉庫提供了使用SpeechBrain微調wav2vec2(基礎)模型進行情感識別所需的所有工具。該模型在IEMOCAP訓練數據上進行訓練。
為了獲得更好的體驗,我們建議您進一步瞭解 SpeechBrain。該模型在IEMOCAP測試集上的性能如下:
版本發佈日期 |
準確率(%) |
21年10月19日 |
78.7(平均:75.3) |
🚀 快速開始
本倉庫提供了使用微調後的wav2vec2(基礎)模型和SpeechBrain進行情感識別的工具,模型在IEMOCAP訓練數據上進行了訓練。你可以按照後續步驟進行安裝和使用。
✨ 主要特性
- 模型架構:系統由wav2vec2模型組成,結合了卷積和殘差塊,使用注意力統計池化提取嵌入,並通過加性邊際軟最大損失進行訓練。
- 音頻處理:代碼在調用
classify_file
時會自動對音頻進行歸一化處理(重採樣和單聲道選擇)。
- 多後端支持:支持使用OpenVINO進行加速推理,也可在GPU上進行推理。
📦 安裝指南
安裝SpeechBrain
首先,請使用以下命令安裝SpeechBrain的開發版本:
pip install git+https://github.com/speechbrain/speechbrain.git@$develop
請注意,我們建議您閱讀我們的教程,進一步瞭解 SpeechBrain。
💻 使用示例
基礎用法
使用外部py_module_file=custom.py
作為外部預測器類,使用speechbrain.pretrained.interfaces
中的foreign_class
函數加載自定義模型:
from speechbrain.inference.interfaces import foreign_class
classifier = foreign_class(source="speechbrain/emotion-recognition-wav2vec2-IEMOCAP", pymodule_file="custom_interface.py", classname="CustomEncoderWav2vec2Classifier")
out_prob, score, index, text_lab = classifier.classify_file("speechbrain/emotion-recognition-wav2vec2-IEMOCAP/anger.wav")
print(text_lab)
預測張量將包含一個元組(嵌入、類別ID、標籤名稱)。
高級用法
使用OpenVINO進行情感識別
使用Intel OpenVINO運行時作為推理“後端”,可以在多種英特爾硬件和環境中進行部署。在custom_interface.py
的CustomEncoderWav2vec2Classifier
類中傳遞backend="openvino"
,支持使用英特爾OpenVINO後端在目標設備上進行模型推理。
步驟1:安裝SpeechBrain和OpenVINO
pip install git+https://github.com/speechbrain/speechbrain.git@develop --extra-index-url https://download.pytorch.org/whl/cpu
pip install "openvino>=2024.1.0"
pip install "transformers>=4.30.0"
步驟2:使用OpenVINO後端運行推理
以下是一個示例腳本(app.py
),展示瞭如何設置和運行模型推理:
config = {hints.performance_mode: hints.PerformanceMode.THROUGHPUT}
ov_opts = {"ov_device": "CPU", "config": config}
torch_device = "cpu"
instance = CustomEncoderWav2vec2Classifier(modules=checkpoint.mods,
hparams=hparams_dict, model=classifier.mods["wav2vec2"].model,
audio_file_path="speechbrain/emotion-recognition-wav2vec2-IEMOCAP/anger.wav",
backend="openvino",
opts=ov_opts,
torch_device=torch_device,
save_ov_model=False)
要執行該應用程序,只需運行:
python app.py
有關使用OpenVINO優化推理的更多詳細信息,請參考 OpenVINO優化指南。
在GPU上進行推理
在調用from_hparams
方法時添加run_opts={"device":"cuda"}
即可在GPU上進行推理。
📚 詳細文檔
管道描述
該系統由wav2vec2模型組成,它是卷積塊和殘差塊的組合。使用注意力統計池化提取嵌入,系統通過加性邊際軟最大損失進行訓練。說話人驗證使用說話人嵌入之間的餘弦距離進行。
系統使用採樣率為16kHz(單聲道)的錄音進行訓練。如果需要,代碼在調用classify_file
時會自動對音頻進行歸一化處理(重採樣和單聲道選擇)。
訓練
該模型使用SpeechBrain(aa018540)進行訓練。要從頭開始訓練,請按照以下步驟操作:
- 克隆SpeechBrain:
git clone https://github.com/speechbrain/speechbrain/
- 安裝:
cd speechbrain
pip install -r requirements.txt
pip install -e .
- 運行訓練:
cd recipes/IEMOCAP/emotion_recognition
python train_with_wav2vec2.py hparams/train_with_wav2vec2.yaml --data_folder=your_data_folder
您可以在 這裡 找到我們的訓練結果(模型、日誌等)。
侷限性
SpeechBrain團隊不對該模型在其他數據集上的性能提供任何保證。
🔧 技術細節
本系統使用的wav2vec2模型結合了卷積和殘差塊,通過注意力統計池化提取嵌入,並使用加性邊際軟最大損失進行訓練。說話人驗證基於餘弦距離。系統在16kHz單聲道音頻上進行訓練,代碼會自動處理音頻歸一化。
📄 許可證
本項目採用Apache 2.0許可證。
引用SpeechBrain
如果您在研究或業務中使用了SpeechBrain,請引用以下內容:
@misc{speechbrain,
title={{SpeechBrain}: A General-Purpose Speech Toolkit},
author={Mirco Ravanelli and Titouan Parcollet and Peter Plantinga and Aku Rouhe and Samuele Cornell and Loren Lugosch and Cem Subakan and Nauman Dawalatabad and Abdelwahab Heba and Jianyuan Zhong and Ju-Chieh Chou and Sung-Lin Yeh and Szu-Wei Fu and Chien-Feng Liao and Elena Rastorgueva and François Grondin and William Aris and Hwidong Na and Yan Gao and Renato De Mori and Yoshua Bengio},
year={2021},
eprint={2106.04624},
archivePrefix={arXiv},
primaryClass={eess.AS},
note={arXiv:2106.04624}
}
關於SpeechBrain
- 網站:https://speechbrain.github.io/
- 代碼倉庫:https://github.com/speechbrain/speechbrain/
- HuggingFace:https://huggingface.co/speechbrain/