m3hrdadfi commited on
Commit
e3bf588
1 Parent(s): 037cf13

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +99 -0
README.md ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language: fa
3
+ datasets:
4
+ - shemo
5
+ tags:
6
+ - audio
7
+ - speech
8
+ - speech-gender-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-gender-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 = [{"Label": 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/female.wav"
79
+ outputs = predict(path, sampling_rate)
80
+ ```
81
+
82
+ ```bash
83
+ [{'Label': 'F', 'Score': '98.2%'}, {'Label': 'M', 'Score': '1.8%'}]
84
+ ```
85
+
86
+
87
+ ## Evaluation
88
+ The following tables summarize the scores obtained by model overall and per each class.
89
+
90
+
91
+ | Emotions | precision | recall | f1-score | accuracy |
92
+ |----------|-----------|--------|----------|----------|
93
+ | F | 0.98 | 0.97 | 0.98 | |
94
+ | M | 0.98 | 0.99 | 0.98 | |
95
+ | | | | Overal | 0.98 |
96
+
97
+
98
+ ## Questions?
99
+ Post a Github issue from [HERE](https://github.com/m3hrdadfi/soxan/issues).