-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
445 lines (384 loc) · 14.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
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import math
import numpy as np
import matplotlib.pyplot as plt
import pandas
import seaborn
import skimage
import skimage.color
import skimage.transform
from torch.optim import lr_scheduler
# Adapted from https://github.com/wkentaro/fcn/blob/master/fcn/utils.py
# -----------------------------------------------------------------------------
# Visualization
# -----------------------------------------------------------------------------
def vis(feature_maps):
n_features = feature_maps.size()[1] # number of channels in the feature map
h = feature_maps.size()[2] # feature map shape (1, size, size, n_features)
w = feature_maps.size()[3]
# We will tile our images in this matrix
if n_features % 8 == 0:
display_grid = np.zeros((h * n_features // 8, w * 8))
else:
display_grid = np.zeros((h * (n_features // 8 + 1), w * 8))
# -------------------------------------------------
# Postprocess the feature to be visually palatable
# -------------------------------------------------
feature_maps = feature_maps.detach().cpu().numpy()
# scale = 20. / n_features
features = feature_maps.transpose(0, 2, 3, 1).reshape(-1, n_features)
# min_feat = np.min(feature_maps, axis=0)
# max_feat = np.max(feature_maps, axis=0)
mean_feat = np.mean(features, axis=0)
plt.bar(range(len(mean_feat)), mean_feat, alpha=0.5)
# plt.bar(range(len(max_feat)), max_feat, alpha=0.5)
# plt.hist(feature_maps.reshape(-1))
# plt.legend((str(np.min(feature_maps)), str(np.max(feature_maps))))
plt.savefig('rgb1_hist')
for i in range(n_features):
x = feature_maps[0, i, :, :]
x -= x.mean()
x /= x.std()
x *= 64
x += 128
x = np.clip(x, 0, 255).astype('uint8')
j = i % 8
display_grid[(i // 8) * h:(i // 8 + 1) * h, j * w: (j + 1) * w] = x # Tile each filter into a horizontal grid
# -----------------
# Display the grid
# -----------------
scale = 20
plt.figure(figsize=(scale, scale * n_features / 8 / 8))
# plt.title ( feature_maps.__name__ )
plt.grid(False)
plt.xticks([])
plt.yticks([])
plt.imshow(display_grid, aspect='auto', cmap='viridis') # viridis
plt.savefig('rgb1', bbox_inches = 'tight', pad_inches = 0)
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 range(y_num):
for x in 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):
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, dataloader, img=None, n_labels=None, alpha=0.5):
if n_labels is None:
n_labels = lbl.max() + 1 # +1 for bg_label 0
cmap = dataloader.dataset.getpalette()
# cmap = getpalette(n_labels)
# cmap = np.array(cmap).reshape([-1, 3]).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
lbl_viz = lbl_viz.astype(np.uint8)
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)
ir = kwargs.pop('ir', None)
lbl_true = kwargs.pop('lbl_true', None)
lbl_pred = kwargs.pop('lbl_pred', None)
n_class = kwargs.pop('n_classes', None)
dataloader = kwargs.pop('dataloader', 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.zeros((lbl_true.shape[0], lbl_true.shape[1],
3))).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, dataloader, n_labels=n_class),
label2rgb(lbl_true, dataloader, img, 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 = [
ir,
label2rgb(lbl_pred, dataloader, n_labels=n_class),
label2rgb(lbl_pred, dataloader, img, 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
# -----------------------------------------------------------------------------
# Utilities
# -----------------------------------------------------------------------------
# Adapted from official CycleGAN implementation
def get_scheduler(optimizer, opt):
"""Return a learning rate scheduler
Parameters:
optimizer -- the optimizer of the network
opt (option class) -- stores all the experiment flags; needs to be a subclass of BaseOptions.
opt.lr_policy is the name of learning rate policy: linear | poly | step | plateau | cosine
For 'linear', we keep the same learning rate for the first <opt.niter> epochs
and linearly decay the rate to zero over the next <opt.niter_decay> epochs.
For other schedulers (step, plateau, and cosine), we use the default PyTorch schedulers.
See https://pytorch.org/docs/stable/optim.html for more details.
"""
if opt.lr_policy == 'linear':
def lambda_rule(epoch):
lr = 1.0 - max(0,
epoch + 1 - opt.epochs) / float(opt.niter_decay + 1)
return lr
scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda_rule)
elif opt.lr_policy == 'poly':
def lambda_rule(epoch):
lr = (1 - epoch / opt.epochs)**0.9
return lr
scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda_rule)
elif opt.lr_policy == 'exp':
def lambda_rule(epoch):
lr = 0.95**(epoch-1)
return lr
scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda_rule)
elif opt.lr_policy == 'step':
scheduler = lr_scheduler.StepLR(
optimizer, step_size=opt.lr_decay_step, gamma=0.1)
elif opt.lr_policy == 'plateau':
scheduler = lr_scheduler.ReduceLROnPlateau(
optimizer, mode='min', factor=0.2, threshold=1e-4, patience=5)
elif opt.lr_policy == 'cosine':
scheduler = lr_scheduler.CosineAnnealingLR(optimizer, T_max=opt.epochs)
elif opt.lr_policy is None:
scheduler = None
else:
return NotImplementedError(
f'learning rate policy {opt.lr_policy} is not implemented')
return scheduler
# Adapted from:
# https://github.com/wkentaro/pytorch-fcn/blob/master/examples/voc/learning_curve.py
def learning_curve(log_file):
print(f'==> Plotting log file: {log_file}')
df = pandas.read_csv(log_file)
colors = ['red', 'green', 'blue', 'purple', 'orange']
colors = seaborn.xkcd_palette(colors)
plt.figure(figsize=(20, 6), dpi=300)
row_min = df.min()
row_max = df.max()
# initialize DataFrame for train
columns = [
'epoch',
'train/loss',
'train/acc',
'train/acc_cls',
'train/mean_iu',
'train/fwavacc',
]
df_train = df[columns]
# if hasattr(df_train, 'rolling'):
# df_train = df_train.rolling(window=10).mean()
# else:
# df_train = pandas.rolling_mean(df_train, window=10)
df_train = df_train.dropna()
# initialize DataFrame for val
columns = [
'epoch',
'valid/loss',
'valid/acc',
'valid/acc_cls',
'valid/mean_iu',
'valid/fwavacc',
]
df_valid = df[columns]
df_valid = df_valid.dropna()
data_frames = {'train': df_train, 'valid': df_valid}
n_row = 2
n_col = 2
for i, split in enumerate(['train', 'valid']):
df_split = data_frames[split]
# loss
plt.subplot(n_row, n_col, i * n_col + 1)
plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
plt.plot(
df_split['epoch'],
df_split[f'{split}/loss'],
'-',
markersize=1,
color=colors[0],
alpha=.5,
label=f'{split} loss')
plt.xlim((1, row_max['epoch']))
plt.ylim(
min(df_split[f'{split}/loss']), max(df_split[f'{split}/loss']))
plt.xlabel('epoch')
plt.ylabel(f'{split} loss')
# loss (log)
# plt.subplot(n_row, n_col, i * n_col + 2)
# plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
# plt.semilogy(df_split['epoch'], df_split[f'{split}/loss'],
# '-', markersize=1, color=colors[0], alpha=.5,
# label=f'{split} loss')
# plt.xlim((1, row_max['epoch']))
# plt.ylim(min(df_split[f'{split}/loss']), max(df_split[f'{split}/loss']))
# plt.xlabel('epoch')
# plt.ylabel('f{split} loss (log)')
# lbl accuracy
plt.subplot(n_row, n_col, i * n_col + 2)
plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
plt.plot(
df_split['epoch'],
df_split[f'{split}/acc'],
'-',
markersize=1,
color=colors[1],
alpha=.5,
label=f'{split} accuracy')
plt.plot(
df_split['epoch'],
df_split[f'{split}/acc_cls'],
'-',
markersize=1,
color=colors[2],
alpha=.5,
label=f'{split} accuracy class')
plt.plot(
df_split['epoch'],
df_split[f'{split}/mean_iu'],
'-',
markersize=1,
color=colors[3],
alpha=.5,
label=f'{split} mean IU')
plt.plot(
df_split['epoch'],
df_split[f'{split}/fwavacc'],
'-',
markersize=1,
color=colors[4],
alpha=.5,
label=f'{split} fwav accuracy')
plt.legend()
plt.xlim((1, row_max['epoch']))
plt.ylim((0, 1))
plt.xlabel('epoch')
plt.ylabel(f'{split} label accuracy')
# out_file = osp.splitext(log_file)[0] + '.png'
out_file = log_file[:-4] + '.png'
plt.savefig(out_file)
print(f'==> Wrote figure to: {out_file}')