🚀 基於微調Wav2Vec 2.0的語音情感識別
本模型是針對語音情感識別(SER)任務,對 jonatasgrosman/wav2vec2-large-xlsr-53-english 進行微調後的版本。它利用多個數據集進行微調訓練,能夠準確識別多種語音情感,為語音情感分析領域提供了有效的解決方案。
🚀 快速開始
安裝依賴
pip install transformers librosa torch
使用示例
from transformers import *
import librosa
import torch
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained("r-f/wav2vec-english-speech-emotion-recognition")
model = Wav2Vec2ForCTC.from_pretrained("r-f/wav2vec-english-speech-emotion-recognition")
def predict_emotion(audio_path):
audio, rate = librosa.load(audio_path, sr=16000)
inputs = feature_extractor(audio, sampling_rate=rate, return_tensors="pt", padding=True)
with torch.no_grad():
outputs = model(inputs.input_values)
predictions = torch.nn.functional.softmax(outputs.logits.mean(dim=1), dim=-1)
predicted_label = torch.argmax(predictions, dim=-1)
emotion = model.config.id2label[predicted_label.item()]
return emotion
emotion = predict_emotion("example_audio.wav")
print(f"Predicted emotion: {emotion}")
>> Predicted emotion: angry
✨ 主要特性
- 多數據集微調:使用多個數據集對原始模型進行微調,包括 Surrey Audio-Visual Expressed Emotion (SAVEE)、Ryerson Audio-Visual Database of Emotional Speech and Song (RAVDESS) 和 Toronto emotional speech set (TESS),增強了模型的泛化能力。
- 多情感分類:支持7種情感分類,分別是憤怒、厭惡、恐懼、快樂、中性、悲傷和驚訝。
- 高準確率:在評估集上取得了較高的準確率(0.97463)和較低的損失(0.104075)。
📦 安裝指南
pip install transformers librosa torch
💻 使用示例
基礎用法
from transformers import *
import librosa
import torch
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained("r-f/wav2vec-english-speech-emotion-recognition")
model = Wav2Vec2ForCTC.from_pretrained("r-f/wav2vec-english-speech-emotion-recognition")
def predict_emotion(audio_path):
audio, rate = librosa.load(audio_path, sr=16000)
inputs = feature_extractor(audio, sampling_rate=rate, return_tensors="pt", padding=True)
with torch.no_grad():
outputs = model(inputs.input_values)
predictions = torch.nn.functional.softmax(outputs.logits.mean(dim=1), dim=-1)
predicted_label = torch.argmax(predictions, dim=-1)
emotion = model.config.id2label[predicted_label.item()]
return emotion
emotion = predict_emotion("example_audio.wav")
print(f"Predicted emotion: {emotion}")
📚 詳細文檔
訓練過程
訓練超參數
訓練過程中使用了以下超參數:
- 學習率:0.0001
- 訓練批次大小:4
- 評估批次大小:4
- 評估步數:500
- 隨機種子:42
- 梯度累積步數:2
- 優化器:Adam(betas=(0.9, 0.999),epsilon=1e-08)
- 訓練輪數:4
- 最大步數:7500
- 保存步數:1500
訓練結果
步數 |
訓練損失 |
驗證損失 |
準確率 |
500 |
1.8124 |
1.365212 |
0.486258 |
1000 |
0.8872 |
0.773145 |
0.79704 |
1500 |
0.7035 |
0.574954 |
0.852008 |
2000 |
0.6879 |
1.286738 |
0.775899 |
2500 |
0.6498 |
0.697455 |
0.832981 |
3000 |
0.5696 |
0.33724 |
0.892178 |
3500 |
0.4218 |
0.307072 |
0.911205 |
4000 |
0.3088 |
0.374443 |
0.930233 |
4500 |
0.2688 |
0.260444 |
0.936575 |
5000 |
0.2973 |
0.302985 |
0.92389 |
5500 |
0.1765 |
0.165439 |
0.961945 |
6000 |
0.1475 |
0.170199 |
0.961945 |
6500 |
0.1274 |
0.15531 |
0.966173 |
7000 |
0.0699 |
0.103882 |
0.976744 |
7500 |
0.083 |
0.104075 |
0.97463 |
📄 許可證
本項目採用 Apache-2.0 許可證。