-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetrics.py
127 lines (104 loc) · 3.96 KB
/
metrics.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
import numpy as np
import torch
def iou_score(output, target):
smooth = 1e-5
if torch.is_tensor(output):
output = torch.sigmoid(output).data.cpu().numpy()
if torch.is_tensor(target):
target = target.data.cpu().numpy()
output_ = output > 0.5
target_ = target > 0.5
intersection = (output_ & target_).sum()
union = (output_ | target_).sum()
return (intersection + smooth) / (union + smooth)
def Recal(output, target):
smooth = 1e-5
if torch.is_tensor(target):
target = target.data.cpu().numpy()
if torch.is_tensor(output):
output = output.data.cpu().numpy()
output_ = output[:, 0, :, :] > 0.5
for i in range(output.shape()[1]-1):
_output_ = output[:, i+1, :, :] > 0.5
output_ = output_ or _output_
output_ = output_ > 0.5
target_ = target > 0.5
intersection = (output_ & target_).sum()
union = target_.sum()
return (intersection + smooth) / (union + smooth)
def Precision(output, target):
smooth = 1e-5
if torch.is_tensor(target):
target = target.data.cpu().numpy()
if torch.is_tensor(output):
output = output.data.cpu().numpy()
output_ = output[:, 0, :, :] > 0.5
for i in range(output.shape()[1]-1):
_output_ = output[:, i+1, :, :] > 0.5
output_ = output_ * _output_
target_ = target > 0.5
intersection = (output_ & target_).sum()
union = output_.sum()
return (intersection + smooth) / (union + smooth)
def Recall_suspect(output, target):
smooth = 1e-5
if torch.is_tensor(target):
target = target.data.cpu().numpy()
if torch.is_tensor(output):
output = output.data.cpu().numpy()
target_ = target > 0.5
output_ = output > 0.5
intersection = (output_ & target_).sum()
union = target_.sum()
return (intersection + smooth) / (union + smooth)
def Precision_certain(output, target):
smooth = 1e-5
if torch.is_tensor(target):
target = target.data.cpu().numpy()
if torch.is_tensor(output):
output = output.data.cpu().numpy()
target_ = target > 0.5
output_ = output > 0.5
intersection = (output_ & target_).sum()
union = output_.sum()
return (intersection + smooth) / (union + smooth)
def accuracy(output, target):
if torch.is_tensor(output):
output = torch.sigmoid(output).view(-1).data.cpu().numpy()
else:
output = torch.from_numpy(output).view(-1).numpy()
if torch.is_tensor(target):
target = target.view(-1).data.cpu().numpy()
else:
target = torch.from_numpy(target).view(-1).numpy()
output = (np.round(output)).astype('int')
target = (np.round(target)).astype('int')
return (output == target).sum() / len(output)
def F1_score(output, target):
smooth = 1e-5
if torch.is_tensor(target):
target = target.data.cpu().numpy()
if torch.is_tensor(output):
output = output.data.cpu().numpy()
output_True = output > 0.5
target_True = target > 0.5
intersection = (output_True & target_True).sum()
A = output_True.sum()
B = target_True.sum()
Precision = (intersection + smooth) / (A+ smooth)
Recall = (intersection+ smooth) / (B+ smooth)
return 2*(Precision*Recall+smooth)/(Precision+Recall+smooth)
def F1_score_special(certain, suspect, target):
smooth = 1e-5
if torch.is_tensor(target):
target = target.data.cpu().numpy()
certain_True = certain > 0.5
suspect_True = suspect > 0.5
target_True = target > 0.5
intersection_certain = (certain_True & target_True).sum()
intersection_suspect = (suspect_True & target_True).sum()
certainA = certain_True.sum()
B = target_True.sum()
Precision = (intersection_certain + smooth) / (certainA + smooth)
Recall = (intersection_suspect+ smooth) / (B + smooth)
return 2*(Precision*Recall+smooth)/(Precision+Recall+smooth)