-
Notifications
You must be signed in to change notification settings - Fork 8
/
score.py
82 lines (66 loc) · 2.98 KB
/
score.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from util.mahalanobis_lib import get_Mahalanobis_score
def get_msp_score(inputs, model, forward_func, method_args, logits=None):
if logits is None:
with torch.no_grad():
logits = forward_func(inputs, model)
scores = np.max(F.softmax(logits, dim=1).detach().cpu().numpy(), axis=1)
return scores
def get_energy_score(inputs, model, forward_func, method_args, logits=None):
if logits is None:
with torch.no_grad():
logits = forward_func(inputs, model)
scores = torch.logsumexp(logits.data.cpu(), dim=1).numpy()
return scores
def get_odin_score(inputs, model, forward_func, method_args):
temper = method_args['temperature']
noiseMagnitude1 = method_args['magnitude']
criterion = nn.CrossEntropyLoss()
inputs = torch.autograd.Variable(inputs, requires_grad = True)
# outputs = model(inputs)
outputs = forward_func(inputs, model)
maxIndexTemp = np.argmax(outputs.data.cpu().numpy(), axis=1)
# Using temperature scaling
outputs = outputs / temper
labels = torch.autograd.Variable(torch.LongTensor(maxIndexTemp).cuda())
loss = criterion(outputs, labels)
loss.backward()
# Normalizing the gradient to binary in {0, 1}
gradient = torch.ge(inputs.grad.data, 0)
gradient = (gradient.float() - 0.5) * 2
# Adding small perturbations to images
tempInputs = torch.add(inputs.data, -noiseMagnitude1, gradient)
# outputs = model(Variable(tempInputs))
with torch.no_grad():
outputs = forward_func(tempInputs, model)
outputs = outputs / temper
# Calculating the confidence after adding perturbations
nnOutputs = outputs.data.cpu()
nnOutputs = nnOutputs.numpy()
nnOutputs = nnOutputs - np.max(nnOutputs, axis=1, keepdims=True)
nnOutputs = np.exp(nnOutputs) / np.sum(np.exp(nnOutputs), axis=1, keepdims=True)
scores = np.max(nnOutputs, axis=1)
return scores
def get_mahalanobis_score(inputs, model, method_args):
num_classes = method_args['num_classes']
sample_mean = method_args['sample_mean']
precision = method_args['precision']
magnitude = method_args['magnitude']
regressor = method_args['regressor']
num_output = method_args['num_output']
Mahalanobis_scores = get_Mahalanobis_score(inputs, model, num_classes, sample_mean, precision, num_output, magnitude)
scores = -regressor.predict_proba(Mahalanobis_scores)[:, 1]
return scores
def get_score(inputs, model, forward_func, method, method_args, logits=None):
if method == "msp":
scores = get_msp_score(inputs, model, forward_func, method_args, logits)
elif method == "odin":
scores = get_odin_score(inputs, model, forward_func, method_args)
elif method == "energy":
scores = get_energy_score(inputs, model, forward_func, method_args, logits)
elif method == "mahalanobis":
scores = get_mahalanobis_score(inputs, model, method_args)
return scores