-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfuncs.py
392 lines (314 loc) · 14.4 KB
/
funcs.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
import cv2,torch
import numpy as np
from PIL import Image
import torchvision.transforms as T
import torch.nn.functional as F
import scipy.signal
mse2psnr = lambda x : -10. * torch.log(x) / torch.log(torch.Tensor([10.]))
def visualize_depth_numpy(depth, minmax=None, cmap=cv2.COLORMAP_JET):
"""
depth: (H, W)
"""
x = np.nan_to_num(depth) # change nan to 0
if minmax is None:
mi = np.min(x[x>0]) # get minimum positive depth (ignore background)
ma = np.max(x)
else:
mi,ma = minmax
x = (x-mi)/(ma-mi+1e-8) # normalize to 0~1
x = (255*x).astype(np.uint8)
x_ = cv2.applyColorMap(x, cmap)
return x_, [mi,ma]
def init_log(log, keys):
for key in keys:
log[key] = torch.tensor([0.0], dtype=float)
return log
def visualize_depth(depth, minmax=None, cmap=cv2.COLORMAP_JET):
"""
depth: (H, W)
"""
if type(depth) is not np.ndarray:
depth = depth.cpu().numpy()
x = np.nan_to_num(depth) # change nan to 0
if minmax is None:
mi = np.min(x[x>0]) # get minimum positive depth (ignore background)
ma = np.max(x)
else:
mi,ma = minmax
x = (x-mi)/(ma-mi+1e-8) # normalize to 0~1
x = (255*x).astype(np.uint8)
x_ = Image.fromarray(cv2.applyColorMap(x, cmap))
x_ = T.ToTensor()(x_) # (3, H, W)
return x_, [mi,ma]
def N_to_reso(n_voxels, bbox):
xyz_min, xyz_max = bbox
dim = len(xyz_min)
voxel_size = ((xyz_max - xyz_min).prod() / n_voxels).pow(1 / dim)
return ((xyz_max - xyz_min) / voxel_size).long().tolist()
def cal_n_samples(reso, step_ratio=0.5):
return int(np.linalg.norm(reso)/step_ratio)
__LPIPS__ = {}
def init_lpips(net_name, device):
assert net_name in ['alex', 'vgg']
import lpips
print(f'init_lpips: lpips_{net_name}')
return lpips.LPIPS(net=net_name, version='0.1').eval().to(device)
def rgb_lpips(np_gt, np_im, net_name, device):
if net_name not in __LPIPS__:
__LPIPS__[net_name] = init_lpips(net_name, device)
gt = torch.from_numpy(np_gt).permute([2, 0, 1]).contiguous().to(device)
im = torch.from_numpy(np_im).permute([2, 0, 1]).contiguous().to(device)
return __LPIPS__[net_name](gt, im, normalize=True).item()
def findItem(items, target):
for one in items:
if one[:len(target)]==target:
return one
return None
''' Evaluation metrics (ssim, lpips)
'''
def rgb_ssim(img0, img1, max_val,
filter_size=11,
filter_sigma=1.5,
k1=0.01,
k2=0.03,
return_map=False):
# Modified from https://github.com/google/mipnerf/blob/16e73dfdb52044dcceb47cda5243a686391a6e0f/internal/math.py#L58
assert len(img0.shape) == 3
assert img0.shape[-1] == 3
assert img0.shape == img1.shape
# Construct a 1D Gaussian blur filter.
hw = filter_size // 2
shift = (2 * hw - filter_size + 1) / 2
f_i = ((np.arange(filter_size) - hw + shift) / filter_sigma)**2
filt = np.exp(-0.5 * f_i)
filt /= np.sum(filt)
# Blur in x and y (faster than the 2D convolution).
def convolve2d(z, f):
return scipy.signal.convolve2d(z, f, mode='valid')
filt_fn = lambda z: np.stack([
convolve2d(convolve2d(z[...,i], filt[:, None]), filt[None, :])
for i in range(z.shape[-1])], -1)
mu0 = filt_fn(img0)
mu1 = filt_fn(img1)
mu00 = mu0 * mu0
mu11 = mu1 * mu1
mu01 = mu0 * mu1
sigma00 = filt_fn(img0**2) - mu00
sigma11 = filt_fn(img1**2) - mu11
sigma01 = filt_fn(img0 * img1) - mu01
# Clip the variances and covariances to valid values.
# Variance must be non-negative:
sigma00 = np.maximum(0., sigma00)
sigma11 = np.maximum(0., sigma11)
sigma01 = np.sign(sigma01) * np.minimum(
np.sqrt(sigma00 * sigma11), np.abs(sigma01))
c1 = (k1 * max_val)**2
c2 = (k2 * max_val)**2
numer = (2 * mu01 + c1) * (2 * sigma01 + c2)
denom = (mu00 + mu11 + c1) * (sigma00 + sigma11 + c2)
ssim_map = numer / denom
ssim = np.mean(ssim_map)
return ssim_map if return_map else ssim
import torch.nn as nn
class TVLoss(nn.Module):
def __init__(self):
super(TVLoss,self).__init__()
def forward(self,x):
batch_size = x.size()[0]
h_x = x.size()[2]
w_x = x.size()[3]
if w_x==1:
count_h = self._tensor_size(x[:,:,1:,:])
h_tv = torch.pow((x[:,:,1:,:]-x[:,:,:h_x-1,:]),2).sum()
return 2*(h_tv/count_h)/batch_size
if h_x==1:
count_w = self._tensor_size(x[:,:,:,1:])
w_tv = torch.pow((x[:,:,:,1:]-x[:,:,:,:w_x-1]),2).sum()
return 2*(w_tv/count_w)/batch_size
count_h = self._tensor_size(x[:,:,1:,:])
count_w = self._tensor_size(x[:,:,:,1:])
h_tv = torch.pow((x[:,:,1:,:]-x[:,:,:h_x-1,:]),2).sum()
w_tv = torch.pow((x[:,:,:,1:]-x[:,:,:,:w_x-1]),2).sum()
return 2*(h_tv/count_h+w_tv/count_w)/batch_size
def _tensor_size(self,t):
return t.size()[1]*t.size()[2]*t.size()[3]
def simple_tv_loss(x):
'''
x: [n,n]
'''
return F.mse_loss(x[:-1, :], x[1:, :]) + F.mse_loss(x[:, :-1], x[:, 1:])
import plyfile
import skimage.measure
def convert_sdf_samples_to_ply(
pytorch_3d_sdf_tensor,
ply_filename_out,
bbox,
level=0.5,
offset=None,
scale=None,
):
"""
Convert sdf samples to .ply
:param pytorch_3d_sdf_tensor: a torch.FloatTensor of shape (n,n,n)
:voxel_grid_origin: a list of three floats: the bottom, left, down origin of the voxel grid
:voxel_size: float, the size of the voxels
:ply_filename_out: string, path of the filename to save to
This function adapted from: https://github.com/RobotLocomotion/spartan
"""
numpy_3d_sdf_tensor = pytorch_3d_sdf_tensor.numpy()
voxel_size = list((bbox[1]-bbox[0]) / np.array(pytorch_3d_sdf_tensor.shape))
verts, faces, normals, values = skimage.measure.marching_cubes(
numpy_3d_sdf_tensor, level=level, spacing=voxel_size
)
faces = faces[...,::-1] # inverse face orientation
# transform from voxel coordinates to camera coordinates
# note x and y are flipped in the output of marching_cubes
mesh_points = np.zeros_like(verts)
mesh_points[:, 0] = bbox[0,0] + verts[:, 0]
mesh_points[:, 1] = bbox[0,1] + verts[:, 1]
mesh_points[:, 2] = bbox[0,2] + verts[:, 2]
# apply additional offset and scale
if scale is not None:
mesh_points = mesh_points / scale
if offset is not None:
mesh_points = mesh_points - offset
# try writing to the ply file
num_verts = verts.shape[0]
num_faces = faces.shape[0]
verts_tuple = np.zeros((num_verts,), dtype=[("x", "f4"), ("y", "f4"), ("z", "f4")])
for i in range(0, num_verts):
verts_tuple[i] = tuple(mesh_points[i, :])
faces_building = []
for i in range(0, num_faces):
faces_building.append(((faces[i, :].tolist(),)))
faces_tuple = np.array(faces_building, dtype=[("vertex_indices", "i4", (3,))])
el_verts = plyfile.PlyElement.describe(verts_tuple, "vertex")
el_faces = plyfile.PlyElement.describe(faces_tuple, "face")
ply_data = plyfile.PlyData([el_verts, el_faces])
print("saving mesh to %s" % (ply_filename_out))
ply_data.write(ply_filename_out)
def get_similarity_matrix(x):
'''
x: [batch_size, dim]
'''
similarity_matrix = F.cosine_similarity(x.unsqueeze(0), x.unsqueeze(1), dim=2)
return similarity_matrix # [batch_size, batch_size]
from loguru import logger
from tqdm import tqdm
def init_logger(log_dir):
logger.remove() # Remove default logger
log_format = "<green>{time:YYYY-MM-DD HH:mm:ss}</green> <level>{message}</level>"
logger.add(lambda msg: tqdm.write(msg, end=""), colorize=True, format=log_format)
logger.add(log_dir / 'log.txt', colorize=False, format=log_format)
# segmentation visualization
def hex_to_rgb(x):
return [int(x[i:i + 2], 16) / 255 for i in (1, 3, 5)]
class DistinctColors:
def __init__(self):
colors = [
'#e6194B', '#3cb44b', '#ffe119', '#4363d8', '#f55031', '#911eb4', '#42d4f4', '#bfef45', '#fabed4', '#469990',
'#dcb1ff', '#404E55', '#fffac8', '#809900', '#aaffc3', '#808000', '#ffd8b1', '#000075', '#a9a9a9', '#f032e6',
'#806020', '#ffffff',
"#FAD09F", "#FF8A9A", "#D157A0", "#BEC459", "#456648", "#0030ED", "#3A2465", "#34362D", "#B4A8BD", "#0086AA",
"#452C2C", "#636375", "#A3C8C9", "#FF913F", "#938A81", "#575329", "#00FECF", "#B05B6F", "#8CD0FF", "#3B9700",
"#04F757", "#C8A1A1", "#1E6E00",
"#7900D7", "#A77500", "#6367A9", "#A05837", "#6B002C", "#772600", "#D790FF", "#9B9700",
"#549E79", "#FFF69F", "#201625", "#72418F", "#BC23FF", "#99ADC0", "#3A2465", "#922329",
"#5B4534", "#FDE8DC", "#404E55", "#0089A3", "#CB7E98", "#A4E804", "#324E72", "#6A3A4C",
]
self.hex_colors = colors
# 0 = crimson / red, 1 = green, 2 = yellow, 3 = blue
# 4 = orange, 5 = purple, 6 = sky blue, 7 = lime green
self.colors = [hex_to_rgb(c) for c in colors]
self.color_assignments = {}
self.color_ctr = 0
self.fast_color_index = torch.from_numpy(np.array([hex_to_rgb(colors[i % len(colors)]) for i in range(8096)] + [hex_to_rgb('#000000')]))
def get_color(self, index, override_color_0=False):
colors = [x for x in self.hex_colors]
if override_color_0:
colors[0] = "#3f3f3f"
colors = [hex_to_rgb(c) for c in colors]
if index not in self.color_assignments:
self.color_assignments[index] = colors[self.color_ctr % len(self.colors)]
self.color_ctr += 1
return self.color_assignments[index]
def get_color_fast_torch(self, index):
return self.fast_color_index[index]
def get_color_fast_numpy(self, index, override_color_0=False):
index = np.array(index).astype(np.int32)
if override_color_0:
colors = [x for x in self.hex_colors]
colors[0] = "#3f3f3f"
fast_color_index = torch.from_numpy(np.array([hex_to_rgb(colors[i % len(colors)]) for i in range(8096)] + [hex_to_rgb('#000000')]))
return fast_color_index[index % fast_color_index.shape[0]].numpy()
else:
return self.fast_color_index[index % self.fast_color_index.shape[0]].numpy()
def apply_colors(self, arr):
out_arr = torch.zeros([arr.shape[0], 3])
for i in range(arr.shape[0]):
out_arr[i, :] = torch.tensor(self.get_color(arr[i].item()))
return out_arr
def apply_colors_fast_torch(self, arr):
return self.fast_color_index[arr % self.fast_color_index.shape[0]]
def apply_colors_fast_numpy(self, arr):
return self.fast_color_index.numpy()[arr % self.fast_color_index.shape[0]]
def get_boundary_mask(arr, dialation_size=1):
import cv2
arr_t, arr_r, arr_b, arr_l = arr[1:, :], arr[:, 1:], arr[:-1, :], arr[:, :-1]
arr_t_1, arr_r_1, arr_b_1, arr_l_1 = arr[2:, :], arr[:, 2:], arr[:-2, :], arr[:, :-2]
kernel = np.ones((dialation_size, dialation_size), 'uint8')
if isinstance(arr, torch.Tensor):
arr_t = torch.cat([arr_t, arr[-1, :].unsqueeze(0)], dim=0)
arr_r = torch.cat([arr_r, arr[:, -1].unsqueeze(1)], dim=1)
arr_b = torch.cat([arr[0, :].unsqueeze(0), arr_b], dim=0)
arr_l = torch.cat([arr[:, 0].unsqueeze(1), arr_l], dim=1)
arr_t_1 = torch.cat([arr_t_1, arr[-2, :].unsqueeze(0), arr[-1, :].unsqueeze(0)], dim=0)
arr_r_1 = torch.cat([arr_r_1, arr[:, -2].unsqueeze(1), arr[:, -1].unsqueeze(1)], dim=1)
arr_b_1 = torch.cat([arr[0, :].unsqueeze(0), arr[1, :].unsqueeze(0), arr_b_1], dim=0)
arr_l_1 = torch.cat([arr[:, 0].unsqueeze(1), arr[:, 1].unsqueeze(1), arr_l_1], dim=1)
boundaries = torch.logical_or(torch.logical_or(torch.logical_or(torch.logical_and(arr_t != arr, arr_t_1 != arr), torch.logical_and(arr_r != arr, arr_r_1 != arr)), torch.logical_and(arr_b != arr, arr_b_1 != arr)), torch.logical_and(arr_l != arr, arr_l_1 != arr))
boundaries = boundaries.cpu().numpy().astype(np.uint8)
boundaries = cv2.dilate(boundaries, kernel, iterations=1)
boundaries = torch.from_numpy(boundaries).to(arr.device)
else:
arr_t = np.concatenate([arr_t, arr[-1, :][np.newaxis, :]], axis=0)
arr_r = np.concatenate([arr_r, arr[:, -1][:, np.newaxis]], axis=1)
arr_b = np.concatenate([arr[0, :][np.newaxis, :], arr_b], axis=0)
arr_l = np.concatenate([arr[:, 0][:, np.newaxis], arr_l], axis=1)
arr_t_1 = np.concatenate([arr_t_1, arr[-2, :][np.newaxis, :], arr[-1, :][np.newaxis, :]], axis=0)
arr_r_1 = np.concatenate([arr_r_1, arr[:, -2][:, np.newaxis], arr[:, -1][:, np.newaxis]], axis=1)
arr_b_1 = np.concatenate([arr[0, :][np.newaxis, :], arr[1, :][np.newaxis, :], arr_b_1], axis=0)
arr_l_1 = np.concatenate([arr[:, 0][:, np.newaxis], arr[:, 1][:, np.newaxis], arr_l_1], axis=1)
boundaries = np.logical_or(np.logical_or(np.logical_or(np.logical_and(arr_t != arr, arr_t_1 != arr), np.logical_and(arr_r != arr, arr_r_1 != arr)), np.logical_and(arr_b != arr, arr_b_1 != arr)), np.logical_and(arr_l != arr, arr_l_1 != arr)).astype(np.uint8)
boundaries = cv2.dilate(boundaries, kernel, iterations=1)
return boundaries
def vis_seg(dc, class_index, H, W, rgb=None, alpha = 0.65):
segmentation_map = dc.apply_colors_fast_torch(class_index)
if rgb is not None:
segmentation_map = segmentation_map * alpha + rgb * (1 - alpha)
boundaries = get_boundary_mask(class_index.view(H, W))
segmentation_map = segmentation_map.reshape(H, W, 3)
segmentation_map[boundaries > 0, :] = 0
segmentation_map = segmentation_map.detach().numpy().astype(np.float32)
segmentation_map *= 255.
segmentation_map = segmentation_map.astype(np.uint8)
return segmentation_map
# point cloud
from pytorch3d.structures import Pointclouds
from pytorch3d.io import IO
def construct_points_coordinates(rays, depth):
'''
Construct points' coordinates of a point cloud, every point corresponds to
a point on one ray with specified depth.
Args:
rays: [n_rays, 6]
depth: [n_rays]
Return:
point_cloud: [n_rays, 3]
'''
rays_o, rays_d = rays[:, :3], rays[:, 3:6]
points_coordinates = rays_o + rays_d * depth[...,None]
return points_coordinates
def save_points_to_ply(points, colors, filename):
point_cloud = Pointclouds(points=[points], features=[colors*255])
IO().save_pointcloud(point_cloud, filename)