forked from shubhamOjha1000/Optimizing-Conformal-Prediction-Sets-for-Pathological-Image_Classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCP_methods.py
199 lines (123 loc) · 5.64 KB
/
CP_methods.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import torch
import torch.nn as nn
import torch.nn.functional as F
class THR():
def __init__(self, softmax, true_class, alpha):
self.prob_output = softmax
self.true_class = true_class
self.alpha = alpha * (1 + (1/softmax.shape[0]))
def conformal_score(self):
conformal_score = self.prob_output[range(self.prob_output.shape[0]), self.true_class]
return conformal_score
def quantile(self):
conformal_scores = self.conformal_score()
quantile_value = torch.quantile(conformal_scores, self.alpha)
return quantile_value
def prediction(self, softmax, quantile_value):
prob_output = softmax
predictions = prob_output >= quantile_value
predictions = predictions.int()
return predictions
class RAPS():
def __init__(self, softmax, true_class, alpha, k_reg, lambd, rand=True):
self.prob_output = softmax
self.true_class = true_class
self.alpha = (1 - alpha) * (1 + (1 / softmax.shape[0]))
self.k_reg = k_reg
self.lambd = lambd
self.rand = rand
def conformal_score(self):
conformal_score = []
for i in range(self.prob_output.shape[0]):
true_class_prob = self.prob_output[i][self.true_class[i]]
current_class_prob = self.prob_output[i]
sorted_class_prob, _ = torch.sort(current_class_prob, descending=True)
index = torch.nonzero(sorted_class_prob == true_class_prob).item()
cumulative_sum = torch.sum(sorted_class_prob[:index + 1])
if index - self.k_reg > 0:
cumulative_sum = cumulative_sum + self.lambd*(index - self.k_reg)
if self.rand:
U = torch.rand(1).item()
cumulative_sum = cumulative_sum - U*sorted_class_prob[index]
conformal_score.append(cumulative_sum)
conformal_score = torch.tensor(conformal_score)
return conformal_score
def quantile(self):
conformal_scores = self.conformal_score()
quantile_value = torch.quantile(conformal_scores, self.alpha)
return quantile_value
def prediction(self, softmax, quantile_value):
prob_output = softmax
prediction = torch.zeros(prob_output.shape[0], prob_output.shape[1])
for i in range(prob_output.shape[0]):
current_class_prob = prob_output[i]
sorted_class_prob, _ = torch.sort(current_class_prob, descending=True)
sum = 0
j = 0
for idx in range(len(sorted_class_prob)):
if sum <= quantile_value:
sum += sorted_class_prob[idx]
if idx - self.k_reg > 0:
sum = sum + self.lambd*(idx - self.k_reg)
j += 1
else:
break
if j != prob_output.shape[1]:
j += 1
"""
if self.rand:
U = torch.rand(1).item()
if j != prob_output.shape[1]:
N = torch.sum(sorted_class_prob[:j + 1]) - quantile_value
else:
N = torch.sum(sorted_class_prob[:j]) - quantile_value
if idx - self.k_reg > 0:
N += self.lambd*(j - self.k_reg)
if j != prob_output.shape[1]:
D = sorted_class_prob[j]
else:
D = sorted_class_prob[j-1]
if idx - self.k_reg > 0:
D += self.lambd
if N/D <= U:
j = j -1
"""
for idx in range(j):
index = torch.nonzero(current_class_prob == sorted_class_prob[idx]).item()
prediction[i][index] = 1.0
return prediction
class APS():
def __init__(self, softmax, true_class, alpha):
self.prob_output = softmax
self.true_class = true_class
self.alpha = (1 - alpha) * (1 + (1 / softmax.shape[0]))
def conformal_score(self):
conformal_score = []
for i in range(self.prob_output.shape[0]):
true_class_prob = self.prob_output[i][self.true_class[i]]
current_class_prob = self.prob_output[i]
sorted_class_prob, _ = torch.sort(current_class_prob, descending=True)
index = torch.nonzero(sorted_class_prob == true_class_prob).item()
cumulative_sum = torch.sum(sorted_class_prob[:index + 1])
conformal_score.append(cumulative_sum)
conformal_score = torch.tensor(conformal_score)
return conformal_score
def quantile(self):
conformal_scores = self.conformal_score()
quantile_value = torch.quantile(conformal_scores, self.alpha)
return quantile_value
def prediction(self, softmax, quantile_value):
prob_output = softmax
prediction = torch.zeros(prob_output.shape[0], prob_output.shape[1])
for i in range(prob_output.shape[0]):
current_class_prob = prob_output[i]
sorted_class_prob, _ = torch.sort(current_class_prob, descending=True)
sum = 0
for idx in range(len(sorted_class_prob)):
if sum <= quantile_value:
sum += sorted_class_prob[idx]
index = torch.nonzero(current_class_prob == sorted_class_prob[idx]).item()
prediction[i][index] = 1.0
else:
break
return prediction