m3hrdadfi commited on
Commit
bd01cf2
1 Parent(s): 4cdc77d

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +110 -0
README.md ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: fa
3
+ datasets:
4
+ - shemo
5
+ tags:
6
+ - audio
7
+ - speech
8
+ - speech-emotion-recognition
9
+ license: apache-2.0
10
+ ---
11
+
12
+ # Emotion Recognition in Persian (fa) Speech using HuBERT
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
+ ```bash
28
+ !git clone https://github.com/m3hrdadfi/soxan.git .
29
+ ```
30
+
31
+ ### Prediction
32
+
33
+ ```python
34
+ import torch
35
+ import torch.nn as nn
36
+ import torch.nn.functional as F
37
+ import torchaudio
38
+ from transformers import AutoConfig, Wav2Vec2FeatureExtractor
39
+ from src.models import Wav2Vec2ForSpeechClassification, HubertForSpeechClassification
40
+
41
+ import librosa
42
+ import IPython.display as ipd
43
+ import numpy as np
44
+ import pandas as pd
45
+ ```
46
+
47
+ ```python
48
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
49
+ model_name_or_path = "m3hrdadfi/hubert-base-persian-speech-emotion-recognition"
50
+ config = AutoConfig.from_pretrained(model_name_or_path)
51
+ feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(model_name_or_path)
52
+ sampling_rate = feature_extractor.sampling_rate
53
+ model = HubertForSpeechClassification.from_pretrained(model_name_or_path).to(device)
54
+ ```
55
+
56
+ ```python
57
+ def speech_file_to_array_fn(path, sampling_rate):
58
+ speech_array, _sampling_rate = torchaudio.load(path)
59
+ resampler = torchaudio.transforms.Resample(_sampling_rate)
60
+ speech = resampler(speech_array).squeeze().numpy()
61
+ return speech
62
+
63
+
64
+ def predict(path, sampling_rate):
65
+ speech = speech_file_to_array_fn(path, sampling_rate)
66
+ inputs = feature_extractor(speech, sampling_rate=sampling_rate, return_tensors="pt", padding=True)
67
+ inputs = {key: inputs[key].to(device) for key in inputs}
68
+
69
+ with torch.no_grad():
70
+ logits = model(**inputs).logits
71
+
72
+ scores = F.softmax(logits, dim=1).detach().cpu().numpy()[0]
73
+ outputs = [{"Emotion": config.id2label[i], "Score": f"{round(score * 100, 3):.1f}%"} for i, score in enumerate(scores)]
74
+ return outputs
75
+ ```
76
+
77
+ ```python
78
+ path = "/path/to/sadness.wav"
79
+ outputs = predict(path, sampling_rate)
80
+ ```
81
+
82
+ ```bash
83
+ [
84
+ {'Label': 'Anger', 'Score': '0.0%'},
85
+ {'Label': 'Fear', 'Score': '0.0%'},
86
+ {'Label': 'Happiness', 'Score': '0.0%'},
87
+ {'Label': 'Neutral', 'Score': '0.0%'},
88
+ {'Label': 'Sadness', 'Score': '99.9%'},
89
+ {'Label': 'Surprise', 'Score': '0.0%'}]
90
+ ]
91
+ ```
92
+
93
+
94
+ ## Evaluation
95
+ The following tables summarize the scores obtained by model overall and per each class.
96
+
97
+
98
+ | Emotions | precision | recall | f1-score | accuracy |
99
+ |:---------:|:---------:|:------:|:--------:|:--------:|
100
+ | Anger | 0.96 | 0.96 | 0.96 | |
101
+ | Fear | 1.00 | 0.50 | 0.67 | |
102
+ | Happiness | 0.79 | 0.87 | 0.83 | |
103
+ | Neutral | 0.93 | 0.94 | 0.93 | |
104
+ | Sadness | 0.87 | 0.94 | 0.91 | |
105
+ | Surprise | 0.97 | 0.75 | 0.85 | |
106
+ | | | | Overal | 0.92 |
107
+
108
+
109
+ ## Questions?
110
+ Post a Github issue from [HERE](https://github.com/m3hrdadfi/soxan/issues).