forked from nianticlabs/monodepth2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvis_utils.py
204 lines (165 loc) · 6.71 KB
/
vis_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
import numpy as np
from PIL import Image
import numpy as np
import torch
import matplotlib
import matplotlib.pylab
import matplotlib.pyplot as plt
import torch.nn.functional as F
DEFAULT_COLORMAP: matplotlib.colors.LinearSegmentedColormap = matplotlib.pylab.cm.plasma
def convert_figure_numpy(figure):
""" Convert figure to numpy image """
figure_np = np.frombuffer(figure.canvas.tostring_rgb(), dtype=np.uint8)
figure_np = figure_np.reshape(figure.canvas.get_width_height()[::-1] + (3,))
return figure_np
def np_uint8_to_pil(np_img: np.ndarray) -> Image.Image:
if np_img.dtype != np.uint8:
raise TypeError(f"Expected np.ndarray of dtype np.uint8, but got dtype {np_img.dtype}")
if np_img.ndim == 3 and np_img.shape[-1] == 1:
np_img = np.squeeze(np_img)
elif np_img.ndim != 2:
raise ValueError(f"Unsupported shape {np_img.shape}")
pil_img = Image.fromarray(np_img, mode='L')
return pil_img
def flow_to_image(flow: np.ndarray, autoscale: bool = True) -> np.ndarray:
"""
Applies colour map to flow which should be a 2 channel image tensor HxWx2. Returns a HxWx3 numpy image
Code adapted from: https://github.com/liruoteng/FlowNet/blob/master/models/flownet/scripts/flowlib.py
"""
u = flow[0, :, :]
v = flow[1, :, :]
# Convert to polar coordinates
rad = np.sqrt(u ** 2 + v ** 2)
maxrad = np.max(rad)
# Normalise flow maps
if autoscale:
u /= maxrad + np.finfo(float).eps
v /= maxrad + np.finfo(float).eps
# visualise flow with cmap
return np_rgb_to_tensorboard_img(np.uint8(compute_color(u, v) * 255))
def apply_colour_map(
image: np.ndarray, cmap: matplotlib.colors.LinearSegmentedColormap = DEFAULT_COLORMAP, autoscale: bool = True
) -> np.ndarray:
"""
Applies a colour map to the given 1 or 2 channel numpy image. if 2 channel, must be 2xHxW.
Returns a HxWx3 numpy image
"""
if image.ndim == 2 or (image.ndim == 3 and image.shape[0] == 1):
if image.ndim == 3:
image = image[0]
# grayscale scalar image
if autoscale:
image = _normalise(image)
return cmap(image)[:, :, :3]
if image.shape[0] == 2:
# 2 dimensional UV
return flow_to_image(image, autoscale=autoscale)
if image.shape[0] == 3:
# normalise rgb channels
if autoscale:
image = _normalise(image)
return np.transpose(image, axes=[1, 2, 0])
raise Exception('Image must be 1, 2 or 3 channel to convert to colour_map (CxHxW)')
def _normalise(image: np.ndarray) -> np.ndarray:
lower = np.min(image)
delta = np.max(image) - lower
if delta == 0:
delta = 1
image = (image.astype(np.float32) - lower) / delta
return image
def heatmap_image(
image: np.ndarray, cmap: matplotlib.colors.LinearSegmentedColormap = DEFAULT_COLORMAP, autoscale: bool = True
) -> np.ndarray:
"""Colorize an 1 or 2 channel image with a colourmap."""
if not issubclass(image.dtype.type, np.floating):
raise ValueError(f"Expected a ndarray of float type, but got dtype {image.dtype}")
if not (image.ndim == 2 or (image.ndim == 3 and image.shape[0] in [1, 2])):
raise ValueError(f"Expected a ndarray of shape [H, W] or [1, H, W] or [2, H, W], but got shape {image.shape}")
heatmap_np = apply_colour_map(image, cmap=cmap, autoscale=autoscale)
heatmap_np = np.uint8(heatmap_np * 255)
return np_rgb_to_tensorboard_img(heatmap_np)
def compute_color(u: np.ndarray, v: np.ndarray) -> np.ndarray:
assert u.shape == v.shape
[h, w] = u.shape
img = np.zeros([h, w, 3])
nan_mask = np.isnan(u) | np.isnan(v)
u[nan_mask] = 0
v[nan_mask] = 0
colorwheel = make_color_wheel()
ncols = np.size(colorwheel, 0)
rad = np.sqrt(u ** 2 + v ** 2)
a = np.arctan2(-v, -u) / np.pi
f_k = (a + 1) / 2 * (ncols - 1) + 1
k_0 = np.floor(f_k).astype(int)
k_1 = k_0 + 1
k_1[k_1 == ncols + 1] = 1
f = f_k - k_0
for i in range(0, np.size(colorwheel, 1)):
tmp = colorwheel[:, i]
col0 = tmp[k_0 - 1] / 255
col1 = tmp[k_1 - 1] / 255
col = (1 - f) * col0 + f * col1
idx = rad <= 1
col[idx] = 1 - rad[idx] * (1 - col[idx])
notidx = np.logical_not(idx)
col[notidx] *= 0.75
img[:, :, i] = col * (1 - nan_mask)
return img
def make_color_wheel() -> np.ndarray:
"""
Create colour wheel.
Code adapted from https://github.com/liruoteng/FlowNet/blob/master/models/flownet/scripts/flowlib.py
"""
red_yellow = 15
yellow_green = 6
green_cyan = 4
cyan_blue = 11
blue_magenta = 13
magenta_red = 6
ncols = red_yellow + yellow_green + green_cyan + cyan_blue + blue_magenta + magenta_red
colorwheel = np.zeros([ncols, 3])
col = 0
# red_yellow
colorwheel[0:red_yellow, 0] = 255
colorwheel[0:red_yellow, 1] = np.transpose(np.floor(255 * np.arange(0, red_yellow) / red_yellow))
col += red_yellow
# yellow_green
colorwheel[col : col + yellow_green, 0] = 255 - np.transpose(
np.floor(255 * np.arange(0, yellow_green) / yellow_green)
)
colorwheel[col : col + yellow_green, 1] = 255
col += yellow_green
# green_cyan
colorwheel[col : col + green_cyan, 1] = 255
colorwheel[col : col + green_cyan, 2] = np.transpose(np.floor(255 * np.arange(0, green_cyan) / green_cyan))
col += green_cyan
# cyan_blue
colorwheel[col : col + cyan_blue, 1] = 255 - np.transpose(np.floor(255 * np.arange(0, cyan_blue) / cyan_blue))
colorwheel[col : col + cyan_blue, 2] = 255
col += cyan_blue
# blue_magenta
colorwheel[col : col + blue_magenta, 2] = 255
colorwheel[col : col + blue_magenta, 0] = np.transpose(np.floor(255 * np.arange(0, blue_magenta) / blue_magenta))
col += +blue_magenta
# magenta_red
colorwheel[col : col + magenta_red, 2] = 255 - np.transpose(np.floor(255 * np.arange(0, magenta_red) / magenta_red))
colorwheel[col : col + magenta_red, 0] = 255
return colorwheel
def np_rgb_to_tensorboard_img(img: np.ndarray) -> torch.Tensor:
tensor_img = _np_img_hwc_to_tensor_chw(img)
tensor_img = tensor_img.to(torch.float32) / 255.0 # [0, 255] -> [0.0, 1.0]
return tensor_img
def _np_img_hwc_to_tensor_chw(img: np.ndarray) -> torch.Tensor:
assert img.dtype == np.uint8
img = _np_img_hwc_to_chw(img) # HWC -> CHW
torch_img = torch.from_numpy(img)
return torch_img
def _np_img_hwc_to_chw(img: np.ndarray) -> np.ndarray:
if img.ndim == 3:
assert img.shape[2] in [1, 3]
img = np.transpose(img, (2, 0, 1))
elif img.ndim == 2:
img = np.expand_dims(img, axis=0)
else:
raise ValueError(f"Expected array with 2 or 3 dimensions, but got array with shape {img.shape}")
return img