🚀 基于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/