库名称: transformers
许可证: apache-2.0
基础模型: openai/whisper-large-v3
标签:
- 训练生成
指标:
- 准确率
- 精确率
- 召回率
- F1值
模型索引:
- 名称: speech-emotion-recognition-with-openai-whisper-large-v3
结果: []
🎧 基于Whisper的语音情感识别
本项目利用Whisper模型实现语音情感识别,目标是将音频分类为快乐、悲伤、惊讶等不同情感类别。
🗂 数据集
训练与评估数据来自多个数据集:
数据集包含标注情感的录音,情感分布如下:
情感 |
数量 |
悲伤 |
752 |
快乐 |
752 |
愤怒 |
752 |
中性 |
716 |
厌恶 |
652 |
恐惧 |
652 |
惊讶 |
652 |
平静 |
192 |
训练时因样本不足排除了"平静"类别。
🎤 预处理
- 音频加载:使用Librosa加载音频并转为numpy数组
- 特征提取:通过Whisper特征提取器标准化音频特征
🔧 模型
采用微调后的Whisper Large V3模型进行音频分类:
将情感标签映射为数字ID用于训练评估。
⚙️ 训练参数
- 学习率: 5e-05
- 训练批大小: 2
- 评估批大小: 2
- 随机种子: 42
- 梯度累积步数: 5
- 有效批大小: 10
- 优化器: Adam(beta=(0.9,0.999),epsilon=1e-08)
- 学习率调度器: 线性
- 预热比例: 0.1
- 训练轮次: 25
- 混合精度训练: 原生AMP
使用Wandb进行实验跟踪。
📊 评估指标
- 损失值: 0.5008
- 准确率: 91.99%
- 精确率: 92.30%
- 召回率: 91.99%
- F1值: 91.98%
高指标值表明模型能有效识别语音情感。
🧪 训练结果
完整结果见Wandb记录:
训练损失 |
轮次 |
步数 |
验证损失 |
准确率 |
精确率 |
召回率 |
F1值 |
0.4948 |
0.9995 |
394 |
0.4911 |
82.86% |
84.49% |
82.86% |
83.02% |
... |
... |
... |
... |
... |
... |
... |
... |
0.0026 |
10.9995 |
4336 |
0.8334 |
87.73% |
89.49% |
87.73% |
87.70% |
🚀 使用指南
from transformers import AutoModelForAudioClassification, AutoFeatureExtractor
import librosa
import torch
import numpy as np
model_id = "firdhokk/speech-emotion-recognition-with-openai-whisper-large-v3"
model = AutoModelForAudioClassification.from_pretrained(model_id)
feature_extractor = AutoFeatureExtractor.from_pretrained(model_id, do_normalize=True)
id2label = model.config.id2label
def preprocess_audio(audio_path, feature_extractor, max_duration=30.0):
audio_array, sampling_rate = librosa.load(audio_path, sr=feature_extractor.sampling_rate)
max_length = int(feature_extractor.sampling_rate * max_duration)
if len(audio_array) > max_length:
audio_array = audio_array[:max_length]
else:
audio_array = np.pad(audio_array, (0, max_length - len(audio_array)))
inputs = feature_extractor(
audio_array,
sampling_rate=feature_extractor.sampling_rate,
max_length=max_length,
truncation=True,
return_tensors="pt",
)
return inputs
def predict_emotion(audio_path, model, feature_extractor, id2label, max_duration=30.0):
inputs = preprocess_audio(audio_path, feature_extractor, max_duration)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
inputs = {key: value.to(device) for key, value in inputs.items()}
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predicted_id = torch.argmax(logits, dim=-1).item()
predicted_label = id2label[predicted_id]
return predicted_label
audio_path = "/content/drive/MyDrive/Audio/Speech_URDU/Happy/SM5_F4_H058.wav"
predicted_emotion = predict_emotion(audio_path, model, feature_extractor, id2label)
print(f"预测情感: {predicted_emotion}")
🎯 框架版本
- Transformers 4.44.2
- Pytorch 2.4.1+cu121
- Datasets 3.0.0
- Tokenizers 0.19.1