-
Notifications
You must be signed in to change notification settings - Fork 3
/
model.py
171 lines (134 loc) · 5.16 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch
import whisper
import numpy as np
class WhisperEncoder(nn.Module):
def __init__(self,):
super().__init__()
self.encoder = whisper.load_model("base.en").encoder
for param in self.encoder.parameters():
param.requires_grad = True
def forward(self, x):
return self.encoder(x)
class SelfAttentionPooling(nn.Module):
"""
Implementation of SelfAttentionPooling
Original Paper: Self-Attention Encoding and Pooling for Speaker Recognition
https://arxiv.org/pdf/2008.01077v1.pdf
"""
def __init__(self, input_dim):
super(SelfAttentionPooling, self).__init__()
self.W = nn.Linear(input_dim, 1)
def forward(self, batch_rep):
"""
input:
batch_rep : size (N, T, H), N: batch size, T: sequence length, H: Hidden dimension
attention_weight:
att_w : size (N, T, 1)
return:
utter_rep: size (N, H)
"""
softmax = nn.functional.softmax
att_w = softmax(self.W(batch_rep).squeeze(-1), dim=1).unsqueeze(-1)
utter_rep = torch.sum(batch_rep * att_w, dim=1)
return utter_rep, att_w
# ----------------------------------------------------------------------------------------------------------------------------
class WhisperBaselineModel(nn.Module):
def __init__(self, feature_dim=512, n_class=56):
super().__init__()
self.encoder = WhisperEncoder()
self.intent_classifier = nn.Sequential(
nn.Linear(feature_dim, n_class),
)
def forward(self, x):
z = self.encoder(x)
z = torch.mean(z, 1)
intent = self.intent_classifier(z)
return intent
# ----------------------------------------------------------------------------------------------------------------------------
class ProsodyBaselineModel(nn.Module):
def __init__(self, feature_dim=512, n_class=68):
super().__init__()
self.encoder = WhisperEncoder()
self.acoustic_proj = nn.Sequential(
nn.Linear(feature_dim, 128),
nn.ReLU(),
)
self.prosody_encoder = nn.Sequential(
nn.Conv1d(6, 128, 5,padding='same'),
nn.GELU(),
nn.Conv1d(128, 128, 5,padding='same'),
nn.GELU(),
)
self.rnn = nn.LSTM(256, 256, 2, batch_first=True, dropout=0.1)
self.intent_classifier = nn.Sequential(
nn.Linear(256, n_class),
)
def concat_fn(self, z, p):
return torch.cat([z, p], dim=2)
def forward(self, x, p):
z = self.encoder(x)
z = self.acoustic_proj(z)
p = self.prosody_encoder(p.transpose(1,2)).transpose(1,2)
z = self.concat_fn(z, p)
z = self.rnn(z)[0]
z = z[:, -1, :]
intent = self.intent_classifier(z)
return intent
# ----------------------------------------------------------------------------------------------------------------------------
class ProsodyAttentionModel(nn.Module):
def __init__(self, feature_dim=512, n_class=68):
super().__init__()
self.encoder = WhisperEncoder()
self.prosody_encoder = nn.Sequential(
nn.Conv1d(6, 128, 5,padding='same'),
nn.GELU(),
)
self.self_attn = SelfAttentionPooling(128)
self.intent_classifier = nn.Sequential(
nn.Linear(512, n_class),
)
def concat_fn(self, z, p):
return torch.cat([z, p], dim=2)
def forward(self, x, p):
z = self.encoder(x)
p = self.prosody_encoder(p.transpose(1,2)).transpose(1,2)
_, attn = self.self_attn(p)
z = torch.sum(z * attn, dim=1)
intent = self.intent_classifier(z)
return intent, attn
# ----------------------------------------------------------------------------------------------------------------------------
class ProsodyDistillationModel(nn.Module):
def __init__(self, feature_dim=512, n_class=56):
super().__init__()
self.encoder = WhisperEncoder()
dim = feature_dim
self.acoustic_proj = nn.Sequential(
nn.Linear(feature_dim, dim),
nn.GELU(),
)
self.prosody_encoder = nn.Sequential(
nn.Conv1d(6, dim, 5, padding='same'),
nn.GELU(),
nn.Conv1d(dim, dim, 5, padding='same'),
nn.GELU(),
)
self.z_pool = SelfAttentionPooling(dim)
self.p_pool = SelfAttentionPooling(dim)
self.p_intent_classifier = nn.Sequential(
nn.Linear(dim, n_class),
)
self.z_intent_classifier = nn.Sequential(
nn.Linear(dim, n_class),
)
def forward(self, x, p):
z = self.encoder(x)
z = self.acoustic_proj(z) # [B, T, 128]
z, z_attn = self.z_pool(z)
zp = self.prosody_encoder(p.transpose(1,2)).transpose(1,2) # [B, T, 128]
zp, zp_zttn = self.p_pool(zp)
intent_p = self.p_intent_classifier(zp)
intent_z = self.z_intent_classifier(z)
return intent_p, intent_z, z, zp, z_attn, zp_zttn