-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
307 lines (260 loc) · 12.6 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
import os
import torch
from sklearn.metrics import precision_score,recall_score,accuracy_score,confusion_matrix,roc_auc_score,roc_curve,auc,f1_score,jaccard_score
import numpy as np
# from pytorch_lightning import metrics
import torchmetrics
import math
def device_check(ids:list):
if torch.cuda.is_available():
devices = []
for i in ids:
assert i < torch.cuda.device_count()
devices.append(torch.device('cuda:'+str(i)))
return devices[0],ids
torch.device('cuda:'+str(gpu_id) if torch.cuda.is_available() else 'cpu')
else:
raise Exception('No cuda available')
class Metrics():
def __init__(self, metrics=['auc','iou','precision','recall','f1','accuracy'], num_classes=2, threshold=0.5, average=None, return_on_update=True):
self.SUPPORTED_METRICS = {'auc':torchmetrics.functional.classification.auc,
'iou': self.calculate_iou,
'precision': self.calculate_precision,
'recall': self.calculate_recall,
'f1': self.calculate_f1,
'accuracy': self.calculate_accuracy
}
self.num_classes = num_classes
self.threshold = threshold
self.average = average
self.return_on_update = return_on_update
self.init_metric_functions(metrics)
def get_metric_names(self):
return list(self.metric_functions.keys())
def calculate_accuracy(self, conf_matrix:torch.tensor, average=None) -> torch.tensor:
return conf_matrix.diag().sum() / conf_matrix.sum()
def calculate_iou(self,conf_matrix:torch.tensor, average=None) -> torch.tensor:
true_positive = torch.diag(conf_matrix)
false_positive = conf_matrix.sum(0) - true_positive
false_negative = conf_matrix.sum(1) - true_positive
iou = true_positive / (true_positive + false_positive + false_negative)
if average== 'macro':
return iou.mean()
else:
return iou
def calculate_precision(self,conf_matrix:torch.tensor, average=None) -> torch.tensor:
true_positive = torch.diag(conf_matrix)
false_positive = conf_matrix.sum(0) - true_positive
precision = true_positive / (true_positive + false_positive)
if average== 'macro':
return precision.mean()
else:
return precision
def calculate_recall(self,conf_matrix:torch.tensor, average=None) -> torch.tensor:
true_positive = torch.diag(conf_matrix)
false_negative = conf_matrix.sum(1) - true_positive
recall = true_positive / (true_positive + false_negative)
if average== 'macro':
return recall.mean()
else:
return recall
def calculate_f1(self,conf_matrix:torch.tensor,average=None) -> torch.tensor:
true_positive = torch.diag(conf_matrix)
false_negative = conf_matrix.sum(1) - true_positive
false_positive = conf_matrix.sum(0) - true_positive
precision = true_positive / (true_positive + false_positive)
recall = true_positive / (true_positive + false_negative)
f1 = 2*precision*recall/(precision+recall)
if average== 'macro':
return f1.mean()
else:
return f1
def init_metric_functions(self,metrics):
self.metric_functions = {}
for metric in metrics:
if metric in self.SUPPORTED_METRICS:
self.metric_functions[metric] = self.SUPPORTED_METRICS[metric]
self.confusion_matrix = torchmetrics.ConfusionMatrix(self.num_classes,threshold=self.threshold,compute_on_step=True)
if 'auc' in self.metric_functions:
self.roc = torchmetrics.ROC(1,compute_on_step=True) # in order to work for binary case roc needs num_classes ==1
def update(self,preds, targets):
targets = targets.long()
if len(self.metric_functions)>0:
conf_matrix = self.confusion_matrix(preds, targets)
if 'auc' in self.metric_functions:
tp,fp,th = self.roc(preds, targets)
res = {metric:(self.metric_functions[metric](conf_matrix,average=self.average).numpy() if metric != 'auc' else self.metric_functions[metric](tp,fp).numpy()) for metric in self.metric_functions }
return res
def compute(self):
if len(self.metric_functions)>0:
conf_matrix = self.confusion_matrix.compute()
if 'auc' in self.metric_functions:
tp,fp,th = self.roc.compute()
res = {metric:(self.metric_functions[metric](conf_matrix,average=self.average).numpy() if metric != 'auc' else self.metric_functions[metric](tp,fp, reorder=True).numpy()) for metric in self.metric_functions }
return res
def reset(self):
if len(self.metric_functions)>0:
self.confusion_matrix.reset()
if 'auc' in self.metric_functions:
self.roc.reset()
class Binary_Metrics():
def __init__(self, num_classes=2, threshold=0.5, average=None, return_on_update=True):
self.num_classes = num_classes
self.threshold = threshold
self.average = average
self.return_on_update = return_on_update
self.init_metric_functions()
self.init_agg_metrics()
def init_agg_metrics(self):
self.aggregated_metrics = {'auc':[],
'iou': [],
'precision':[],
'recall':[],
'f1':[],
'accuracy': [],
}
def get_metric_names(self):
return list(self.conf_f.keys()) + list(self.auc_f.keys())
def calculate_accuracy(self, conf_matrix:torch.tensor, average=None) -> torch.tensor:
return conf_matrix.diag().sum() / conf_matrix.sum()
def calculate_iou(self,conf_matrix:torch.tensor, average=None) -> torch.tensor:
true_positive = torch.diag(conf_matrix)
false_positive = conf_matrix.sum(0) - true_positive
false_negative = conf_matrix.sum(1) - true_positive
iou = true_positive / (true_positive + false_positive + false_negative)
if average== 'macro':
return iou.mean()
else:
return iou
def calculate_precision(self,conf_matrix:torch.tensor, average=None) -> torch.tensor:
true_positive = torch.diag(conf_matrix)
false_positive = conf_matrix.sum(0) - true_positive
precision = true_positive / (true_positive + false_positive)
if average== 'macro':
return precision.mean()
else:
return precision
def calculate_recall(self,conf_matrix:torch.tensor, average=None) -> torch.tensor:
true_positive = torch.diag(conf_matrix)
false_negative = conf_matrix.sum(1) - true_positive
recall = true_positive / (true_positive + false_negative)
if average== 'macro':
return recall.mean()
else:
return recall
def calculate_f1(self,conf_matrix:torch.tensor,average=None) -> torch.tensor:
true_positive = torch.diag(conf_matrix)
false_negative = conf_matrix.sum(1) - true_positive
false_positive = conf_matrix.sum(0) - true_positive
precision = true_positive / (true_positive + false_positive)
recall = true_positive / (true_positive + false_negative)
f1 = 2*precision*recall/(precision+recall)
if average== 'macro':
return f1.mean()
else:
return f1
def init_metric_functions(self):
self.auc_f = {'auc':torchmetrics.functional.auc}
self.conf_f = {
'iou': self.calculate_iou,
'precision': self.calculate_precision,
'recall': self.calculate_recall,
'f1': self.calculate_f1,
'accuracy': self.calculate_accuracy
}
self.confusion_matrix = torchmetrics.ConfusionMatrix(self.num_classes,threshold=self.threshold, compute_on_step=False)
self.roc = torchmetrics.ROC(1,compute_on_step=False) # in order to work for binary case roc needs num_classes ==1
def update(self,preds, targets):
self.confusion_matrix(preds, targets)
try:
self.roc(preds, targets)
except Exception as e:
pass
conf_matrix = self.confusion_matrix.compute()
# print(conf_matrix)
tp,fp,th = self.roc.compute()
conf_res = {metric:self.conf_f[metric](conf_matrix,average=self.average).numpy() for metric in self.conf_f }
try:
auc_res = {metric:self.auc_f[metric](tp,fp).numpy() for metric in self.auc_f }
except Exception as e:
auc_res = {metric:self.auc_f[metric](tp,fp, reorder=True).numpy() for metric in self.auc_f }
# auc_res = {metric:0.0 for metric in self.auc_f }
res = {**conf_res, **auc_res}
for metric in self.aggregated_metrics:
if self.average == 'macro':
if math.isnan(conf_res['f1']):
continue
self.aggregated_metrics[metric].append(res[metric])
else:
if math.isnan(conf_res['f1'][1]):
continue
self.aggregated_metrics[metric].append(res[metric])
return res
def compute(self):
# conf_matrix = self.confusion_matrix.compute()
# tp,fp,th = self.roc.compute()
# conf_res = {metric:self.conf_f[metric](conf_matrix,average=self.average).numpy() for metric in self.conf_f }
# try:
# auc_res = {metric:self.auc_f[metric](tp,fp).numpy() for metric in self.auc_f }
# except Exception as e:
# auc_res = {metric:0.0 for metric in self.auc_f }
# return {**conf_res, **auc_res}
res = {metric:np.stack(self.aggregated_metrics[metric]).mean(axis=0) for metric in self.aggregated_metrics}
self.init_agg_metrics()
return res
class Binary_Sklearn_Metrics():
def __init__(self, metric_names=None, threshold=0.5):
self.init_metric_functions(metric_names)
self.threshold = threshold
self.init_metrics()
def get_metric_names(self):
return list(self.metric_functions.keys())
def custom_auc(self,gt, pred):
fpr, tpr, thresholds = roc_curve(gt, pred, drop_intermediate =True)
return auc(fpr,tpr)
def init_metric_functions(self,metric_names):
metrics = {'accuracy':accuracy_score,
'precision':precision_score,
'recall':recall_score,
'f1':f1_score,
'auc':self.custom_auc,
#'confusion_matrix':confusion_matrix,
'iou':jaccard_score}
if metric_names:
self.metric_functions = {}
for metric_name in metric_names:
if metric_name in metrics:
self.metric_functions[metric_name] = metrics[metric_name]
else:
self.metric_functions = metrics
def init_metrics(self):
self.metrics = {metric:[] for metric in self.get_metric_names()}
def aggregate_metrics(self):
return {metric:np.mean(self.metrics[metric]) for metric in self.metrics}
def compute(self, gt, pred, threshold=None,average=None):
if not threshold:
threshold = self.threshold
result ={}
for metric in self.metrics:
if metric != 'auc':
pred_ = np.greater_equal(pred,threshold)
else:
pred_ = pred
if metric in ['recall','precision','f1']:
result[metric] = self.metric_functions[metric](gt, pred_,average=average)
else:
result[metric] = self.metric_functions[metric](gt, pred_)
self.metrics[metric].append(result[metric])
return result
def plot_roc_curve(gt, pred):
fpr, tpr, thresholds = roc_curve(gt, pred,drop_intermediate =False)
roc_auc = auc(fpr,tpr)
fig, ax = plt.subplots(1,1)
ax.plot(fpr, tpr, label='ROC curve (area = %0.2f)' % roc_auc)
ax.plot([0, 1], [0, 1], 'k--')
ax.set_xlim([0.0, 1.0])
ax.set_ylim([0.0, 1.05])
ax.set_xlabel('False Positive Rate')
ax.set_ylabel('True Positive Rate')
ax.set_title('Receiver operating characteristic example')
ax.legend(loc="lower right")