def compute(self, X, r=None): N = len(X) r_array = pairwise_distance(X, X) mean_dist = r_array.mean() r_array_n = r_array / mean_dist r_bin_edges = logspace(log10(self.r_inner), log10(self.r_outer), self.nbins_r) r_array_q = zeros((N, N), dtype=int) for m in xrange(self.nbins_r): r_array_q += (r_array_n < r_bin_edges[m]) fz = r_array_q > 0 theta_array = self._get_angles(X) # 2Pi shifted theta_array_2 = theta_array + 2 * math.pi * (theta_array < 0) theta_array_q = int_(1 + floor(theta_array_2 / (2 * math.pi / self.nbins_theta))) # norming by mass(mean) angle v.0.1 ############################################ # By Andrey Nikishaev # theta_array_delta = theta_array - theta_array.mean() # theta_array_delta_2 = theta_array_delta + 2*math.pi * (theta_array_delta < 0) # theta_array_q = 1 + floor(theta_array_delta_2 /(2 * math.pi / self.nbins_theta)) ################################################################################ BH = zeros((N, self.nbins)) for i in xrange(N): sn = zeros((self.nbins_r, self.nbins_theta)) for j in xrange(N): if (fz[i, j]): sn[r_array_q[i, j] - 1, theta_array_q[i, j] - 1] += 1 BH[i] = sn.reshape(self.nbins) return BH
def wFM_on_sphere(self, inputs): # print("---------------------------------\n[wFMLayer]") # print("===\nSize: {}".format(self.w.size())) # print("===\nWeight: \n{}\n".format(self.w)) # print("---------------------------------\n") # Get Dimensions of Input B, N, D, C = inputs.shape v = self.conv(inputs) inputs = inputs.contiguous() inputs = inputs.view(B, N, D * C) # Downsampling if self.down_sample_rate != 1: inputs = down_sampling(inputs, v.squeeze(), int(N * self.down_sample_rate)) N = int(N * self.down_sample_rate) inputs = inputs.view(B, N, D, C) # Get KNN Matrix adj = utils.pairwise_distance(inputs) print("---------------------------------\n[Adj Matrix") print(adj) print("---------------------------------\n") knn_matrix = utils.knn(adj, k=self.k, include_myself=True) knn_matrix = torch.Tensor(knn_matrix).long() idx = torch.arange( B ) * N # IDs for later processing, used because we flatten the tensor idx = idx.view((B, 1, 1)) # reshape to be added to knn indices # Combine in * k and normalize there # Get [B * N * K * D * C] k2 = knn_matrix + idx ptcld = inputs.view(B * N, D, C) # [(B*N) * (D*C)] ptcld = ptcld.view(B * N, D * C) gathered = ptcld[k2] # [B * N * K * (D*C)] gathered = gathered.view(B, N, self.k, D, C) # [B * N * K * D * C] gathered = gathered.permute(0, 1, 3, 4, 2) # [B * N * D * C * K] weighted = gathered * weight_normalize(self.w1, dim=1) # [B * N * D * C * K] weighted = torch.sum(weighted, dim=-1) # [B * N * D * C] weighted = torch.matmul(weighted, weight_normalize(self.w2, dim=0)) # [B * N * D * Cout] return weighted
def calc_laplacian_mat(points, k): num_of_points = points.shape[0] adj_mat = utls.pairwise_distance(points, points) distance, indices = utls.knn(adj_mat, k=k) dst_1_k = 1/tf.cast(distance, dtype=tf.float64)[:, 1:] dst_0 = tf.reduce_sum(dst_1_k, axis=1, keepdims=True) distance = tf.concat((-dst_0, dst_1_k), axis=1) data = tf.reshape(distance, [-1]) columns = tf.reshape(indices, [-1, 1]) rows = tf.reshape(tf.range(num_of_points), [-1, 1]) rows = tf.keras.backend.repeat(rows, k) rows = tf.reshape(rows, [-1, 1]) index = tf.cast(tf.concat((rows, columns), axis=1), dtype=tf.int64) return tf.sparse.reorder(tf.SparseTensor(indices=index, values=data, dense_shape=(num_of_points, num_of_points)))
def setup(self, bottom, top): self.numNN = int(self.param_str) self.numpts = int(bottom[0].data.shape[3]) self.point_cloud = np.squeeze(bottom[0].data).transpose(0, 2, 1) adj = utils.pairwise_distance(self.point_cloud) self.nn_idx = utils.knn(adj, k=self.numNN)
def main(): logger = setup_logger('Data_Generation') args = load_args() logger.info("Received Args: \n{}".format(args)) ########################################################## # Data Loading # ########################################################## logger.info("Start Loading MNIST Data and Configurations") f_train = h5py.File(args.train_file_path) f_test = h5py.File(args.test_file_path) logger.info("--> Loading Configurations") if args.test: f_data = f_test output_file_name = "test.gz" output_path = join(args.output_prefix, "test") else: f_data = f_train output_file_name = "train.gz" output_path = join(args.output_prefix, "train") """ Data Size (num of images to be loaded)""" if args.demo: data_size = args.demo # Number of images to be loaded output_file_name = "demo_" + output_file_name output_path = join(args.output_prefix, "demo") else: data_size = f_data['data'].shape[0] """ Number of Points (MNIST => 512)""" num_points = f_data['data'].shape[1] """ Load Labels """ logger.info("--> Loading Labels") np_labels = np.array(f_data['labels'])[0:data_size] # [1, 2, 3] tensor_labels = torch.from_numpy(np_labels) """ Load Raw Data Set """ logger.info("--> Loading Data Set") np_dataset = np.array(f_data['data'])[0:data_size] # (data_size, 512, 2) tensor_dataset = torch.from_numpy(np_dataset).float() # convert from numpy.ndarray to torch.Tensor """ Adjust Data Dimension """ if tensor_dataset.shape[-1] == 2: """ if deal with 2D point set, have to add one dimension as z dimension z dimension should be padded with 0, since point is ranged from -1 to 1, 0 is the average value """ # (train_size * num_points, 3) -> z-dimension additionally padded by 0 -> (x, y, 0) logger.info("--> Data Dimension Adjustment Operated") zero_padding = torch.zeros((data_size, num_points, 1), dtype=tensor_dataset.dtype) tensor_dataset = torch.cat((tensor_dataset, zero_padding), -1) # (data_size, num_points, 3) logger.info("Finish Loading MNIST Data and Basic Configuration") ########################################################## # Adjacent Matrix # ########################################################## logger.info("Start Computing Adjacent Matrix") """ Adjacent Matrix """ batch_size = args.batch_size start, end = 0, args.batch_size total_count = tensor_dataset.size()[0] adj_tensor_datasets = [] while end < tensor_dataset.size()[0]: tensor_dataset_subset = tensor_dataset[start : end] tensor_dataset_subset_adj = utils.pairwise_distance(tensor_dataset_subset) adj_tensor_datasets.append(tensor_dataset_subset_adj) progress(end, total_count) start += batch_size end += batch_size tensor_dataset_subset = tensor_dataset[start: tensor_dataset.size()[0]] tensor_dataset_subset_adj = utils.pairwise_distance(tensor_dataset_subset) adj_tensor_datasets.append(tensor_dataset_subset_adj) progress(total_count, total_count) adjacent_matrix = torch.cat(tuple(adj_tensor_datasets), dim=0) logger.info("Finish Computing Adjacent Matrix".ljust(60)) ########################################################## # Data Generation # ########################################################## logger.info("Start Computing Dataset Generation") """ Normalization (Raw) """ logger.info("--> Normalizing Raw Data") tensor_dataset = raw_data_normalization(tensor_dataset) """ Grid """ logger.info("--> Constructing Grid") grid_size = args.grid_size grid = grid_generation(grid_size) """ Mapping and Normalization """ logger.info("--> Computing Mapping and Normalization") sigma = args.sigma batch_size = args.batch_size start, end = 0, args.batch_size # index, save_batch = 0, args.save_batch # save_start = 0 total_count = tensor_dataset.size()[0] mapped_tensor_datasets = list() while end < tensor_dataset.size()[0]: tensor_dataset_subset = tensor_dataset[start : end] # (batch_size, num_points, 3) tensor_dataset_mapped_norm = map_and_norm(tensor_dataset_subset, grid, sigma) # (batch_size, num_points, grid_size^3) mapped_tensor_datasets.append(tensor_dataset_mapped_norm) # """ Saving Processes """ # if len(mapped_tensor_datasets) >= save_batch: # save_file_name = "part{index}_{filename}".format(index=index, filename=output_file_name) # tensor_dataset_part = torch.cat(tuple(mapped_tensor_datasets), dim=0) # tensor_labels_part = tensor_labels[save_start : end] # adjacent_matrix_part = adjacent_matrix[save_start : end] # print(tensor_dataset_part.size()) # print(tensor_labels_part.size()) # print(adjacent_matrix_part.size()) # dataConstructor = DatasetConstructor(tensor_dataset_part, tensor_labels_part, adjacent_matrix_part) # print(sys.getsizeof(object)) # with gzip.open(os.path.join(output_path, save_file_name), 'wb') as file: # pickle.dump(dataConstructor, file) # mapped_tensor_datasets = list() # save_start = end # index += 1 progress(end, total_count) start += batch_size end += batch_size tensor_dataset_subset = tensor_dataset[start: tensor_dataset.size()[0]] tensor_dataset_mapped_norm = map_and_norm(tensor_dataset_subset, grid, sigma) mapped_tensor_datasets.append(tensor_dataset_mapped_norm) tensor_dataset = torch.cat(tuple(mapped_tensor_datasets), dim=0) # print(tensor_dataset_part.size()) # print(tensor_labels_part.size()) # print(adjacent_matrix_part.size()) # save_file_name = "part{index}_{filename}".format(index=index, filename=output_file_name) # tensor_dataset_part = torch.cat(tuple(mapped_tensor_datasets), dim=0) # tensor_labels_part = tensor_labels[save_start : tensor_dataset.size()[0]] # adjacent_matrix_part = adjacent_matrix[save_start : tensor_dataset.size()[0]] # with gzip.open(os.path.join(output_path, save_file_name), 'wb') as file: # pickle.dump(DatasetConstructor(tensor_dataset_part, tensor_labels_part, adjacent_matrix_part), file) progress(total_count, total_count) logger.info("Finish Dataset Generation Processes".ljust(60)) ########################################################## # Data Saving # ########################################################## logger.info("Start Saving Dataset") total_count = tensor_dataset.size()[0] index, save_batch = 0, args.save_batch start, end = 0, args.save_batch while end < tensor_dataset.size()[0]: output_file_name = "part{index}_{filename}".format(index=index, filename=output_file_name) tensor_dataset_part = tensor_dataset[start : end] tensor_labels_part = tensor_labels[start : end] adjacent_matrix_part = adjacent_matrix[start : end] with gzip.open(os.path.join(output_path, output_file_name), 'wb') as file: pickle.dump(DatasetConstructor(tensor_dataset_part, tensor_labels_part, adjacent_matrix_part), file) progress(end, total_count) index += 1 start += save_batch end += save_batch logger.info("Finish Saving Dataset")
def gaussian_kernel(X, Y, sigma, device=None): D = utils.pairwise_distance(X, Y, device) ### YOUR CODE HERE ### return np.exp(-D / (2 * sigma**2))