-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation.py
103 lines (92 loc) · 3.05 KB
/
evaluation.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
import numpy as np
import torch
import cv2
def cal_miou(img_pre, img_gt, isunetpp = False) :
img_pre = torch.argmax(img_pre, 1)
bs, w, h = img_pre.shape
miou = 0
for i in range(bs):
pred, mask = img_pre[i], img_gt[i]
union = torch.logical_or(pred, mask).sum()
inter = ((pred + mask) == 2).sum()
if union < 1e-5:
return 0
miou += inter / union
#print(i, miou)
return miou / bs
def get_boundary(img, is_mask) :
if not is_mask:
img = torch.argmax(img, 1).cpu().numpy().astype('float64')
else:
img = img.cpu().numpy()
bs, w, h = img.shape
new_img = np.zeros([bs, w + 2, h + 2])
mask_erode = np.zeros([bs, w, h])
dil = int(round(0.02 * np.sqrt(w ** 2 + h ** 2)))
if dil < 1:
dil = 1
for i in range(bs):
new_img[i] = cv2.copyMakeBorder(img[i], 1, 1, 1, 1, \
cv2.BORDER_CONSTANT, value=0)
kernel = np.ones((3, 3), dtype=np.uint8)
for i in range(bs):
img_erode = cv2.erode(new_img[i], kernel, iterations = dil)
mask_erode[i] = img_erode[1: w + 1, 1: h + 1]
return torch.from_numpy(img - mask_erode)
def cal_biou(img_pre, img_gt) :
img_pre = get_boundary(img_pre, is_mask=False)
img_gt = get_boundary(img_gt, is_mask=True)
bs, w, h = img_pre.shape
inter, union = 0, 0
for i in range(bs):
pred, mask = img_pre[i], img_gt[i]
inter += ((pred * mask) > 0).sum()
union += ((pred + mask) > 0).sum()
if union < 1:
return 0
biou = inter / union
return biou
def cal_miou_pp(img_pre, img_gt):
img_pre = img_pre.round().squeeze(1)
bs, w, h = img_pre.shape
miou = 0
for i in range(bs):
pred, mask = img_pre[i], img_gt[i]
union = torch.logical_or(pred, mask).sum()
inter = ((pred + mask) == 2).sum()
if union < 1e-5:
return 0
miou += inter / union
return miou / bs
def get_boundary_pp(pic, is_mask):
if not is_mask:
pic = pic.round().squeeze(1).cpu().numpy().astype('float64')
else:
pic = pic.cpu().numpy()
bs, w, h = pic.shape
new_pic = np.zeros([bs, w + 2, h + 2])
mask_erode = np.zeros([bs, w, h])
dil = int(round(0.02 * np.sqrt(w ** 2 + h ** 2)))
if dil < 1:
dil = 1
for i in range(bs):
new_pic[i] = cv2.copyMakeBorder(pic[i], 1, 1, 1, 1, \
cv2.BORDER_CONSTANT, value = 0)
kernel = np.ones((3, 3), dtype = np.uint8)
for i in range(bs):
pic_erode = cv2.erode(new_pic[i], kernel, iterations = dil)
mask_erode[i] = pic_erode[1: w + 1, 1: h + 1]
return torch.from_numpy(pic - mask_erode)
def cal_biou_pp(img_pre, img_gt):
img_pre = get_boundary_pp(img_pre, is_mask=False)
img_gt = get_boundary_pp(img_gt, is_mask=True)
bs, w, h = img_pre.shape
inter, union = 0, 0
for i in range(bs):
pred, mask = img_pre[i], img_gt[i]
inter += ((pred * mask) > 0).sum()
union += ((pred + mask) > 0).sum()
if union < 1:
return 0
biou = inter / union
return biou