-
Notifications
You must be signed in to change notification settings - Fork 3
/
pos_score_calculator.py
165 lines (130 loc) · 4.76 KB
/
pos_score_calculator.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
from __future__ import print_function
from __future__ import absolute_import
import os
import numpy as np
import time
import logging
import argparse
from collections import OrderedDict
import faiss
import torch
import torch.nn as nn
from tqdm import tqdm
from models import SupResNet, SSLResNet
from utils import (
get_features,
get_roc_sklearn,
get_pr_sklearn,
get_fpr,
get_scores_one_cluster,
)
import sklearn.metrics as skm
import data
# local utils for evaluation
def get_scores(ftrain, ftest, food, clusters):
if clusters == 1:
return get_scores_one_cluster(ftrain, ftest, food)
else:
ypred = get_clusters(ftrain, clusters)
return get_scores_multi_cluster(ftrain, ftest, food, ypred)
def get_clusters(ftrain, nclusters):
kmeans = faiss.Kmeans(
ftrain.shape[1], nclusters, niter=100, verbose=False, gpu=False
)
ftrain = ftrain.astype(np.float32)
kmeans.train(np.random.permutation(ftrain))
_, ypred = kmeans.assign(ftrain)
return ypred
def get_scores_multi_cluster(ftrain, ftest, food, ypred):
xc = [ftrain[ypred == i] for i in np.unique(ypred)]
din = [
np.sum(
(ftest - np.mean(x, axis=0, keepdims=True))
* (
np.linalg.pinv(np.cov(x.T, bias=True)).dot(
(ftest - np.mean(x, axis=0, keepdims=True)).T
)
).T,
axis=-1,
)
for x in xc
]
dood = [
np.sum(
(food - np.mean(x, axis=0, keepdims=True))
* (
np.linalg.pinv(np.cov(x.T, bias=True)).dot(
(food - np.mean(x, axis=0, keepdims=True)).T
)
).T,
axis=-1,
)
for x in xc
]
din = np.min(din, axis=0)
dood = np.min(dood, axis=0)
return din, dood
def get_eval_results_score(ftrain, ftest, food, clusters):
"""
None.
"""
# standardize data
ftrain /= np.linalg.norm(ftrain, axis=-1, keepdims=True) + 1e-10
ftest /= np.linalg.norm(ftest, axis=-1, keepdims=True) + 1e-10
food /= np.linalg.norm(food, axis=-1, keepdims=True) + 1e-10
m, s = np.mean(ftrain, axis=0, keepdims=True), np.std(ftrain, axis=0, keepdims=True)
ftrain = (ftrain - m) / (s + 1e-10)
ftest = (ftest - m) / (s + 1e-10)
food = (food - m) / (s + 1e-10)
dtest, dood = get_scores(ftrain, ftest, food, clusters)
#Calculate the distance of each data point in the test and the distance of each data point in the ood, respectively.
fpr95 = get_fpr(dtest, dood)
auroc, aupr = get_roc_sklearn(dtest, dood), get_pr_sklearn(dtest, dood)
return fpr95, auroc, aupr
# return dtest,dood
def get_eval_results_dis_feats(ftrain, ftest, food, clusters):
"""
None.
"""
# standardize data
ftrain /= np.linalg.norm(ftrain, axis=-1, keepdims=True) + 1e-10
ftest /= np.linalg.norm(ftest, axis=-1, keepdims=True) + 1e-10
food /= np.linalg.norm(food, axis=-1, keepdims=True) + 1e-10
m, s = np.mean(ftrain, axis=0, keepdims=True), np.std(ftrain, axis=0, keepdims=True)
ftrain = (ftrain - m) / (s + 1e-10)
ftest = (ftest - m) / (s + 1e-10)
food = (food - m) / (s + 1e-10)
dtest, dood = get_scores(ftrain, ftest, food, clusters)
#Calculate the distance of each data point in the test and the distance of each data point in the ood, respectively.
return dtest,dood
def get_score_and_dis(num_cluster,seed,neg_feats_training,neg_feats_testing,pos_neg_feats_testing,pos_pos_feats_testing):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
all_test_neg = np.vstack((neg_feats_testing,pos_neg_feats_testing))
fpr95, auroc, aupr = get_eval_results_score(
np.copy(neg_feats_training),
np.copy(all_test_neg),
np.copy(pos_pos_feats_testing), num_cluster)
return auroc
def get_score_and_dis_MAE(num_cluster,seed,neg_feats_training,neg_feats_testing,pos_feats_testing):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
fpr95, auroc, aupr = get_eval_results_score(
np.copy(neg_feats_training),
np.copy(neg_feats_testing),
np.copy(pos_feats_testing), num_cluster)
return auroc
def get_score_and_dis_feats(num_cluster,seed,neg_feats_training,pos_feats_training):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
dis_neg_train,dis_pos_train = get_eval_results_dis_feats(
np.copy(neg_feats_training),
np.copy(neg_feats_training), #Calculate the more abnormal inside itself
np.copy(pos_feats_training), num_cluster)
return dis_neg_train, dis_pos_train
def get_auc_score(labels,data):
auroc = skm.roc_auc_score(labels, data)
return auroc