Update README.md
Browse files
README.md
CHANGED
@@ -14,22 +14,93 @@ license: apache-2.0
|
|
14 |
|
15 |
|
16 |
## How to use
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
|
20 |
## Evaluation
|
21 |
The following tables summarize the scores obtained by model overall and per each class.
|
22 |
|
23 |
|
24 |
-
|
|
25 |
-
|
26 |
-
|
|
27 |
-
|
|
28 |
-
|
|
29 |
-
| happiness
|
30 |
-
|
|
31 |
-
|
|
32 |
|
33 |
|
34 |
## Questions?
|
35 |
-
Post a Github issue from [HERE](https://github.com/m3hrdadfi/
|
|
|
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, Wav2Vec2Processor
|
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-greek-speech-emotion-recognition"
|
46 |
+
config = AutoConfig.from_pretrained(model_name_or_path)
|
47 |
+
processor = Wav2Vec2Processor.from_pretrained(model_name_or_path)
|
48 |
+
sampling_rate = processor.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 |
+
features = processor(speech, sampling_rate=sampling_rate, return_tensors="pt", padding=True)
|
63 |
+
|
64 |
+
input_values = features.input_values.to(device)
|
65 |
+
attention_mask = features.attention_mask.to(device)
|
66 |
+
|
67 |
+
with torch.no_grad():
|
68 |
+
logits = model(input_values, attention_mask=attention_mask).logits
|
69 |
+
|
70 |
+
scores = F.softmax(logits, dim=1).detach().cpu().numpy()[0]
|
71 |
+
outputs = [{"Emotion": config.id2label[i], "Score": f"{round(score * 100, 3):.1f}%"} for i, score in enumerate(scores)]
|
72 |
+
return outputs
|
73 |
+
```
|
74 |
+
|
75 |
+
```python
|
76 |
+
path = "/path/to/audio.wav"
|
77 |
+
outputs = predict(path, sampling_rate)
|
78 |
+
```
|
79 |
+
|
80 |
+
```bash
|
81 |
+
[
|
82 |
+
{'Emotion': 'anger', 'Score': '0.0%'},
|
83 |
+
{'Emotion': 'disgust', 'Score': '99.2%'},
|
84 |
+
{'Emotion': 'fear', 'Score': '0.1%'},
|
85 |
+
{'Emotion': 'happiness', 'Score': '0.3%'},
|
86 |
+
{'Emotion': 'sadness', 'Score': '0.5%'}
|
87 |
+
]
|
88 |
+
```
|
89 |
|
90 |
|
91 |
## Evaluation
|
92 |
The following tables summarize the scores obtained by model overall and per each class.
|
93 |
|
94 |
|
95 |
+
| Emotions | precision | recall | f1-score | accuracy |
|
96 |
+
|-----------|-----------|--------|----------|----------|
|
97 |
+
| anger | 0.92 | 1.00 | 0.96 | |
|
98 |
+
| disgust | 0.85 | 0.96 | 0.90 | |
|
99 |
+
| fear | 0.88 | 0.88 | 0.88 | |
|
100 |
+
| happiness | 0.94 | 0.71 | 0.81 | |
|
101 |
+
| sadness | 0.96 | 1.00 | 0.98 | |
|
102 |
+
| | | | Overal | 0.91 |
|
103 |
|
104 |
|
105 |
## Questions?
|
106 |
+
Post a Github issue from [HERE](https://github.com/m3hrdadfi/soxan/issues).
|