frankenjoe commited on
Commit
0dad9f3
1 Parent(s): 3f7a513

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +113 -1
README.md CHANGED
@@ -9,4 +9,116 @@ datasets:
9
  tags:
10
  - speech
11
  license: cc-by-nc-sa-4.0
12
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  tags:
10
  - speech
11
  license: cc-by-nc-sa-4.0
12
+ ---
13
+
14
+ # Model for Dimensional Speech Emotion Recognition based on Wav2vec 2.0
15
+
16
+ The model expects a raw audio signal as input and outputs predictions for arousal, dominance and valence in a range of approximately 0...1. In addition, it also provides the pooled states of the last transformer layer. The model was created by fine-tuning [
17
+ Wav2Vec2-Large-Robust](https://huggingface.co/facebook/wav2vec2-large-robust) on [MSP-Podcast](https://ecs.utdallas.edu/research/researchlabs/msp-lab/MSP-Podcast.html) (v1.7). The model was pruned from 24 to 12 transformer layers before fine-tuning. An [ONNX](https://onnx.ai/") export of the model is available from [doi:10.5281/zenodo.6221127](https://zenodo.org/record/6221127). Further details are given in the associated [paper](https://arxiv.org/abs/2203.07378).
18
+
19
+ # How to
20
+
21
+ ```python
22
+ import numpy as np
23
+ import torch
24
+ import torch.nn as nn
25
+ from transformers import Wav2Vec2Processor
26
+ from transformers.models.wav2vec2.modeling_wav2vec2 import (
27
+ Wav2Vec2Model,
28
+ Wav2Vec2PreTrainedModel,
29
+ )
30
+
31
+
32
+ class RegressionHead(nn.Module):
33
+ r"""Classification head."""
34
+
35
+ def __init__(self, config):
36
+
37
+ super().__init__()
38
+
39
+ self.dense = nn.Linear(config.hidden_size, config.hidden_size)
40
+ self.dropout = nn.Dropout(config.final_dropout)
41
+ self.out_proj = nn.Linear(config.hidden_size, config.num_labels)
42
+
43
+ def forward(self, features, **kwargs):
44
+
45
+ x = features
46
+ x = self.dropout(x)
47
+ x = self.dense(x)
48
+ x = torch.tanh(x)
49
+ x = self.dropout(x)
50
+ x = self.out_proj(x)
51
+
52
+ return x
53
+
54
+
55
+ class EmotionModel(Wav2Vec2PreTrainedModel):
56
+ r"""Speech emotion classifier."""
57
+
58
+ def __init__(self, config):
59
+
60
+ super().__init__(config)
61
+
62
+ self.config = config
63
+ self.wav2vec2 = Wav2Vec2Model(config)
64
+ self.classifier = RegressionHead(config)
65
+ self.init_weights()
66
+
67
+ def forward(
68
+ self,
69
+ input_values,
70
+ ):
71
+
72
+ outputs = self.wav2vec2(input_values)
73
+ hidden_states = outputs[0]
74
+ hidden_states = torch.mean(hidden_states, dim=1)
75
+ logits = self.classifier(hidden_states)
76
+
77
+ return hidden_states, logits
78
+
79
+
80
+
81
+ # load model from hub
82
+ device = 'cpu'
83
+ model_name = 'audeering/wav2vec2-large-robust-12-ft-emotion-msp-dim'
84
+ processor = Wav2Vec2Processor.from_pretrained(model_name)
85
+ model = EmotionModel.from_pretrained(model_name)
86
+
87
+ # dummy signal
88
+ sampling_rate = 16000
89
+ signal = np.zeros((1, sampling_rate), dtype=np.float32)
90
+
91
+
92
+ def process_func(
93
+ x: np.ndarray,
94
+ sampling_rate: int,
95
+ embeddings: bool = False,
96
+ ) -> np.ndarray:
97
+ r"""Predict emotions or extract embeddings from raw audio signal."""
98
+
99
+ # run through processor to normalize signal
100
+ # always returns a batch, so we just get the first entry
101
+ # then we put it on the device
102
+ y = processor(x, sampling_rate=sampling_rate)
103
+ y = y['input_values'][0]
104
+ y = torch.from_numpy(y).to(device)
105
+
106
+ # run through model
107
+ with torch.no_grad():
108
+ y = model(y)[0 if embeddings else 1]
109
+
110
+ # convert to numpy
111
+ y = y.detach().cpu().numpy()
112
+
113
+ return y
114
+
115
+
116
+ process_func(signal, sampling_rate)
117
+ # Arousal dominance valence
118
+ # [[0.5460759 0.6062269 0.4043165]]
119
+
120
+ process_func(signal, sampling_rate, embeddings=True)
121
+ # Pooled hidden states of last transformer layer
122
+ # [[-0.00752167 0.0065819 -0.00746339 ... 0.00663631 0.00848747
123
+ # 0.00599209]]
124
+ ```