-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodel.py
More file actions
279 lines (224 loc) · 9.53 KB
/
model.py
File metadata and controls
279 lines (224 loc) · 9.53 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
class PositionalEncoding(nn.Module):
def __init__(self, d_model, max_len=5000):
super(PositionalEncoding, self).__init__()
pe = torch.zeros(max_len, d_model)
position = torch.arange(0, max_len, dtype=torch.float).unsqueeze(1)
div_term = torch.exp(torch.arange(0, d_model, 2).float() * (-torch.log(torch.tensor(10000.0)) / d_model))
pe[:, 0::2] = torch.sin(position * div_term)
pe[:, 1::2] = torch.cos(position * div_term)
pe = pe.unsqueeze(0).transpose(0, 1)
self.register_buffer('pe', pe)
def forward(self, x):
x = x + self.pe[:x.size(0), :]
return x
def scaled_dot_product(q, k, v, mask=None):
d_k = q.size()[-1]
attn_logits = torch.matmul(q, k.transpose(-2, -1))
attn_logits = attn_logits / math.sqrt(d_k)
if mask is not None:
attn_logits = attn_logits.masked_fill(mask == 0, -9e15)
attention = F.softmax(attn_logits, dim=-1)
values = torch.matmul(attention, v)
return values, attention
class MultiheadAttention(nn.Module):
def __init__(self, input_dim, embed_dim, num_heads):
super().__init__()
assert (
embed_dim % num_heads == 0
), "Embedding dimension must be 0 modulo number of heads."
self.embed_dim = embed_dim
self.num_heads = num_heads
self.head_dim = embed_dim // num_heads
# Stack all weight matrices 1...h together for efficiency
# Note that in many implementations you see "bias=False" which is optional
self.qkv_proj = nn.Linear(input_dim, 3 * embed_dim)
self.o_proj = nn.Linear(embed_dim, embed_dim)
self._reset_parameters()
def _reset_parameters(self):
# Original Transformer initialization, see PyTorch documentation
nn.init.xavier_uniform_(self.qkv_proj.weight)
self.qkv_proj.bias.data.fill_(0)
nn.init.xavier_uniform_(self.o_proj.weight)
self.o_proj.bias.data.fill_(0)
def forward(self, x, mask=None, return_attention=False):
batch_size, seq_length, embed_dim = x.size()
qkv = self.qkv_proj(x)
# Separate Q, K, V from linear output
qkv = qkv.reshape(batch_size, seq_length, self.num_heads, 3 * self.head_dim)
qkv = qkv.permute(0, 2, 1, 3) # [Batch, Head, SeqLen, Dims]
q, k, v = qkv.chunk(3, dim=-1)
# Determine value outputs
values, attention = scaled_dot_product(q, k, v, mask=mask)
values = values.permute(0, 2, 1, 3) # [Batch, SeqLen, Head, Dims]
values = values.reshape(batch_size, seq_length, embed_dim)
o = self.o_proj(values)
if return_attention:
return o, attention
else:
return o
class EncoderBlock(nn.Module):
def __init__(self, input_dim, num_heads, dim_feedforward, dropout=0.0):
"""
Inputs:
input_dim - Dimensionality of the input
num_heads - Number of heads to use in the attention block
dim_feedforward - Dimensionality of the hidden layer in the MLP
dropout - Dropout probability to use in the dropout layers
"""
super().__init__()
# Attention layer
self.self_attn = MultiheadAttention(input_dim, input_dim, num_heads)
# Two-layer MLP
self.linear_net = nn.Sequential(
nn.Linear(input_dim, dim_feedforward),
nn.Dropout(dropout),
nn.ReLU(inplace=True),
nn.Linear(dim_feedforward, input_dim),
)
# Layers to apply in between the main layers
self.norm1 = nn.LayerNorm(input_dim)
self.norm2 = nn.LayerNorm(input_dim)
self.dropout = nn.Dropout(dropout)
def forward(self, x, mask=None):
# Attention part
attn_out = self.self_attn(x, mask=mask)
x = x + self.dropout(attn_out)
x = self.norm1(x)
# MLP part
# linear_out = self.linear_net(x)
# x = x + self.dropout(linear_out)
# x = self.norm2(x)
return x
class _TransformerEncoder(nn.Module):
def __init__(self, num_layers, **block_args):
super().__init__()
self.layers = nn.ModuleList(
[EncoderBlock(**block_args) for _ in range(num_layers)]
)
def forward(self, x, mask=None):
for l in self.layers:
x = l(x, mask=mask)
return x
def get_attention_maps(self, x, mask=None):
attention_maps = []
for l in self.layers:
_, attn_map = l.self_attn(x, mask=mask, return_attention=True)
attention_maps.append(attn_map)
x = l(x)
return attention_maps
class Self_attention(nn.Module):
def __init__(self,
input_dim=None,
model_dim=None,
num_layers=2,
num_heads=4,
dropout=0.2):
super(Self_attention, self).__init__()
# Transformer
self.transformer = _TransformerEncoder(
num_layers=num_layers,
input_dim=model_dim,
dim_feedforward=2 * model_dim,
num_heads=num_heads,
dropout=dropout,
)
def forward(self, x, mask=None):
x = self.transformer(x, mask=mask)
return x
class Cross_attention_Encoder_Layer(nn.Module):
def __init__(self, d_model, nhead, dropout=0.2):
super(Cross_attention_Encoder_Layer, self).__init__()
self.cross_attn = nn.MultiheadAttention(embed_dim=d_model, num_heads=nhead, dropout=dropout)
self.linear1 = nn.Linear(d_model, d_model)
self.dropout = nn.Dropout(dropout)
self.linear2 = nn.Linear(d_model, d_model)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
self.dropout1 = nn.Dropout(dropout)
self.dropout2 = nn.Dropout(dropout)
def forward(self, Q, KV, src_mask=None, src_key_padding_mask=None):
src, attention_map = self.cross_attn(Q, KV, KV)
Q2 = Q + self.dropout1(src)
Q = self.norm1(Q2)
src2 = self.linear2(self.dropout(F.relu(self.linear1(Q))))
Q2 = Q + self.dropout2(src2)
Q = self.norm2(Q2)
return Q, attention_map
class MMSC_model(nn.Module):
def __init__(self, output_dim, num_layers, heads, dropout=0.2):
super(MMSC_model, self).__init__()
self.output_dim = output_dim
self.num_layers = num_layers
self.heads = heads
self.dropout = dropout
# Text-Vision encoder
self.tm_cross_attention = Cross_attention_Encoder_Layer(d_model=1024, nhead=self.heads)
# Acoustic context encoder
audio_encoder_layers = nn.TransformerEncoderLayer(d_model=512, nhead=self.heads, dropout=self.dropout)
self.acoustic_context_encoder = nn.TransformerEncoder(audio_encoder_layers, num_layers=num_layers)
# Visual context encoder
video_encoder_layers = nn.TransformerEncoderLayer(d_model=1024, nhead=self.heads, dropout=self.dropout)
self.visual_context_encoder = nn.TransformerEncoder(video_encoder_layers, num_layers=num_layers)
# projectors
self.audio_proj = nn.Sequential(
nn.Linear(512, 1024),
nn.GELU(),
nn.Linear(1024, self.output_dim)
)
self.video_proj = nn.Sequential(
nn.Linear(1024, 1024),
nn.GELU(),
nn.Linear(1024, self.output_dim)
)
# Multi-modal fusion
self.tma_cross_attention = Cross_attention_Encoder_Layer(d_model=self.output_dim, nhead=self.heads, dropout=self.dropout)
# trailerness prediction head
self.trailerness_proj = nn.Sequential(
nn.Linear(self.output_dim, self.output_dim),
nn.GELU(),
nn.Linear(self.output_dim, 1)
)
self.trailerness_sigmoid = nn.Sigmoid()
# emotional prediection head
self.emotional_proj = nn.Sequential(
nn.Linear(self.output_dim, self.output_dim),
nn.GELU(),
nn.Linear(self.output_dim, 1)
)
self.emotional_sigmoid = nn.Sigmoid()
def forward(self, v_I, a_A, w_label, w_plot, label_exist, plot_exist):
# concat text and video
if label_exist == True and plot_exist == True:
w = torch.cat((w_label, w_plot), dim=0)
# Text-Vision encoder
wv_I, attention_map_1 = self.tm_cross_attention(v_I, w)
elif label_exist == True and plot_exist == False:
w = w_label
# Text-Vision encoder
wv_I, attention_map_1 = self.tm_cross_attention(v_I, w)
elif label_exist == False and plot_exist == True:
w = w_plot
# Text-Vision encoder
wv_I, attention_map_1 = self.tm_cross_attention(v_I, w)
else:
wv_I = v_I
# Acoustic context encoder
a_A = self.acoustic_context_encoder(a_A)
# Visual context encoder
wv_I = self.visual_context_encoder(wv_I)
# through projectors
a_A_hat = self.audio_proj(a_A)
wv_I_hat = self.video_proj(wv_I)
# Multi-modal fusion
wva_I_hat, attention_map_2 = self.tma_cross_attention(wv_I_hat, a_A_hat)
# trailerness prediction head
trailerness_linear = self.trailerness_proj(wva_I_hat)
trailerness_pre = self.trailerness_sigmoid(trailerness_linear)
# emotion prediction head
emotional_linear = self.emotional_proj(wva_I_hat)
emotional_pre = self.emotional_sigmoid(emotional_linear)
return trailerness_pre, emotional_pre