forked from cvlab-yonsei/MNAD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
68 lines (46 loc) · 1.81 KB
/
utils.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
import math
import copy
import torch
import numpy as np
import torch.nn as nn
from sklearn.metrics import roc_auc_score
def rmse(predictions, targets):
return np.sqrt(((predictions - targets) ** 2).mean())
def psnr(mse):
return 10 * math.log10(1 / mse)
def get_lr(optimizer):
for param_group in optimizer.param_groups:
return param_group['lr']
pass
def normalize_img(img):
img_re = copy.copy(img)
img_re = (img_re - np.min(img_re)) / (np.max(img_re) - np.min(img_re))
return img_re
def point_score(outputs, imgs):
loss_func_mse = nn.MSELoss(reduction='none')
error = loss_func_mse((outputs[0]+1)/2,(imgs[0]+1)/2)
normal = (1-torch.exp(-error))
score = (torch.sum(normal*loss_func_mse((outputs[0]+1)/2,(imgs[0]+1)/2)) / torch.sum(normal)).item()
return score
def anomaly_score(psnr, max_psnr, min_psnr):
return ((psnr - min_psnr) / (max_psnr-min_psnr))
def anomaly_score_inv(psnr, max_psnr, min_psnr):
return (1.0 - ((psnr - min_psnr) / (max_psnr-min_psnr)))
def anomaly_score_list(psnr_list):
anomaly_score_list = list()
for i in range(len(psnr_list)):
anomaly_score_list.append(anomaly_score(psnr_list[i], np.max(psnr_list), np.min(psnr_list)))
return anomaly_score_list
def anomaly_score_list_inv(psnr_list):
anomaly_score_list = list()
for i in range(len(psnr_list)):
anomaly_score_list.append(anomaly_score_inv(psnr_list[i], np.max(psnr_list), np.min(psnr_list)))
return anomaly_score_list
def AUC(anomal_scores, labels):
frame_auc = roc_auc_score(y_true=np.squeeze(labels, axis=0), y_score=np.squeeze(anomal_scores))
return frame_auc
def score_sum(list1, list2, alpha):
list_result = []
for i in range(len(list1)):
list_result.append((alpha*list1[i]+(1-alpha)*list2[i]))
return list_result