3loi commited on
Commit
45e6e02
1 Parent(s): c925be4

Upload model

Browse files
Files changed (3) hide show
  1. config.json +19 -0
  2. pipeline_utils.py +179 -0
  3. pytorch_model.bin +3 -0
config.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "SERModel"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "pipeline_utils.SERConfig",
7
+ "AutoModelForAudioClassification": "pipeline_utils.SERModel"
8
+ },
9
+ "classifier_dropout_prob": 0.5,
10
+ "classifier_hidden_layers": 1,
11
+ "hidden_size": 1024,
12
+ "model_type": "ser",
13
+ "num_attention_heads": 16,
14
+ "num_classes": 3,
15
+ "num_hidden_layers": 24,
16
+ "ssl_type": "microsoft/wavlm-large",
17
+ "torch_dtype": "float32",
18
+ "transformers_version": "4.34.0.dev0"
19
+ }
pipeline_utils.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Common pooling methods
3
+
4
+ Authors:
5
+ * Leo 2022
6
+ * Haibin Wu 2022
7
+ """
8
+
9
+ import torch
10
+ import torch.nn as nn
11
+ import torch.nn.functional as F
12
+ from transformers import AutoModel
13
+ from transformers.modeling_utils import PreTrainedModel ,PretrainedConfig
14
+
15
+
16
+ __all__ = [
17
+ "MeanPooling",
18
+ "AttentiveStatisticsPooling"
19
+ ]
20
+
21
+
22
+ class Pooling(nn.Module):
23
+ def __init__(self):
24
+ super().__init__()
25
+ def compute_length_from_mask(self, mask):
26
+ """
27
+ mask: (batch_size, T)
28
+ Assuming that the sampling rate is 16kHz, the frame shift is 20ms
29
+ """
30
+ wav_lens = torch.sum(mask, dim=1) # (batch_size, )
31
+ feat_lens = torch.div(wav_lens-1, 16000*0.02, rounding_mode="floor") + 1
32
+ feat_lens = feat_lens.int().tolist()
33
+ return feat_lens
34
+
35
+ def forward(self, x, mask):
36
+ raise NotImplementedError
37
+
38
+ class MeanPooling(Pooling):
39
+ def __init__(self):
40
+ super().__init__()
41
+ def forward(self, xs, mask):
42
+ """
43
+ xs: (batch_size, T, feat_dim)
44
+ mask: (batch_size, T)
45
+
46
+ => output: (batch_size, feat_dim)
47
+ """
48
+ feat_lens = self.compute_length_from_mask(mask)
49
+ pooled_list = []
50
+ for x, feat_len in zip(xs, feat_lens):
51
+ pooled = torch.mean(x[:feat_len], dim=0) # (feat_dim, )
52
+ pooled_list.append(pooled)
53
+ pooled = torch.stack(pooled_list, dim=0) # (batch_size, feat_dim)
54
+ return pooled
55
+
56
+
57
+ class AttentiveStatisticsPooling(Pooling):
58
+ """
59
+ AttentiveStatisticsPooling
60
+ Paper: Attentive Statistics Pooling for Deep Speaker Embedding
61
+ Link: https://arxiv.org/pdf/1803.10963.pdf
62
+ """
63
+ def __init__(self, input_size):
64
+ super().__init__()
65
+ self._indim = input_size
66
+ self.sap_linear = nn.Linear(input_size, input_size)
67
+ self.attention = nn.Parameter(torch.FloatTensor(input_size, 1))
68
+ torch.nn.init.normal_(self.attention, mean=0, std=1)
69
+
70
+ def forward(self, xs, mask):
71
+ """
72
+ xs: (batch_size, T, feat_dim)
73
+ mask: (batch_size, T)
74
+
75
+ => output: (batch_size, feat_dim*2)
76
+ """
77
+ feat_lens = self.compute_length_from_mask(mask)
78
+ pooled_list = []
79
+ for x, feat_len in zip(xs, feat_lens):
80
+ x = x[:feat_len].unsqueeze(0)
81
+ h = torch.tanh(self.sap_linear(x))
82
+ w = torch.matmul(h, self.attention).squeeze(dim=2)
83
+ w = F.softmax(w, dim=1).view(x.size(0), x.size(1), 1)
84
+ mu = torch.sum(x * w, dim=1)
85
+ rh = torch.sqrt((torch.sum((x**2) * w, dim=1) - mu**2).clamp(min=1e-5))
86
+ x = torch.cat((mu, rh), 1).squeeze(0)
87
+ pooled_list.append(x)
88
+ return torch.stack(pooled_list)
89
+
90
+
91
+
92
+
93
+ class EmotionRegression(nn.Module):
94
+ def __init__(self, *args, **kwargs):
95
+ super(EmotionRegression, self).__init__()
96
+ input_dim = args[0]
97
+ hidden_dim = args[1]
98
+ num_layers = args[2]
99
+ output_dim = args[3]
100
+ p = kwargs.get("dropout", 0.5)
101
+
102
+ self.fc=nn.ModuleList([
103
+ nn.Sequential(
104
+ nn.Linear(input_dim, hidden_dim), nn.LayerNorm(hidden_dim), nn.ReLU(), nn.Dropout(p)
105
+ )
106
+ ])
107
+ for lidx in range(num_layers-1):
108
+ self.fc.append(
109
+ nn.Sequential(
110
+ nn.Linear(hidden_dim, hidden_dim), nn.LayerNorm(hidden_dim), nn.ReLU(), nn.Dropout(p)
111
+ )
112
+ )
113
+ self.out = nn.Sequential(
114
+ nn.Linear(hidden_dim, output_dim)
115
+ )
116
+
117
+ self.inp_drop = nn.Dropout(p)
118
+ def get_repr(self, x):
119
+ h = self.inp_drop(x)
120
+ for lidx, fc in enumerate(self.fc):
121
+ h=fc(h)
122
+ return h
123
+
124
+ def forward(self, x):
125
+ h=self.get_repr(x)
126
+ result = self.out(h)
127
+ return result
128
+
129
+ class SERConfig(PretrainedConfig):
130
+ model_type = "ser"
131
+
132
+ def __init__(
133
+ self,
134
+ num_classes: int = 3,
135
+ num_attention_heads = 16,
136
+ num_hidden_layers = 24,
137
+ hidden_size = 1024,
138
+ classifier_hidden_layers = 1,
139
+ classifier_dropout_prob = 0.5,
140
+ ssl_type= "microsoft/wavlm-large",
141
+ torch_dtype= "float32",
142
+ **kwargs,
143
+ ):
144
+ self.num_classes = num_classes
145
+ self.num_attention_heads = num_attention_heads
146
+ self.num_hidden_layers = num_hidden_layers
147
+ self.hidden_size = hidden_size
148
+ self.classifier_hidden_layers = classifier_hidden_layers
149
+ self.classifier_dropout_prob = classifier_dropout_prob
150
+ self.ssl_type = ssl_type
151
+ self.torch_dtype = torch_dtype
152
+ super().__init__(**kwargs)
153
+
154
+ class SERModel(PreTrainedModel):
155
+ config_class = SERConfig
156
+
157
+ def __init__(self, config):
158
+ super().__init__(config)
159
+ self.ssl_model = AutoModel.from_pretrained(config.ssl_type)
160
+ self.ssl_model.freeze_feature_encoder()
161
+
162
+ self.pool_model = AttentiveStatisticsPooling(config.hidden_size)
163
+
164
+ self.ser_model = EmotionRegression(config.hidden_size*2,
165
+ config.hidden_size,
166
+ config.classifier_hidden_layers,
167
+ config.num_classes,
168
+ dropout=config.classifier_dropout_prob)
169
+
170
+
171
+ def forward(self, x, mask):
172
+ ssl = self.ssl_model(x, attention_mask=mask).last_hidden_state
173
+
174
+ ssl = self.pool_model(ssl, mask)
175
+
176
+ pred = self.ser_model(ssl)
177
+
178
+ return pred
179
+
pytorch_model.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5c34b4fd571efce7b4530a7539f1928213d535f6be19b2324bceca0c08c3e601
3
+ size 1274593809