🚀 XLS - R Wav2Vec2 用於俄語語音情感分類
本項目提供了一個基於 XLS - R Wav2Vec2 的模型,可用於俄語語音的情感分類,能識別憤怒、厭惡、熱情等多種情感。
🚀 快速開始
準備與導入
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchaudio
from transformers import AutoConfig, AutoModel, Wav2Vec2FeatureExtractor
import librosa
import numpy as np
def speech_file_to_array_fn(path, sampling_rate):
speech_array, _sampling_rate = torchaudio.load(path)
resampler = torchaudio.transforms.Resample(_sampling_rate)
speech = resampler(speech_array).squeeze().numpy()
return speech
def predict(path, sampling_rate):
speech = speech_file_to_array_fn(path, sampling_rate)
inputs = feature_extractor(speech, sampling_rate=sampling_rate, return_tensors="pt", padding=True)
inputs = {key: inputs[key].to(device) for key in inputs}
with torch.no_grad():
logits = model_(**inputs).logits
scores = F.softmax(logits, dim=1).detach().cpu().numpy()[0]
outputs = [{"Emotion": config.id2label[i], "Score": f"{round(score * 100, 3):.1f}%"} for i, score in enumerate(scores)]
return outputs
模型加載
TRUST = True
config = AutoConfig.from_pretrained('Aniemore/wav2vec2-xlsr-53-russian-emotion-recognition', trust_remote_code=TRUST)
model_ = AutoModel.from_pretrained("Aniemore/wav2vec2-xlsr-53-russian-emotion-recognition", trust_remote_code=TRUST)
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained("Aniemore/wav2vec2-xlsr-53-russian-emotion-recognition")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model_.to(device)
💻 使用示例
基礎用法
result = predict("/path/to/russian_audio_speech.wav", 16000)
print(result)
輸出示例
[{'Emotion': 'anger', 'Score': '0.0%'},
{'Emotion': 'disgust', 'Score': '100.0%'},
{'Emotion': 'enthusiasm', 'Score': '0.0%'},
{'Emotion': 'fear', 'Score': '0.0%'},
{'Emotion': 'happiness', 'Score': '0.0%'},
{'Emotion': 'neutral', 'Score': '0.0%'},
{'Emotion': 'sadness', 'Score': '0.0%'}]
📚 詳細文檔
模型信息
屬性 |
詳情 |
模型類型 |
XLS - R Wav2Vec2 用於俄語語音情感分類 |
訓練數據 |
Aniemore/resd |
評估結果
情感類別 |
精確率 |
召回率 |
F1 - 分數 |
樣本數 |
憤怒 |
0.97 |
0.86 |
0.92 |
44 |
厭惡 |
0.71 |
0.78 |
0.74 |
37 |
熱情 |
0.51 |
0.80 |
0.62 |
40 |
恐懼 |
0.80 |
0.62 |
0.70 |
45 |
快樂 |
0.66 |
0.70 |
0.68 |
44 |
中立 |
0.81 |
0.66 |
0.72 |
38 |
悲傷 |
0.79 |
0.59 |
0.68 |
32 |
準確率 |
|
|
0.72 |
280 |
宏平均 |
0.75 |
0.72 |
0.72 |
280 |
加權平均 |
0.75 |
0.72 |
0.73 |
280 |
📄 許可證
本項目採用 MIT 許可證。
📖 引用
@misc{Aniemore,
author = {Артем Аментес, Илья Лубенец, Никита Давидчук},
title = {Открытая библиотека искусственного интеллекта для анализа и выявления эмоциональных оттенков речи человека},
year = {2022},
publisher = {Hugging Face},
journal = {Hugging Face Hub},
howpublished = {\url{https://huggingface.com/aniemore/Aniemore}},
email = {hello@socialcode.ru}
}