Create README.md
Browse files
README.md
CHANGED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
language: fa
|
3 |
+
datasets:
|
4 |
+
- ShEMO
|
5 |
+
tags:
|
6 |
+
- audio
|
7 |
+
- automatic-speech-recognition
|
8 |
+
- speech
|
9 |
+
- speech-emotion-recognition
|
10 |
+
license: apache-2.0
|
11 |
+
---
|
12 |
+
|
13 |
+
# Emotion Recognition in Persian (Farsi - fa) Speech using Wav2Vec 2.0
|
14 |
+
|
15 |
+
|
16 |
+
## How to use
|
17 |
+
|
18 |
+
### Requirements
|
19 |
+
|
20 |
+
```bash
|
21 |
+
# requirement packages
|
22 |
+
!pip install git+https://github.com/huggingface/datasets.git
|
23 |
+
!pip install git+https://github.com/huggingface/transformers.git
|
24 |
+
!pip install torchaudio
|
25 |
+
!pip install librosa
|
26 |
+
```
|
27 |
+
|
28 |
+
### Prediction
|
29 |
+
|
30 |
+
```python
|
31 |
+
import torch
|
32 |
+
import torch.nn as nn
|
33 |
+
import torch.nn.functional as F
|
34 |
+
import torchaudio
|
35 |
+
from transformers import AutoConfig, Wav2Vec2FeatureExtractor
|
36 |
+
|
37 |
+
import librosa
|
38 |
+
import IPython.display as ipd
|
39 |
+
import numpy as np
|
40 |
+
import pandas as pd
|
41 |
+
```
|
42 |
+
|
43 |
+
```python
|
44 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
45 |
+
model_name_or_path = "m3hrdadfi/wav2vec2-xlsr-persian-speech-emotion-recognition"
|
46 |
+
config = AutoConfig.from_pretrained(model_name_or_path)
|
47 |
+
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(model_name_or_path)
|
48 |
+
sampling_rate = feature_extractor.sampling_rate
|
49 |
+
model = Wav2Vec2ForSpeechClassification.from_pretrained(model_name_or_path).to(device)
|
50 |
+
```
|
51 |
+
|
52 |
+
```python
|
53 |
+
def speech_file_to_array_fn(path, sampling_rate):
|
54 |
+
speech_array, _sampling_rate = torchaudio.load(path)
|
55 |
+
resampler = torchaudio.transforms.Resample(_sampling_rate)
|
56 |
+
speech = resampler(speech_array).squeeze().numpy()
|
57 |
+
return speech
|
58 |
+
|
59 |
+
|
60 |
+
def predict(path, sampling_rate):
|
61 |
+
speech = speech_file_to_array_fn(path, sampling_rate)
|
62 |
+
inputs = feature_extractor(speech, sampling_rate=sampling_rate, return_tensors="pt", padding=True)
|
63 |
+
inputs = {key: inputs[key].to(device) for key in inputs}
|
64 |
+
|
65 |
+
with torch.no_grad():
|
66 |
+
logits = model(**inputs).logits
|
67 |
+
|
68 |
+
scores = F.softmax(logits, dim=1).detach().cpu().numpy()[0]
|
69 |
+
outputs = [{"Emotion": config.id2label[i], "Score": f"{round(score * 100, 3):.1f}%"} for i, score in enumerate(scores)]
|
70 |
+
return outputs
|
71 |
+
```
|
72 |
+
|
73 |
+
```python
|
74 |
+
path = "/path/to/sadness.wav"
|
75 |
+
outputs = predict(path, sampling_rate)
|
76 |
+
```
|
77 |
+
|
78 |
+
```bash
|
79 |
+
[
|
80 |
+
{'Label': 'Anger', 'Score': '0.0%'},
|
81 |
+
{'Label': 'Fear', 'Score': '0.0%'},
|
82 |
+
{'Label': 'Happiness', 'Score': '0.0%'},
|
83 |
+
{'Label': 'Neutral', 'Score': '0.0%'},
|
84 |
+
{'Label': 'Sadness', 'Score': '99.9%'},
|
85 |
+
{'Label': 'Surprise', 'Score': '0.0%'}
|
86 |
+
]
|
87 |
+
```
|
88 |
+
|
89 |
+
|
90 |
+
## Evaluation
|
91 |
+
The following tables summarize the scores obtained by model overall and per each class.
|
92 |
+
|
93 |
+
|
94 |
+
| Emotions | precision | recall | f1-score | accuracy |
|
95 |
+
|:---------:|:---------:|:------:|:--------:|:--------:|
|
96 |
+
| Anger | 0.95 | 0.95 | 0.95 | |
|
97 |
+
| Fear | 0.33 | 0.17 | 0.22 | |
|
98 |
+
| Happiness | 0.69 | 0.69 | 0.69 | |
|
99 |
+
| Neutral | 0.91 | 0.94 | 0.93 | |
|
100 |
+
| Sadness | 0.92 | 0.85 | 0.88 | |
|
101 |
+
| Surprise | 0.81 | 0.88 | 0.84 | |
|
102 |
+
| | | | Overal | 0.90 |
|
103 |
+
|
104 |
+
|
105 |
+
## Questions?
|
106 |
+
Post a Github issue from [HERE](https://github.com/m3hrdadfi/soxan/issues).
|