-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutil_vis.py
More file actions
328 lines (310 loc) · 12.9 KB
/
util_vis.py
File metadata and controls
328 lines (310 loc) · 12.9 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
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
import numpy as np
import os,sys,time
import torch
import torch.nn.functional as torch_F
import torchvision
import torchvision.transforms.functional as torchvision_F
import matplotlib.pyplot as plt
from matplotlib.colors import BoundaryNorm
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import PIL
import imageio
from easydict import EasyDict as edict
import wandb
import camera
@torch.no_grad()
def tb_wandb_image(opt,tb,step,group,name,images,num_vis=None,from_range=(0,1),cmap="gray", log_wandb=True):
# images should have shape: (num_images, channels, H, W)
# num_vis should have form [num_H, num_W], which represent image grid dimension (i.e. how have image per row)/column?)
images = preprocess_vis_image(opt,images,from_range=from_range,cmap=cmap)
num_H,num_W = num_vis or opt.tb.num_images
images = images[:num_H*num_W]
image_grid = torchvision.utils.make_grid(images[:,:3],nrow=num_W,pad_value=1.)
if images.shape[1]==4:
mask_grid = torchvision.utils.make_grid(images[:,3:],nrow=num_W,pad_value=1.)[:1]
image_grid = torch.cat([image_grid,mask_grid],dim=0)
tag = "{0}/{1}".format(group,name)
tb.add_image(tag,image_grid,step)
if log_wandb:
image = wandb.Image(image_grid, caption=f"{group}/{name}")
wandb.log({f"{group}.{name}": image}, step=step)
return image_grid
def preprocess_vis_image(opt,images,from_range=(0,1),cmap="gray"):
if images.shape[1]==1: # single channel
images = get_heatmap(opt,images.cpu(),cmap=cmap)
else:
min,max = from_range
images = (images-min)/(max-min)
images = images.clamp(min=0,max=1).cpu()
return images
def dump_images(opt,idx,name,images,masks=None,from_range=(0,1),cmap="gray"):
images = preprocess_vis_image(opt,images,masks=masks,from_range=from_range,cmap=cmap) # [B,3,H,W]
images = images.cpu().permute(0,2,3,1).numpy() # [B,H,W,3]
for i,img in zip(idx,images):
fname = "{}/dump/{}_{}.png".format(opt.output_path,i,name)
img_uint8 = (img*255).astype(np.uint8)
imageio.imsave(fname,img_uint8)
def get_heatmap(opt,gray,cmap): # [N,1,H,W]
"""
define the colormap to distinguish positive and negative values
"""
# code copy from https://stackoverflow.com/questions/23994020/colorplot-that-distinguishes-between-positive-and-negative-values
# code copy from https://stackoverflow.com/questions/7821518/matplotlib-save-plot-to-numpy-array
cmap = plt.get_cmap('PuOr')
# extract all colors from the .jet map
cmaplist = [cmap(i) for i in range(cmap.N)]
# create the new map
cmap = cmap.from_list('Custom cmap', cmaplist, cmap.N)
# define the bins and normalize and forcing 0 to be part of the colorbar!
print(f"{gray.shape=}")
ming, maxg = np.min(gray.cpu().numpy()), np.max(gray.cpu().numpy())
r = max(abs(ming), abs(maxg))
r = max(0.5, r)
if r > 5:
bounds = np.arange(-5, 5 , 0.2)
rr = 5.0
while rr < r:
rr *= 1.3
bounds = np.insert(bounds,0,-rr)
bounds = np.append(bounds,rr)
else:
bounds = np.arange(-r, r , r*0.02)
#bounds = np.arange(-2.0,2.0,0.2)
idx=np.searchsorted(bounds,0)
bounds=np.insert(bounds,idx,0)
norm = BoundaryNorm(bounds, cmap.N)
# plot heatmap with matplotlib then convert back to numpy
def plot_np_headmap(gray_img):
# gray_img has shape (1,H,W)
gray_img = gray_img.cpu().permute(1,2,0).numpy()
fig=plt.figure()
fig.add_subplot(111)
plt.imshow(gray_img, interpolation='none',norm=norm,cmap=cmap)
plt.colorbar()
fig.tight_layout(pad=0)
fig.canvas.draw()
# Now we can save it to a numpy array.
data = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
data = data.reshape(fig.canvas.get_width_height()[::-1] + (3,))
data = torch.from_numpy(data).permute(2,0,1) # 3,H,W
data = (data / 255.0).to(gray)
print(f"{data.shape=} {data.dtype=}")
return data
return torch.stack([
plot_np_headmap(gray[i]) for i in range(gray.shape[0])
],axis=0)
def color_border(images,colors,width=3):
images_pad = []
for i,image in enumerate(images):
image_pad = torch.ones(3,image.shape[1]+width*2,image.shape[2]+width*2)*(colors[i,:,None,None]/255.0)
image_pad[:,width:-width,width:-width] = image
images_pad.append(image_pad)
images_pad = torch.stack(images_pad,dim=0)
return images_pad
@torch.no_grad()
def vis_cameras(opt,vis,step,poses=[],colors=["blue","magenta"],plot_dist=True):
win_name = "{}/{}".format(opt.group,opt.name)
data = []
# set up plots
centers = []
for pose,color in zip(poses,colors):
pose = pose.detach().cpu()
vertices,faces,wireframe = get_camera_mesh(pose,depth=opt.camera.visualize_depth)
center = vertices[:,-1]
centers.append(center)
# camera centers
data.append(dict(
type="scatter3d",
x=[float(n) for n in center[:,0]],
y=[float(n) for n in center[:,1]],
z=[float(n) for n in center[:,2]],
mode="markers",
marker=dict(color=color,size=3),
))
# colored camera mesh
vertices_merged,faces_merged = merge_meshes(vertices,faces)
data.append(dict(
type="mesh3d",
x=[float(n) for n in vertices_merged[:,0]],
y=[float(n) for n in vertices_merged[:,1]],
z=[float(n) for n in vertices_merged[:,2]],
i=[int(n) for n in faces_merged[:,0]],
j=[int(n) for n in faces_merged[:,1]],
k=[int(n) for n in faces_merged[:,2]],
flatshading=True,
color=color,
opacity=0.05,
))
# camera wireframe
wireframe_merged = merge_wireframes(wireframe)
data.append(dict(
type="scatter3d",
x=wireframe_merged[0],
y=wireframe_merged[1],
z=wireframe_merged[2],
mode="lines",
line=dict(color=color,),
opacity=0.3,
))
if plot_dist:
# distance between two poses (camera centers)
center_merged = merge_centers(centers[:2])
data.append(dict(
type="scatter3d",
x=center_merged[0],
y=center_merged[1],
z=center_merged[2],
mode="lines",
line=dict(color="red",width=4,),
))
if len(centers)==4:
center_merged = merge_centers(centers[2:4])
data.append(dict(
type="scatter3d",
x=center_merged[0],
y=center_merged[1],
z=center_merged[2],
mode="lines",
line=dict(color="red",width=4,),
))
# send data to visdom
vis._send(dict(
data=data,
win="poses",
eid=win_name,
layout=dict(
title="({})".format(step),
autosize=True,
margin=dict(l=30,r=30,b=30,t=30,),
showlegend=False,
yaxis=dict(
scaleanchor="x",
scaleratio=1,
)
),
opts=dict(title="{} poses ({})".format(win_name,step),),
))
def get_camera_mesh(pose,depth=1):
vertices = torch.tensor([[-0.5,-0.5,1],
[0.5,-0.5,1],
[0.5,0.5,1],
[-0.5,0.5,1],
[0,0,0]])*depth
faces = torch.tensor([[0,1,2],
[0,2,3],
[0,1,4],
[1,2,4],
[2,3,4],
[3,0,4]])
vertices = camera.cam2world(vertices[None],pose)
wireframe = vertices[:,[0,1,2,3,0,4,1,2,4,3]]
return vertices,faces,wireframe
def merge_wireframes(wireframe):
wireframe_merged = [[],[],[]]
for w in wireframe:
wireframe_merged[0] += [float(n) for n in w[:,0]]+[None]
wireframe_merged[1] += [float(n) for n in w[:,1]]+[None]
wireframe_merged[2] += [float(n) for n in w[:,2]]+[None]
return wireframe_merged
def merge_meshes(vertices,faces):
mesh_N,vertex_N = vertices.shape[:2]
faces_merged = torch.cat([faces+i*vertex_N for i in range(mesh_N)],dim=0)
vertices_merged = vertices.view(-1,vertices.shape[-1])
return vertices_merged,faces_merged
def merge_centers(centers):
center_merged = [[],[],[]]
for c1,c2 in zip(*centers):
center_merged[0] += [float(c1[0]),float(c2[0]),None]
center_merged[1] += [float(c1[1]),float(c2[1]),None]
center_merged[2] += [float(c1[2]),float(c2[2]),None]
return center_merged
def plot_save_poses(opt,fig,pose,pose_ref=None,path=None,ep=None):
# get the camera meshes
_,_,cam = get_camera_mesh(pose,depth=opt.camera.visualize_depth)
cam = cam.numpy()
if pose_ref is not None:
_,_,cam_ref = get_camera_mesh(pose_ref,depth=opt.camera.visualize_depth)
cam_ref = cam_ref.numpy()
# set up plot window(s)
plt.title("epoch {}".format(ep))
ax1 = fig.add_subplot(121,projection="3d")
ax2 = fig.add_subplot(122,projection="3d")
setup_3D_plot(ax1,elev=-90,azim=-90,lim=edict(x=(-1,1),y=(-1,1),z=(-1,1)))
setup_3D_plot(ax2,elev=0,azim=-90,lim=edict(x=(-1,1),y=(-1,1),z=(-1,1)))
ax1.set_title("forward-facing view",pad=0)
ax2.set_title("top-down view",pad=0)
plt.subplots_adjust(left=0,right=1,bottom=0,top=0.95,wspace=0,hspace=0)
plt.margins(tight=True,x=0,y=0)
# plot the cameras
N = len(cam)
color = plt.get_cmap("gist_rainbow")
for i in range(N):
if pose_ref is not None:
ax1.plot(cam_ref[i,:,0],cam_ref[i,:,1],cam_ref[i,:,2],color=(0.3,0.3,0.3),linewidth=1)
ax2.plot(cam_ref[i,:,0],cam_ref[i,:,1],cam_ref[i,:,2],color=(0.3,0.3,0.3),linewidth=1)
ax1.scatter(cam_ref[i,5,0],cam_ref[i,5,1],cam_ref[i,5,2],color=(0.3,0.3,0.3),s=40)
ax2.scatter(cam_ref[i,5,0],cam_ref[i,5,1],cam_ref[i,5,2],color=(0.3,0.3,0.3),s=40)
c = np.array(color(float(i)/N))*0.8
ax1.plot(cam[i,:,0],cam[i,:,1],cam[i,:,2],color=c)
ax2.plot(cam[i,:,0],cam[i,:,1],cam[i,:,2],color=c)
ax1.scatter(cam[i,5,0],cam[i,5,1],cam[i,5,2],color=c,s=40)
ax2.scatter(cam[i,5,0],cam[i,5,1],cam[i,5,2],color=c,s=40)
png_fname = "{}/{}.png".format(path,ep)
plt.savefig(png_fname,dpi=75)
# clean up
plt.clf()
def plot_save_poses_blender(opt,fig,pose,pose_ref=None,path=None,ep=None,lim=edict(x=(-3,3),y=(-3,3),z=(-3,2.4))):
# get the camera meshes
_,_,cam = get_camera_mesh(pose,depth=opt.camera.visualize_depth)
cam = cam.numpy()
if pose_ref is not None:
_,_,cam_ref = get_camera_mesh(pose_ref,depth=opt.camera.visualize_depth)
cam_ref = cam_ref.numpy()
# set up plot window(s)
ax = fig.add_subplot(111,projection="3d")
ax.set_title("epoch {}".format(ep),pad=0)
setup_3D_plot(ax,elev=45,azim=35,lim=lim)
plt.subplots_adjust(left=0,right=1,bottom=0,top=0.95,wspace=0,hspace=0)
plt.margins(tight=True,x=0,y=0)
# plot the cameras
N = len(cam)
ref_color = (0.7,0.2,0.7)
pred_color = (0,0.6,0.7)
ax.add_collection3d(Poly3DCollection([v[:4] for v in cam_ref],alpha=0.2,facecolor=ref_color))
for i in range(N):
ax.plot(cam_ref[i,:,0],cam_ref[i,:,1],cam_ref[i,:,2],color=ref_color,linewidth=0.5)
ax.scatter(cam_ref[i,5,0],cam_ref[i,5,1],cam_ref[i,5,2],color=ref_color,s=20)
if ep==0:
png_fname = "{}/GT.png".format(path)
plt.savefig(png_fname,dpi=75)
ax.add_collection3d(Poly3DCollection([v[:4] for v in cam],alpha=0.2,facecolor=pred_color))
for i in range(N):
ax.plot(cam[i,:,0],cam[i,:,1],cam[i,:,2],color=pred_color,linewidth=1)
ax.scatter(cam[i,5,0],cam[i,5,1],cam[i,5,2],color=pred_color,s=20)
for i in range(N):
ax.plot([cam[i,5,0],cam_ref[i,5,0]],
[cam[i,5,1],cam_ref[i,5,1]],
[cam[i,5,2],cam_ref[i,5,2]],color=(1,0,0),linewidth=3)
png_fname = "{}/{}.png".format(path,ep)
plt.savefig(png_fname,dpi=75)
# clean up
plt.clf()
def plot_save_poses_t2(opt,fig,pose,pose_ref=None,path=None,ep=None):
plot_save_poses_blender(opt, fig, pose, pose_ref, path, ep, edict(x=(-7,7),y=(-7,7),z=(-7,7)))
def setup_3D_plot(ax,elev,azim,lim=None):
ax.xaxis.set_pane_color((1.0,1.0,1.0,0.0))
ax.yaxis.set_pane_color((1.0,1.0,1.0,0.0))
ax.zaxis.set_pane_color((1.0,1.0,1.0,0.0))
ax.xaxis._axinfo["grid"]["color"] = (0.9,0.9,0.9,1)
ax.yaxis._axinfo["grid"]["color"] = (0.9,0.9,0.9,1)
ax.zaxis._axinfo["grid"]["color"] = (0.9,0.9,0.9,1)
ax.xaxis.set_tick_params(labelsize=8)
ax.yaxis.set_tick_params(labelsize=8)
ax.zaxis.set_tick_params(labelsize=8)
ax.set_xlabel("X",fontsize=16)
ax.set_ylabel("Y",fontsize=16)
ax.set_zlabel("Z",fontsize=16)
ax.set_xlim(lim.x[0],lim.x[1])
ax.set_ylim(lim.y[0],lim.y[1])
ax.set_zlim(lim.z[0],lim.z[1])
ax.view_init(elev=elev,azim=azim)