forked from shubhamOjha1000/Optimizing-Conformal-Prediction-Sets-for-Pathological-Image_Classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
334 lines (199 loc) · 11 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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import torch.nn as nn
import torch
import torch.nn.functional as F
import math
from sklearn.metrics import roc_auc_score
from sklearn.metrics import accuracy_score
def Multiclass_classification_metrices(y_true, y_pred, num_classes):
accuracy = accuracy_score(y_true, y_pred)
# AUC :-
auc = []
for i in range(num_classes):
y_true_class = [1 if label == i else 0 for label in y_true]
y_pred_class = [1 if pred == i else 0 for pred in y_pred]
try:
auc_class = roc_auc_score(y_true_class, y_pred_class)
except ValueError as e:
print("ValueError occurred:", e)
auc_class = 0
auc.append(auc_class)
macro_AUC = sum(auc)/num_classes
return macro_AUC, accuracy
def avg_set_size_metric(conformal_set):
lengths = torch.sum(conformal_set, dim=1)
avg_set_size_len = torch.sum(lengths)/conformal_set.shape[0]
return avg_set_size_len
def coverage_gap_metric(conformal_set, df_true_class_test, alpha):
true_class = conformal_set[range(conformal_set.shape[0]), df_true_class_test]
tensor_sum = torch.sum(true_class)
coverage = tensor_sum/true_class.shape[0]
coverage_gap = (abs((1-alpha) - coverage)/(1-alpha))*100
return coverage_gap, coverage
def hinge_loss(metric, output_logits, label, distance_in_hinge_loss, dataset):
loss = 0
x = distance_in_hinge_loss
if dataset == 'Cervical_cancer' :
for i in range(output_logits.shape[0]):
true_class_logits = output_logits[i][label[i]]
current_logits = output_logits[i]
if metric == 'class_Overlap_metric':
if label[i] == 0 or label[i] == 1 or label[i] == 2:
#margin_list = [1, 1, 1, 5, 5, 5, 5, 5]
margin_list = [1, 1, 1, x, x, x, x, x]
margin_list[label[i]] = 0
elif label[i] == 4 or label[i] == 6 or label[i] == 7:
#margin_list = [5, 5, 5, 5, 1, 5, 1, 1]
margin_list = [x, x, x, x, 1, x, 1, 1]
margin_list[label[i]] = 0
elif label[i] == 3 or label[i] == 5:
#margin_list = [5, 5, 5, 1, 5, 1, 5, 5]
margin_list = [x, x, x, 1, x, 1, x, x]
margin_list[label[i]] = 0
elif metric == 'expt2':
if label[i] == 0 or label[i] == 1 or label[i] == 2:
#margin_list = [5, 5, 5, 5, 5, 5, 5, 5]
#margin_list = [20, 20, 20, 20, 20, 20, 20, 20]
margin_list = [x, x, x, x, x, x, x, x]
margin_list[label[i]] = 0
elif label[i] == 4 or label[i] == 6 or label[i] == 7:
#margin_list = [5, 5, 5, 5, 1, 5, 1, 1]
#margin_list = [10, 10, 10, 10, 1, 10, 1, 1]
margin_list = [x, x, x, 1, 1, 1, 1, 1]
margin_list[label[i]] = 0
elif label[i] == 3 or label[i] == 5:
#margin_list = [5, 5, 5, 1, 5, 1, 5, 5]
#margin_list = [15, 15, 15, 1, 15, 1, 15, 15]
margin_list = [x, x, x, 1, x, 1, x, x]
margin_list[label[i]] = 0
elif metric == 'confusion_set_Overlap_metric':
if label[i] == 4:
#margin_list = [1, 1, 1, 1, 1, 1, 5, 1]
margin_list = [1, 1, 1, 1, 1, 1, x, 1]
margin_list[label[i]] = 0
elif label[i] == 6:
#margin_list = [1, 1, 1, 1, 5, 1, 1, 1]
margin_list = [1, 1, 1, 1, x, 1, 1, 1]
margin_list[label[i]] = 0
elif label[i] == 3:
#margin_list = [1, 1, 1, 1, 1, 5, 1, 1]
margin_list = [1, 1, 1, 1, 1, x, 1, 1]
margin_list[label[i]] = 0
elif label[i] == 5:
#margin_list = [1, 1, 1, 5, 1, 1, 1, 1]
margin_list = [1, 1, 1, x, 1, 1, 1, 1]
margin_list[label[i]] = 0
else:
margin_list = [1, 1, 1, 1, 1, 1, 1, 1]
margin_list[label[i]] = 0
loss_0 = max(0, (current_logits[0] - true_class_logits + margin_list[0]))
loss_1 = max(0, (current_logits[1] - true_class_logits + margin_list[1]))
loss_2 = max(0, (current_logits[2] - true_class_logits + margin_list[2]))
loss_3 = max(0, (current_logits[3] - true_class_logits + margin_list[3]))
loss_4 = max(0, (current_logits[4] - true_class_logits + margin_list[4]))
loss_5 = max(0, (current_logits[5] - true_class_logits + margin_list[5]))
loss_6 = max(0, (current_logits[6] - true_class_logits + margin_list[6]))
loss_7 = max(0, (current_logits[7] - true_class_logits + margin_list[7]))
current_loss = loss_0 + loss_1 + loss_2 + loss_3 + loss_4 + loss_5 + loss_6 + loss_7
loss += current_loss
return loss/output_logits.shape[0]
elif dataset == 'Breast_cancer' :
for i in range(output_logits.shape[0]):
true_class_logits = output_logits[i][label[i]]
current_logits = output_logits[i]
if metric == 'expt2':
if label[i] == 0 or label[i] == 1 or label[i] == 2 or label[i] == 3:
margin_list = [x, x, x, x, x, x, x, x]
margin_list[label[i]] = 0
elif label[i] == 4 or label[i] == 5 or label[i] == 6 or label[i] == 7:
margin_list = [1, 1, 1, 1, 1, 1, 1, 1]
margin_list[label[i]] = 0
elif metric == 'class_Overlap_metric':
if label[i] == 0 or label[i] == 1 or label[i] == 2 or label[i] == 3:
margin_list = [1, 1, 1, 1, x, x, x, x]
margin_list[label[i]] = 0
elif label[i] == 4 or label[i] == 5 or label[i] == 6 or label[i] == 7:
margin_list = [x, x, x, x, 1, 1, 1, 1]
margin_list[label[i]] = 0
elif metric == 'confusion_set_Overlap_metric':
if label[i] == 1:
#margin_list = [1, 1, 1, 1, 1, 1, 5, 1]
margin_list = [1, 1, x, 1, 1, 1, 1, 1]
margin_list[label[i]] = 0
elif label[i] == 2:
#margin_list = [1, 1, 1, 1, 5, 1, 1, 1]
margin_list = [1, x, 1, 1, 1, 1, 1, 1]
margin_list[label[i]] = 0
elif label[i] == 4:
#margin_list = [1, 1, 1, 1, 5, 1, 1, 1]
margin_list = [1, 1, 1, 1, 1, x, 1, 1]
margin_list[label[i]] = 0
elif label[i] == 5:
#margin_list = [1, 1, 1, 1, 5, 1, 1, 1]
margin_list = [1, 1, 1, 1, x, 1, 1, 1]
margin_list[label[i]] = 0
else:
margin_list = [1, 1, 1, 1, 1, 1, 1, 1]
margin_list[label[i]] = 0
loss_0 = max(0, (current_logits[0] - true_class_logits + margin_list[0]))
loss_1 = max(0, (current_logits[1] - true_class_logits + margin_list[1]))
loss_2 = max(0, (current_logits[2] - true_class_logits + margin_list[2]))
loss_3 = max(0, (current_logits[3] - true_class_logits + margin_list[3]))
loss_4 = max(0, (current_logits[4] - true_class_logits + margin_list[4]))
loss_5 = max(0, (current_logits[5] - true_class_logits + margin_list[5]))
loss_6 = max(0, (current_logits[6] - true_class_logits + margin_list[6]))
loss_7 = max(0, (current_logits[7] - true_class_logits + margin_list[7]))
current_loss = loss_0 + loss_1 + loss_2 + loss_3 + loss_4 + loss_5 + loss_6 + loss_7
loss += current_loss
#loss = loss/output_logits.shape[0]
#loss = torch.tensor(loss, requires_grad=True)
return loss/output_logits.shape[0]
def breast_cancer_class_Overlap_metric(conformal_set, label):
overlap_count = 0
for i in range(conformal_set.shape[0]):
current_set = conformal_set[i]
if (label[i] == 0 or label[i] == 1 or label[i] == 2 or label[i] == 3 ) and (current_set[4] == 1 or current_set[5] == 1 or current_set[6] == 1 or current_set[7] == 1):
overlap_count += 1
elif (label[i] == 4 or label[i] == 5 or label[i] == 6 or label[i] == 7 ) and (current_set[0] == 1 or current_set[1] == 1 or current_set[2] == 1 or current_set[3] == 1):
overlap_count += 1
perecentage_of_overlap = (overlap_count/conformal_set.shape[0])*100
return perecentage_of_overlap
def class_Overlap_metric(conformal_set, label):
overlap_count = 0
for i in range(conformal_set.shape[0]):
current_set = conformal_set[i]
if (label[i] == 0 or label[i] == 1 or label[i] == 2) and (current_set[4] == 1 or current_set[6] == 1 or current_set[7] == 1 or current_set[3] == 1 or current_set[5] == 1):
overlap_count += 1
elif (label[i] == 4 or label[i] == 6 or label[i] == 7) and (current_set[0] == 1 or current_set[1] == 1 or current_set[2] or current_set[3] == 1 or current_set[5] == 1):
overlap_count += 1
elif (label[i] == 3 or label[i] == 5) and (current_set[4] == 1 or current_set[6] == 1 or current_set[7] == 1 or current_set[0] == 1 or current_set[1] == 1 or current_set[2] == 1):
overlap_count += 1
perecentage_of_overlap = (overlap_count/conformal_set.shape[0])*100
return perecentage_of_overlap
def breast_cancer_confusion_set_Overlap_metric(conformal_set, label):
overlap_count = 0
for i in range(conformal_set.shape[0]):
current_set = conformal_set[i]
if (label[i] == 1) and (current_set[2] == 1):
overlap_count += 1
elif (label[i] == 2) and (current_set[1] == 1):
overlap_count += 1
elif (label[i] == 4) and (current_set[5] == 1):
overlap_count += 1
elif (label[i] == 5) and (current_set[4] == 1):
overlap_count += 1
perecentage_of_overlap = (overlap_count/conformal_set.shape[0])*100
return perecentage_of_overlap
def confusion_set_Overlap_metric(conformal_set, label):
overlap_count = 0
for i in range(conformal_set.shape[0]):
current_set = conformal_set[i]
if (label[i] == 4) and (current_set[6] == 1):
overlap_count += 1
elif (label[i] == 6) and (current_set[4] == 1):
overlap_count += 1
elif (label[i] == 3) and (current_set[5] == 1):
overlap_count += 1
elif (label[i] == 5) and (current_set[3] == 1):
overlap_count += 1
perecentage_of_overlap = (overlap_count/conformal_set.shape[0])*100
return perecentage_of_overlap