def inverse_render_model(points: np.ndarray, sgrid: np.ndarray, lib): # wait for implementing #print("InverseRenderModel") try: x = points[:, 0] except IndexError: print(points.shape[0], points.shape[1]) y = points[:, 1] z = points[:, 2] # Fistly, we get the centroid, then translate the points centroid_x = np.sum(x) / points.shape[0] centroid_y = np.sum(y) / points.shape[0] centroid_z = np.sum(z) / points.shape[0] centroid = np.array([centroid_x, centroid_y, centroid_z]) points = points.astype(np.float) points -= centroid # After normalization, compute the distance between the sphere and points radius = np.sqrt(points[:, 0]**2 + points[:, 1]**2 + points[:, 2]**2) # dist = 1 - (1 / np.max(radius)) * radius # Projection from lie_learn.spaces import S2 radius = np.repeat(radius, 3).reshape(-1, 3) points_on_sphere = points / radius # ssgrid = sgrid.reshape(-1, 3) # phi, theta = S2.change_coordinates(ssgrid, p_from='C', p_to='S') out = S2.change_coordinates(points_on_sphere, p_from='C', p_to='S') phi = out[..., 0] theta = out[..., 1] phi = phi theta = theta % (np.pi * 2) # Interpolate b = sgrid.shape[0] / 2 # bandwidth # By computing the m,n, we can find # the neighbours on the sphere m = np.trunc((phi - np.pi / (4 * b)) / (np.pi / (2 * b))) m = m.astype(int) n = np.trunc(theta / (np.pi / b)) n = n.astype(int) dist_im, center_grid, east_grid, south_grid, southeast_grid = interpolate( m=m, n=n, sgrid=sgrid, points_on_sphere=points_on_sphere, radius=radius) dot_img, cross_img = angle(m=m, n=n, sgrid=sgrid, dist_im=dist_im, lib=lib) #dist_im=dist_im.reshape(-1,1) #wait for validation im = np.stack((dist_im, dot_img, cross_img), axis=0) return im
def make_sgrid(b): theta, phi = S2.meshgrid(b=b, grid_type='Driscoll-Healy') sgrid = S2.change_coordinates(np.c_[theta[..., None], phi[..., None]], p_from='S', p_to='C') sgrid = sgrid.reshape((-1, 3)) return sgrid
def make_sgrid_(b): theta = np.linspace(0, m.pi, num=b) phi = np.linspace(0, 2 * m.pi, num=b) theta_m, phi_m = np.meshgrid(theta, phi) sgrid = S2.change_coordinates(np.c_[theta_m[..., None], phi_m[..., None]], p_from='S', p_to='C') sgrid = sgrid.reshape((-1, 3)) return sgrid
def make_sgrid(b): from lie_learn.spaces import S2 theta, phi = S2.meshgrid(b=b, grid_type='SOFT') sgrid = S2.change_coordinates(np.c_[theta[..., None], phi[..., None]], p_from='S', p_to='C') sgrid = sgrid.reshape((-1, 3)) return (theta, phi), sgrid
def make_sgrid(b, alpha, beta, gamma, grid_type): theta, phi = S2.meshgrid(b=b, grid_type=grid_type) sgrid = S2.change_coordinates(np.c_[theta[..., None], phi[..., None]], p_from='S', p_to='C') sgrid = sgrid.reshape((-1, 3)) R = mesh_op.rotmat(alpha, beta, gamma, hom_coord=False) sgrid = np.einsum('ij,nj->ni', R, sgrid) return sgrid
def make_sgrid(b, alpha, beta, gamma): from lie_learn.spaces import S2 theta, phi = S2.meshgrid(b=b, grid_type='SOFT') sgrid = S2.change_coordinates(np.c_[theta[..., None], phi[..., None]], p_from='S', p_to='C') sgrid = sgrid.reshape((-1, 3)) R = rotmat(alpha, beta, gamma, hom_coord=False) sgrid = np.einsum('ij,nj->ni', R, sgrid) return sgrid
def get_projection_grid(b, grid_type="Driscoll-Healy"): """ returns the spherical grid in euclidean coordinates, where the sphere's center is moved to (0, 0, 1) """ theta, phi = S2.meshgrid(b=b, grid_type=grid_type) grid = S2.change_coordinates(np.c_[theta[..., None], phi[..., None]], p_from='S', p_to='C') grid = grid.reshape((-1, 3)).astype(np.float32) return grid
def spherical_voxel_optimized(points: np.ndarray, size_bandwidth: int, size_radial_divisions: int, radius_support: float, do_random_sampling: bool, num_random_points: int) \ -> Tuple[np.ndarray, np.ndarray]: """Compute spherical voxel using the C++ code. Compute Spherical Voxel signal as defined in: Pointwise Rotation-Invariant Network withAdaptive Sampling and 3D Spherical Voxel Convolution. Yang You, Yujing Lou, Qi Liu, Yu-Wing Tai, Weiming Wang, Lizhuang Ma and Cewu Lu. AAAI 2020. :param points: the points to convert. :param size_bandwidth: alpha and beta bandwidth. :param size_radial_divisions: the number of bins along radial dimension. :param radius_support: the radius used to compute the points in the support. :param do_random_sampling: if true a subset of random points will be used to compute the spherical voxel. :param num_random_points: the number of points to keep if do_random_sampling is true. :return: A tuple containing: The spherical voxel, shape(size_radial_divisions, 2 * size_bandwidth, 2 * size_bandwidth). The points used to compute the signal normalized according the the farthest point. """ if do_random_sampling: min_limit = 1 if points.shape[0] > 1 else 0 indices_random = np.random.randint(min_limit, points.shape[0], num_random_points) points = points[indices_random] pts_norm = np.linalg.norm(points, axis=1) # Scale points to fit unit sphere pts_normed = points / pts_norm[:, None] pts_normed = np.clip(pts_normed, -1, 1) pts_s2_coord = S2.change_coordinates(pts_normed, p_from='C', p_to='S') # Convert to spherical voxel indices pts_s2_coord[:, 0] *= 2 * size_bandwidth / np.pi # [0, pi] pts_s2_coord[:, 1] *= size_bandwidth / np.pi pts_s2_coord[:, 1][pts_s2_coord[:, 1] < 0] += 2 * size_bandwidth # Adaptive sampling factor daas_weights = np.sin(np.pi * (2 * np.arange(2 * size_bandwidth) + 1) / 4 / size_bandwidth).astype(np.float32) voxel = np.asarray(sv.compute(pts_on_s2=pts_s2_coord, pts_norm=pts_norm, size_bandwidth=size_bandwidth, size_radial_divisions=size_radial_divisions, radius_support=radius_support, daas_weights=daas_weights)) pts_normed = points / np.max(pts_norm) return voxel.astype(np.float32), pts_normed.astype(np.float32)
def __getitem__(self, index): b = self.bw pts = np.array(self.pts[index]) # randomly sample points sub_idx = np.random.randint(0, pts.shape[0], 2048) pts = pts[sub_idx] if self.aug: rot = rnd_rot() pts = np.einsum('ij,nj->ni', rot, pts) pts += np.random.rand(3)[None, :] * 0.05 pts = np.einsum('ij,nj->ni', rot.T, pts) segs = np.array(self.segs[index]) segs = segs[sub_idx] labels = self.labels[index] pts_norm = np.linalg.norm(pts, axis=1) pts_normed = pts / pts_norm[:, None] rand_rot = rnd_rot() if self.rand_rot else np.eye(3) rotated_pts_normed = np.clip(pts_normed @ rand_rot, -1, 1) pts_s2 = S2.change_coordinates(rotated_pts_normed, p_from='C', p_to='S') pts_s2[:, 0] *= 2 * b / np.pi # [0, pi] pts_s2[:, 1] *= b / np.pi pts_s2[:, 1][pts_s2[:, 1] < 0] += 2 * b pts_s2_float = pts_s2 # N * 3 pts_so3 = np.stack([ pts_norm * 2 - 1, pts_s2_float[:, 1] / (2 * b - 1) * 2 - 1, pts_s2_float[:, 0] / (2 * b - 1) * 2 - 1 ], axis=1) pts_so3 = np.clip(pts_so3, -1, 1) features = np.asarray( compute(pts_s2_float, np.linalg.norm(pts, axis=1), 2 * b, b, np.sin(np.pi * (2 * np.arange(2 * b) + 1) / 4 / b))) features = np.moveaxis(features, [0, 1, 2], [2, 0, 1])[None] return features.astype(np.float32), pts_so3.astype( np.float32), segs.astype(np.int64), pts @ rand_rot, labels.astype( np.int64)
def __getitem__(self, index): b = hyper.BANDWIDTH_IN pts = np.array(self.pts[index]) # randomly sample points sub_idx = np.random.randint(0, pts.shape[0], hyper.N_PTCLOUD) pts = pts[sub_idx] if self.aug: rot = rnd_rot() pts = np.einsum('ij,nj->ni', rot, pts) pts += np.random.rand(3)[None, :] * 0.05 pts = np.einsum('ij,nj->ni', rot.T, pts) segs = np.array(self.segs[index]) segs = segs[sub_idx] labels = self.labels[index] pts_norm = np.linalg.norm(pts, axis=1) pts_normed = pts / pts_norm[:, None] rand_rot = rnd_rot() if self.rand_rot else np.eye(3) rotated_pts_normed = np.clip(pts_normed @ rand_rot, -1, 1) pts_s2 = S2.change_coordinates(rotated_pts_normed, p_from='C', p_to='S') pts_s2[:, 0] *= 2 * b / np.pi # [0, pi] pts_s2[:, 1] *= b / np.pi pts_s2[:, 1][pts_s2[:, 1] < 0] += 2 * b pts_s2_float = pts_s2 # N * 3 pts_so3 = np.stack([pts_norm * 2 - 1, pts_s2_float[:, 1] / (2 * b - 1) * 2 - 1, pts_s2_float[:, 0] / (2 * b - 1) * 2 - 1], axis=1) pts_so3 = np.clip(pts_so3, -1, 1) # one hundred times speed up ! features = np.asarray(compute(pts_s2_float, np.linalg.norm(pts, axis=1), hyper.R_IN, b, np.sin(np.pi * (2 * np.arange(2 * b) + 1) / 4 / b))) print('SSS', type(pts), pts.shape, pts_so3.shape, features.shape) # 2048 x 3, 2048 x 3, 64 x 64 x 64 print('Mins/maxs/avg pts', pts.min(0), pts.max(0), pts.mean(0)) print('Mins/maxs p so3ts', pts_so3.min(0), pts_so3.max(0)) return features.astype(np.float32), pts_so3.astype(np.float32), segs.astype(np.int64), pts @ rand_rot, labels.astype(np.int64)
def __getitem__(self, index): b = hyper.BANDWIDTH_IN pts = np.array(self.pts[index]) # randomly sample points sub_idx = np.random.randint(0, pts.shape[0], hyper.N_PTCLOUD) pts = pts[sub_idx] if self.aug: rot = rnd_rot() np.einsum('ij,nj->ni', rot, pts) pts += np.random.rand(3)[None, :] * 0.05 np.einsum('ij,nj->ni', rot.T, pts) segs = np.array(self.segs[index]) segs = segs[sub_idx] labels = self.labels[index] pts_norm = np.linalg.norm(pts, axis=1) pts_normed = pts / pts_norm[:, None] rand_rot = rnd_rot() if self.rand_rot else np.eye(3) rotated_pts_normed = np.clip(pts_normed @ rand_rot, -1, 1) pts_s2 = S2.change_coordinates(rotated_pts_normed, p_from='C', p_to='S') pts_s2[:, 0] *= 2 * b / np.pi # [0, pi] pts_s2[:, 1] *= b / np.pi pts_s2[:, 1][pts_s2[:, 1] < 0] += 2 * b pts_s2_float = pts_s2 pts_s2 = (pts_s2 + 0.5).astype(np.int) pts_s2[:, 0] = np.clip(pts_s2[:, 0], 0, 2 * b - 1) pts_s2[:, 1] = np.clip(pts_s2[:, 1], 0, 2 * b - 1) # [0, 2pi] # N * 3 pts_so3 = np.stack([ pts_norm * 2 - 1, pts_s2_float[:, 1] / (2 * b - 1) * 2 - 1, pts_s2_float[:, 0] / (2 * b - 1) * 2 - 1 ], axis=1) pts_so3 = np.clip(pts_so3, -1, 1) # cache data try: if self.cache_dir is None: raise FileNotFoundError features = np.load( os.path.join(self.cache_dir, 'features%d.npy' % index)) except (OSError, FileNotFoundError): features = [] interval = 1. / hyper.R_IN dist = np.linalg.norm(pts, axis=1) # adaptive sampling wt = np.sin(np.pi * (2 * np.arange(2 * b) + 1) / 4 / b) # TODO: rewrite this in an efficient way for i in range(hyper.R_IN): idx = (dist < (i + 2) * interval) & (dist > i * interval) pts_idx = pts_s2[idx] pts_idx_float = pts_s2_float[idx] im = np.zeros([2 * b, 2 * b], np.float32) for beta in range(2 * b): filt = (pts_idx[:, 0] == beta) for alpha in range(2 * b): filt_alpha = filt & ( pts_idx_float[:, 1] > alpha - 1. / 2 / wt[beta] ) & (pts_idx_float[:, 1] < alpha + 1. / 2 / wt[beta]) if not np.any(filt_alpha): continue im[beta, alpha] = (1 - np.abs(dist[idx][filt_alpha] - (i + 1) * interval) / interval ).sum() / np.count_nonzero(filt_alpha) features.append(im) features = np.stack(features, axis=0) if self.cache_dir is not None: np.save(os.path.join(self.cache_dir, 'features%d.npy' % index), features) return features, pts_so3.astype(np.float32), segs.astype( np.int64), pts @ rand_rot, labels.astype(np.int64)
def inverse_render_model(points: np.ndarray, sgrid: np.ndarray): # wait for implementing print("Aloha") x = points[:, 0] y = points[:, 1] z = points[:, 2] # Fistly, we get the centroid, then translate the points centroid_x = np.sum(x) / points.shape[0] centroid_y = np.sum(y) / points.shape[0] centroid_z = np.sum(z) / points.shape[0] centroid = np.array([centroid_x, centroid_y, centroid_z]) points = points.astype(np.float) points -= centroid # After normalization, compute the distance between the sphere and points radius = np.sqrt(points[:, 0] ** 2 + points[:, 1] ** 2 + points[:, 2] ** 2) # dist = 1 - (1 / np.max(radius)) * radius # Projection from lie_learn.spaces import S2 radius = np.repeat(radius, 3).reshape(-1, 3) points_on_sphere = points / radius # ssgrid = sgrid.reshape(-1, 3) # phi, theta = S2.change_coordinates(ssgrid, p_from='C', p_to='S') out = S2.change_coordinates(points_on_sphere, p_from='C', p_to='S') phi = out[..., 0] theta = out[..., 1] phi = phi theta = theta % (np.pi * 2) # Interpolate b = sgrid.shape[0] / 2 # bandwidth # By computing the m,n, we can find # the neighbours on the sphere m = np.trunc((phi - np.pi / (4 * b)) / (np.pi / (2 * b))) m = m.astype(int) n = np.trunc(theta / (np.pi / b)) n = n.astype(int) dist_im, center_grid, east_grid, south_grid, southeast_grid = interpolate(m=m, n=n, sgrid=sgrid, points_on_sphere=points_on_sphere, radius=radius) coef, intercept,center_points =angle(m=m,n=n,sgrid=sgrid,dist_im=dist_im) # utilizing linear regression to create a plane # use a mask to avoid the index out of the boundary """ mask_m = m - 1 >= 0 mask = mask_m m = m[mask] n = n[mask] """ # ======================================================================================= fig = plt.figure() grid = make_sgrid(bandwidth, 0, 0, 0) grid = grid.reshape((-1, 3)) xx = grid[:, 0] yy = grid[:, 1] zz = grid[:, 2] xx = xx.reshape(-1, 1) yy = yy.reshape(-1, 1) zz = zz.reshape(-1, 1) ax = Axes3D(fig) ax.scatter(0, 0, 0) ax.scatter(points[:, 0], points[:, 1], points[:, 2]) ax.scatter(points_on_sphere[:, 0], points_on_sphere[:, 1], points_on_sphere[:, 2]) ax.scatter(center_grid[:, 0], center_grid[:, 1], center_grid[:, 2]) ax.scatter(east_grid[:, 0], east_grid[:, 1], east_grid[:, 2]) ax.scatter(south_grid[:, 0], south_grid[:, 1], south_grid[:, 2]) ax.scatter(southeast_grid[:, 0], southeast_grid[:, 1], southeast_grid[:, 2]) # ax.scatter(xx, yy, zz) # plt.legend() # draw line ax = fig.gca(projection='3d') zero = np.zeros(points_on_sphere.shape[0]) ray_x = np.stack((zero, points_on_sphere[:, 0]), axis=1).reshape(-1, 2) ray_y = np.stack((zero, points_on_sphere[:, 1]), axis=1).reshape(-1, 2) ray_z = np.stack((zero, points_on_sphere[:, 2]), axis=1).reshape(-1, 2) for index in range(points_on_sphere.shape[0]): ax.plot(ray_x[index], ray_y[index], ray_z[index]) # draw plane for index in range(center_points.shape[0]): X = np.arange(center_points[index, 0] - 0.05, center_points[index, 0] + 0.05, 0.01) Y = np.arange(center_points[index, 1] - 0.05, center_points[index, 1] + 0.05, 0.01) X, Y = np.meshgrid(X, Y) a1 = coef[index, 0] a2 = coef[index, 1] b = intercept[index] Z = a1 * X + a2 * Y + b surf = ax.plot_surface(X, Y, Z) plt.show() im = dist_im return im
def f1a(xs): xc = S2.change_coordinates(coords=xs, p_from='S', p_to='C') return xc[..., 0]**2 * xc[..., 1] - 1.4 * xc[..., 2] * xc[ ..., 1]**3 + xc[..., 1] - xc[..., 2]**2 + 2.