-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.py
93 lines (75 loc) · 2.72 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
import numpy as np
import torch
import torch.nn.functional as F
import pdb
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()
# pdb.set_trace()
output_ = output > 0.5
target_ = target > 0.5
intersection = (output_ & target_).sum()
union = (output_ | target_).sum()
iou = (intersection + smooth) / (union + smooth)
dice = (2* iou) / (iou+1)
return iou, dice
def iou_score_m(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()
iou1 = (intersection + smooth) / (union + smooth)
wt = (2* iou1) / (iou1+1)
output_ = output[:,0:1,:,:] > 0.5
target_ = target[:,0:1,:,:] > 0.5
intersection = (output_ & target_).sum()
union = (output_ | target_).sum()
iou2 = (intersection + smooth) / (union + smooth)
tc = (2* iou2) / (iou2+1)
output_ = output[:,2,:,:] > 0.5
target_ = target[:,2,:,:] > 0.5
intersection = (output_ & target_).sum()
union = (output_ | target_).sum()
iou3 = (intersection + smooth) / (union + smooth)
et = (2* iou3) / (iou3+1)
return wt,et,tc
def dice_brats(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()
iou1 = (intersection + smooth) / (union + smooth)
wt = (2* iou1) / (iou1+1)
output_ = output[:,0:1,:,:] > 0.5
target_ = target[:,0:1,:,:] > 0.5
intersection = (output_ & target_).sum()
union = (output_ | target_).sum()
iou2 = (intersection + smooth) / (union + smooth)
tc = (2* iou2) / (iou2+1)
output_ = output[:,2,:,:] > 0.5
target_ = target[:,2,:,:] > 0.5
intersection = (output_ & target_).sum()
union = (output_ | target_).sum()
iou3 = (intersection + smooth) / (union + smooth)
et = (2* iou3) / (iou3+1)
return wt,et,tc
def dice_coef(output, target):
smooth = 1e-5
output = torch.sigmoid(output).view(-1).data.cpu().numpy()
target = target.view(-1).data.cpu().numpy()
intersection = (output * target).sum()
return (2. * intersection + smooth) / \
(output.sum() + target.sum() + smooth)