forked from thuiar/MMSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MLF_DNN.py
165 lines (141 loc) · 6.3 KB
/
MLF_DNN.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
import torch
import torch.nn as nn
import torch.nn.functional as F
__all__ = ['MLF_DNN']
class SubNet(nn.Module):
'''
The subnetwork that is used in TFN for video and audio in the pre-fusion stage
'''
def __init__(self, in_size, hidden_size, dropout):
'''
Args:
in_size: input dimension
hidden_size: hidden layer dimension
dropout: dropout probability
Output:
(return value in forward) a tensor of shape (batch_size, hidden_size)
'''
super(SubNet, self).__init__()
self.norm = nn.BatchNorm1d(in_size)
self.drop = nn.Dropout(p=dropout)
self.linear_1 = nn.Linear(in_size, hidden_size)
self.linear_2 = nn.Linear(hidden_size, hidden_size)
self.linear_3 = nn.Linear(hidden_size, hidden_size)
def forward(self, x):
'''
Args:
x: tensor of shape (batch_size, in_size)
'''
normed = self.norm(x)
dropped = self.drop(normed)
y_1 = F.relu(self.linear_1(dropped))
y_2 = F.relu(self.linear_2(y_1))
y_3 = F.relu(self.linear_3(y_2))
return y_3
class TextSubNet(nn.Module):
'''
The LSTM-based subnetwork that is used in TFN for text
'''
def __init__(self, in_size, hidden_size, out_size, num_layers=1, dropout=0.2, bidirectional=False):
'''
Args:
in_size: input dimension
hidden_size: hidden layer dimension
num_layers: specify the number of layers of LSTMs.
dropout: dropout probability
bidirectional: specify usage of bidirectional LSTM
Output:
(return value in forward) a tensor of shape (batch_size, out_size)
'''
super(TextSubNet, self).__init__()
if num_layers == 1:
dropout = 0.0
self.rnn = nn.LSTM(in_size, hidden_size, num_layers=num_layers, dropout=dropout, bidirectional=bidirectional, batch_first=True)
self.dropout = nn.Dropout(dropout)
self.linear_1 = nn.Linear(hidden_size, out_size)
def forward(self, x):
'''
Args:
x: tensor of shape (batch_size, sequence_len, in_size)
'''
_, final_states = self.rnn(x)
h = self.dropout(final_states[0].squeeze())
y_1 = self.linear_1(h)
return y_1
class MLF_DNN(nn.Module):
"""
late fusion using DNN
"""
def __init__(self, args):
super(MLF_DNN, self).__init__()
self.text_in, self.audio_in, self.video_in = args.feature_dims
self.text_hidden, self.audio_hidden, self.video_hidden = args.hidden_dims
self.text_out = args.text_out
self.audio_prob, self.video_prob, self.text_prob = args.dropouts
self.post_text_prob, self.post_audio_prob, self.post_video_prob, self.post_fusion_prob = args.post_dropouts
self.post_fusion_dim = args.post_fusion_dim
self.post_text_dim = args.post_text_dim
self.post_audio_dim = args.post_audio_dim
self.post_video_dim = args.post_video_dim
# define the pre-fusion subnetworks
self.audio_subnet = SubNet(self.audio_in, self.audio_hidden, self.audio_prob)
self.video_subnet = SubNet(self.video_in, self.video_hidden, self.video_prob)
self.text_subnet = TextSubNet(self.text_in, self.text_hidden, self.text_out, dropout=self.text_prob)
# define the post_fusion layers
self.post_fusion_dropout = nn.Dropout(p=self.post_fusion_prob)
self.post_fusion_layer_1 = nn.Linear(self.text_out + self.video_hidden + self.audio_hidden, self.post_fusion_dim)
self.post_fusion_layer_2 = nn.Linear(self.post_fusion_dim, self.post_fusion_dim)
self.post_fusion_layer_3 = nn.Linear(self.post_fusion_dim, 1)
# define the classify layer for text
self.post_text_dropout = nn.Dropout(p=self.post_text_prob)
self.post_text_layer_1 = nn.Linear(self.text_out, self.post_text_dim)
self.post_text_layer_2 = nn.Linear(self.post_text_dim, self.post_text_dim)
self.post_text_layer_3 = nn.Linear(self.post_text_dim, 1)
# define the classify layer for audio
self.post_audio_dropout = nn.Dropout(p=self.post_audio_prob)
self.post_audio_layer_1 = nn.Linear(self.audio_hidden, self.post_audio_dim)
self.post_audio_layer_2 = nn.Linear(self.post_audio_dim, self.post_audio_dim)
self.post_audio_layer_3 = nn.Linear(self.post_audio_dim, 1)
# define the classify layer for video
self.post_video_dropout = nn.Dropout(p=self.post_video_prob)
self.post_video_layer_1 = nn.Linear(self.video_hidden, self.post_video_dim)
self.post_video_layer_2 = nn.Linear(self.post_video_dim, self.post_video_dim)
self.post_video_layer_3 = nn.Linear(self.post_video_dim, 1)
def forward(self, text_x, audio_x, video_x):
audio_x = audio_x.squeeze(1)
video_x = video_x.squeeze(1)
audio_h = self.audio_subnet(audio_x)
video_h = self.video_subnet(video_x)
text_h = self.text_subnet(text_x)
# text
x_t = self.post_text_dropout(text_h)
x_t = F.relu(self.post_text_layer_1(x_t), inplace=True)
x_t = F.relu(self.post_text_layer_2(x_t), inplace=True)
output_text = self.post_text_layer_3(x_t)
# audio
x_a = self.post_audio_dropout(audio_h)
x_a = F.relu(self.post_audio_layer_1(x_a), inplace=True)
x_a = F.relu(self.post_audio_layer_2(x_a), inplace=True)
output_audio = self.post_audio_layer_3(x_a)
# video
x_v = self.post_video_dropout(video_h)
x_v = F.relu(self.post_video_layer_1(x_v), inplace=True)
x_v = F.relu(self.post_video_layer_2(x_v), inplace=True)
output_video = self.post_video_layer_3(x_v)
# fusion
fusion_h = torch.cat([audio_h, video_h, text_h], dim=-1)
x = self.post_fusion_dropout(fusion_h)
x = F.relu(self.post_fusion_layer_1(x), inplace=True)
x = F.relu(self.post_fusion_layer_2(x), inplace=True)
output_fusion = self.post_fusion_layer_3(x)
res = {
'Feature_t': text_h,
'Feature_a': audio_h,
'Feature_v': video_h,
'Feature_f': fusion_h,
'M': output_fusion,
'T': output_text,
'A': output_audio,
'V': output_video
}
return res