-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutil_vis.py
344 lines (317 loc) · 13 KB
/
util_vis.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
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 mpl_toolkits.mplot3d.art3d import Poly3DCollection
import PIL
import imageio
from easydict import EasyDict as edict
import camera
@torch.no_grad()
def tb_image(opt,tb,step,group,name,images,num_vis=None,from_range=(0,1),cmap="gray"):
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)
def preprocess_vis_image(opt,images,from_range=(0,1),cmap="gray"):
min,max = from_range
images = (images-min)/(max-min)
images = images.clamp(min=0,max=1).cpu()
if images.shape[1]==1:
images = get_heatmap(opt,images[:,0].cpu(),cmap=cmap)
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,H,W]
color = plt.get_cmap(cmap)(gray.numpy())
color = torch.from_numpy(color[...,:3]).permute(0,3,1,2).float() # [N,3,H,W]
return color
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.visdom.cam_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.visdom.cam_depth)
cam = cam.numpy()
if pose_ref is not None:
_,_,cam_ref = get_camera_mesh(pose_ref,depth=opt.visdom.cam_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")
if pose_ref is not None:
for i in range(N):
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)
if ep==0:
png_fname = "{}/GT.png".format(path)
plt.savefig(png_fname,dpi=75)
for i in range(N):
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):
# get the camera meshes
_,_,cam = get_camera_mesh(pose,depth=opt.visdom.cam_depth)
cam = cam.numpy()
if pose_ref is not None:
_,_,cam_ref = get_camera_mesh(pose_ref,depth=opt.visdom.cam_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=edict(x=(-3,3),y=(-3,3),z=(-3,2.4)))
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 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)
# Copyright 2022 The Nerfstudio Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" Helper functions for visualizing outputs """
import torch
from matplotlib import cm
def apply_colormap(image, cmap="viridis"):
"""Convert single channel to a color image.
Args:
image: Single channel image. : TensorType["bs":..., 1]
cmap: Colormap for image.
Returns:
TensorType: Colored image -> TensorType["bs":..., "rgb":3]
"""
colormap = cm.get_cmap(cmap)
colormap = torch.tensor(colormap.colors).to(image.device) # type: ignore
image_long = (image * 255).long()
image_long[image_long<0]=0
image_long[image_long>255]=255
image_long_min = torch.min(image_long)
image_long_max = torch.max(image_long)
assert image_long_min >= 0, f"the min value is {image_long_min}"
assert image_long_max <= 255, f"the max value is {image_long_max}"
return colormap[image_long[..., 0]]
def apply_depth_colormap(
depth,
accumulation= None,
near_plane= None,
far_plane= None,
cmap="turbo",
):
"""Converts a depth image to color for easier analysis.
Args:
depth: Depth image.: TensorType["bs":..., 1]
accumulation: Ray accumulation used for masking vis. : Optional[TensorType["bs":..., 1]]
near_plane: Closest depth to consider. If None, use min image value. : Optional[float]
far_plane: Furthest depth to consider. If None, use max image value. : Optional[float]
cmap: Colormap to apply. # inferno turbo viridis
Returns:
Colored depth image -> TensorType["bs":..., "rgb":3]
"""
# near_plane = near_plane or float(torch.min(depth))
# far_plane = far_plane or float(torch.max(depth))
# depth = (depth - near_plane) / (far_plane - near_plane + 1e-10)
depth = torch.clip(depth, 0 , 1)
colored_image = apply_colormap(depth, cmap=cmap)
if accumulation is not None:
colored_image = colored_image * accumulation + (1 - accumulation)
return colored_image