forked from emma-sjwang/BEAL
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.py
executable file
·265 lines (223 loc) · 10.5 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
from scipy.misc import imsave
import os.path as osp
import numpy as np
import os
import cv2
from skimage import morphology
import scipy
from PIL import Image
from matplotlib.pyplot import imsave
# from keras.preprocessing import image
from skimage.measure import label, regionprops
from skimage.transform import rotate, resize
from skimage import measure, draw
import matplotlib.pyplot as plt
plt.switch_backend('agg')
from scipy.misc import imsave
from utils.metrics import *
import cv2
def construct_color_img(prob_per_slice):
shape = prob_per_slice.shape
img = np.zeros((shape[0], shape[1], 3), dtype=np.uint8)
img[:, :, 0] = prob_per_slice * 255
img[:, :, 1] = prob_per_slice * 255
img[:, :, 2] = prob_per_slice * 255
im_color = cv2.applyColorMap(img, cv2.COLORMAP_JET)
return im_color
def normalize_ent(ent):
'''
Normalizate ent to 0 - 1
:param ent:
:return:
'''
min = np.amin(ent)
return (ent - min) / 0.4
def draw_ent(prediction, save_root, name):
'''
Draw the entropy information for each img and save them to the save path
:param prediction: [2, h, w] numpy
:param save_path: string including img name
:return: None
'''
if not os.path.exists(os.path.join(save_root, 'disc')):
os.makedirs(os.path.join(save_root, 'disc'))
if not os.path.exists(os.path.join(save_root, 'cup')):
os.makedirs(os.path.join(save_root, 'cup'))
smooth = 1e-8
cup = prediction[0]
disc = prediction[1]
cup_ent = - cup * np.log(cup + smooth)
disc_ent = - disc * np.log(disc + smooth)
cup_ent = normalize_ent(cup_ent)
disc_ent = normalize_ent(disc_ent)
disc = construct_color_img(disc_ent)
cv2.imwrite(os.path.join(save_root, 'disc', name.split('.')[0]) + '.png', disc)
cup = construct_color_img(cup_ent)
cv2.imwrite(os.path.join(save_root, 'cup', name.split('.')[0]) + '.png', cup)
def draw_mask(prediction, save_root, name):
'''
Draw the mask probability for each img and save them to the save path
:param prediction: [2, h, w] numpy
:param save_path: string including img name
:return: None
'''
if not os.path.exists(os.path.join(save_root, 'disc')):
os.makedirs(os.path.join(save_root, 'disc'))
if not os.path.exists(os.path.join(save_root, 'cup')):
os.makedirs(os.path.join(save_root, 'cup'))
cup = prediction[0]
disc = prediction[1]
disc = construct_color_img(disc)
cv2.imwrite(os.path.join(save_root, 'disc', name.split('.')[0]) + '.png', disc)
cup = construct_color_img(cup)
cv2.imwrite(os.path.join(save_root, 'cup', name.split('.')[0]) + '.png', cup)
def draw_boundary(prediction, save_root, name):
'''
Draw the mask probability for each img and save them to the save path
:param prediction: [2, h, w] numpy
:param save_path: string including img name
:return: None
'''
if not os.path.exists(os.path.join(save_root, 'boundary')):
os.makedirs(os.path.join(save_root, 'boundary'))
boundary = prediction[0]
boundary = construct_color_img(boundary)
cv2.imwrite(os.path.join(save_root, 'boundary', name.split('.')[0]) + '.png', boundary)
def get_largest_fillhole(binary):
label_image = label(binary)
regions = regionprops(label_image)
area_list = []
for region in regions:
area_list.append(region.area)
if area_list:
idx_max = np.argmax(area_list)
binary[label_image != idx_max + 1] = 0
return scipy.ndimage.binary_fill_holes(np.asarray(binary).astype(int))
def postprocessing(prediction, threshold=0.75, dataset='G'):
if dataset[0] == 'D':
prediction = prediction.numpy()
prediction_copy = np.copy(prediction)
disc_mask = prediction[1]
cup_mask = prediction[0]
disc_mask = (disc_mask > 0.5) # return binary mask
cup_mask = (cup_mask > 0.1) # return binary mask
disc_mask = disc_mask.astype(np.uint8)
cup_mask = cup_mask.astype(np.uint8)
for i in range(5):
disc_mask = scipy.signal.medfilt2d(disc_mask, 7)
cup_mask = scipy.signal.medfilt2d(cup_mask, 7)
disc_mask = morphology.binary_erosion(disc_mask, morphology.diamond(7)).astype(np.uint8) # return 0,1
cup_mask = morphology.binary_erosion(cup_mask, morphology.diamond(7)).astype(np.uint8) # return 0,1
disc_mask = get_largest_fillhole(disc_mask).astype(np.uint8) # return 0,1
cup_mask = get_largest_fillhole(cup_mask).astype(np.uint8)
prediction_copy[0] = cup_mask
prediction_copy[1] = disc_mask
return prediction_copy
else:
prediction = prediction.numpy()
prediction = (prediction > threshold) # return binary mask
prediction = prediction.astype(np.uint8)
prediction_copy = np.copy(prediction)
disc_mask = prediction[1]
cup_mask = prediction[0]
for i in range(5):
disc_mask = scipy.signal.medfilt2d(disc_mask, 7)
cup_mask = scipy.signal.medfilt2d(cup_mask, 7)
disc_mask = morphology.binary_erosion(disc_mask, morphology.diamond(7)).astype(np.uint8) # return 0,1
cup_mask = morphology.binary_erosion(cup_mask, morphology.diamond(7)).astype(np.uint8) # return 0,1
disc_mask = get_largest_fillhole(disc_mask).astype(np.uint8) # return 0,1
cup_mask = get_largest_fillhole(cup_mask).astype(np.uint8)
prediction_copy[0] = cup_mask
prediction_copy[1] = disc_mask
return prediction_copy
def joint_val_image(image, prediction, mask):
ratio = 0.5
_pred_cup = np.zeros([mask.shape[-2], mask.shape[-1], 3])
_pred_disc = np.zeros([mask.shape[-2], mask.shape[-1], 3])
_mask = np.zeros([mask.shape[-2], mask.shape[-1], 3])
image = np.transpose(image, (1, 2, 0))
_pred_cup[:, :, 0] = prediction[0]
_pred_cup[:, :, 1] = prediction[0]
_pred_cup[:, :, 2] = prediction[0]
_pred_disc[:, :, 0] = prediction[1]
_pred_disc[:, :, 1] = prediction[1]
_pred_disc[:, :, 2] = prediction[1]
_mask[:,:,0] = mask[0]
_mask[:,:,1] = mask[1]
pred_cup = np.add(ratio * image, (1 - ratio) * _pred_cup)
pred_disc = np.add(ratio * image, (1 - ratio) * _pred_disc)
mask_img = np.add(ratio * image, (1 - ratio) * _mask)
joint_img = np.concatenate([image, mask_img, pred_cup, pred_disc], axis=1)
return joint_img
def save_val_img(path, epoch, img):
name = osp.join(path, "visualization", "epoch_%d.png" % epoch)
out = osp.join(path, "visualization")
if not osp.exists(out):
os.makedirs(out)
img_shape = img[0].shape
stack_image = np.zeros([len(img) * img_shape[0], img_shape[1], img_shape[2]])
for i in range(len(img)):
stack_image[i * img_shape[0] : (i + 1) * img_shape[0], :, : ] = img[i]
imsave(name, stack_image)
def save_per_img(patch_image, data_save_path, img_name, prob_map, mask_path=None, ext="bmp"):
path1 = os.path.join(data_save_path, 'overlay', img_name.split('.')[0]+'.png')
path0 = os.path.join(data_save_path, 'original_image', img_name.split('.')[0]+'.png')
if not os.path.exists(os.path.dirname(path0)):
os.makedirs(os.path.dirname(path0))
if not os.path.exists(os.path.dirname(path1)):
os.makedirs(os.path.dirname(path1))
disc_map = prob_map[0]
cup_map = prob_map[1]
size = disc_map.shape
disc_map[:, 0] = np.zeros(size[0])
disc_map[:, size[1] - 1] = np.zeros(size[0])
disc_map[0, :] = np.zeros(size[1])
disc_map[size[0] - 1, :] = np.zeros(size[1])
size = cup_map.shape
cup_map[:, 0] = np.zeros(size[0])
cup_map[:, size[1] - 1] = np.zeros(size[0])
cup_map[0, :] = np.zeros(size[1])
cup_map[size[0] - 1, :] = np.zeros(size[1])
disc_mask = (disc_map > 0.75) # return binary mask
cup_mask = (cup_map > 0.75)
disc_mask = disc_mask.astype(np.uint8)
cup_mask = cup_mask.astype(np.uint8)
for i in range(5):
disc_mask = scipy.signal.medfilt2d(disc_mask, 7)
cup_mask = scipy.signal.medfilt2d(cup_mask, 7)
disc_mask = morphology.binary_erosion(disc_mask, morphology.diamond(7)).astype(np.uint8) # return 0,1
cup_mask = morphology.binary_erosion(cup_mask, morphology.diamond(7)).astype(np.uint8) # return 0,1
disc_mask = get_largest_fillhole(disc_mask)
cup_mask = get_largest_fillhole(cup_mask)
disc_mask = morphology.binary_dilation(disc_mask, morphology.diamond(7)).astype(np.uint8) # return 0,1
cup_mask = morphology.binary_dilation(cup_mask, morphology.diamond(7)).astype(np.uint8) # return 0,1
disc_mask = get_largest_fillhole(disc_mask).astype(np.uint8) # return 0,1
cup_mask = get_largest_fillhole(cup_mask).astype(np.uint8)
contours_disc = measure.find_contours(disc_mask, 0.5)
contours_cup = measure.find_contours(cup_mask, 0.5)
patch_image2 = patch_image.astype(np.uint8)
patch_image2 = Image.fromarray(patch_image2)
patch_image2.save(path0)
for n, contour in enumerate(contours_cup):
patch_image[(contour[:, 0]).astype(int), (contour[:, 1]).astype(int), :] = [0, 255, 0]
patch_image[(contour[:, 0] + 1.0).astype(int), (contour[:, 1]).astype(int), :] = [0, 255, 0]
patch_image[(contour[:, 0] + 1.0).astype(int), (contour[:, 1] + 1.0).astype(int), :] = [0, 255, 0]
patch_image[(contour[:, 0]).astype(int), (contour[:, 1] + 1.0).astype(int), :] = [0, 255, 0]
patch_image[(contour[:, 0] - 1.0).astype(int), (contour[:, 1]).astype(int), :] = [0, 255, 0]
patch_image[(contour[:, 0] - 1.0).astype(int), (contour[:, 1] - 1.0).astype(int), :] = [0, 255, 0]
patch_image[(contour[:, 0]).astype(int), (contour[:, 1] - 1.0).astype(int), :] = [0, 255, 0]
for n, contour in enumerate(contours_disc):
patch_image[contour[:, 0].astype(int), contour[:, 1].astype(int), :] = [0, 0, 255]
patch_image[(contour[:, 0] + 1.0).astype(int), (contour[:, 1]).astype(int), :] = [0, 0, 255]
patch_image[(contour[:, 0] + 1.0).astype(int), (contour[:, 1] + 1.0).astype(int), :] = [0, 0, 255]
patch_image[(contour[:, 0]).astype(int), (contour[:, 1] + 1.0).astype(int), :] = [0, 0, 255]
patch_image[(contour[:, 0] - 1.0).astype(int), (contour[:, 1]).astype(int), :] = [0, 0, 255]
patch_image[(contour[:, 0] - 1.0).astype(int), (contour[:, 1] - 1.0).astype(int), :] = [0, 0, 255]
patch_image[(contour[:, 0]).astype(int), (contour[:, 1] - 1.0).astype(int), :] = [0, 0, 255]
patch_image = patch_image.astype(np.uint8)
patch_image = Image.fromarray(patch_image)
patch_image.save(path1)
def untransform(img, lt):
img = (img + 1) * 127.5
lt = lt * 128
return img, lt