-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
416 lines (357 loc) · 13.7 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import tensorflow as tf
import matplotlib.pyplot as plt
from distutils.version import LooseVersion
import math
import warnings
try:
import cv2
except ImportError:
cv2 = None
import numpy as np
import scipy.ndimage
import six
import skimage
import skimage.color
import skimage.transform
def generate_images(model, input_image, target_image, plots=1):
"""plot input_image, target_image and prediction in one row, all with
shape [batch_size, height, width, channels].
:param model: trained model
:param input_image: a batch of input images
:param target_image: a batch of target images
:param plots: numbers of image groups you want to plot, default 1
"""
assert plots <= input_image.shape[
0], "plots number should be less than batch size"
classes = target_image.shape[-1]
prediction = model.predict(input_image)
plt.figure(figsize=(20, 20))
target_image = tf.argmax(input=target_image, axis=-1)
prediction = tf.argmax(input=prediction, axis=-1)
for i in range(plots):
plt.subplot(plots, 3, i * 3 + 1)
plt.imshow(input_image[i], vmin=0, vmax=classes)
plt.subplot(plots, 3, i * 3 + 2)
plt.imshow(target_image[i], vmin=0, vmax=classes)
plt.subplot(plots, 3, i * 3 + 3)
plt.imshow(prediction[i], vmin=0, vmax=classes)
plt.show()
# https://github.com/wkentaro/fcn/blob/master/fcn/utils.py
# -----------------------------------------------------------------------------
# Chainer Util
# -----------------------------------------------------------------------------
def batch_to_vars(batch, device=-1):
import chainer
from chainer import cuda
in_arrays = [np.asarray(x) for x in zip(*batch)]
if device >= 0:
in_arrays = [cuda.to_gpu(x, device=device) for x in in_arrays]
in_vars = [chainer.Variable(x) for x in in_arrays]
return in_vars
# -----------------------------------------------------------------------------
# Color Util
# -----------------------------------------------------------------------------
def bitget(byteval, idx):
return ((byteval & (1 << idx)) != 0)
def labelcolormap(*args, **kwargs):
warnings.warn('labelcolormap is renamed to label_colormap.',
DeprecationWarning)
return label_colormap(*args, **kwargs)
def label_colormap(N=256):
cmap = np.zeros((N, 3))
for i in six.moves.range(0, N):
id = i
r, g, b = 0, 0, 0
for j in six.moves.range(0, 8):
r = np.bitwise_or(r, (bitget(id, 0) << 7 - j))
g = np.bitwise_or(g, (bitget(id, 1) << 7 - j))
b = np.bitwise_or(b, (bitget(id, 2) << 7 - j))
id = (id >> 3)
cmap[i, 0] = r
cmap[i, 1] = g
cmap[i, 2] = b
cmap = cmap.astype(np.float32) / 255
return cmap
def visualize_labelcolormap(*args, **kwargs):
warnings.warn(
'visualize_labelcolormap is renamed to visualize_label_colormap',
DeprecationWarning)
return visualize_label_colormap(*args, **kwargs)
def visualize_label_colormap(cmap):
n_colors = len(cmap)
ret = np.zeros((n_colors, 10 * 10, 3))
for i in six.moves.range(n_colors):
ret[i, ...] = cmap[i]
return ret.reshape((n_colors * 10, 10, 3))
def get_label_colortable(n_labels, shape):
if cv2 is None:
raise RuntimeError('get_label_colortable requires OpenCV (cv2)')
rows, cols = shape
if rows * cols < n_labels:
raise ValueError
cmap = label_colormap(n_labels)
table = np.zeros((rows * cols, 50, 50, 3), dtype=np.uint8)
for lbl_id, color in enumerate(cmap):
color_uint8 = (color * 255).astype(np.uint8)
table[lbl_id, :, :] = color_uint8
text = '{:<2}'.format(lbl_id)
cv2.putText(table[lbl_id], text, (5, 35),
cv2.cv.CV_FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 255), 3)
table = table.reshape(rows, cols, 50, 50, 3)
table = table.transpose(0, 2, 1, 3, 4)
table = table.reshape(rows * 50, cols * 50, 3)
return table
# -----------------------------------------------------------------------------
# Evaluation
# -----------------------------------------------------------------------------
def _fast_hist(label_true, label_pred, n_class):
mask = (label_true >= 0) & (label_true < n_class)
hist = np.bincount(
n_class * label_true[mask].astype(int) +
label_pred[mask], minlength=n_class ** 2).reshape(n_class, n_class)
return hist
def label_accuracy_score(label_trues, label_preds, n_class):
"""Returns accuracy score evaluation result.
- overall accuracy
- mean accuracy
- mean IU
- fwavacc
"""
hist = np.zeros((n_class, n_class))
for lt, lp in zip(label_trues, label_preds):
hist += _fast_hist(lt.flatten(), lp.flatten(), n_class)
acc = np.diag(hist).sum() / hist.sum()
with np.errstate(divide='ignore', invalid='ignore'):
acc_cls = np.diag(hist) / hist.sum(axis=1)
acc_cls = np.nanmean(acc_cls)
with np.errstate(divide='ignore', invalid='ignore'):
iu = np.diag(hist) / (
hist.sum(axis=1) + hist.sum(axis=0) - np.diag(hist)
)
mean_iu = np.nanmean(iu)
freq = hist.sum(axis=1) / hist.sum()
fwavacc = (freq[freq > 0] * iu[freq > 0]).sum()
return acc, acc_cls, mean_iu, fwavacc
# -----------------------------------------------------------------------------
# Visualization
# -----------------------------------------------------------------------------
def centerize(src, dst_shape, margin_color=None):
"""Centerize image for specified image size
@param src: image to centerize
@param dst_shape: image shape (height, width) or (height, width, channel)
"""
if src.shape[:2] == dst_shape[:2]:
return src
centerized = np.zeros(dst_shape, dtype=src.dtype)
if margin_color:
centerized[:, :] = margin_color
pad_vertical, pad_horizontal = 0, 0
h, w = src.shape[:2]
dst_h, dst_w = dst_shape[:2]
if h < dst_h:
pad_vertical = (dst_h - h) // 2
if w < dst_w:
pad_horizontal = (dst_w - w) // 2
centerized[pad_vertical:pad_vertical + h,
pad_horizontal:pad_horizontal + w] = src
return centerized
def _tile_images(imgs, tile_shape, concatenated_image):
"""Concatenate images whose sizes are same.
@param imgs: image list which should be concatenated
@param tile_shape: shape for which images should be concatenated
@param concatenated_image: returned image.
if it is None, new image will be created.
"""
y_num, x_num = tile_shape
one_width = imgs[0].shape[1]
one_height = imgs[0].shape[0]
if concatenated_image is None:
if len(imgs[0].shape) == 3:
n_channels = imgs[0].shape[2]
assert all(im.shape[2] == n_channels for im in imgs)
concatenated_image = np.zeros(
(one_height * y_num, one_width * x_num, n_channels),
dtype=np.uint8,
)
else:
concatenated_image = np.zeros(
(one_height * y_num, one_width * x_num), dtype=np.uint8)
for y in six.moves.range(y_num):
for x in six.moves.range(x_num):
i = x + y * x_num
if i >= len(imgs):
pass
else:
concatenated_image[y * one_height:(y + 1) * one_height,
x * one_width:(x + 1) * one_width] = imgs[i]
return concatenated_image
def get_tile_image(imgs, tile_shape=None, result_img=None, margin_color=None):
"""Concatenate images whose sizes are different.
@param imgs: image list which should be concatenated
@param tile_shape: shape for which images should be concatenated
@param result_img: numpy array to put result image
"""
def resize(*args, **kwargs):
# anti_aliasing arg cannot be passed to skimage<0.14
# use LooseVersion to allow 0.14dev.
if LooseVersion(skimage.__version__) < LooseVersion('0.14'):
kwargs.pop('anti_aliasing', None)
return skimage.transform.resize(*args, **kwargs)
def get_tile_shape(img_num):
x_num = 0
y_num = int(math.sqrt(img_num))
while x_num * y_num < img_num:
x_num += 1
return y_num, x_num
if tile_shape is None:
tile_shape = get_tile_shape(len(imgs))
# get max tile size to which each image should be resized
max_height, max_width = np.inf, np.inf
for img in imgs:
max_height = min([max_height, img.shape[0]])
max_width = min([max_width, img.shape[1]])
# resize and concatenate images
for i, img in enumerate(imgs):
h, w = img.shape[:2]
dtype = img.dtype
h_scale, w_scale = max_height / h, max_width / w
scale = min([h_scale, w_scale])
h, w = int(scale * h), int(scale * w)
img = resize(
image=img,
output_shape=(h, w),
mode='reflect',
preserve_range=True,
anti_aliasing=True,
).astype(dtype)
if len(img.shape) == 3:
img = centerize(img, (max_height, max_width, 3), margin_color)
else:
img = centerize(img, (max_height, max_width), margin_color)
imgs[i] = img
return _tile_images(imgs, tile_shape, result_img)
def label2rgb(lbl, img=None, label_names=None, n_labels=None,
alpha=0.5, thresh_suppress=0):
if label_names is None:
if n_labels is None:
n_labels = lbl.max() + 1 # +1 for bg_label 0
else:
if n_labels is None:
n_labels = len(label_names)
else:
assert n_labels == len(label_names)
cmap = label_colormap(n_labels)
cmap = (cmap * 255).astype(np.uint8)
lbl_viz = cmap[lbl]
lbl_viz[lbl == -1] = (0, 0, 0) # unlabeled
if img is not None:
img_gray = skimage.color.rgb2gray(img)
img_gray = skimage.color.gray2rgb(img_gray)
img_gray *= 255
lbl_viz = alpha * lbl_viz + (1 - alpha) * img_gray
lbl_viz = lbl_viz.astype(np.uint8)
if label_names is None:
return lbl_viz
# cv2 is required only if label_names is not None
import cv2
if cv2 is None:
warnings.warn('label2rgb with label_names requires OpenCV (cv2), '
'so ignoring label_names values.')
return lbl_viz
np.random.seed(1234)
for label in np.unique(lbl):
if label == -1:
continue # unlabeled
mask = lbl == label
if 1. * mask.sum() / mask.size < thresh_suppress:
continue
mask = (mask * 255).astype(np.uint8)
y, x = scipy.ndimage.center_of_mass(mask)
y, x = map(int, [y, x])
if lbl[y, x] != label:
Y, X = np.where(mask)
point_index = np.random.randint(0, len(Y))
y, x = Y[point_index], X[point_index]
text = label_names[label]
font_face = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 0.7
thickness = 2
text_size, baseline = cv2.getTextSize(
text, font_face, font_scale, thickness)
def get_text_color(color):
if color[0] * 0.299 + color[1] * 0.587 + color[2] * 0.114 > 170:
return (0, 0, 0)
return (255, 255, 255)
color = get_text_color(lbl_viz[y, x])
cv2.putText(lbl_viz, text,
(x - text_size[0] // 2, y),
font_face, font_scale, color, thickness)
return lbl_viz
def visualize_segmentation(**kwargs):
"""Visualize segmentation.
Parameters
----------
img: ndarray
Input image to predict label.
lbl_true: ndarray
Ground truth of the label.
lbl_pred: ndarray
Label predicted.
n_class: int
Number of classes.
label_names: dict or list
Names of each label value.
Key or index is label_value and value is its name.
Returns
-------
img_array: ndarray
Visualized image.
"""
img = kwargs.pop('img', None)
lbl_true = kwargs.pop('lbl_true', None)
lbl_pred = kwargs.pop('lbl_pred', None)
n_class = kwargs.pop('n_class', None)
label_names = kwargs.pop('label_names', None)
if kwargs:
raise RuntimeError(
'Unexpected keys in kwargs: {}'.format(kwargs.keys()))
if lbl_true is None and lbl_pred is None:
raise ValueError('lbl_true or lbl_pred must be not None.')
mask_unlabeled = None
viz_unlabeled = None
if lbl_true is not None:
mask_unlabeled = lbl_true == -1
lbl_true[mask_unlabeled] = 0
viz_unlabeled = (
np.random.random((lbl_true.shape[0], lbl_true.shape[1], 3)) * 255
).astype(np.uint8)
if lbl_pred is not None:
lbl_pred[mask_unlabeled] = 0
vizs = []
if lbl_true is not None:
viz_trues = [
img,
label2rgb(lbl_true, label_names=label_names, n_labels=n_class),
label2rgb(lbl_true, img, label_names=label_names,
n_labels=n_class),
]
viz_trues[1][mask_unlabeled] = viz_unlabeled[mask_unlabeled]
viz_trues[2][mask_unlabeled] = viz_unlabeled[mask_unlabeled]
vizs.append(get_tile_image(viz_trues, (1, 3)))
if lbl_pred is not None:
viz_preds = [
img,
label2rgb(lbl_pred, label_names=label_names, n_labels=n_class),
label2rgb(lbl_pred, img, label_names=label_names,
n_labels=n_class),
]
if mask_unlabeled is not None and viz_unlabeled is not None:
viz_preds[1][mask_unlabeled] = viz_unlabeled[mask_unlabeled]
viz_preds[2][mask_unlabeled] = viz_unlabeled[mask_unlabeled]
vizs.append(get_tile_image(viz_preds, (1, 3)))
if len(vizs) == 1:
return vizs[0]
elif len(vizs) == 2:
return get_tile_image(vizs, (2, 1))
else:
raise RuntimeError