forked from Andy-zhujunwen/UNET-ZOO
-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.py
165 lines (151 loc) · 6.23 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
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
import cv2
import numpy as np
from scipy.spatial.distance import directed_hausdorff
import matplotlib.pyplot as plt
from skimage.io import imread
import imageio
class IOUMetric:
"""
Class to calculate mean-iou using fast_hist method
"""
def __init__(self, num_classes):
self.num_classes = num_classes
self.hist = np.zeros((num_classes, num_classes))
def _fast_hist(self, label_pred, label_true):
mask = (label_true >= 0) & (label_true < self.num_classes)
hist = np.bincount(
self.num_classes * label_true[mask].astype(int) +
label_pred[mask], minlength=self.num_classes ** 2).reshape(self.num_classes, self.num_classes)
return hist
def add_batch(self, predictions, gts):
for lp, lt in zip(predictions, gts):
self.hist += self._fast_hist(lp.flatten(), lt.flatten())
def evaluate(self):
acc = np.diag(self.hist).sum() / self.hist.sum()
acc_cls = np.diag(self.hist) / self.hist.sum(axis=1)
acc_cls = np.nanmean(acc_cls)
iu = np.diag(self.hist) / (self.hist.sum(axis=1) + self.hist.sum(axis=0) - np.diag(self.hist))
mean_iu = np.nanmean(iu)
freq = self.hist.sum(axis=1) / self.hist.sum()
fwavacc = (freq[freq > 0] * iu[freq > 0]).sum()
return acc, acc_cls, iu, mean_iu, fwavacc
def get_iou(mask_name,predict):
image_mask = cv2.imread(mask_name,0)
if np.all(image_mask == None):
image_mask = imageio.mimread(mask_name)
image_mask = np.array(image_mask)[0]
image_mask = cv2.resize(image_mask,(576,576))
#image_mask = mask
# print(image.shape)
height = predict.shape[0]
weight = predict.shape[1]
# print(height*weight)
o = 0
for row in range(height):
for col in range(weight):
if predict[row, col] < 0.5: #由于输出的predit是0~1范围的,其中值越靠近1越被网络认为是肝脏目标,所以取0.5为阈值
predict[row, col] = 0
else:
predict[row, col] = 1
if predict[row, col] == 0 or predict[row, col] == 1:
o += 1
height_mask = image_mask.shape[0]
weight_mask = image_mask.shape[1]
for row in range(height_mask):
for col in range(weight_mask):
if image_mask[row, col] < 125: #由于mask图是黑白的灰度图,所以少于125的可以看作是黑色
image_mask[row, col] = 0
else:
image_mask[row, col] = 1
if image_mask[row, col] == 0 or image_mask[row, col] == 1:
o += 1
predict = predict.astype(np.int16)
interArea = np.multiply(predict, image_mask)
tem = predict + image_mask
unionArea = tem - interArea
inter = np.sum(interArea)
union = np.sum(unionArea)
iou_tem = inter / union
# Iou = IOUMetric(2) #2表示类别,肝脏类+背景类
# Iou.add_batch(predict, image_mask)
# a, b, c, d, e= Iou.evaluate()
print('%s:iou=%f' % (mask_name,iou_tem))
return iou_tem
def get_dice(mask_name,predict):
image_mask = cv2.imread(mask_name, 0)
if np.all(image_mask == None):
image_mask = imageio.mimread(mask_name)
image_mask = np.array(image_mask)[0]
image_mask = cv2.resize(image_mask,(576,576))
height = predict.shape[0]
weight = predict.shape[1]
o = 0
for row in range(height):
for col in range(weight):
if predict[row, col] < 0.5: # 由于输出的predit是0~1范围的,其中值越靠近1越被网络认为是肝脏目标,所以取0.5为阈值
predict[row, col] = 0
else:
predict[row, col] = 1
if predict[row, col] == 0 or predict[row, col] == 1:
o += 1
height_mask = image_mask.shape[0]
weight_mask = image_mask.shape[1]
for row in range(height_mask):
for col in range(weight_mask):
if image_mask[row, col] < 125: # 由于mask图是黑白的灰度图,所以少于125的可以看作是黑色
image_mask[row, col] = 0
else:
image_mask[row, col] = 1
if image_mask[row, col] == 0 or image_mask[row, col] == 1:
o += 1
predict = predict.astype(np.int16)
intersection = (predict*image_mask).sum()
dice = (2. *intersection) /(predict.sum()+image_mask.sum())
return dice
def get_hd(mask_name,predict):
image_mask = cv2.imread(mask_name, 0)
# print(mask_name)
# print(image_mask)
if np.all(image_mask == None):
image_mask = imageio.mimread(mask_name)
image_mask = np.array(image_mask)[0]
image_mask = cv2.resize(image_mask,(576,576))
#image_mask = mask
height = predict.shape[0]
weight = predict.shape[1]
o = 0
for row in range(height):
for col in range(weight):
if predict[row, col] < 0.5: # 由于输出的predit是0~1范围的,其中值越靠近1越被网络认为是肝脏目标,所以取0.5为阈值
predict[row, col] = 0
else:
predict[row, col] = 1
if predict[row, col] == 0 or predict[row, col] == 1:
o += 1
height_mask = image_mask.shape[0]
weight_mask = image_mask.shape[1]
for row in range(height_mask):
for col in range(weight_mask):
if image_mask[row, col] < 125: # 由于mask图是黑白的灰度图,所以少于125的可以看作是黑色
image_mask[row, col] = 0
else:
image_mask[row, col] = 1
if image_mask[row, col] == 0 or image_mask[row, col] == 1:
o += 1
hd1 = directed_hausdorff(image_mask, predict)[0]
hd2 = directed_hausdorff(predict, image_mask)[0]
res = None
if hd1>hd2 or hd1 == hd2:
res=hd1
return res
else:
res=hd2
return res
def show(predict):
height = predict.shape[0]
weight = predict.shape[1]
for row in range(height):
for col in range(weight):
predict[row, col] *= 255
plt.imshow(predict)
plt.show()