-
Notifications
You must be signed in to change notification settings - Fork 72
/
utils.py
executable file
·156 lines (117 loc) · 4.43 KB
/
utils.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
'''
* @author [Zizhao Zhang]
* @email [[email protected]]
* @create date 2017-07-03 11:25:40
* @modify date 2017-07-03 11:25:40
* @desc [description]
'''
# from keras import backend as K
import numpy as np
from PIL import Image
import os, copy, shutil, json
from skimage import measure
class VIS:
def __init__(self, save_path):
self.path = save_path
# TODO
self.semantic_label = None
if os.path.isdir(self.path):
shutil.rmtree(self.path)
os.mkdir(self.path)
self.mean_iu = []
self.cls_iu = []
self.score_history = {}
self.suffix = str(np.random.randint(1000))
self.palette = Image.open('palette_refer.png').palette
def save_seg(self, label_im, name, im=None, gt=None):
seg = Image.fromarray(label_im.astype(np.uint8), mode='P') # must convert to int8 first
seg.palette = copy.copy(self.palette)
if gt is not None or im is not None:
gt = Image.fromarray(gt.astype(np.uint8), mode='P') # must convert to int8 first]
gt.palette = copy.copy(self.palette)
im = Image.fromarray(im.astype(np.uint8), mode='RGB')
I = Image.new('RGB', (label_im.shape[1]*3, label_im.shape[0]))
I.paste(im,(0,0))
I.paste(gt,(256,0))
I.paste(seg,(512,0))
I.save(os.path.join(self.path, name))
else:
seg.save(os.path.join(self.path, name))
def add_sample(self, pred, gt):
score_mean, score_cls = mean_IU(pred, gt)
self.mean_iu.append(score_mean)
self.cls_iu.append(score_cls)
return score_mean
def compute_scores(self, suffix=0):
meanIU = np.mean(np.array(self.mean_iu))
meanIU_per_cls = np.mean(np.array(self.cls_iu), axis=0)
print ('-'*20)
print ('overall mean IU: {} '.format(meanIU))
print ('mean IU per class')
for i, c in enumerate(meanIU_per_cls):
print ('\t class {}: {}'.format(i,c))
print ('-'*20)
data = {'mean_IU': '%.2f' % (meanIU), 'mean_IU_cls': ['%.2f'%(a) for a in meanIU_per_cls.tolist()]}
self.score_history['%.10d' % suffix] = data
json.dump(self.score_history, open(os.path.join(self.path, 'meanIU{}.json'.format(self.suffix)),'w'), indent=2, sort_keys=True)
def mean_IU(eval_segm, gt_segm):
'''
(1/n_cl) * sum_i(n_ii / (t_i + sum_j(n_ji) - n_ii))
'''
check_size(eval_segm, gt_segm)
cl, n_cl = union_classes(eval_segm, gt_segm)
_, n_cl_gt = extract_classes(gt_segm)
eval_mask, gt_mask = extract_both_masks(eval_segm, gt_segm, cl, n_cl)
IU = list([0]) * n_cl
for i, c in enumerate(cl):
curr_eval_mask = eval_mask[i, :, :]
curr_gt_mask = gt_mask[i, :, :]
if (np.sum(curr_eval_mask) == 0) or (np.sum(curr_gt_mask) == 0):
continue
n_ii = np.sum(np.logical_and(curr_eval_mask, curr_gt_mask))
t_i = np.sum(curr_gt_mask)
n_ij = np.sum(curr_eval_mask)
IU[i] = n_ii / (t_i + n_ij - n_ii)
mean_IU_ = np.sum(IU) / n_cl_gt
return mean_IU_, IU
'''Used by Tensorflow'''
def dice_coef(y_true, y_pred):
smooth = 1.
y_true_f = K.flatten(y_true)
y_pred_f = K.flatten(y_pred)
intersection = K.sum(y_true_f * y_pred_f)
return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)
def dice_coef_loss(y_true, y_pred):
return -dice_coef(y_true, y_pred)
def extract_both_masks(eval_segm, gt_segm, cl, n_cl):
eval_mask = extract_masks(eval_segm, cl, n_cl)
gt_mask = extract_masks(gt_segm, cl, n_cl)
return eval_mask, gt_mask
def extract_classes(segm):
cl = np.unique(segm)
n_cl = len(cl)
return cl, n_cl
def union_classes(eval_segm, gt_segm):
eval_cl, _ = extract_classes(eval_segm)
gt_cl, _ = extract_classes(gt_segm)
cl = np.union1d(eval_cl, gt_cl)
n_cl = len(cl)
return cl, n_cl
def extract_masks(segm, cl, n_cl):
h, w = segm_size(segm)
masks = np.zeros((n_cl, h, w))
for i, c in enumerate(cl):
masks[i, :, :] = segm == c
return masks
def segm_size(segm):
try:
height = segm.shape[0]
width = segm.shape[1]
except IndexError:
raise
return height, width
def check_size(eval_segm, gt_segm):
h_e, w_e = segm_size(eval_segm)
h_g, w_g = segm_size(gt_segm)
if (h_e != h_g) or (w_e != w_g):
raise ValueError('Uneuqal image and mask size')