def check_neighbor(self, idx, cluster_idx, layer): #if it is visited already radius = self.search_radius self.data_status_[idx] = False result_set = RadiusNNResultSet(radius) #print(np.shape(self.data_[idx]),(self.data_[idx])) kdtree.kdtree_radius_search(self.root_, self.data_, result_set, self.data_[idx]) nis = result_set.index_list ndists = result_set.dist_list #print("idx ",idx," nis",nis," ndists",ndists) true_nis = np.count_nonzero(self.data_status_[nis]) if (true_nis > self.min_samples): layer += 1 #print("set idx ",idx,"as label",cluster_idx) self.data_label_[idx] = cluster_idx for sub_idx in nis: #print("nis" ,nis) if self.data_status_[sub_idx] == True: #print("set idx inner ",sub_idx,"as label",cluster_idx) self.data_label_[sub_idx] = cluster_idx self.check_neighbor(sub_idx, cluster_idx, layer) #print("iter layer ",layer) return layer else: #print("layer ",layer) return layer
def KDTreeBenchmark(root_dir, files, k, leaf_size, radius, feature=None, feature2=None): construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 brute_time_sum = 0 iteration_num = 0 for file in files: if file.find('bin') == -1: continue iteration_num += 1 filename = os.path.join(root_dir, file) db_np = read_velodyne_bin(filename) begin_t = time.time() root = kdtree.kdtree_construction(db_np, leaf_size, feature, feature2) construction_time_sum += time.time() - begin_t query = db_np[0, :] begin_t = time.time() result_set = KNNResultSet(capacity=k) kdtree.kdtree_knn_search(root, db_np, result_set, query) print("result set from KD Tree\n", result_set) knn_time_sum += time.time() - begin_t # print("--------") begin_t = time.time() result_set = RadiusNNResultSet(radius=radius) kdtree.kdtree_radius_search(root, db_np, result_set, query) #print(result_set) radius_time_sum += time.time() - begin_t #print("--------") begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] #print(nn_idx[0:k]) #print(nn_dist[0:k]) brute_time_sum += time.time() - begin_t depth = [0] max_depth = [0] kdtree.traverse_kdtree(root, depth, max_depth) print("tree depth: %d, max depth: %d" % (depth[0], max_depth[0])) print("Kdtree: build %.3f, knn %.3f, radius %.3f, brute %.3f" % (construction_time_sum * 1000 / iteration_num, knn_time_sum * 1000 / iteration_num, radius_time_sum * 1000 / iteration_num, brute_time_sum * 1000 / iteration_num))
def fit(self,data,method="radius"): data_num = np.shape(data)[0] print("number of data ",data_num) self.simi_graph=np.zeros((data_num,data_num)) if method=="fully_connect": for i in range(np.shape(data)[0]): self.simi_graph[i] = get_dist_array(data,data[i]) #self.simi_graph=np.max(self.simi_graph)*2- self.simi_graph self.simi_graph= gauss(self.simi_graph) #for i in range(np.shape(data)[0]): # self.simi_graph[i,i]=0 #print(self.simi_graph) elif (method=="radius"): root = kdtree.kdtree_construction(data, 16) for di, datum in enumerate(data): #print(datum) result_set = RadiusNNResultSet(radius=10) kdtree.kdtree_radius_search(root, data, result_set, datum) nis = result_set.index_list ndists = result_set.dist_list #print("nis",nis) #print("ndists",ndists) for ni,ndist in zip(nis,ndists): self.simi_graph[di][ni]=self.simi_graph[ni][di]=gauss(ndist,0.2) #print("graph",self.simi_graph) elif (method=="knn"): print("knn is implemented") tree=KDTree(data) for di, datum in enumerate(data): ndists,nis = tree.query([datum],20) nis=nis[0] ndists = ndists[0] for ni,ndist in zip(nis,ndists): if ni==di: continue #print("HHHHHH",ni,di,ndist) self.simi_graph[di][ni]=self.simi_graph[ni][di]=gauss(ndist) else: print("not available")
def main(): # configuration leaf_size = 32 min_extent = 0.0001 k = 8 radius = 1 root_dir = './velodyne' # 数据集路径 cat = os.listdir(root_dir) iteration_num = len(cat) print("scipy-KDTree --------------") construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 brute_time_sum = 0 for i in range(iteration_num): filename = os.path.join(root_dir, cat[i]) db_np = read_velodyne_bin(filename) # print(db_np.shape) begin_t = time.time() tree = KDTree(db_np, leafsize=leaf_size) construction_time_sum += time.time() - begin_t query = db_np[222, :] begin_t = time.time() distance, indices = tree.query(x=query, k=k) print(distance) print(indices) knn_time_sum += time.time() - begin_t begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] brute_time_sum += time.time() - begin_t # print(nn_idx) print("Octree: build %.3f, knn %.3f, brute %.3f" % (construction_time_sum*1000/iteration_num, knn_time_sum*1000/iteration_num, brute_time_sum*1000/iteration_num)) print("octree --------------") construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 brute_time_sum = 0 for i in range(iteration_num): filename = os.path.join(root_dir, cat[i]) db_np = read_velodyne_bin(filename) begin_t = time.time() root = octree.octree_construction(db_np, leaf_size, min_extent) construction_time_sum += time.time() - begin_t query = db_np[222, :] begin_t = time.time() result_set = KNNResultSet(capacity=k) octree.octree_knn_search(root, db_np, result_set, query) print("result ------------") print(result_set) knn_time_sum += time.time() - begin_t begin_t = time.time() result_set = RadiusNNResultSet(radius=radius) octree.octree_radius_search_fast(root, db_np, result_set, query) # print("result ------------") # print(result_set) radius_time_sum += time.time() - begin_t begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] brute_time_sum += time.time() - begin_t # print(nn_idx) print("Octree: build %.3f, knn %.3f, radius %.3f, brute %.3f" % (construction_time_sum*1000/iteration_num, knn_time_sum*1000/iteration_num, radius_time_sum*1000/iteration_num, brute_time_sum*1000/iteration_num)) otc_knn = knn_time_sum*1000 otc_radius = radius_time_sum*1000 otc_brute = brute_time_sum*1000 print("kdtree --------------") construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 brute_time_sum = 0 for i in range(iteration_num): filename = os.path.join(root_dir, cat[i]) db_np = read_velodyne_bin(filename) begin_t = time.time() root = kdtree.kdtree_construction(db_np, leaf_size) construction_time_sum += time.time() - begin_t query = db_np[222, :] begin_t = time.time() result_set = KNNResultSet(capacity=k) kdtree.kdtree_knn_search(root, db_np, result_set, query) knn_time_sum += time.time() - begin_t print("result ------------") print(result_set) begin_t = time.time() result_set = RadiusNNResultSet(radius=radius) kdtree.kdtree_radius_search(root, db_np, result_set, query) radius_time_sum += time.time() - begin_t # print("result ------------") # print(result_set) begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] brute_time_sum += time.time() - begin_t # print(nn_idx) kd_knn = knn_time_sum * 1000 kd_radius = radius_time_sum * 1000 kd_brute = brute_time_sum * 1000 print("Kdtree: build %.3f, knn %.3f, radius %.3f, brute %.3f" % (construction_time_sum * 1000 / iteration_num, knn_time_sum * 1000 / iteration_num, radius_time_sum * 1000 / iteration_num, brute_time_sum * 1000 / iteration_num)) print("Octree knn/brute = %.3f, radius/bruet = %.3f" % (otc_knn/otc_brute, otc_radius/otc_brute)) print("Kdtree knn/brute = %.3f, radius/bruet = %.3f" % (kd_knn / kd_brute, kd_radius / kd_brute))
def get_neightbor_ids(self, root, query): result_set = RadiusNNResultSet(self.radius) kdtree.kdtree_radius_search(root, self.data_, result_set, query) nis = result_set.index_list return nis
def main(): # configuration db_size = 64000 dim = 3 leaf_size = 32 min_extent = 0.0001 k = 8 radius = 1 iteration_num = 100 print("octree --------------") construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 brute_time_sum = 0 for i in range(iteration_num): db_np = (np.random.rand(db_size, dim) - 0.5) * 100 begin_t = time.time() root = octree.octree_construction(db_np, leaf_size, min_extent) construction_time_sum += time.time() - begin_t query = np.random.rand(3) begin_t = time.time() result_set = KNNResultSet(capacity=k) octree.octree_knn_search(root, db_np, result_set, query) knn_time_sum += time.time() - begin_t begin_t = time.time() result_set = RadiusNNResultSet(radius=radius) octree.octree_radius_search_fast(root, db_np, result_set, query) radius_time_sum += time.time() - begin_t begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] brute_time_sum += time.time() - begin_t print("Octree: build %.3f, knn %.3f, radius %.3f, brute %.3f" % (construction_time_sum * 1000 / iteration_num, knn_time_sum * 1000 / iteration_num, radius_time_sum * 1000 / iteration_num, brute_time_sum * 1000 / iteration_num)) print("kdtree --------------") construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 brute_time_sum = 0 for i in range(iteration_num): db_np = np.random.rand(db_size, dim) begin_t = time.time() root = kdtree.kdtree_construction(db_np, leaf_size) construction_time_sum += time.time() - begin_t query = np.random.rand(3) begin_t = time.time() result_set = KNNResultSet(capacity=k) kdtree.kdtree_knn_search(root, db_np, result_set, query) knn_time_sum += time.time() - begin_t begin_t = time.time() result_set = RadiusNNResultSet(radius=radius) kdtree.kdtree_radius_search(root, db_np, result_set, query) radius_time_sum += time.time() - begin_t begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] brute_time_sum += time.time() - begin_t print("Kdtree: build %.3f, knn %.3f, radius %.3f, brute %.3f" % (construction_time_sum * 1000 / iteration_num, knn_time_sum * 1000 / iteration_num, radius_time_sum * 1000 / iteration_num, brute_time_sum * 1000 / iteration_num))
def main(): # configuration leaf_size = 32 min_extent = 0.0001 k = 8 radius = 1 # data root_dir = '../data/bin' cat = os.listdir(root_dir) iteration_num = len(cat) print("Brute-force --------------") brute_time_sum = 0 for i in range(iteration_num): filename = os.path.join(root_dir, cat[i]) db_np = read_velodyne_bin(filename) query = db_np[0, :] begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] brute_time_sum += time.time() - begin_t print("Brute-force: %.3f" % (brute_time_sum * 1000 / iteration_num)) # scipy.spatial.KDTree print("scipy.spatial.KDTree --------------") construction_time_sum = 0 knn_time_sum = 0 for i in range(iteration_num): filename = os.path.join(root_dir, cat[i]) db_np = read_velodyne_bin(filename) begin_t = time.time() tree = spatial.KDTree(db_np, leaf_size) construction_time_sum += time.time() - begin_t query = db_np[0, :] begin_t = time.time() res = tree.query(query, k) knn_time_sum += time.time() - begin_t print("scipy.spatial.KDTree: build %.3f, knn %.3f" % (construction_time_sum * 1000 / iteration_num, knn_time_sum * 1000 / iteration_num)) print("Octree --------------") construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 brute_time_sum = 0 for i in range(iteration_num): filename = os.path.join(root_dir, cat[i]) db_np = read_velodyne_bin(filename) begin_t = time.time() root = octree.octree_construction(db_np, leaf_size, min_extent) construction_time_sum += time.time() - begin_t query = db_np[0, :] begin_t = time.time() result_set = KNNResultSet(capacity=k) octree.octree_knn_search(root, db_np, result_set, query) knn_time_sum += time.time() - begin_t begin_t = time.time() result_set = RadiusNNResultSet(radius=radius) octree.octree_radius_search_fast(root, db_np, result_set, query) radius_time_sum += time.time() - begin_t begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] brute_time_sum += time.time() - begin_t print("Octree: build %.3f, knn %.3f, radius %.3f, brute %.3f" % (construction_time_sum * 1000 / iteration_num, knn_time_sum * 1000 / iteration_num, radius_time_sum * 1000 / iteration_num, brute_time_sum * 1000 / iteration_num)) print("KD-Tree --------------") construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 brute_time_sum = 0 for i in range(iteration_num): filename = os.path.join(root_dir, cat[i]) db_np = read_velodyne_bin(filename) begin_t = time.time() root = kdtree.kdtree_construction(db_np, leaf_size) construction_time_sum += time.time() - begin_t query = db_np[0, :] begin_t = time.time() result_set = KNNResultSet(capacity=k) kdtree.kdtree_knn_search(root, db_np, result_set, query) knn_time_sum += time.time() - begin_t begin_t = time.time() result_set = RadiusNNResultSet(radius=radius) kdtree.kdtree_radius_search(root, db_np, result_set, query) radius_time_sum += time.time() - begin_t begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] brute_time_sum += time.time() - begin_t print("KD-Tree: build %.3f, knn %.3f, radius %.3f, brute %.3f" % (construction_time_sum * 1000 / iteration_num, knn_time_sum * 1000 / iteration_num, radius_time_sum * 1000 / iteration_num, brute_time_sum * 1000 / iteration_num))
def main(): # configuration leaf_size = 32 min_extent = 0.001 k = 8 radius = 1 root_dir = '/home/xcy/myWork/pointCloudProcessing/lesson2/bin' # 数据集路径 cat = os.listdir(root_dir) iteration_num = len(cat) print("octree --------------") construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 radius_fast_time_sum = 0 brute_time_sum = 0 for i in range(iteration_num): filename = os.path.join(root_dir, cat[i]) db_np = read_velodyne_bin(filename) begin_t = time.time() root = octree.octree_construction(db_np, leaf_size, min_extent) construction_time_sum += time.time() - begin_t query = db_np[0, :] begin_t = time.time() result_set = KNNResultSet(capacity=k) octree.octree_knn_search(root, db_np, result_set, query) knn_time_sum += time.time() - begin_t # begin_t = time.time() result_set = RadiusNNResultSet(radius=radius) octree.octree_radius_search(root, db_np, result_set, query) radius_time_sum += time.time() - begin_t begin_t = time.time() result_set = RadiusNNResultSet(radius=radius) octree.octree_radius_search_fast(root, db_np, result_set, query) radius_fast_time_sum += time.time() - begin_t begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] brute_time_sum += time.time() - begin_t print( "Octree: build %.3f, knn %.3f, radius %.3f, radius_fast %.3f, brute %.3f" % (construction_time_sum * 1000 / iteration_num, knn_time_sum * 1000 / iteration_num, radius_time_sum * 1000 / iteration_num, radius_fast_time_sum * 1000 / iteration_num, brute_time_sum * 1000 / iteration_num)) print("kdtree --------------") construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 brute_time_sum = 0 for i in range(iteration_num): filename = os.path.join(root_dir, cat[i]) db_np = read_velodyne_bin(filename) begin_t = time.time() root = kdtree.kdtree_construction(db_np, leaf_size) construction_time_sum += time.time() - begin_t # begin_t = time.time() # root = kdtree.kdtree_construction_median(db_np, leaf_size) # construction_time_sum += time.time() - begin_t query = db_np[0, :] begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] brute_time_sum += time.time() - begin_t begin_t = time.time() result_set = KNNResultSet(capacity=k) kdtree.kdtree_knn_search(root, db_np, result_set, query) knn_time_sum += time.time() - begin_t begin_t = time.time() result_set = RadiusNNResultSet(radius=radius) kdtree.kdtree_radius_search(root, db_np, result_set, query) radius_time_sum += time.time() - begin_t print("Kdtree: build %.3f, knn %.3f, radius %.3f, brute %.3f" % (construction_time_sum * 1000 / iteration_num, knn_time_sum * 1000 / iteration_num / (brute_time_sum * 1000 / iteration_num), radius_time_sum * 1000 / iteration_num / (brute_time_sum * 1000 / iteration_num), brute_time_sum * 1000 / iteration_num))
def main(): # configuration leaf_size = 32 min_extent = 0.0001 k = 8 radius = 1 filename = '000000.bin' db_np = read_velodyne_bin(filename) iteration_num = 1 cat = [] print("octree --------------") construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 brute_time_sum = 0 for i in range(iteration_num): db_np = read_velodyne_bin(filename) begin_t = time.time() root = octree.octree_construction(db_np, leaf_size, min_extent) construction_time_sum += time.time() - begin_t query = db_np[0, :] begin_t = time.time() result_set = KNNResultSet(capacity=k) octree.octree_knn_search(root, db_np, result_set, query) knn_time_sum += time.time() - begin_t begin_t = time.time() result_set = RadiusNNResultSet(radius=radius) octree.octree_radius_search_fast(root, db_np, result_set, query) radius_time_sum += time.time() - begin_t begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] brute_time_sum += time.time() - begin_t print("Octree: build %.3f, knn %.3f, radius %.3f, brute %.3f" % (construction_time_sum * 1000 / iteration_num, knn_time_sum * 1000 / iteration_num, radius_time_sum * 1000 / iteration_num, brute_time_sum * 1000 / iteration_num)) print("kdtree --------------") construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 brute_time_sum = 0 for i in range(iteration_num): #filename = os.path.join(root_dir, cat[i]) db_np = read_velodyne_bin(filename) begin_t = time.time() root = kdtree.kdtree_construction(db_np, leaf_size) construction_time_sum += time.time() - begin_t query = db_np[0, :] begin_t = time.time() result_set = KNNResultSet(capacity=k) kdtree.kdtree_knn_search(root, db_np, result_set, query) knn_time_sum += time.time() - begin_t begin_t = time.time() result_set = RadiusNNResultSet(radius=radius) kdtree.kdtree_radius_search(root, db_np, result_set, query) radius_time_sum += time.time() - begin_t begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] brute_time_sum += time.time() - begin_t print("Kdtree: build %.3f, knn %.3f, radius %.3f, brute %.3f" % (construction_time_sum * 1000 / iteration_num, knn_time_sum * 1000 / iteration_num, radius_time_sum * 1000 / iteration_num, brute_time_sum * 1000 / iteration_num))
def main(): # configuration leaf_size = 32 min_extent = 0.005 k = 8 radius = 1 nums = 1 # 点的数量(搜索) abs_path = os.path.abspath( os.path.dirname(__file__)) # 数据集路径 (数据集直接放在当前路径下) filename = os.path.join(abs_path, '000000.bin') # 只读取一个文件,如果要读取所有文件,需要循环读入 iteration_num = 1 # 读取数据并进行采样 db_np_origin = read_velodyne_bin(filename) # N*3 db_np_idx = np.random.choice(db_np_origin.shape[0], size=(30000, )) # 随机采样30000个点 db_np = db_np_origin[db_np_idx] #root_dir = '/Users/renqian/cloud_lesson/kitti' # 数据集路径 #cat = os.listdir(root_dir) #iteration_num = len(cat) print("搜索数据集中的 %d 个点: " % nums) print( "---------------------------------Octree ----------------------------------" ) construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 brute_time_sum = 0 for i in range(iteration_num): #filename = os.path.join(root_dir, cat[i]) ''' db_np_origin = read_velodyne_bin(filename) # N*3 db_np_idx = np.random.choice(db_np_origin.shape[0],size =(30000,)) # 随机采样30000个点 db_np = db_np_origin[db_np_idx] ''' begin_t = time.time() root = octree.octree_construction(db_np, leaf_size, min_extent) # Octree Construction construction_time_sum += time.time() - begin_t print("The sum of the points: ", db_np.shape[0]) nums = db_np.shape[0] # 查找nums个点 for num in range(nums): query = db_np[num, :] # 搜索点数组(假设第一个点为搜索点) # knn search begin_t = time.time() result_set = KNNResultSet(capacity=k) octree.octree_knn_search(root, db_np, result_set, query) knn_time_sum += time.time() - begin_t # radius search begin_t = time.time() result_set = RadiusNNResultSet(radius=radius) #octree.octree_radius_search_fast(root, db_np, result_set, query) octree.octree_radius_search(root, db_np, result_set, query, search='fast') radius_time_sum += time.time() - begin_t # brute search begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] brute_time_sum += time.time() - begin_t print("Octree: build %.3f, knn %.3f, radius %.3f, brute %.3f" % (construction_time_sum * 1000 / iteration_num, knn_time_sum * 1000 / iteration_num, radius_time_sum * 1000 / iteration_num, brute_time_sum * 1000 / iteration_num)) print( "---------------------------------Kdtree ----------------------------------" ) construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 brute_time_sum = 0 knn_scipy_time_sum = 0 radius_scipy_time_sum = 0 for i in range(iteration_num): #filename = os.path.join(root_dir, cat[i]) #db_np = read_velodyne_bin(filename) # N*3 print("The sum of the points: ", db_np.shape[0]) begin_t = time.time() root = kdtree.kdtree_construction(db_np, leaf_size) # kd tree construction construction_time_sum += time.time() - begin_t tree = spatial.KDTree(db_np) # 利用spatial库函数构造kdtree for num in range(nums): query = db_np[num, :] # scipy.spatial.KDtree begin_t = time.time() tree.query(query, k=k) knn_scipy_time_sum += time.time() - begin_t #print(tree.query(query, k=k)) # knn search begin_t = time.time() result_set = KNNResultSet(capacity=k) kdtree.kdtree_knn_search(root, db_np, result_set, query) knn_time_sum += time.time() - begin_t #print(result_set) # scipy.spatial.KDtree (radius search) begin_t = time.time() tree.query_ball_point(query, radius) radius_scipy_time_sum += time.time() - begin_t # radius search begin_t = time.time() result_set = RadiusNNResultSet(radius=radius) kdtree.kdtree_radius_search(root, db_np, result_set, query) radius_time_sum += time.time() - begin_t # print(result_set) # brute search begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] brute_time_sum += time.time() - begin_t print( "Kdtree: build %.3f, knn_scipy %.3f, knn %.3f, radius_scipy %.3f, radius %.3f, brute %.3f" % (construction_time_sum * 1000 / iteration_num, knn_scipy_time_sum * 1000 / iteration_num, knn_time_sum * 1000 / iteration_num, radius_scipy_time_sum * 1000 / iteration_num, radius_time_sum * 1000 / iteration_num, brute_time_sum * 1000 / iteration_num))
def main(): # configuration leaf_size = 32 min_extent = 0.0001 k = 8 radius = 1 root_dir = '/home/gc/github_repository/DeepBlue/lesson_2/kitty/' # 数据集路径 cat = os.listdir(root_dir) iteration_num = len(cat) print("octree --------------") construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 brute_time_sum = 0 for i in range(iteration_num): filename = os.path.join(root_dir, cat[i]) db_np = read_velodyne_bin(filename) db_np = db_np.T begin_t = time.time() root = octree.octree_construction(db_np, leaf_size, min_extent) construction_time_sum += time.time() - begin_t query = db_np[0,:] begin_t = time.time() result_set = KNNResultSet(capacity=k) octree.octree_knn_search(root, db_np, result_set, query) knn_time_sum += time.time() - begin_t begin_t = time.time() result_set = RadiusNNResultSet(radius=radius) octree.octree_radius_search_fast(root, db_np, result_set, query) radius_time_sum += time.time() - begin_t begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] brute_time_sum += time.time() - begin_t print("Octree: build %.3f, knn %.3f, radius %.3f, brute %.3f" % (construction_time_sum*1000/iteration_num, knn_time_sum*1000/iteration_num, radius_time_sum*1000/iteration_num, brute_time_sum*1000/iteration_num)) otc_knn = knn_time_sum*1000 otc_radius = radius_time_sum*1000 otc_brute = brute_time_sum*1000 print("kdtree --------------") construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 brute_time_sum = 0 for i in range(iteration_num): filename = os.path.join(root_dir, cat[i]) db_np = read_velodyne_bin(filename) begin_t = time.time() root = kdtree.kdtree_construction(db_np, leaf_size) construction_time_sum += time.time() - begin_t query = db_np[0,:] begin_t = time.time() result_set = KNNResultSet(capacity=k) kdtree.kdtree_knn_search(root, db_np, result_set, query) knn_time_sum += time.time() - begin_t begin_t = time.time() result_set = RadiusNNResultSet(radius=radius) kdtree.kdtree_radius_search(root, db_np, result_set, query) radius_time_sum += time.time() - begin_t begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] brute_time_sum += time.time() - begin_t print("Kdtree: build %.3f, knn %.3f, radius %.3f, brute %.3f" % (construction_time_sum * 1000 / iteration_num, knn_time_sum * 1000 / iteration_num, radius_time_sum * 1000 / iteration_num, brute_time_sum * 1000 / iteration_num)) kd_knn = knn_time_sum * 1000 kd_radius = radius_time_sum * 1000 kd_brute = brute_time_sum * 1000 print("Octree knn/brute = %.3f, radius/bruet = %.3f" % (otc_knn/otc_brute, otc_radius/otc_brute) ) print("Kdtree knn/brute = %.3f, radius/bruet = %.3f" % (kd_knn / otc_brute, kd_radius / otc_brute) )
def main(): # configuration leaf_size = 32 min_extent = 0.0001 k = 8 radius = 1 #root_dir = '/Users/renqian/cloud_lesson/kitti' # 数据集路径 #cat = os.listdir(root_dir) #iteration_num = len(cat) # load date filename = '/home/ljn/SLAM/dateset/000000.bin' db_np = read_velodyne_bin(filename) iteration = 1 print("octree -----------------------------------") construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 brute_time_sum = 0 result_set_knn = KNNResultSet(capacity=k) query = db_np[95, :] begin_t = time.time() root = octree.octree_construction(db_np, leaf_size, min_extent) construction_time_sum += time.time() - begin_t begin_t = time.time() octree.octree_knn_search(root, db_np, result_set_knn, query) knn_time_sum += time.time() - begin_t print('knn search result\n', result_set_knn) begin_t = time.time() result_set_rnn = RadiusNNResultSet(radius=radius) octree.octree_radius_search_fast(root, db_np, result_set_rnn, query) radius_time_sum += time.time() - begin_t print('rnn search result\n', result_set_rnn) begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] nn_dist_idx_pre = np.linspace(0, nn_dist.shape[0], nn_dist.shape[0]) nn_dist_idx = nn_dist_idx_pre[nn_idx] brute_time_sum += time.time() - begin_t #brute knn search result_set_knn_brute = KNNResultSet(capacity=k) for index in range(k): result_set_knn_brute.add_point(nn_dist[index], nn_dist_idx[index]) # brute radiusNN search result_set_rnn_brute = RadiusNNResultSet(radius=radius) for index in range(nn_dist.shape[0]): if nn_dist[index] < radius: result_set_rnn_brute.add_point(nn_dist[index], nn_dist_idx[index]) continue else: break print("Octree: build %.3f, knn %.3f, radius %.3f, brute %.3f" % (construction_time_sum * 1000, knn_time_sum * 1000, radius_time_sum * 1000, brute_time_sum * 1000)) print("kdtree -----------------------------------") construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 brute_time_sum = 0 for i in range(iteration): query = db_np[95, :] begin_t = time.time() root = kdtree.kdtree_construction(db_np, leaf_size) construction_time_sum += time.time() - begin_t begin_t = time.time() result_set_knn = KNNResultSet(capacity=k) kdtree.kdtree_knn_search(root, db_np, result_set_knn, query) knn_time_sum += time.time() - begin_t begin_t = time.time() result_set_rnn = RadiusNNResultSet(radius=radius) kdtree.kdtree_radius_search(root, db_np, result_set_rnn, query) radius_time_sum += time.time() - begin_t begin_t = time.time() diff = np.linalg.norm(np.expand_dims(query, 0) - db_np, axis=1) nn_idx = np.argsort(diff) nn_dist = diff[nn_idx] brute_time_sum += time.time() - begin_t nn_dist_idx_pre = np.linspace(0, nn_dist.shape[0] - 1, nn_dist.shape[0]) nn_dist_idx = nn_dist_idx_pre[nn_idx] # brute knn search result_set_knn_brute = KNNResultSet(capacity=k) for index in range(k): result_set_knn_brute.add_point(nn_dist[index], nn_dist_idx[index]) # brute radiusNN search result_set_rnn_brute = RadiusNNResultSet(radius=radius) for index in range(nn_dist.shape[0]): if nn_dist[index] < radius: result_set_rnn_brute.add_point(nn_dist[index], nn_dist_idx[index]) continue else: break print("Kdtree: build %.3f, knn %.3f, radius %.3f, brute %.3f" % (construction_time_sum * 1000, knn_time_sum * 1000, radius_time_sum * 1000, brute_time_sum * 1000)) print("scipy kdtree -----------------------------------") construction_time_sum = 0 knn_time_sum = 0 radius_time_sum = 0 query = db_np[95, :] begin_t = time.time() tree = spatial.KDTree(db_np, leaf_size) construction_time_sum += time.time() - begin_t #no knn begin_t = time.time() knn_time_sum += time.time() - begin_t begin_t = time.time() result_set_rnn = tree.query_ball_point(query, radius) radius_time_sum += time.time() - begin_t print('rnn search result\n', result_set_rnn) print("Octree: build %.3f, knn %.3f, radius %.3f" % (construction_time_sum * 1000, knn_time_sum * 1000, radius_time_sum * 1000))