-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.py
232 lines (193 loc) · 8.08 KB
/
main.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
from os import path
import os
import numpy as np
import cv2
import time
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import torch.nn as nn
import torch
from torchsummary import summary
from torch.utils.data import TensorDataset, DataLoader
import argparse
from distutils.util import strtobool
class STSTNet(nn.Module):
def __init__(self, in_channels=3, out_channels=3):
super(STSTNet, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels=3, kernel_size=3, padding=2)
self.conv2 = nn.Conv2d(in_channels, out_channels=5, kernel_size=3, padding=2)
self.conv3 = nn.Conv2d(in_channels, out_channels=8, kernel_size=3, padding=2)
self.relu = nn.ReLU()
self.bn1 = nn.BatchNorm2d(3)
self.bn2 = nn.BatchNorm2d(5)
self.bn3 = nn.BatchNorm2d(8)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=3, padding=1)
self.avgpool = nn.AvgPool2d(kernel_size=2, stride=2, padding=0)
self.dropout = nn.Dropout(p=0.5)
self.fc = nn.Linear(in_features=5*5*16, out_features=out_channels)
def forward(self, x):
x1 = self.conv1(x)
x1 = self.relu(x1)
x1 = self.bn1(x1)
x1 = self.maxpool(x1)
x1 = self.dropout(x1)
x2 = self.conv2(x)
x2 = self.relu(x2)
x2 = self.bn2(x2)
x2 = self.maxpool(x2)
x2 = self.dropout(x2)
x3 = self.conv3(x)
x3 = self.relu(x3)
x3 = self.bn3(x3)
x3 = self.maxpool(x3)
x3 = self.dropout(x3)
x = torch.cat((x1, x2, x3),1)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
def reset_weights(m): # Reset the weights for network to avoid weight leakage
for layer in m.children():
if hasattr(layer, 'reset_parameters'):
# print(f'Reset trainable parameters of layer = {layer}')
layer.reset_parameters()
def confusionMatrix(gt, pred, show=False):
TN, FP, FN, TP = confusion_matrix(gt, pred).ravel()
f1_score = (2*TP) / (2*TP + FP + FN)
num_samples = len([x for x in gt if x==1])
average_recall = TP / num_samples
return f1_score, average_recall
def recognition_evaluation(final_gt, final_pred, show=False):
label_dict = { 'negative' : 0, 'positive' : 1, 'surprise' : 2 }
#Display recognition result
f1_list = []
ar_list = []
try:
for emotion, emotion_index in label_dict.items():
gt_recog = [1 if x==emotion_index else 0 for x in final_gt]
pred_recog = [1 if x==emotion_index else 0 for x in final_pred]
try:
f1_recog, ar_recog = confusionMatrix(gt_recog, pred_recog)
f1_list.append(f1_recog)
ar_list.append(ar_recog)
except Exception as e:
pass
UF1 = np.mean(f1_list)
UAR = np.mean(ar_list)
return UF1, UAR
except:
return '', ''
def main(config):
learning_rate = 0.00005
batch_size = 256
epochs = 800
is_cuda = torch.cuda.is_available()
if is_cuda:
device = torch.device('cuda')
else:
device = torch.device('cpu')
loss_fn = nn.CrossEntropyLoss()
if(config.train):
if not path.exists('STSTNet_Weights'):
os.mkdir('STSTNet_Weights')
print('lr=%f, epochs=%d, device=%s\n' % (learning_rate, epochs, device))
total_gt = []
total_pred = []
t = time.time()
main_path = 'norm_u_v_os'
subName = os.listdir(main_path)
for n_subName in subName:
print('Subject:', n_subName)
X_train = []
y_train = []
X_test = []
y_test = []
# Get train dataset
expression = os.listdir(main_path+'/'+n_subName+'/u_train')
for n_expression in expression:
img = os.listdir(main_path+'/'+n_subName+'/u_train/'+n_expression)
for n_img in img:
y_train.append(int(n_expression))
X_train.append(cv2.imread(main_path+'/'+n_subName+'/u_train/'+n_expression+'/'+n_img))
# Get test dataset
expression = os.listdir(main_path+'/'+n_subName+'/u_test')
for n_expression in expression:
img = os.listdir(main_path+'/'+n_subName+'/u_test/'+n_expression)
for n_img in img:
y_test.append(int(n_expression))
X_test.append(cv2.imread(main_path+'/'+n_subName+'/u_test/'+n_expression+'/'+n_img))
weight_path = 'STSTNet_Weights' + '/' + n_subName + '.pth'
# Reset or load model weigts
model = STSTNet().to(device)
if(config.train):
model.apply(reset_weights)
else:
model.load_state_dict(torch.load(weight_path))
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Initialize training dataloader
X_train = torch.Tensor(X_train).permute(0,3,1,2)
y_train = torch.Tensor(y_train).to(dtype=torch.long)
dataset_train = TensorDataset(X_train, y_train)
train_dl = DataLoader(dataset_train, batch_size=batch_size)
# Initialize testing dataloader
X_test = torch.Tensor(X_test).permute(0,3,1,2)
y_test = torch.Tensor(y_test).to(dtype=torch.long)
dataset_test = TensorDataset(X_test, y_test)
test_dl = DataLoader(dataset_test, batch_size=batch_size)
for epoch in range(1, epochs+1):
if(config.train):
# Training
model.train()
train_loss = 0.0
num_train_correct = 0
num_train_examples = 0
for batch in train_dl:
optimizer.zero_grad()
x = batch[0].to(device)
y = batch[1].to(device)
yhat = model(x)
loss = loss_fn(yhat, y)
loss.backward()
optimizer.step()
train_loss += loss.data.item() * x.size(0)
num_train_correct += (torch.max(yhat, 1)[1] == y).sum().item()
num_train_examples += x.shape[0]
train_acc = num_train_correct / num_train_examples
train_loss = train_loss / len(train_dl.dataset)
# Testing
model.eval()
val_loss = 0.0
num_val_correct = 0
num_val_examples = 0
for batch in test_dl:
x = batch[0].to(device)
y = batch[1].to(device)
yhat = model(x)
loss = loss_fn(yhat, y)
val_loss += loss.data.item() * x.size(0)
num_val_correct += (torch.max(yhat, 1)[1] == y).sum().item()
num_val_examples += y.shape[0]
val_acc = num_val_correct / num_val_examples
val_loss = val_loss / len(test_dl.dataset)
# if epoch == 1 or epoch % 50 == 0:
# print('Epoch %3d/%3d, train loss: %5.4f, train acc: %5.4f, val loss: %5.4f, val acc: %5.4f' % (epoch, epochs, train_loss, train_acc, val_loss, val_acc))
# Save Weights
if(config.train):
torch.save(model.state_dict(), weight_path)
# For UF1 and UAR computation
print('Predicted :', torch.max(yhat, 1)[1].tolist())
print('Ground Truth :', y.tolist())
print('Evaluation until this subject: ')
total_pred.extend(torch.max(yhat, 1)[1].tolist())
total_gt.extend(y.tolist())
UF1, UAR = recognition_evaluation(total_gt, total_pred, show=True)
print('UF1:', round(UF1, 4), '| UAR:', round(UAR, 4))
print('Final Evaluation: ')
UF1, UAR = recognition_evaluation(total_gt, total_pred)
print('Total Time Taken:', time.time() - t)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# input parameters
parser.add_argument('--train', type=strtobool, default=False) #Train or use pre-trained weight for prediction
config = parser.parse_args()
main(config)