-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path9. MultiModalFusionModelfoldWise.py
397 lines (296 loc) · 13.5 KB
/
9. MultiModalFusionModelfoldWise.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
FOLDER_NAME = '../../'
"""Video classification model part
"""
import os
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.models as models
import torchvision.transforms as transforms
import torch.utils.data as data
import torchvision
from torch.autograd import Variable
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder, LabelEncoder
from sklearn.metrics import accuracy_score
from PIL import Image
import pickle
from tqdm import tqdm
from sklearn.metrics import *
import torch
import pandas as pd
#import librosa
import numpy as np
from torch.utils.data import TensorDataset, DataLoader, random_split
#import librosa.display
import matplotlib.pyplot as plt
import tarfile
import torch.nn as nn
import torch.nn.functional as F
import random
def fix_the_random(seed_val = 2021):
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
random.seed(seed_val)
np.random.seed(seed_val)
torch.manual_seed(seed_val)
torch.cuda.manual_seed_all(seed_val)
fix_the_random(2021)
class Text_Model(nn.Module):
def __init__(self, input_size, fc1_hidden, fc2_hidden, output_size):
super().__init__()
self.network=nn.Sequential(
nn.Linear(input_size,fc1_hidden),
nn.ReLU(),
nn.Linear(fc1_hidden, fc2_hidden),
nn.ReLU(),
nn.Linear(fc2_hidden, output_size),
)
def forward(self, xb):
return self.network(xb)
class LSTM(nn.Module):
def __init__(self, input_emb_size = 768, no_of_frames = 100):
super(LSTM, self).__init__()
self.lstm = nn.LSTM(input_emb_size, 128)
self.fc = nn.Linear(128*no_of_frames, 64)
def forward(self, x):
x, _ = self.lstm(x)
x = x.view(x.shape[0], -1)
x = self.fc(x)
return x
class Aud_Model(nn.Module):
def __init__(self, input_size, fc1_hidden, fc2_hidden, output_size):
super().__init__()
self.network=nn.Sequential(
nn.Linear(input_size,fc1_hidden),
nn.ReLU(),
nn.Linear(fc1_hidden, fc2_hidden),
nn.ReLU(),
nn.Linear(fc2_hidden, output_size),
)
def forward(self, xb):
return self.network(xb)
class Combined_model(nn.Module):
def __init__(self, text_model, video_model, audio_model, num_classes):
super().__init__()
self.text_model = text_model
self.audio_model = audio_model
self.video_model = video_model
self.fc_output = nn.Linear(3*64, num_classes)
def forward(self, x_text, x_vid, x_audio):
tex_out = self.text_model(x_text)
vid_out = self.video_model(x_vid)
aud_out = self.audio_model(x_audio)
inp = torch.cat((tex_out, vid_out, aud_out), dim = 1)
out = self.fc_output(inp)
return out
## ---------------------- Dataloader ---------------------- ##
class Dataset_3DCNN(data.Dataset):
"Characterizes a dataset for PyTorch"
def __init__(self, folders, labels):
"Initialization"
self.labels = labels
self.folders = folders
def __len__(self):
"Denotes the total number of samples"
return len(self.folders)
def read_text(self,selected_folder):
return torch.tensor(textData[selected_folder]), torch.tensor(vidData[selected_folder]), torch.tensor(audData[selected_folder])
def __getitem__(self, index):
"Generates one sample of data"
# Select sample
folder = self.folders[index]
try:
# Load data
X_text, X_vid, X_audio = self.read_text(folder)
y = torch.LongTensor([self.labels[index]]) # (labels) LongTensor are for int64 instead of FloatTensor
except:
with open("Exceptions.txt","a") as f:
f.write("{}\n".format(folder))
return None
return X_text, X_vid, X_audio, y
def evalMetric(y_true, y_pred):
try:
accuracy = accuracy_score(y_true, y_pred)
mf1Score = f1_score(y_true, y_pred, average='macro')
f1Score = f1_score(y_true, y_pred, labels = np.unique(y_pred))
fpr, tpr, _ = roc_curve(y_true, y_pred)
area_under_c = auc(fpr, tpr)
recallScore = recall_score(y_true, y_pred, labels = np.unique(y_pred))
precisionScore = precision_score(y_true, y_pred, labels = np.unique(y_pred))
except:
return dict({"accuracy": 0, 'mF1Score': 0, 'f1Score': 0, 'auc': 0,'precision': 0, 'recall': 0})
return dict({"accuracy": accuracy, 'mF1Score': mf1Score, 'f1Score': f1Score, 'auc': area_under_c,
'precision': precisionScore, 'recall': recallScore})
#loading Audio features
import pickle
with open(FOLDER_NAME+'all_HateXPlainembedding.p','rb') as fp:
#with open(FOLDER_NAME+'all_rawBERTembedding.p','rb') as fp:
textData = pickle.load(fp)
with open(FOLDER_NAME+'vgg19_audFeatureMap.p','rb') as fp:
#with open(FOLDER_NAME+'MFCCFeatures.p','rb') as fp:
audData = pickle.load(fp)
with open(FOLDER_NAME+'final_allNewData.p', 'rb') as fp:
allDataAnnotation = pickle.load(fp)
# train, test split
train_list, train_label= allDataAnnotation['train']
val_list, val_label = allDataAnnotation['val']
test_list, test_label = allDataAnnotation['test']
allVidList = []
allVidLab = []
allVidList.extend(train_list)
allVidList.extend(val_list)
allVidList.extend(test_list)
allVidLab.extend(train_label)
allVidLab.extend(val_label)
allVidLab.extend(test_label)
vidData ={}
for i in allVidList:
with open(FOLDER_NAME+"VITF/"+i+"_vit.p", 'rb') as fp:
vidData[i] = np.array(pickle.load(fp))
# Audio parameters
input_size_text = 768 #40 #76800 #
input_size_audio = 1000 #76800 #
fc1_hidden_audio, fc2_hidden_audio = 128, 128
# training parameters
k = 2 # number of target category
epochs = 20
batch_size = 10
learning_rate = 1e-4
log_interval = 1
def train(log_interval, model, device, train_loader, optimizer, epoch):
# set model as training mode
model.train()
losses = []
scores = []
N_count = 0 # counting total trained sample in one epoch
for batch_idx, (X_text, X_vid, X_aud, y) in enumerate(train_loader):
# distribute data to device
X_text, X_vid, X_aud, y = (X_text.float()).to(device), (X_vid.float()).to(device), (X_aud.float()).to(device), y.to(device).view(-1, )
N_count += X_text.size(0)
optimizer.zero_grad()
output = model(X_text, X_vid, X_aud) # output size = (batch, number of classes)
loss = F.cross_entropy(output, y, weight=torch.FloatTensor([0.41, 0.59]).to(device))
#loss = F.cross_entropy(output, y)
losses.append(loss.item())
# to compute accuracy
y_pred = torch.max(output, 1)[1] # y_pred != output
metrics = evalMetric(y.cpu().data.squeeze().numpy(), y_pred.cpu().data.squeeze().numpy())
scores.append(metrics) # computed on CPU
loss.backward()
optimizer.step()
# show information
if (batch_idx + 1) % log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}, Accu: {:.2f}%, MF1 Score: {:.4f}, F1 Score: {:.4f}, Area Under Curve: {:.4f}, Precision: {:.4f}, Recall Score: {:.4f}'.format(epoch + 1, N_count, len(train_loader.dataset), 100. * (batch_idx + 1) / len(train_loader), loss.item(), 100 * metrics['accuracy'], metrics['mF1Score'], metrics['f1Score'], metrics['auc'], metrics['precision'], metrics['recall']))
return losses, scores
def validation(model, device, optimizer, test_loader, testingType = "Test"):
# set model as testing mode
model.eval()
test_loss = 0
all_y = []
all_y_pred = []
with torch.no_grad():
for X_text, X_vid, X_aud, y in test_loader:
# distribute data to device
X_text, X_vid, X_aud, y = (X_text.float()).to(device), (X_vid.float()).to(device), (X_aud.float()).to(device), y.to(device).view(-1, )
output = model(X_text, X_vid, X_aud)
loss = F.cross_entropy(output, y, reduction='sum')
test_loss += loss.item() # sum up batch loss
y_pred = output.max(1, keepdim=True)[1] # (y_pred != output) get the index of the max log-probability
# collect all y and y_pred in all batches
all_y.extend(y)
all_y_pred.extend(y_pred)
test_loss /= len(test_loader.dataset)
# to compute accuracy
all_y = torch.stack(all_y, dim=0)
all_y_pred = torch.stack(all_y_pred, dim=0)
print("====================")
# try:
metrics = evalMetric(all_y.cpu().data.squeeze().numpy(), all_y_pred.cpu().data.squeeze().numpy())
# except:
# metrics = None
# show information
print('\n '+testingType+' set: ({:d} samples): Average loss: {:.4f}, Accuracy: {:.2f}%, MF1 Score: {:.4f}, F1 Score: {:.4f}, Area Under Curve: {:.4f}, Precision: {:.4f}, Recall Score: {:.4f}'.format(
len(all_y), test_loss, 100 * metrics['accuracy'], metrics['mF1Score'], metrics['f1Score'], metrics['auc'], metrics['precision'], metrics['recall']))
# # save Pytorch models of best record
# torch.save(model.state_dict(), os.path.join(save_model_path, '3dcnn_epoch{}.pt'.format(epoch + 1))) # save spatial_encoder
# torch.save(optimizer.state_dict(), os.path.join(save_model_path, '3dcnn_optimizer_epoch{}.pt'.format(epoch + 1))) # save optimizer
# print("Epoch {} model saved!".format(epoch + 1))
print(testingType + " Len:", len(list(all_y_pred.cpu().data.squeeze().numpy())))
return test_loss, metrics, list(all_y_pred.cpu().data.squeeze().numpy())
# Detect devices
use_cuda = torch.cuda.is_available() # check if GPU exists
device = torch.device("cuda" if use_cuda else "cpu") # use CPU or GPU
params = {'batch_size': batch_size, 'shuffle': True, 'num_workers': 2, 'pin_memory': True} if use_cuda else {}
valParams = {'batch_size': batch_size, 'shuffle': False, 'num_workers': 2, 'pin_memory': True} if use_cuda else {}
with open(FOLDER_NAME+'allFoldDetails.p', 'rb') as fp:
allDataAnnotation = pickle.load(fp)
def collate_fn(batch):
batch = list(filter(lambda x: x is not None, batch))
return torch.utils.data.dataloader.default_collate(batch)
allF = ['fold1', 'fold2', 'fold3', 'fold4', 'fold5']
finalOutputAccrossFold ={}
for fold in allF:
# train, test split
train_list, train_label= allDataAnnotation[fold]['train']
val_list, val_label = allDataAnnotation[fold]['val']
test_list, test_label = allDataAnnotation[fold]['test']
train_set, valid_set , test_set = Dataset_3DCNN(train_list, train_label), Dataset_3DCNN(val_list, val_label), Dataset_3DCNN(test_list, test_label)
train_loader = data.DataLoader(train_set, collate_fn = collate_fn, **params)
test_loader = data.DataLoader(test_set, collate_fn = collate_fn, **valParams)
valid_loader = data.DataLoader(valid_set, collate_fn = collate_fn, **valParams)
tex = Text_Model(input_size_text, fc1_hidden_audio, fc2_hidden_audio, 64).to(device)
vid = LSTM().to(device)
aud = Aud_Model(input_size_audio, fc1_hidden_audio, fc2_hidden_audio, 64).to(device)
comb = Combined_model(tex, vid, aud, k).to(device)
# Parallelize model to multiple GPUs
if torch.cuda.device_count() > 1:
print("Using", torch.cuda.device_count(), "GPUs!")
comb = nn.DataParallel(comb)
optimizer = torch.optim.Adam(comb.parameters(), lr=learning_rate) # optimize all cnn parameters
epoch_train_losses = []
epoch_train_scores = []
epoch_test_losses = []
epoch_test_scores = []
validFinalValue = None
testFinalValue = None
finalScoreAcc =0
prediction = None
# start training
for epoch in range(epochs):
# train, test model
train_losses, train_scores = train(log_interval, comb, device, train_loader, optimizer, epoch)
test_loss, test_scores, veTest_pred = validation(comb, device, optimizer, test_loader, 'Test')
test_loss1, test_scores1, veValid_pred = validation(comb, device, optimizer, valid_loader, 'Valid')
if (test_scores1['mF1Score']>finalScoreAcc):
finalScoreAcc = test_scores1['mF1Score']
validFinalValue = test_scores1
testFinalValue = test_scores
print("veTest_pred", len(veTest_pred))
prediction = {'test_list': test_list , 'test_label': test_label, 'test_pred': veTest_pred}
# save results
epoch_train_losses.append(train_losses)
epoch_train_scores.append(list(x['accuracy'] for x in train_scores))
epoch_test_losses.append(test_loss)
epoch_test_scores.append(test_scores['accuracy'])
# save all train test results
A = np.array(epoch_train_losses)
B = np.array(epoch_train_scores)
C = np.array(epoch_test_losses)
D = np.array(epoch_test_scores)
finalOutputAccrossFold[fold] = {'validation':validFinalValue, 'test': testFinalValue, 'test_prediction': prediction}
with open('foldWiseRes_vit_hateX_audioVGG19_lstm.p', 'wb') as fp:
pickle.dump(finalOutputAccrossFold,fp)
allValueDict ={}
for fold in allF:
for val in finalOutputAccrossFold[fold]['test']:
try:
allValueDict[val].append(finalOutputAccrossFold[fold]['test'][val])
except:
allValueDict[val]=[finalOutputAccrossFold[fold]['test'][val]]
import numpy as np
for i in allValueDict:
print(f"{i} : Mean {np.mean(allValueDict[i])} STD: {np.std(allValueDict[i])}")