m3hrdadfi commited on
Commit
7dc408d
1 Parent(s): 8af3d4c

Create README.md

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