-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevaluate.py
275 lines (226 loc) · 9.47 KB
/
evaluate.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
from models import models_vit
import sys
import argparse
import os
import time
import shutil
import torch
import torch.nn as nn
import torch.nn.parallel
import torch.backends.cudnn as cudnn
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
from models.Generate_Model import GenerateModel
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import itertools
import datetime
from dataloader.video_dataloader import train_data_loader, test_data_loader
from sklearn.metrics import confusion_matrix
import tqdm
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
import random
seed = 1
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', type=str, default='DFEW')
parser.add_argument('--checkpoint', type=str)
parser.add_argument('--temporal-layers', type=int, default=1)
parser.add_argument('--img-size', type=int, default=224)
parser.add_argument('--workers', type=int, default=8)
parser.add_argument('--fold', type=int, default=1)
args = parser.parse_args()
return args
def main(set, args):
data_set = set
if args.dataset == "DFEW":
print("*********** DFEW Dataset Fold " + str(data_set) + " ***********")
test_annotation_file_path = "./annotation/DFEW_set_"+str(data_set)+"_test.txt"
args.number_class = 7
elif args.dataset == "MAFW":
print("*********** MAFW Dataset Fold " + str(data_set) + " ***********")
test_annotation_file_path = "./annotation/MAFW_set_"+str(data_set)+"_test_faces.txt"
args.number_class = 11
model = GenerateModel(args=args)
model = torch.nn.DataParallel(model).cuda()
test_data = test_data_loader(list_file=test_annotation_file_path,
num_segments=16,
duration=1,
image_size=args.img_size)
val_loader = torch.utils.data.DataLoader(test_data,
batch_size=1, #args.batch_size,
shuffle=False,
num_workers=args.workers,
pin_memory=True)
uar, war = computer_uar_war(val_loader, model, args.checkpoint, data_set)
return uar, war
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self, name, fmt=':f'):
self.name = name
self.fmt = fmt
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def __str__(self):
fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})'
return fmtstr.format(**self.__dict__)
class ProgressMeter(object):
def __init__(self, num_batches, meters, prefix="", log_txt_path=""):
self.batch_fmtstr = self._get_batch_fmtstr(num_batches)
self.meters = meters
self.prefix = prefix
self.log_txt_path = log_txt_path
def display(self, batch):
entries = [self.prefix + self.batch_fmtstr.format(batch)]
entries += [str(meter) for meter in self.meters]
print_txt = '\t'.join(entries)
print(print_txt)
with open(self.log_txt_path, 'a') as f:
f.write(print_txt + '\n')
def _get_batch_fmtstr(self, num_batches):
num_digits = len(str(num_batches // 1))
fmt = '{:' + str(num_digits) + 'd}'
return '[' + fmt + '/' + fmt.format(num_batches) + ']'
def accuracy(output, target, topk=(1,)):
"""Computes the accuracy over the k top predictions for the specified values of k"""
with torch.no_grad():
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].contiguous().view(-1).float().sum(0, keepdim=True)
res.append(correct_k.mul_(100.0 / batch_size))
return res
class RecorderMeter(object):
"""Computes and stores the minimum loss value and its epoch index"""
def __init__(self, total_epoch):
self.reset(total_epoch)
def reset(self, total_epoch):
self.total_epoch = total_epoch
self.current_epoch = 0
self.epoch_losses = np.zeros((self.total_epoch, 2), dtype=np.float32) # [epoch, train/val]
self.epoch_accuracy = np.zeros((self.total_epoch, 2), dtype=np.float32) # [epoch, train/val]
def update(self, idx, train_loss, train_acc, val_loss, val_acc):
self.epoch_losses[idx, 0] = train_loss * 50
self.epoch_losses[idx, 1] = val_loss * 50
self.epoch_accuracy[idx, 0] = train_acc
self.epoch_accuracy[idx, 1] = val_acc
self.current_epoch = idx + 1
def plot_curve(self, save_path):
title = 'the accuracy/loss curve of train/val'
dpi = 80
width, height = 1600, 800
legend_fontsize = 10
figsize = width / float(dpi), height / float(dpi)
fig = plt.figure(figsize=figsize)
x_axis = np.array([i for i in range(self.total_epoch)]) # epochs
y_axis = np.zeros(self.total_epoch)
plt.xlim(0, self.total_epoch)
plt.ylim(0, 100)
interval_y = 5
interval_x = 1
plt.xticks(np.arange(0, self.total_epoch + interval_x, interval_x))
plt.yticks(np.arange(0, 100 + interval_y, interval_y))
plt.grid()
plt.title(title, fontsize=20)
plt.xlabel('the training epoch', fontsize=16)
plt.ylabel('accuracy', fontsize=16)
y_axis[:] = self.epoch_accuracy[:, 0]
plt.plot(x_axis, y_axis, color='g', linestyle='-', label='train-accuracy', lw=2)
plt.legend(loc=4, fontsize=legend_fontsize)
y_axis[:] = self.epoch_accuracy[:, 1]
plt.plot(x_axis, y_axis, color='y', linestyle='-', label='valid-accuracy', lw=2)
plt.legend(loc=4, fontsize=legend_fontsize)
y_axis[:] = self.epoch_losses[:, 0]
plt.plot(x_axis, y_axis, color='g', linestyle=':', label='train-loss-x50', lw=2)
plt.legend(loc=4, fontsize=legend_fontsize)
y_axis[:] = self.epoch_losses[:, 1]
plt.plot(x_axis, y_axis, color='y', linestyle=':', label='valid-loss-x50', lw=2)
plt.legend(loc=4, fontsize=legend_fontsize)
if save_path is not None:
fig.savefig(save_path, dpi=dpi, bbox_inches='tight')
# print('Curve was saved')
plt.close(fig)
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`.
"""
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], fmt), fontsize=12,
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.ylabel('True label', fontsize=18)
plt.xlabel('Predicted label', fontsize=18)
plt.tight_layout()
def computer_uar_war(val_loader, model, checkpoint_path, data_set):
pre_trained_dict = torch.load(checkpoint_path)['state_dict']
model.load_state_dict(pre_trained_dict)
model.eval()
correct = 0
with torch.no_grad():
for i, (images, target, audio) in enumerate(tqdm.tqdm(val_loader)):
images = images.cuda()
target = target.cuda()
audio = audio.cuda()
output = model(images, audio)
predicted = output.argmax(dim=1, keepdim=True)
correct += predicted.eq(target.view_as(predicted)).sum().item()
if i == 0:
all_predicted = predicted
all_targets = target
else:
all_predicted = torch.cat((all_predicted, predicted), 0)
all_targets = torch.cat((all_targets, target), 0)
war = 100. * correct / len(val_loader.dataset)
# Compute confusion matrix
_confusion_matrix = confusion_matrix(all_targets.data.cpu().numpy(), all_predicted.cpu().numpy())
np.set_printoptions(precision=4)
normalized_cm = _confusion_matrix.astype('float') / _confusion_matrix.sum(axis=1)[:, np.newaxis]
normalized_cm = normalized_cm * 100
list_diag = np.diag(normalized_cm)
uar = list_diag.mean()
print("Confusion Matrix Diag:", list_diag)
print("UAR: %0.2f" % uar)
print("WAR: %0.2f" % war)
return uar, war
if __name__ == '__main__':
args = parse_args()
print('************************')
for k, v in vars(args).items():
print(k,'=',v)
print('************************')
uar, war = main(args.fold, args)
print('********* Final Results *********')
print("UAR: %0.2f" % (uar))
print("WAR: %0.2f" % (war))
print('*********************************')