harshit345
commited on
Commit
·
f7cac30
1
Parent(s):
ca91701
Update README.md
Browse files
README.md
CHANGED
@@ -1,81 +1,89 @@
|
|
1 |
---
|
2 |
-
language:
|
3 |
-
datasets:
|
4 |
-
- aesdd
|
5 |
tags:
|
6 |
- audio
|
7 |
-
-
|
8 |
- speech
|
|
|
9 |
license: apache-2.0
|
10 |
---
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
14 |
# requirement packages
|
15 |
!pip install git+https://github.com/huggingface/datasets.git
|
16 |
!pip install git+https://github.com/huggingface/transformers.git
|
17 |
!pip install torchaudio
|
18 |
!pip install librosa
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
# prediction
|
25 |
-
~~~
|
26 |
import torch
|
27 |
import torch.nn as nn
|
28 |
import torch.nn.functional as F
|
29 |
import torchaudio
|
30 |
from transformers import AutoConfig, Wav2Vec2FeatureExtractor
|
31 |
-
|
32 |
import librosa
|
33 |
import IPython.display as ipd
|
34 |
import numpy as np
|
35 |
import pandas as pd
|
36 |
-
|
37 |
-
|
38 |
-
~~~
|
39 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
40 |
-
model_name_or_path = "
|
41 |
config = AutoConfig.from_pretrained(model_name_or_path)
|
42 |
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(model_name_or_path)
|
43 |
sampling_rate = feature_extractor.sampling_rate
|
44 |
model = Wav2Vec2ForSpeechClassification.from_pretrained(model_name_or_path).to(device)
|
45 |
-
|
46 |
-
|
47 |
-
~~~
|
48 |
def speech_file_to_array_fn(path, sampling_rate):
|
49 |
speech_array, _sampling_rate = torchaudio.load(path)
|
50 |
resampler = torchaudio.transforms.Resample(_sampling_rate)
|
51 |
speech = resampler(speech_array).squeeze().numpy()
|
52 |
return speech
|
53 |
-
|
54 |
-
|
55 |
def predict(path, sampling_rate):
|
56 |
speech = speech_file_to_array_fn(path, sampling_rate)
|
57 |
inputs = feature_extractor(speech, sampling_rate=sampling_rate, return_tensors="pt", padding=True)
|
58 |
inputs = {key: inputs[key].to(device) for key in inputs}
|
59 |
-
|
60 |
with torch.no_grad():
|
61 |
logits = model(**inputs).logits
|
62 |
-
|
63 |
scores = F.softmax(logits, dim=1).detach().cpu().numpy()[0]
|
64 |
outputs = [{"Emotion": config.id2label[i], "Score": f"{round(score * 100, 3):.1f}%"} for i, score in enumerate(scores)]
|
65 |
return outputs
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
~~~
|
70 |
-
# path for a sample
|
71 |
-
path = '/data/jtes_v1.1/wav/f01/ang/f01_ang_01.wav'
|
72 |
outputs = predict(path, sampling_rate)
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
{'Emotion': 'disgust', 'Score': '0.0%'},
|
78 |
-
{'Emotion': 'fear', 'Score': '0.4%'},
|
79 |
-
{'Emotion': 'happiness', 'Score': '0.7%'},
|
80 |
-
{'Emotion': 'sadness', 'Score': '0.5%'}]
|
81 |
-
~~~
|
|
|
1 |
---
|
2 |
+
language: en
|
3 |
+
datasets: Toronto emotional speech set (TESS)(https://www.kaggle.com/ejlok1/toronto-emotional-speech-set-tess)
|
|
|
4 |
tags:
|
5 |
- audio
|
6 |
+
- automatic-speech-recognition
|
7 |
- speech
|
8 |
+
- speech-emotion-recognition
|
9 |
license: apache-2.0
|
10 |
---
|
11 |
+
# Emotion Recognition in Speech using Wav2Vec 2.0
|
12 |
+
## How to use
|
13 |
+
### Requirements
|
14 |
+
```bash
|
15 |
# requirement packages
|
16 |
!pip install git+https://github.com/huggingface/datasets.git
|
17 |
!pip install git+https://github.com/huggingface/transformers.git
|
18 |
!pip install torchaudio
|
19 |
!pip install librosa
|
20 |
+
```
|
21 |
+
### Prediction
|
22 |
+
```python
|
|
|
|
|
|
|
|
|
23 |
import torch
|
24 |
import torch.nn as nn
|
25 |
import torch.nn.functional as F
|
26 |
import torchaudio
|
27 |
from transformers import AutoConfig, Wav2Vec2FeatureExtractor
|
|
|
28 |
import librosa
|
29 |
import IPython.display as ipd
|
30 |
import numpy as np
|
31 |
import pandas as pd
|
32 |
+
```
|
33 |
+
```python
|
|
|
34 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
35 |
+
model_name_or_path = "harshit345/xlsr-wav2vec-speech-emotion-recognition"
|
36 |
config = AutoConfig.from_pretrained(model_name_or_path)
|
37 |
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(model_name_or_path)
|
38 |
sampling_rate = feature_extractor.sampling_rate
|
39 |
model = Wav2Vec2ForSpeechClassification.from_pretrained(model_name_or_path).to(device)
|
40 |
+
```
|
41 |
+
```python
|
|
|
42 |
def speech_file_to_array_fn(path, sampling_rate):
|
43 |
speech_array, _sampling_rate = torchaudio.load(path)
|
44 |
resampler = torchaudio.transforms.Resample(_sampling_rate)
|
45 |
speech = resampler(speech_array).squeeze().numpy()
|
46 |
return speech
|
|
|
|
|
47 |
def predict(path, sampling_rate):
|
48 |
speech = speech_file_to_array_fn(path, sampling_rate)
|
49 |
inputs = feature_extractor(speech, sampling_rate=sampling_rate, return_tensors="pt", padding=True)
|
50 |
inputs = {key: inputs[key].to(device) for key in inputs}
|
|
|
51 |
with torch.no_grad():
|
52 |
logits = model(**inputs).logits
|
|
|
53 |
scores = F.softmax(logits, dim=1).detach().cpu().numpy()[0]
|
54 |
outputs = [{"Emotion": config.id2label[i], "Score": f"{round(score * 100, 3):.1f}%"} for i, score in enumerate(scores)]
|
55 |
return outputs
|
56 |
+
```
|
57 |
+
```python
|
58 |
+
path = "/path/to/disgust.wav"
|
|
|
|
|
|
|
59 |
outputs = predict(path, sampling_rate)
|
60 |
+
```
|
61 |
+
```bash
|
62 |
+
[
|
63 |
+
{'Emotion': 'anger', 'Score': '12.2%'},
|
64 |
+
{'Emotion': 'disgust', 'Score': '78.8%'},
|
65 |
+
{'Emotion': 'fear', 'Score': '7.2%'},
|
66 |
+
{'Emotion': 'happiness', 'Score': '1.3%'},
|
67 |
+
{'Emotion': 'sadness', 'Score': '1.5%'}
|
68 |
+
]
|
69 |
+
```
|
70 |
+
|
71 |
+
|
72 |
+
## Evaluation
|
73 |
+
The following tables summarize the scores obtained by model overall and per each class.
|
74 |
+
|
75 |
+
|
76 |
+
| Emotions | precision | recall | f1-score | accuracy |
|
77 |
+
|-----------|-----------|--------|----------|----------|
|
78 |
+
| anger | 0.82 | 1.00 | 0.81 | |
|
79 |
+
| disgust | 0.85 | 0.96 | 0.85 | |
|
80 |
+
| fear | 0.78 | 0.88 | 0.80 | |
|
81 |
+
| happiness | 0.84 | 0.71 | 0.78 | |
|
82 |
+
| sadness | 0.86 | 1.00 | 0.79 | |
|
83 |
+
| | | | Overall | 0.806 |
|
84 |
+
|
85 |
+
|
86 |
+
##
|
87 |
|
88 |
+
Colab Notebook
|
89 |
+
https://colab.research.google.com/drive/1aPPb_ZVS5dlFVZySly8Q80a44La1XjJu?usp=sharing
|
|
|
|
|
|
|
|
|
|