forked from thuiar/MMSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMISA.py
More file actions
282 lines (206 loc) · 12.4 KB
/
MISA.py
File metadata and controls
282 lines (206 loc) · 12.4 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
280
281
282
"""
From: https://github.com/declare-lab/MISA
Paper: MISA: Modality-Invariant and -Specific Representations for Multimodal Sentiment Analysis
"""
import numpy as np
import random
import math
import torch
import torch.nn as nn
from torch.autograd import Function
from torch.nn.utils.rnn import pad_sequence, pack_padded_sequence, pad_packed_sequence
from transformers import BertModel, BertConfig
from models.subNets.BertTextEncoder import BertTextEncoder
__all__ = ['MISA']
class ReverseLayerF(Function):
"""
Adapted from https://github.com/fungtion/DSN/blob/master/functions.py
"""
@staticmethod
def forward(ctx, x, p):
ctx.p = p
return x.view_as(x)
@staticmethod
def backward(ctx, grad_output):
output = grad_output.neg() * ctx.p
return output, None
# let's define a simple model that can deal with multimodal variable length sequence
class MISA(nn.Module):
def __init__(self, config):
super(MISA, self).__init__()
assert config.use_bert == True
self.config = config
self.text_size = config.feature_dims[0]
self.visual_size = config.feature_dims[2]
self.acoustic_size = config.feature_dims[1]
self.input_sizes = input_sizes = [self.text_size, self.visual_size, self.acoustic_size]
self.hidden_sizes = hidden_sizes = [int(self.text_size), int(self.visual_size), int(self.acoustic_size)]
self.output_size = output_size = config.num_classes if config.train_mode == "classification" else 1
self.dropout_rate = dropout_rate = config.dropout
self.activation = nn.ReLU()
self.tanh = nn.Tanh()
rnn = nn.LSTM if self.config.rnncell == "lstm" else nn.GRU
# defining modules - two layer bidirectional LSTM with layer norm in between
if config.use_bert:
# text subnets
self.bertmodel = BertTextEncoder(language=config.language, use_finetune=config.use_finetune)
self.vrnn1 = rnn(input_sizes[1], hidden_sizes[1], bidirectional=True)
self.vrnn2 = rnn(2*hidden_sizes[1], hidden_sizes[1], bidirectional=True)
self.arnn1 = rnn(input_sizes[2], hidden_sizes[2], bidirectional=True)
self.arnn2 = rnn(2*hidden_sizes[2], hidden_sizes[2], bidirectional=True)
##########################################
# mapping modalities to same sized space
##########################################
if self.config.use_bert:
self.project_t = nn.Sequential()
self.project_t.add_module('project_t', nn.Linear(in_features=768, out_features=config.hidden_size))
self.project_t.add_module('project_t_activation', self.activation)
self.project_t.add_module('project_t_layer_norm', nn.LayerNorm(config.hidden_size))
else:
self.project_t = nn.Sequential()
self.project_t.add_module('project_t', nn.Linear(in_features=hidden_sizes[0]*4, out_features=config.hidden_size))
self.project_t.add_module('project_t_activation', self.activation)
self.project_t.add_module('project_t_layer_norm', nn.LayerNorm(config.hidden_size))
self.project_v = nn.Sequential()
self.project_v.add_module('project_v', nn.Linear(in_features=hidden_sizes[1]*4, out_features=config.hidden_size))
self.project_v.add_module('project_v_activation', self.activation)
self.project_v.add_module('project_v_layer_norm', nn.LayerNorm(config.hidden_size))
self.project_a = nn.Sequential()
self.project_a.add_module('project_a', nn.Linear(in_features=hidden_sizes[2]*4, out_features=config.hidden_size))
self.project_a.add_module('project_a_activation', self.activation)
self.project_a.add_module('project_a_layer_norm', nn.LayerNorm(config.hidden_size))
##########################################
# private encoders
##########################################
self.private_t = nn.Sequential()
self.private_t.add_module('private_t_1', nn.Linear(in_features=config.hidden_size, out_features=config.hidden_size))
self.private_t.add_module('private_t_activation_1', nn.Sigmoid())
self.private_v = nn.Sequential()
self.private_v.add_module('private_v_1', nn.Linear(in_features=config.hidden_size, out_features=config.hidden_size))
self.private_v.add_module('private_v_activation_1', nn.Sigmoid())
self.private_a = nn.Sequential()
self.private_a.add_module('private_a_3', nn.Linear(in_features=config.hidden_size, out_features=config.hidden_size))
self.private_a.add_module('private_a_activation_3', nn.Sigmoid())
##########################################
# shared encoder
##########################################
self.shared = nn.Sequential()
self.shared.add_module('shared_1', nn.Linear(in_features=config.hidden_size, out_features=config.hidden_size))
self.shared.add_module('shared_activation_1', nn.Sigmoid())
##########################################
# reconstruct
##########################################
self.recon_t = nn.Sequential()
self.recon_t.add_module('recon_t_1', nn.Linear(in_features=config.hidden_size, out_features=config.hidden_size))
self.recon_v = nn.Sequential()
self.recon_v.add_module('recon_v_1', nn.Linear(in_features=config.hidden_size, out_features=config.hidden_size))
self.recon_a = nn.Sequential()
self.recon_a.add_module('recon_a_1', nn.Linear(in_features=config.hidden_size, out_features=config.hidden_size))
##########################################
# shared space adversarial discriminator
##########################################
if not self.config.use_cmd_sim:
self.discriminator = nn.Sequential()
self.discriminator.add_module('discriminator_layer_1', nn.Linear(in_features=config.hidden_size, out_features=config.hidden_size))
self.discriminator.add_module('discriminator_layer_1_activation', self.activation)
self.discriminator.add_module('discriminator_layer_1_dropout', nn.Dropout(dropout_rate))
self.discriminator.add_module('discriminator_layer_2', nn.Linear(in_features=config.hidden_size, out_features=len(hidden_sizes)))
##########################################
# shared-private collaborative discriminator
##########################################
self.sp_discriminator = nn.Sequential()
self.sp_discriminator.add_module('sp_discriminator_layer_1', nn.Linear(in_features=config.hidden_size, out_features=4))
self.fusion = nn.Sequential()
self.fusion.add_module('fusion_layer_1', nn.Linear(in_features=self.config.hidden_size*6, out_features=self.config.hidden_size*3))
self.fusion.add_module('fusion_layer_1_dropout', nn.Dropout(dropout_rate))
self.fusion.add_module('fusion_layer_1_activation', self.activation)
self.fusion.add_module('fusion_layer_3', nn.Linear(in_features=self.config.hidden_size*3, out_features= output_size))
self.tlayer_norm = nn.LayerNorm((hidden_sizes[0]*2,))
self.vlayer_norm = nn.LayerNorm((hidden_sizes[1]*2,))
self.alayer_norm = nn.LayerNorm((hidden_sizes[2]*2,))
encoder_layer = nn.TransformerEncoderLayer(d_model=self.config.hidden_size, nhead=2)
self.transformer_encoder = nn.TransformerEncoder(encoder_layer, num_layers=1)
def extract_features(self, sequence, lengths, rnn1, rnn2, layer_norm):
packed_sequence = pack_padded_sequence(sequence, lengths, batch_first=True, enforce_sorted=False)
if self.config.rnncell == "lstm":
packed_h1, (final_h1, _) = rnn1(packed_sequence)
else:
packed_h1, final_h1 = rnn1(packed_sequence)
padded_h1, _ = pad_packed_sequence(packed_h1)
padded_h1 = padded_h1.permute(1, 0, 2)
normed_h1 = layer_norm(padded_h1)
packed_normed_h1 = pack_padded_sequence(normed_h1, lengths, batch_first=True, enforce_sorted=False)
if self.config.rnncell == "lstm":
_, (final_h2, _) = rnn2(packed_normed_h1)
else:
_, final_h2 = rnn2(packed_normed_h1)
return final_h1, final_h2
def alignment(self, text, acoustic, visual):
# bert_sent_mask : consists of seq_len of 1, followed by padding of 0.
bert_sent, bert_sent_mask, bert_sent_type = text[:,0,:], text[:,1,:], text[:,2,:]
batch_size = text.size(0)
if self.config.use_bert:
bert_output = self.bertmodel(text) # [batch_size, seq_len, 768]
# Use the mean value of bert of the front real sentence length as the final representation of text.
masked_output = torch.mul(bert_sent_mask.unsqueeze(2), bert_output)
mask_len = torch.sum(bert_sent_mask, dim=1, keepdim=True)
bert_output = torch.sum(masked_output, dim=1, keepdim=False) / mask_len
utterance_text = bert_output
lengths = mask_len.squeeze().int().detach().cpu().view(-1)
# extract features from visual modality
final_h1v, final_h2v = self.extract_features(visual, lengths, self.vrnn1, self.vrnn2, self.vlayer_norm)
utterance_video = torch.cat((final_h1v, final_h2v), dim=2).permute(1, 0, 2).contiguous().view(batch_size, -1)
# extract features from acoustic modality
final_h1a, final_h2a = self.extract_features(acoustic, lengths, self.arnn1, self.arnn2, self.alayer_norm)
utterance_audio = torch.cat((final_h1a, final_h2a), dim=2).permute(1, 0, 2).contiguous().view(batch_size, -1)
# Shared-private encoders
self.shared_private(utterance_text, utterance_video, utterance_audio)
if not self.config.use_cmd_sim:
# discriminator
reversed_shared_code_t = ReverseLayerF.apply(self.utt_shared_t, self.config.reverse_grad_weight)
reversed_shared_code_v = ReverseLayerF.apply(self.utt_shared_v, self.config.reverse_grad_weight)
reversed_shared_code_a = ReverseLayerF.apply(self.utt_shared_a, self.config.reverse_grad_weight)
self.domain_label_t = self.discriminator(reversed_shared_code_t)
self.domain_label_v = self.discriminator(reversed_shared_code_v)
self.domain_label_a = self.discriminator(reversed_shared_code_a)
else:
self.domain_label_t = None
self.domain_label_v = None
self.domain_label_a = None
self.shared_or_private_p_t = self.sp_discriminator(self.utt_private_t)
self.shared_or_private_p_v = self.sp_discriminator(self.utt_private_v)
self.shared_or_private_p_a = self.sp_discriminator(self.utt_private_a)
self.shared_or_private_s = self.sp_discriminator( (self.utt_shared_t + self.utt_shared_v + self.utt_shared_a)/3.0 )
# For reconstruction
self.reconstruct()
# 1-LAYER TRANSFORMER FUSION
h = torch.stack((self.utt_private_t, self.utt_private_v, self.utt_private_a, self.utt_shared_t, self.utt_shared_v, self.utt_shared_a), dim=0)
h = self.transformer_encoder(h)
h = torch.cat((h[0], h[1], h[2], h[3], h[4], h[5]), dim=1)
o = self.fusion(h)
return o
def reconstruct(self,):
self.utt_t = (self.utt_private_t + self.utt_shared_t)
self.utt_v = (self.utt_private_v + self.utt_shared_v)
self.utt_a = (self.utt_private_a + self.utt_shared_a)
self.utt_t_recon = self.recon_t(self.utt_t)
self.utt_v_recon = self.recon_v(self.utt_v)
self.utt_a_recon = self.recon_a(self.utt_a)
def shared_private(self, utterance_t, utterance_v, utterance_a):
# Projecting to same sized space
self.utt_t_orig = utterance_t = self.project_t(utterance_t)
self.utt_v_orig = utterance_v = self.project_v(utterance_v)
self.utt_a_orig = utterance_a = self.project_a(utterance_a)
# Private-shared components
self.utt_private_t = self.private_t(utterance_t)
self.utt_private_v = self.private_v(utterance_v)
self.utt_private_a = self.private_a(utterance_a)
self.utt_shared_t = self.shared(utterance_t)
self.utt_shared_v = self.shared(utterance_v)
self.utt_shared_a = self.shared(utterance_a)
def forward(self, text, audio, video):
output = self.alignment(text, audio, video)
tmp = {
"M": output
}
return tmp