-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplot.py
More file actions
executable file
·112 lines (83 loc) · 3.71 KB
/
plot.py
File metadata and controls
executable file
·112 lines (83 loc) · 3.71 KB
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
import matplotlib.pyplot as plt
import numpy as np
from functools import reduce
import cv2
def normalise_mask(mask, threshold=0.5):
mask[mask > threshold] = 1
mask[mask <= threshold] = 0
return mask
def metrics_line(data):
phases = list(data.keys())
metrics = list(data[phases[0]][0].keys())
i = 0
fig, axs = plt.subplots(1, len(metrics))
fig.set_figheight(4)
fig.set_figwidth(4 * len(metrics))
for metric in metrics:
for phase in phases:
axs[i].plot([i[metric] for i in data[phase]], label=phase)
axs[i].set_title(metric)
i += 1
plt.legend()
plt.show()
class Plot(object):
def __init__(self, plots_dir):
super().__init__()
self.plots_dir = plots_dir
self.segments_colors = [(201, 58, 64), (242, 207, 1), (0, 152, 75),
(101, 172, 228), (56, 34, 132), (160, 194, 56)]
def reverse_transform(self, inp):
inp = inp.numpy().transpose((1, 2, 0))
inp = np.clip(inp, 0, 1)
inp = (inp * 255).astype(np.uint8)
return inp
def plot_img_array(self, img_array, ncol=3, index=None, is_mask=False):
nrow = len(img_array) // ncol
f, plots = plt.subplots(nrow, ncol, sharex='all',
sharey='all', figsize=(ncol * 4, nrow * 4))
for i in range(len(img_array)):
plots[i].imshow(img_array[i])
plt.savefig(self.plots_dir + 'PLOT_{}_{: 03d}.png'.format(
'MASK' if is_mask else 'BBOXS', index))
def plot_image_truemask_predictedmask(self, images, labels, preds, index):
input_images_rgb = [self.reverse_transform(x) for x in images]
target_masks_rgb = [self.masks_to_coloredmasks(x) for x in labels]
pred_rgb = [self.masks_to_coloredmasks(x) for x in preds]
img_arrays = [input_images_rgb, target_masks_rgb, pred_rgb]
flatten_list = reduce(lambda x, y: x+y, zip(*img_arrays))
self.plot_img_array(np.array(flatten_list),
ncol=len(img_arrays), index=index,
is_mask=True)
def draw_bboxs(self, im, bboxs):
for label, bbox in bboxs:
color = self.segments_colors[label]
xmin, ymin, xmax, ymax = bbox
im = cv2.rectangle(im, (xmin, ymin), (xmax, ymax), color, 2)
if type(im) is not np.ndarray:
im = im.get()
return im
def plot_image_truebbox_predictedbbox(self, images, bboxs, bboxs_preds, index):
input_image_rgb = [self.reverse_transform(img) for img in images]
image_with_real_masks = [self.draw_bboxs(
img, bboxs) for img in input_image_rgb]
image_with_pred_masks = [self.draw_bboxs(
img, bboxs_preds) for img in input_image_rgb]
img_arrays = [input_image_rgb,
image_with_real_masks,
image_with_pred_masks]
flatten_list = reduce(lambda x, y: x+y, zip(*img_arrays))
self.plot_img_array(np.array(flatten_list),
ncol=len(img_arrays), index=index)
def apply_mask_color(self, mask, mask_color):
colored_mask = np.concatenate(
([mask[..., np.newaxis] * color for color in mask_color]), axis=2)
return colored_mask.astype(np.uint8)
def masks_to_coloredmasks(self, mask, normalise=True):
if normalise:
normalise_mask(mask)
mask_colored = np.concatenate(
[[self.apply_mask_color(mask[i], self.segments_colors[i])] for i in range(len(mask))])
mask_colored = np.max(mask_colored, axis=0)
mask_colored = np.where(
mask_colored.any(-1, keepdims=True), mask_colored, 255)
return mask_colored