-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathferPlus_train.py
258 lines (196 loc) · 9.13 KB
/
ferPlus_train.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
import os
import sys
from tqdm import tqdm
import argparse
from PIL import Image
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.utils.data as data
from torchvision import transforms, datasets
from sklearn.metrics import balanced_accuracy_score
import matplotlib.pyplot as plt
import itertools
from networks.DDAM import DDAMNet
import torch.nn.functional as F
from sklearn.metrics import confusion_matrix
eps = sys.float_info.epsilon
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--fer_path', type=str, default='/data/ferPlus/', help='ferPlus-DB dataset path.')
parser.add_argument('--batch_size', type=int, default=256, help='Batch size.')
parser.add_argument('--lr', type=float, default=0.01, help='Initial learning rate for sgd.')
parser.add_argument('--workers', default=8, type=int, help='Number of data loading workers.')
parser.add_argument('--epochs', type=int, default=80, help='Total training epochs.')
parser.add_argument('--num_head', type=int, default=2, help='Number of attention head.')
return parser.parse_args()
class AttentionLoss(nn.Module):
def __init__(self, ):
super(AttentionLoss, self).__init__()
def forward(self, x):
num_head = len(x)
loss = 0
cnt = 0
if num_head > 1:
for i in range(num_head-1):
for j in range(i+1, num_head):
mse = F.mse_loss(x[i], x[j])
cnt = cnt+1
loss = loss+mse
loss = cnt/(loss + eps)
else:
loss = 0
return loss
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.
Normalization can be applied by setting `normalize=True`.
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print("Normalized confusion matrix")
else:
print('Confusion matrix, without normalization')
print(cm)
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title, fontsize=16)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j]*100, fmt)+'%',
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.ylabel('Actual', fontsize=18)
plt.xlabel('Predicted', fontsize=18)
plt.tight_layout()
class_names = ['Neutral', 'Happy', 'Sad', 'Surprise', 'Fear', 'Disgust', 'Angry','Contempt']
def run_training():
args = parse_args()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if torch.cuda.is_available():
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.enabled = True
model = DDAMNet(num_class=8,num_head=args.num_head)
model.to(device)
data_transforms = transforms.Compose([
transforms.Resize((112, 112)),
transforms.RandomHorizontalFlip(),
transforms.ColorJitter(),
transforms.RandomApply([
transforms.RandomRotation(10),
transforms.RandomCrop(112, padding=16)
], p=0.2),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
transforms.RandomErasing(scale=(0.02,0.25)),
])
train_dataset = datasets.ImageFolder(f'{args.fer_path}/train', transform = data_transforms)
print('Whole train set size:', train_dataset.__len__())
train_loader = torch.utils.data.DataLoader(train_dataset,
batch_size = args.batch_size,
num_workers = args.workers,
shuffle = True,
pin_memory = True)
data_transforms_val = transforms.Compose([
transforms.Resize((112, 112)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])])
val_dataset = datasets.ImageFolder(f'{args.fer_path}/test', transform = data_transforms_val)
print('Validation set size:', val_dataset.__len__())
val_loader = torch.utils.data.DataLoader(val_dataset,
batch_size = args.batch_size,
num_workers = args.workers,
shuffle = False,
pin_memory = True)
criterion_cls = torch.nn.CrossEntropyLoss()
criterion_at = AttentionLoss()
params = list(model.parameters())
optimizer = torch.optim.SGD(params,lr=args.lr, weight_decay = 1e-4, momentum=0.9)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=10, gamma=0.1)
best_acc = 0
for epoch in tqdm(range(1, args.epochs + 1)):
running_loss = 0.0
correct_sum = 0
iter_cnt = 0
model.train()
for (imgs, targets) in train_loader:
iter_cnt += 1
optimizer.zero_grad()
imgs = imgs.to(device)
targets = targets.to(device)
out,feat,heads = model(imgs)
loss = criterion_cls(out,targets) + 0.1* criterion_at(heads)
loss.backward()
optimizer.step()
running_loss += loss
_, predicts = torch.max(out, 1)
correct_num = torch.eq(predicts, targets).sum()
correct_sum += correct_num
acc = correct_sum.float() / float(train_dataset.__len__())
running_loss = running_loss/iter_cnt
tqdm.write('[Epoch %d] Training accuracy: %.4f. Loss: %.3f. LR %.6f' % (epoch, acc, running_loss,optimizer.param_groups[0]['lr']))
with torch.no_grad():
running_loss = 0.0
iter_cnt = 0
bingo_cnt = 0
sample_cnt = 0
## for calculating balanced accuracy
y_true = []
y_pred = []
model.eval()
for (imgs, targets) in val_loader:
imgs = imgs.to(device)
targets = targets.to(device)
out,feat,heads = model(imgs)
loss = criterion_cls(out,targets) + 0.1* criterion_at(heads)
running_loss += loss
_, predicts = torch.max(out, 1)
correct_num = torch.eq(predicts,targets)
bingo_cnt += correct_num.sum().cpu()
sample_cnt += out.size(0)
y_true.append(targets.cpu().numpy())
y_pred.append(predicts.cpu().numpy())
if iter_cnt == 0:
all_predicted = predicts
all_targets = targets
else:
all_predicted = torch.cat((all_predicted, predicts),0)
all_targets = torch.cat((all_targets, targets),0)
iter_cnt+=1
running_loss = running_loss/iter_cnt
scheduler.step()
acc = bingo_cnt.float()/float(sample_cnt)
acc = np.around(acc.numpy(),4)
best_acc = max(acc,best_acc)
y_true = np.concatenate(y_true)
y_pred = np.concatenate(y_pred)
balanced_acc = np.around(balanced_accuracy_score(y_true, y_pred),4)
tqdm.write("[Epoch %d] Validation accuracy:%.4f. bacc:%.4f. Loss:%.3f" % (epoch, acc, balanced_acc, running_loss))
tqdm.write("best_acc:" + str(best_acc))
if acc > 0.905 and acc == best_acc:
torch.save({'iter': epoch,
'model_state_dict': model.state_dict(),
'optimizer_state_dict': optimizer.state_dict(),},
os.path.join('checkpoints', "ferPlus_epoch"+str(epoch)+"_acc"+str(acc)+"_bacc"+str(balanced_acc)+".pth"))
tqdm.write('Model saved.')
# Compute confusion matrix
matrix = confusion_matrix(all_targets.data.cpu().numpy(), all_predicted.cpu().numpy())
np.set_printoptions(precision=2)
plt.figure(figsize=(10, 8))
# Plot normalized confusion matrix
plot_confusion_matrix(matrix, classes=class_names, normalize=True, title= 'ferPlus Confusion Matrix (acc: %0.2f%%)' %(acc*100))
plt.savefig(os.path.join('checkpoints', "ferPlus_epoch"+str(epoch)+"_acc"+str(acc)+"_bacc"+str(balanced_acc)+".png"))
plt.close()
if __name__ == "__main__":
run_training()