-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacc_analysis.py
385 lines (306 loc) · 13.7 KB
/
acc_analysis.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
import os
import time
from PIL import Image
import numpy as np
from sklearn import metrics
from matplotlib import pyplot as plt
import argparse
import torch
import torchvision.transforms as transforms
from vidaug import augment as aug
from Code_tomse2 import Model_Parts, util
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', default=r'/DATA_VD/mood_face/Data-S375-align', type=str, metavar='N',
help='the directory of videos need to be predicted')
parser.add_argument('--label_list', default=r'./Data/acc_test.txt', type=str,
help='the list of video names need to be predicted')
parser.add_argument('--save_dir', default=r'./Data', type=str, metavar='N',
help='the directory where preditions need to be predicted')
parser.add_argument('--model_dir', default=r'./model/03-23_19-37_lr1e-05wd0.001/model/epoch29_loss_0.0584_acc_0.982', type=str, metavar='N',
help='the directory where preditions need to be predicted')
parser.add_argument('--num_classes', default=2, type=int, help='predicted class')
parser.add_argument('--batch_size', default=8, type=int, help='batch size')
parser.add_argument('--loss_alpha', default=0.1, type=float,
help='adjust loss for crossentrophy')
args = parser.parse_args()
return args
def get_savepath(save_dir, model_dir):
save_path = os.path.join(save_dir, model_dir.split('/')[2], model_dir.split('/')[4])
if os.path.exists(save_path):
pass
else:
os.makedirs(save_path)
return save_path
# load model
def LoadParameter(_structure, _parameterDir):
device = "cuda" if torch.cuda.is_available() else "cpu"
checkpoint = torch.load(_parameterDir, map_location=torch.device(device))
pretrained_state_dict = checkpoint['state_dict']
model_state_dict = _structure.state_dict()
for key in pretrained_state_dict:
model_state_dict[key.replace('module.', '')] = pretrained_state_dict[key]
_structure.load_state_dict(model_state_dict)
return _structure
# load data
def load_imgs_total_frame(video_root, video_list):
imgs_first_dict = {}
imgs_first = []
video_names = []
with open(video_list, 'r') as imf:
imf = imf.readlines()
for id, line in enumerate(imf):
video_label = line.strip().split(' ')
video_name, fatigue = video_label
fatigue = (np.float32(fatigue) - 1) / 4.0
if video_name.split('.')[-1] == 'mp4':
video_path = os.path.join(video_root, video_name.replace(".mp4", ""))
elif video_name.split('.')[-1] == 'mov':
video_path = os.path.join(video_root, video_name.replace(".mov", ""))
else:
video_path = os.path.join(video_root, video_name)
img_lists = os.listdir(video_path)
img_lists.sort(key=lambda x: int(x.split('.')[0])) # sort files by ascending
imgs_first_dict[video_name] = []
for frame in img_lists:
imgs_first_dict[video_name].append(
(os.path.join(video_path, frame), fatigue))
### return video frame index #####
ed_list = sample_seg_full(imgs_first_dict[video_name])
imgs_first.append(ed_list)
video_names.append(video_name)
return imgs_first, video_names
def sample_seg_full(orig_list, seg_num=32):
ed_list = []
part = int(len(orig_list)) // seg_num
if part == 0:
print('less 32')
else:
for n in range(int(part)):
ed_list.append(orig_list[n * seg_num: n * seg_num + seg_num])
return ed_list
# dataloader
class PredDataSet(torch.utils.data.Dataset):
'''
This dataset return entire frames for a video. this means that the number of return for each time is different.
sample_rate: num_of_image per second
'''
def __init__(self, imgs_dict, transform=None, transformVideoAug=None):
self.imgs_first_dict = imgs_dict
self.transform = transform
self.transformVideoAug = transformVideoAug
def __getitem__(self, index):
image_label = self.imgs_first_dict[index]
image_list = []
# target_list = []
for item, fatigue in image_label:
img = Image.open(item).convert("RGB")
img_ = img
image_list.append(img_)
sample = float(fatigue)
target_list = sample
# target_list.append(sample)
if self.transformVideoAug is not None:
image_list = self.transformVideoAug(image_list)
if self.transform is not None:
image_list = [self.transform(image) for image in image_list]
image_list = torch.stack(image_list, dim=0)
target_list = [torch.tensor(target_list)]
return image_list, target_list
def __len__(self):
return len(self.imgs_first_dict)
def LoadPredData(imgs_dict, batch_size):
pred_dataset = PredDataSet(
imgs_dict=imgs_dict,
# transformVideoAug=transforms.Compose([aug.Resize([112, 112])]),
transform=transforms.Compose([transforms.ToTensor(),
transforms.Normalize(mean=(0.5, 0.5, 0.5),
std=(0.5, 0.5, 0.5))])
)
pred_loader = torch.utils.data.DataLoader(
pred_dataset,
batch_size=batch_size, shuffle=False,
num_workers=0, pin_memory=True, drop_last=False)
return pred_loader
# prediction
def predict(data_id, val_loader, model, criterion1, criterion2, loss_alpha, summary_statistics):
device = "cuda" if torch.cuda.is_available() else "cpu"
losses = util.AverageMeter()
data_time = util.AverageMeter()
accuracies = util.AverageMeter()
end = time.time()
mse_list = []
ce_list = []
label_list = []
con_label_list = []
model.eval()
with torch.no_grad():
for batch_idx, (input_image, sample) in enumerate(val_loader):
sample = sample[0]
sample_catego = util.label_to_categorical(sample, args.num_classes)
sample = sample.to(device)
sample_catego = sample_catego.to(device)
input_var = torch.autograd.Variable(input_image).permute((0, 2, 1, 3, 4))
input_var = input_var.to(device)
outputs = model(input_var)
summary_statistics.update(sample_catego.data.cpu().numpy(), outputs.data.cpu().numpy())
fatigue_loss_ce = criterion1(outputs, sample_catego)
outputs_cont = util.output_tomse(outputs, args.num_classes)
fatigue_loss_mse = criterion2(outputs_cont, sample)
acc = util.calculate_accuracy(outputs, sample_catego)
loss = loss_alpha * fatigue_loss_ce + fatigue_loss_mse
accuracies.update(acc, input_var.size(0))
losses.update(loss.item(), input_var.size(0))
mse_list.append(outputs_cont)
ce_list.append(outputs)
label_list.append(sample_catego)
con_label_list.append(sample)
# measure prediction time
data_time.update(time.time() - end)
# outputs and targets for model
mses = torch.cat(mse_list)
ces = torch.cat(ce_list)
labels = torch.cat(label_list)
con_labels = torch.cat(con_label_list)
''' Compute Loss '''
print(' Data {} prediction overview=============='.format(data_id))
print(' Pred Time: {}'.format(round(float(data_time.avg), 3)))
print(' Loss: {}'.format(round(float(losses.avg), 4)))
print(' Avg Acc: {}'.format(round(float(accuracies.avg), 3)))
return losses.avg, accuracies.avg, mses, ces, labels, con_labels
class SummaryStatistics(object):
"""Generate train/test summary stats
Tracks the following metrics:
- Confusion Matrix
- Average & Per-class precision
- Average & Per-class recall
- Average & Per-class acuuracy
- Average & Per-class f1-score
"""
def __init__(self, n_classes=4):
"""Constructor
Args:
n_classes (int, optional): Number of output classes. Defaults to 4.
"""
self.n_classes = n_classes
self.reset() #confusion matrix
def reset(self):
"""Reset the stats."""
self.confusion_matrix = np.zeros((self.n_classes, self.n_classes))
def update(self, true_labels, pred_labels):
"""Update the confusion matrix and metrics
Args:
true_labels (np.array): Actual labels
pred_labels (np.array): Predicted labels
"""
if len(pred_labels.shape) > 1:
pred_labels = np.argmax(pred_labels, axis=-1) #output the class with largest probability
conf_matrix = np.bincount(
self.n_classes * true_labels.astype(int) + pred_labels.astype(int),
minlength=self.n_classes ** 2
).reshape(self.n_classes, self.n_classes)
self.confusion_matrix += conf_matrix
def get_metrics(self):
"""Generate/Retrieve the summary metrics.
Returns:
[dict]: All metrics mentioned above.
"""
conf_matrix = self.confusion_matrix
precision_per_class = np.nan_to_num(
np.diag(conf_matrix) / np.sum(conf_matrix, axis=0))
recall_per_class = np.nan_to_num(
np.diag(conf_matrix) / np.sum(conf_matrix, axis=1))
acc_per_class = np.nan_to_num(np.diag(conf_matrix) / (np.sum(
conf_matrix, axis=1) + np.sum(conf_matrix, axis=0) - np.diag(conf_matrix)))
f1_per_class = np.nan_to_num(
2 * precision_per_class * recall_per_class / (precision_per_class + recall_per_class))
avg_precision = np.nanmean(precision_per_class)
avg_recall = np.nanmean(recall_per_class)
avg_acc = np.nanmean(acc_per_class)
avg_f1 = 2 * avg_precision * avg_recall / (avg_precision + avg_recall)
result = {
'conf_matrix': conf_matrix,
'stats_per_class': {
'class_precision': precision_per_class,
'class_recall': recall_per_class,
'class_accuracy': acc_per_class,
'class_f1': f1_per_class
},
'avg_stats': {
'avg_precision': avg_precision,
'avg_recall': avg_recall,
'avg_accuracy': avg_acc,
'avg_f1': avg_f1
}
}
return result
args = get_args()
pred_save_path = get_savepath(args.save_dir, args.model_dir)
model = Model_Parts.FullModal_VisualFeatureAttention(num_class=args.num_classes, feature_dim=256, non_local_pos=3,
first_channel=64)
model = LoadParameter(model, args.model_dir)
model = torch.nn.DataParallel(model)
criterion1 = torch.nn.CrossEntropyLoss()
criterion2 = torch.nn.MSELoss()
summary_statistics = SummaryStatistics(args.num_classes)
ce_results_agg = []
labels_agg = []
imgs_first, video_names = load_imgs_total_frame(args.data_dir, args.label_list)
for data_id, (imgs_dict, video_name) in enumerate(zip(imgs_first, video_names)):
pred_loader = LoadPredData(imgs_dict, args.batch_size)
totalloss, acc, mse_results, ce_results, labels, con_labels = predict(data_id, pred_loader, model, criterion1,
criterion2, args.loss_alpha, summary_statistics)
_, pred = ce_results.topk(1, 1, largest=True, sorted=True)
pred = pred.t().squeeze(0)
avg_labels = 1 if len(pred[pred == 1]) > len(pred) - len(pred[pred == 1]) else 0
avg_mse_labels = mse_results.mean().cpu().numpy()
with open(os.path.join(pred_save_path, "prediction.txt"), "a+") as f:
f.write(video_name + ' ' + str(totalloss) + ' ' + str(acc) + ' ' +
str(avg_mse_labels) + ' ' + str(con_labels[0].cpu().numpy()) + ' ' +
str(avg_labels) + ' ' + str(labels[0].cpu().numpy()) + '\n')
ce_results_agg.append(ce_results.cpu())
labels_agg.append(labels.cpu())
result_summary = summary_statistics.get_metrics()
whole_ce_results = torch.cat(ce_results_agg)
whole_labels = torch.cat(labels_agg)
whole_probs = torch.nn.functional.softmax(whole_ce_results, dim=1)
# 计算每一类的ROC
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(args.num_classes):
fpr[i], tpr[i], _ = metrics.roc_curve(whole_labels.numpy(), whole_probs[:, i].numpy(), pos_label=i)
roc_auc[i] = metrics.auc(fpr[i], tpr[i])
# Compute macro-average ROC curve and ROC area
# First aggregate all false positive rates
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(args.num_classes)]))
# Then interpolate all ROC curves at this points
mean_tpr = np.zeros_like(all_fpr)
for i in range(args.num_classes):
mean_tpr += np.interp(all_fpr, fpr[i], tpr[i])
# Finally average it and compute AUC
mean_tpr /= args.num_classes
fpr["avg"] = all_fpr
tpr["avg"] = mean_tpr
roc_auc["avg"] = metrics.auc(fpr["avg"], tpr["avg"])
result_summary['roc_auc'] = roc_auc
with open(os.path.join(pred_save_path, 'pred_summary.txt'), 'w') as handle:
handle.write(str(result_summary))
for key in fpr.keys():
roc_data = np.vstack((fpr[key], tpr[key])).T
np.savetxt(os.path.join(pred_save_path, 'roc_data_class{}.txt'.format(key)), roc_data, delimiter=' ')
plt.clf()
plt.figure()
colors = ['green', 'darkorange', 'navy']
for key, color in zip(fpr.keys(), colors):
plt.plot(fpr[key], tpr[key], color=color,
lw=2, label='ROC curve of class {} (area = {:.2f})'.format(key, roc_auc[key]))
plt.plot([0, 1], [0, 1], color='gray', lw=2, linestyle='--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver Operating Characteristic Curve')
plt.legend(loc="lower right")
plt.savefig(os.path.join(pred_save_path, 'roc_curve.jpg'))
# plt.show()