-
Notifications
You must be signed in to change notification settings - Fork 2
/
dual_attention.py
353 lines (311 loc) · 12.4 KB
/
dual_attention.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
import sys
import glob
import scipy.io as sio
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
import torchvision.models as models
from matplotlib import pyplot as plt
import numpy as np
import h5py
from PIL import Image
from sklearn.externals import joblib
import shutil
import os
import random
import pickle
import time
import gc
import re
from tensorboardX import SummaryWriter
import time
import math
from torchvision import datasets, models, transforms
import matplotlib.cm as cm
import cv2
import pandas as pd
from sklearn.metrics import precision_score, recall_score, confusion_matrix, classification_report, accuracy_score, f1_score
from torch.utils.data import Dataset, DataLoader
from mosei_dataloader import mosei
preprocess = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
'---------------------------------------------------LSTM VocalNet-------------------------------------------------------'
class VocalNet(nn.Module):
def __init__(self,input_size,hidden_size,num_layers):
super(VocalNet, self).__init__()
self.lstm = nn.LSTM(input_size,hidden_size,num_layers,bidirectional=True)
self.linear = nn.Linear(hidden_size, 6)
def forward(self,x):
x = torch.transpose(x,0,1)
hiddens,_ = self.lstm(x)
hiddens = hiddens.squeeze(1)
return hiddens
'---------------------------------------------------LSTM VisualNet-------------------------------------------------------'
class VisionNet(nn.Module):
def __init__(self,input_size,hidden_size,num_layers):
super(VisionNet, self).__init__()
self.lstm = nn.LSTM(input_size,hidden_size,num_layers,bidirectional=True)
def forward(self,x):
x = torch.transpose(x,0,1)
hiddens,_ = self.lstm(x)
hiddens = hiddens.squeeze(1)
return hiddens
'---------------------------------------------------Gated Attention----------------------------------------------------'
class GatedAttention(nn.Module):
def __init__(self,att_input_size,att_hidden_size,att_num_layers,no_of_emotions):
super(GatedAttention, self).__init__()
self.lstm = nn.LSTM(att_input_size,att_hidden_size,att_num_layers)
self.linear = nn.Linear(att_hidden_size, no_of_emotions)
def forward(self,vocal,vision):
vocal = vocal.repeat(45,1)
vision = vision.squeeze(2)
vision = vision.squeeze(2)
fusion = vocal*vision
fusion = fusion.unsqueeze(0)
fusion = fusion.transpose(0,1)
hiddens,_ = self.lstm(fusion)
outputs = self.linear(hiddens[-1])
return outputs
'---------------------------------------------------Dual Attention----------------------------------------------------'
class DualAttention(nn.Module):
def __init__(self,no_of_emotions,dan_hidden_size):
super(DualAttention, self).__init__()
N = dan_hidden_size
''' K= 1 '''
self.Wvision_1 = nn.Linear(N,N)
self.Wvision_m1 = nn.Linear(N,N)
self.Wvision_h1 = nn.Linear(N,N)
self.Wvocal_1 = nn.Linear(N,N)
self.Wvocal_m1 = nn.Linear(N,N)
self.Wvocal_h1 = nn.Linear(N,N)
''' K = 2 '''
self.Wvision_2 = nn.Linear(N,N)
self.Wvision_m2 = nn.Linear(N,N)
self.Wvision_h2 = nn.Linear(N,N)
self.Wvocal_2 = nn.Linear(N,N)
self.Wvocal_m2 = nn.Linear(N,N)
self.Wvocal_h2 = nn.Linear(N,N)
self.fc = nn.Linear(N, no_of_emotions)
def forward(self,vocal,vision):
# Sorting out vision
# print(resnet_output.size())
# resnet_output = resnet_output.mean(0)
# resnet_output = resnet_output.view(512,49)
# vision = resnet_output.transpose(0,1)
'-------------------------------------------------Initializing Memory--------------------------------------'
vision_zero = vision.mean(0).unsqueeze(0)
vocal_zero = vocal.mean(0).unsqueeze(0)
m_zero = vision_zero * vocal_zero
m_zero_vision = m_zero.repeat(vision.size(0),1)
m_zero_vocal = m_zero.repeat(vocal.size(0),1)
'--------------------------------------------------K = 1 ---------------------------------------------------'
# Visual Attention
h_one_vision = F.tanh(self.Wvision_1(vision))*F.tanh(self.Wvision_m1(m_zero_vision))
a_one_vision = F.softmax(self.Wvision_h1(h_one_vision),dim=-1)
vision_one = (a_one_vision*vision).mean(0).unsqueeze(0)
# Vocal Attention
h_one_vocal = F.tanh(self.Wvocal_1(vocal))*F.tanh(self.Wvocal_m1(m_zero_vocal))
a_one_vocal = F.softmax(self.Wvocal_h1(h_one_vocal),dim=-1)
vocal_one = (a_one_vocal*vocal).mean(0).unsqueeze(0)
# Memory Update
m_one = m_zero + vision_one * vocal_one
m_one_vision = m_one.repeat(vision.size(0),1)
m_one_vocal = m_one.repeat(vocal.size(0),1)
'--------------------------------------------------K = 2 ---------------------------------------------------'
# Visual Attention
h_two_vision = F.tanh(self.Wvision_2(vision))*F.tanh(self.Wvision_m2(m_one_vision))
a_two_vision = F.softmax(self.Wvision_h2(h_two_vision),dim=-1)
vision_two = (a_two_vision*vision).mean(0).unsqueeze(0)
# Vocal Attention
h_two_vocal = F.tanh(self.Wvocal_2(vocal))*F.tanh(self.Wvocal_m2(m_one_vocal))
a_two_vocal = F.softmax(self.Wvocal_h2(h_two_vocal),dim=-1)
vocal_two = (a_two_vocal*vocal).mean(0).unsqueeze(0)
# Memory Update
m_two = m_one + vision_two * vocal_two
return m_two
'-------------------------------------------------Prediction--------------------------------------------------'
# return m_two
# outputs = self.fc(m_two)
# # print(outputs)
# return outputs
'---------------------------------------------------Memory to Emotion Decoder------------------------------------------'
class predictor(nn.Module):
def __init__(self,no_of_emotions,hidden_size):
super(predictor, self).__init__()
self.fc = nn.Linear(hidden_size, no_of_emotions)
def forward(self,x):
x = self.fc(x)
return x
'------------------------------------------------------Hyperparameters-------------------------------------------------'
batch_size = 1
mega_batch_size = 1
no_of_emotions = 6
# vocal_seq_len = 150
# vision_seq_len = 45
use_CUDA = True
use_pretrained = True
num_workers = 20
test_mode = False
val_mode = False
train_mode = True
no_of_epochs = 1000
vocal_input_size = 74 # Dont Change
vision_input_size = 35 # Dont Change
vocal_num_layers = 2
vision_num_layers = 2
vocal_hidden_size = 1024
vision_hidden_size = 1024
dan_hidden_size = 2048
'----------------------------------------------------------------------------------------------------------------------'
Vocal_encoder = VocalNet(vocal_input_size, vocal_hidden_size, vocal_num_layers)
Vision_encoder = VisionNet(vision_input_size, vision_hidden_size, vision_num_layers)
Attention = DualAttention(no_of_emotions,dan_hidden_size)
Predictor = predictor(no_of_emotions,dan_hidden_size)
if train_mode:
train_dataset = mosei(mode= "train")
data_loader = torch.utils.data.DataLoader(dataset=train_dataset,
batch_size=batch_size,
shuffle=True,num_workers = num_workers)
elif val_mode:
val_dataset = mosei(mode = "val")
data_loader = torch.utils.data.DataLoader(dataset=val_dataset,
batch_size=1,
shuffle=False,num_workers = num_workers)
no_of_epochs = 1
else:
test_dataset = mosei(mode = "test")
data_loader = torch.utils.data.DataLoader(dataset=test_dataset,
batch_size=1,
shuffle=False,num_workers = num_workers)
no_of_epochs = 1
curr_epoch = 0
total = 0
'----------------------------------------------------------------------------------------------------------------------'
Vocal_encoder = Vocal_encoder.cuda()
Attention = Attention.cuda()
Vision_encoder = Vision_encoder.cuda()
Predictor = Predictor.cuda()
'----------------------------------------------------------------------------------------------------------------------'
criterion = nn.MSELoss(size_average = False)
params = list(Vocal_encoder.parameters())+ list(Attention.parameters()) +list(Vision_encoder.parameters()) + list(Predictor.parameters())
print('Parameters in the model = ' + str(len(params)))
optimizer = torch.optim.Adam(params, lr = 0.0001)
# optimizer = torch.optim.SGD(params, lr =0.001,momentum = 0.9 )
'------------------------------------------Saving Intermediate Models--------------------------------------------------'
def save_checkpoint(state, is_final, filename='attention_net'):
filename = filename +'_'+str(state['epoch'])+'.pth.tar'
os.system("mkdir -p DAN")
torch.save(state, './DAN/'+filename)
if is_final:
shutil.copyfile(filename, 'model_final.pth.tar')
'-------------------------------------------Setting into train mode----------------------------------------------------'
if not train_mode:
Vision_encoder.train(False)
Vocal_encoder.train(False)
Attention.train(False)
Predictor.train(False)
else:
Vision_encoder.train(True)
Vocal_encoder.train(True)
Attention.train(True)
Predictor.train(True)
'----------------------------------------------------------------------------------------------------------------------'
epoch = 0
y_true = []
y_pred = []
while epoch<no_of_epochs:
j_start = 0
running_loss = 0
running_corrects = 0
if use_pretrained:
# pretrained_file = './DAN/dual_attention_net_iter_8000_0.pth.tar'
pretrained_file = './DAN/dual_attention_net__4.pth.tar'
checkpoint = torch.load(pretrained_file)
Vocal_encoder.load_state_dict(checkpoint['Vocal_encoder'])
Vision_encoder.load_state_dict(checkpoint['Vision_encoder'])
Attention.load_state_dict(checkpoint['Attention'])
Predictor.load_state_dict(checkpoint['Predictor'])
use_pretrained = False
if train_mode:
epoch = checkpoint['epoch']+1
optimizer.load_state_dict(checkpoint['optimizer'])
K = 0
for i,(vision,vocal,emb,gt) in enumerate(data_loader):
if use_CUDA:
vision = Variable(vision.float()).cuda()
vocal = Variable(vocal.float()).cuda()
gt = Variable(gt.float()).cuda()
vision_output = Vision_encoder(vision)
vocal_output = Vocal_encoder(vocal)
output = Attention(vocal_output,vision_output)
outputs = Predictor(output)
outputs = torch.clamp(outputs,0,3)
loss = criterion(outputs, gt)
if train_mode and K%mega_batch_size==0:
loss.backward()
optimizer.step()
optimizer.zero_grad()
Vocal_encoder.zero_grad()
Vision_encoder.zero_grad()
Attention.zero_grad()
Predictor.zero_grad()
# outputs_ = Variable(torch.FloatTensor([ 0.1565 ,0.1233, 0.0401, 0.4836 , 0.1596, 0.04842])).cuda()
# loss = criterion(outputs_, gt)
running_loss += loss.data[0]
K+=1
average_loss = running_loss/K
if train_mode and K%mega_batch_size==0:
print('Training -- Epoch [%d], Sample [%d], Average Loss: %.4f'
% (epoch+1, K, average_loss))
elif val_mode:
print('Validating -- Epoch [%d], Sample [%d], Average Loss: %.4f'
% (epoch+1, K, average_loss))
elif test_mode:
print('Testing -- Epoch [%d], Sample [%d], Average Loss: %.4f'
% (epoch+1, K, average_loss))
if train_mode:
if K%4000==0:
save_checkpoint({
'epoch': epoch,
'loss' : running_loss,
'j_start' : 0,
'Vocal_encoder': Vocal_encoder.state_dict(),
'Vision_encoder' : Vision_encoder.state_dict(),
'Attention' : Attention.state_dict(),
'Predictor' : Predictor.state_dict(),
'optimizer': optimizer.state_dict(),
}, False,'dual_attention_net_iter_'+str(K))
'-------------------------------------------------Saving model after every epoch-----------------------------------'
if train_mode:
save_checkpoint({
'epoch': epoch,
'loss' : running_loss,
'correct' : running_corrects,
'j_start' : 0,
'Vocal_encoder': Vocal_encoder.state_dict(),
'Vision_encoder' : Vision_encoder.state_dict(),
'Attention' : Attention.state_dict(),
'Predictor' : Predictor.state_dict(),
'optimizer': optimizer.state_dict(),
}, False,'dual_attention_net_')
epoch+= 1
'------------------------------------------------------Saving model after training completion--------------------------'
if train_mode:
save_checkpoint({
'epoch': epoch,
'loss' : running_loss,
'j_start' : 0,
'Vocal_encoder': Vocal_encoder.state_dict(),
'Vision_encoder' : Vision_encoder.state_dict(),
'Attention' : Attention.state_dict(),
'Predictor' : Predictor.state_dict(),
'optimizer': optimizer.state_dict(),
}, False)
# print('Accuracy:', accuracy_score(y_true, y_pred))
# print('F1 score:', f1_score(y_true, y_pred,average = 'weighted'))
# print('Recall:', recall_score(y_true, y_pred,average ='weighted'))
# print('Precision:', precision_score(y_true, y_pred,average = 'weighted'))