-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmetrics.py
More file actions
executable file
·85 lines (74 loc) · 2.54 KB
/
metrics.py
File metadata and controls
executable file
·85 lines (74 loc) · 2.54 KB
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
import numpy as np
import torch
import cv2
from sklearn.metrics import roc_auc_score
class AverageMeter(object):
def __init__(self):
self.initialized = False
self.val = None
self.avg = None
self.sum = None
self.count = None
def initialize(self, val, weight):
self.val = val
self.avg = val
self.sum = np.multiply(val, weight)
self.count = weight
self.initialized = True
def update(self, val, weight=1):
if not self.initialized:
self.initialize(val, weight)
else:
self.add(val, weight)
def add(self, val, weight):
self.val = val
self.sum = np.add(self.sum, np.multiply(val, weight))
self.count = self.count + weight
self.avg = self.sum / self.count
@property
def value(self):
return np.round(self.val, 4)
@property
def average(self):
return np.round(self.avg, 4)
def get_metrics(predict, target, threshold=None, predict_b=None):
predict = torch.sigmoid(predict).cpu().detach().numpy().flatten()
if predict_b is not None:
predict_b = predict_b.flatten()
else:
predict_b = np.where(predict >= threshold, 1, 0)
if torch.is_tensor(target):
target = target.cpu().detach().numpy().flatten()
else:
target = target.flatten()
tp = (predict_b * target).sum()
tn = ((1 - predict_b) * (1 - target)).sum()
fp = ((1 - target) * predict_b).sum()
fn = ((1 - predict_b) * target).sum()
auc = roc_auc_score(target, predict)
acc = (tp + tn) / (tp + fp + fn + tn)
pre = tp / (tp + fp)
sen = tp / (tp + fn)
spe = tn / (tn + fp)
iou = tp / (tp + fp + fn)
f1 = 2 * pre * sen / (pre + sen)
return {
"AUC": np.round(auc, 4),
"F1": np.round(f1, 4),
"Acc": np.round(acc, 4),
"Sen": np.round(sen, 4),
"Spe": np.round(spe, 4),
"pre": np.round(pre, 4),
"IOU": np.round(iou, 4),
}
def count_connect_component(predict, target, threshold=None, connectivity=8):
if threshold != None:
predict = torch.sigmoid(predict).cpu().detach().numpy()
predict = np.where(predict >= threshold, 1, 0)
if torch.is_tensor(target):
target = target.cpu().detach().numpy()
pre_n, _, _, _ = cv2.connectedComponentsWithStats(np.asarray(
predict, dtype=np.uint8)*255, connectivity=connectivity)
gt_n, _, _, _ = cv2.connectedComponentsWithStats(np.asarray(
target, dtype=np.uint8)*255, connectivity=connectivity)
return pre_n/gt_n