-
Notifications
You must be signed in to change notification settings - Fork 2
/
vision_only.py
223 lines (197 loc) · 7.38 KB
/
vision_only.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
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
torch.manual_seed(777)
torch.cuda.manual_seed(777)
np.random.seed(777)
preprocess = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
'---------------------------------------------------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)
return hiddens[-1]
'---------------------------------------------------Memory to Emotion Decoder------------------------------------------'
class predictor(nn.Module):
def __init__(self,no_of_emotions,input_size):
super(predictor, self).__init__()
self.fc = nn.Linear(input_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
use_CUDA = True
use_pretrained = True
num_workers = 20
test_mode = True
val_mode = False
train_mode = False
no_of_epochs = 12
vision_input_size = 35 # Dont Change
vision_num_layers = 2
vision_hidden_size = 512
predictor_input_size = 1024
'----------------------------------------------------------------------------------------------------------------------'
Vision_encoder = VisionNet(vision_input_size, vision_hidden_size, vision_num_layers)
Predictor = predictor(no_of_emotions,predictor_input_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
'----------------------------------------------------------------------------------------------------------------------'
Vision_encoder = Vision_encoder.cuda()
Predictor = Predictor.cuda()
'----------------------------------------------------------------------------------------------------------------------'
criterion = nn.MSELoss(size_average = False)
params = 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='vision_net'):
filename = filename +'_'+str(state['epoch'])+'.pth.tar'
os.system("mkdir -p vision_only")
torch.save(state, './vision_only/'+filename)
if is_final:
shutil.copyfile(filename, 'model_final.pth.tar')
'-------------------------------------------Setting into train mode----------------------------------------------------'
if not train_mode:
Vision_encoder.train(False)
Predictor.train(False)
else:
Vision_encoder.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 = './vision_only/vision_net_iter_8000_0.pth.tar'
pretrained_file = './vision_only/vision_net__11.pth.tar'
checkpoint = torch.load(pretrained_file)
Vision_encoder.load_state_dict(checkpoint['Vision_encoder'])
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()
gt = Variable(gt.float()).cuda()
vision_output = Vision_encoder(vision)
outputs = Predictor(vision_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()
Vision_encoder.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,
'Vision_encoder' : Vision_encoder.state_dict(),
'Predictor' : Predictor.state_dict(),
'optimizer': optimizer.state_dict(),
}, False,'vision_net_iter_'+str(K))
'-------------------------------------------------Saving model after every epoch-----------------------------------'
if train_mode:
save_checkpoint({
'epoch': epoch,
'loss' : running_loss,
'j_start' : 0,
'Vision_encoder' : Vision_encoder.state_dict(),
'Predictor' : Predictor.state_dict(),
'optimizer': optimizer.state_dict(),
}, False,'vision_net_')
epoch+= 1
'------------------------------------------------------Saving model after training completion--------------------------'
if train_mode:
save_checkpoint({
'epoch': epoch,
'loss' : running_loss,
'j_start' : 0,
'Vision_encoder' : Vision_encoder.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'))