def tf_map(self, batch_pc, batch_label, batch_pc_idx, batch_cloud_idx): features = batch_pc input_points = [] input_neighbors = [] input_pools = [] input_up_samples = [] for i in range(cfg.num_layers): #4 neighbour_idx = DP.knn_search(batch_pc, batch_pc, cfg.k_n) #16 return index[B,N,K] #取前1/4N 个点 sub_points = batch_pc[:, :batch_pc.shape[1] // cfg.sub_sampling_ratio[i], :] pool_i = neighbour_idx[:, :batch_pc.shape[1] // cfg.sub_sampling_ratio[i], :] #[B,N,1] 原始点集对应sub点集的idx up_i = DP.knn_search(sub_points, batch_pc, 1) input_points.append(batch_pc) #[N, N/4, N/16,N/64] input_neighbors.append(neighbour_idx) #[N, N/4, N/16,N/64],k input_pools.append(pool_i) #[N/4,N/16,N/64,N/256],k input_up_samples.append(up_i) #[N, N/4, N/16,N/64],1 batch_pc = sub_points input_list = input_points + input_neighbors + input_pools + input_up_samples input_list += [features, batch_label, batch_pc_idx, batch_cloud_idx] return input_list
def np_map(self, batch_xyz, batch_features, batch_labels, batch_pc_idx, batch_cloud_idx): batch_features = np.concatenate([batch_xyz, batch_features], axis=-1) input_points = [] input_neighbors = [] input_pools = [] input_up_samples = [] for i in range(cfg.num_layers): neighbour_idx = DP.knn_search(batch_xyz, batch_xyz, cfg.k_n) sub_points = batch_xyz[:, :batch_xyz.shape[1] // cfg.sub_sampling_ratio[i], :] pool_i = neighbour_idx[:, :batch_xyz.shape[1] // cfg.sub_sampling_ratio[i], :] up_i = DP.knn_search(sub_points, batch_xyz, 1) input_points.append(batch_xyz) input_neighbors.append(neighbour_idx) input_pools.append(pool_i) input_up_samples.append(up_i) batch_xyz = sub_points input_list = input_points + input_neighbors + input_pools + input_up_samples input_list += [ batch_features, batch_labels, batch_pc_idx, batch_cloud_idx ] return input_list
def np_map(self, batch_xyz, batch_features, batch_labels, batch_pc_idx, batch_cloud_idx): # erase tf batch_features = self.np_augment_input([batch_xyz, batch_features]) input_points = [] input_neighbors = [] input_pools = [] input_up_samples = [] # erase tf for i in range(cfg.num_layers): neigh_idx = DP.knn_search(batch_xyz, batch_xyz, cfg.k_n) sub_points = batch_xyz[:, :batch_xyz.shape[1] // cfg.sub_sampling_ratio[i], :] pool_i = neigh_idx[:, :batch_pc.shape[1] // cfg.sub_sampling_ratio[i], :] up_i = DP.knn_search(sub_points, batch_xyz, 1) input_points.append(batch_xyz) input_neighbors.append(neigh_idx) input_pools.append(pool_i) input_up_samples.append(up_i) batch_xyz = sub_points input_list = input_points + input_neighbors + input_pools + input_up_samples input_list += [ batch_features, batch_labels, batch_pc_idx, batch_cloud_idx ] return input_list
def main(): cfg = ConfigTest device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") net = Network(cfg).to(device) print("model parameters:", sum(param.numel() for param in net.parameters())) for i in tqdm(range(10)): npts = cfg.num_points pcld = np.random.rand(1, npts, 3) feat = np.random.rand(1, 6, npts) n_layers = 4 sub_s_r = [16, 1, 4, 1] inputs = {} for i in range(n_layers): nei_idx = DP.knn_search(pcld, pcld, 16) sub_pts = pcld[:, :pcld.shape[1] // sub_s_r[i], :] pool_i = nei_idx[:, :pcld.shape[1] // sub_s_r[i], :] up_i = torch.LongTensor(DP.knn_search(sub_pts, pcld, 1)) inputs['xyz'] = inputs.get('xyz', []) + [torch.from_numpy(pcld).float().to(device)] inputs['neigh_idx'] = inputs.get('neigh_idx', []) + [torch.LongTensor(nei_idx).to(device)] inputs['sub_idx'] = inputs.get('sub_idx', []) + [torch.LongTensor(pool_i).to(device)] inputs['interp_idx'] = inputs.get('interp_idx', []) + [torch.LongTensor(up_i).to(device)] pcld = sub_pts inputs['features'] = torch.from_numpy(feat).float().to(device) end_points = net(inputs) for k, v in end_points.items(): if type(v) == list: for ii, item in enumerate(v): print(k+'%d'%ii, item.size()) else: print(k, v.size())
def tf_map(self, batch_pc, batch_label, batch_pc_idx, batch_cloud_idx): features = batch_pc input_points = [] input_neighbors = [] input_pools = [] input_up_samples = [] for i in range(cfg.num_layers): neighbour_idx = DP.knn_search(batch_pc, batch_pc, cfg.k_n) sub_points = batch_pc[:, :batch_pc.shape[1] // cfg.sub_sampling_ratio[i], :] pool_i = neighbour_idx[:, :batch_pc.shape[1] // cfg.sub_sampling_ratio[i], :] up_i = DP.knn_search(sub_points, batch_pc, 1) input_points.append(batch_pc) input_neighbors.append(neighbour_idx) input_pools.append(pool_i) input_up_samples.append(up_i) batch_pc = sub_points input_list = input_points + input_neighbors + input_pools + input_up_samples input_list += [features, batch_label, batch_pc_idx, batch_cloud_idx] return input_list
def gen_one_sample(point_cloud): point_cloud_input = tf.random.shuffle(point_cloud, seed=None, name=None) batch_xyz = tf.expand_dims(point_cloud_input, axis=0) # [Npts,3] => [1,Npts,3] point_cloud = point_cloud.reshape(1, *point_cloud.shape) batch_feature_input = point_cloud # batch_xyz = tf.expand_dims(batch_xyz, axis=2) print("batch_xyz.shape", batch_xyz.shape) input_points = [] input_neighbors = [] input_pools = [] input_up_samples = [] # batch_feature = tf.concat([batch_xyz, batch_xyz], axis=-1) batch_feature = batch_xyz # with open("./BATCH_FEATURE.txt", 'w') as f: # for p in range(batch_feature.shape[1]): # f.write(str(batch_feature_input[0,p,0]) + " ") # f.write(str(batch_feature_input[0,p,1]) + " ") # f.write(str(batch_feature_input[0,p,2]) + " ") # if(p < batch_feature.shape[1] - 1): # f.write('\n') tf_holder_batch_feature = tf.compat.v1.placeholder(tf.float32, shape=batch_xyz.shape, name='Batch_Feature') # tf_holder_batch_feature = tf.compat.v1.placeholder(tf.float32, shape=(batch_xyz.shape[0], batch_xyz.shape[1] , batch_xyz.shape[2] ), name='Batch_Feature') # tf_batch_feature = tf.compat.v1.placeholder(tf.float32, shape=(batch_xyz.shape[0], batch_xyz.shape[1] , batch_xyz.shape[2] ), name='Batch_Feature') # tf_batch_feature_map = Semantic3D.tf_augment_input([tf.squeeze(tf_batch_feature,axis=0)]) # tf_holder_batch_feature = tf.expand_dims(tf_batch_feature_map, axis=0) # print("tf_batch_feature.size: ", tf_batch_feature.shape) # print("tf_holder_batch_size: ", tf_holder_batch_feature.shape) tf_holder_input_points = [] tf_holder_input_neighbors = [] tf_holder_input_pools = [] tf_holder_input_up_samples = [] for i in range(cfg.num_layers): # neigh_idx = self.knn_search(batch_xyz, batch_xyz, cfg.k_n) # neigh_idx = tf.py_func(DP.knn_search, [batch_xyz, batch_xyz, cfg.k_n], tf.int32) # neigh_idx = tf.compat.v1.placeholder(tf.int64, shape=(batch_xyz.shape[0], batch_xyz.shape[1], cfg.k_n), name='Neighbor_Index_' + str(i)) # # sub_points = tf.compat.v1.placeholder(tf.float32, shape=(batch_xyz.shape[0], batch_xyz.shape[1] // cfg.sub_sampling_ratio[i], batch_xyz.shape[2]), name='Subpoints_' + str(i)) # pool_i = tf.compat.v1.placeholder(tf.int64, shape=(batch_xyz.shape[0], batch_xyz.shape[1] // cfg.sub_sampling_ratio[i], cfg.k_n), name='Pool_I_' + str(i)) # up_i = tf.compat.v1.placeholder(tf.int64, shape=(batch_xyz.shape[0], batch_xyz.shape[1], 1), name='Up_I_' + str(i)) neigh_idx = tf.compat.v1.placeholder(tf.int64, shape=(point_cloud.shape[0], point_cloud.shape[1], cfg.k_n), name='Neighbor_Index_' + str(i)) # sub_points = tf.compat.v1.placeholder(tf.float32, shape=(batch_xyz.shape[0], batch_xyz.shape[1] // cfg.sub_sampling_ratio[i], batch_xyz.shape[2]), name='Subpoints_' + str(i)) pool_i = tf.compat.v1.placeholder( tf.int64, shape=(point_cloud.shape[0], point_cloud.shape[1] // cfg.sub_sampling_ratio[i], cfg.k_n), name='Pool_I_' + str(i)) up_i = tf.compat.v1.placeholder(tf.int64, shape=(point_cloud.shape[0], point_cloud.shape[1], 1), name='Up_I_' + str(i)) # neigh_idx = DP.tf_knn_search(batch_xyz, batch_xyz, cfg.k_n) neigh_idx_np = DP.knn_search(point_cloud, point_cloud, cfg.k_n) # sub_points = batch_xyz[:, :tf.shape(batch_xyz)[1] // cfg.sub_sampling_ratio[i], :] sub_points_np = point_cloud[:, :point_cloud.shape[1] // cfg.sub_sampling_ratio[i], :] # pool_i = neigh_idx[:, :tf.shape(batch_xyz)[1] // cfg.sub_sampling_ratio[i], :] pool_i_np = neigh_idx_np[:, :point_cloud.shape[1] // cfg.sub_sampling_ratio[i], :] # # up_i = tf.py_func(DP.knn_search, [sub_points, batch_xyz, 1], tf.int32) # up_i = DP.tf_knn_search(sub_points, batch_xyz, 1) up_i_np = DP.knn_search(sub_points_np, point_cloud, 1) # up_i = self.knn_search(sub_points, batch_xyz, 1) # print("neigh_idx: ", neigh_idx.shape, "sub_points: ", sub_points.shape , "pool_i: ", pool_i.shape, "up_i: ", up_i.shape) # tf_holder_batch_xyz = tf.compat.v1.placeholder(tf.float32, shape=(batch_xyz.shape[0], batch_xyz.shape[1] , batch_xyz.shape[2]), name='Batch_XYZ_' +str(i)) tf_holder_batch_xyz = tf.compat.v1.placeholder( tf.float32, shape=(point_cloud.shape[0], point_cloud.shape[1], point_cloud.shape[2]), name='Batch_XYZ_' + str(i)) tf_holder_input_points.append(tf_holder_batch_xyz) # input_points.append(batch_xyz) # input_neighbors.append(neigh_idx) # input_pools.append(pool_i) # input_up_samples.append(up_i) input_points.append(point_cloud) input_neighbors.append(neigh_idx_np) input_pools.append(pool_i_np) input_up_samples.append(up_i_np) point_cloud = sub_points_np # batch_xyz = sub_points # print("sub_points.shape", sub_points.shape) # print("neigh_idx.shape", neigh_idx.shape) # print("pool_i.shape", pool_i.shape) # print("up_i.shape", up_i.shape) # print("sub_points.shape", sub_points.shape) # print("neigh_idx.shape", neigh_idx.shape) # print("pool_i.shape", pool_i.shape) # print("up_i.shape", up_i.shape) print("neigh_idx_np.shape", neigh_idx_np.shape) print("sub_points_np.shape", sub_points_np.shape) print("pool_i_np.shape", pool_i_np.shape) print("up_i_np.shape", up_i_np.shape) tf_holder_input_neighbors.append(neigh_idx) tf_holder_input_pools.append(pool_i) tf_holder_input_up_samples.append(up_i) input_list_data = input_points + input_neighbors + input_pools + input_up_samples input_list_data += [batch_feature_input] input_list = tf_holder_input_points + tf_holder_input_neighbors + tf_holder_input_pools + tf_holder_input_up_samples input_list += [tf_holder_batch_feature] for i, tf_holder in enumerate(input_list): print("tf_holder ", i, " ", tf_holder.shape) return input_list, input_list_data pass