-
Notifications
You must be signed in to change notification settings - Fork 77
/
test_10crop.py
44 lines (37 loc) · 1.54 KB
/
test_10crop.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
import matplotlib.pyplot as plt
import torch
from sklearn.metrics import auc, roc_curve, precision_recall_curve
import numpy as np
def test(dataloader, model, args, viz, device):
with torch.no_grad():
model.eval()
pred = torch.zeros(0, device=device)
for i, input in enumerate(dataloader):
input = input.to(device)
input = input.permute(0, 2, 1, 3)
score_abnormal, score_normal, feat_select_abn, feat_select_normal, feat_abn_bottom, feat_select_normal_bottom, logits, \
scores_nor_bottom, scores_nor_abn_bag, feat_magnitudes = model(inputs=input)
logits = torch.squeeze(logits, 1)
logits = torch.mean(logits, 0)
sig = logits
pred = torch.cat((pred, sig))
if args.dataset == 'shanghai':
gt = np.load('list/gt-sh.npy')
else:
gt = np.load('list/gt-ucf.npy')
pred = list(pred.cpu().detach().numpy())
pred = np.repeat(np.array(pred), 16)
fpr, tpr, threshold = roc_curve(list(gt), pred)
np.save('fpr.npy', fpr)
np.save('tpr.npy', tpr)
rec_auc = auc(fpr, tpr)
print('auc : ' + str(rec_auc))
precision, recall, th = precision_recall_curve(list(gt), pred)
pr_auc = auc(recall, precision)
np.save('precision.npy', precision)
np.save('recall.npy', recall)
viz.plot_lines('pr_auc', pr_auc)
viz.plot_lines('auc', rec_auc)
viz.lines('scores', pred)
viz.lines('roc', tpr, fpr)
return rec_auc