forked from nghia9691/OSDN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openmax.py
91 lines (65 loc) · 3.96 KB
/
openmax.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
import numpy as np
from sklearn.metrics.pairwise import paired_distances
from evt import query_weibull
def compute_distance(a, b):
return paired_distances(a, b, metric="cosine", n_jobs=1)
def recalibrate_scores(activation_vector, weibull_model, labels, alpha_rank=10):
ranked_list = activation_vector.argsort().ravel()[::-1]
alpha_weights = [((alpha_rank + 1) - i) / float(alpha_rank) for i in range(1, alpha_rank + 1)]
ranked_alpha = np.zeros((len(labels)))
for i in range(alpha_rank):
ranked_alpha[ranked_list[i]] = alpha_weights[i]
# Now recalibrate score for each class to include probability of unknown
openmax_score = []
openmax_score_unknown = []
for label_index, label in enumerate(labels):
# get distance between current channel and mean vector
weibull = query_weibull(label, weibull_model)
av_distance = compute_distance(activation_vector.reshape(1, -1), weibull[0][0])
# obtain w_score for the distance and compute probability of the distance being unknown wrt to mean training vector
wscore = weibull[2][0].w_score(av_distance)
modified_score = activation_vector[label_index] * (1 - wscore * ranked_alpha[label_index])
openmax_score += [modified_score]
openmax_score_unknown += [activation_vector[label_index] - modified_score]
openmax_score = np.array(openmax_score)
openmax_score_unknown = np.array(openmax_score_unknown)
# Pass the re-calibrated scores for the image into OpenMax
openmax_probab = compute_openmax_probability(openmax_score, openmax_score_unknown, labels)
softmax_probab = compute_softmax_probability(activation_vector) # Calculate SoftMax ???
return np.array(openmax_probab), softmax_probab
def compute_softmax_probability(scores):
exp_scores = np.exp(scores)
return exp_scores / np.sum(exp_scores)
def compute_openmax_probability(openmax_score, openmax_score_unknown, labels):
exp_scores = []
for label_index, label in enumerate(labels):
exp_scores += [np.exp(openmax_score[label_index])]
total_denominator = np.sum(np.exp(openmax_score)) + np.exp(np.sum(openmax_score_unknown))
prob_scores = np.array(exp_scores) / total_denominator
prob_unknown = np.exp(np.sum(openmax_score_unknown)) / total_denominator
return prob_scores.tolist() + [prob_unknown]
# -------------------------------------------------------------------------------------------------
def recalibrate_scores_custom(activation_vector, softmax_score, weibull_model, labels, alpha_rank=10):
ranked_list = softmax_score.argsort().ravel()[::-1]
alpha_weights = [((alpha_rank + 1) - i) / float(alpha_rank) for i in range(1, alpha_rank + 1)]
ranked_alpha = np.zeros((len(labels)))
for i in range(alpha_rank):
ranked_alpha[ranked_list[i]] = alpha_weights[i]
# Now recalibrate score for each class to include probability of unknown
openmax_score = []
openmax_score_unknown = []
for label_index, label in enumerate(labels):
# get distance between current channel and mean vector
weibull = query_weibull(label, weibull_model)
av_distance = compute_distance(activation_vector.reshape(1, -1), weibull[0][0])
# obtain w_score for the distance and compute probability of the distance being unknown wrt to mean training vector
wscore = weibull[2][0].w_score(av_distance)
modified_score = softmax_score[label_index] * (1 - wscore * ranked_alpha[label_index])
openmax_score += [modified_score]
openmax_score_unknown += [softmax_score[label_index] - modified_score]
openmax_score = np.array(openmax_score)
openmax_score_unknown = np.array(openmax_score_unknown)
# Pass the re-calibrated scores for the image into OpenMax
openmax_probab = compute_openmax_probability(openmax_score, openmax_score_unknown, labels)
softmax_probab = compute_softmax_probability(softmax_score) # Calculate SoftMax ???
return np.array(openmax_probab), softmax_probab