Example #1
0
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

    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))
Example #3
0
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))
Example #4
0
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))
Example #5
0
def main():
    # configuration
    leaf_size = 32
    min_extent = 0.0001
    k = 8
    radius = 1.0

    root_dir = './'  # 数据集路径
    files = os.listdir(root_dir)

    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)
        #point_indices=np.array([1,2,3,4,5,6,7])
        #db_larger=point_indices[db_np[point_indices,1]>0.4]
        #db_larger_idx=np.where
        #print("test data",db_np[point_indices,1])
        #print("test index",db_np[point_indices,1]>0.4)
        #print("test1",db_larger,db_np[db_larger,1])

        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("--------------")
    KDTreeBenchmark(root_dir, files, k, leaf_size, radius)
    print("--------------")
    KDTreeBenchmark(root_dir, files, k, leaf_size, radius, "mean")
    print("--------------")
    KDTreeBenchmark(root_dir, files, k, leaf_size, radius, "meanraw")

    print("--------------")
    KDTreeBenchmark(root_dir, files, k, leaf_size, radius, "median", "adapt")
    print("--------------")
    KDTreeBenchmark(root_dir, files, k, leaf_size, radius, "meanraw", "adapt")
    print("--------------")
    KDTreeBenchmark(root_dir, files, k, leaf_size, radius, "mean", "adapt")

    # 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)
    #     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))

    print("--------------")
    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()
        tree = KDTree(db_np)
        construction_time_sum += time.time() - begin_t

        query = db_np[0, :]

        begin_t = time.time()
        result_set = tree.query(([query[0], query[1], query[2]]), k=k, p=2)
        print("result set", result_set)
        knn_time_sum += time.time() - begin_t
        # print("--------")
        begin_t = time.time()
        result_set = tree.query_ball_point(([query[0], query[1], query[2]]),
                                           radius)
        # 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]
        brute_time_sum += time.time() - begin_t
        print("scipy idx", nn_idx[0:k])
        print("scipy dist", nn_dist[0:k])
    print("Scipy 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))
Example #6
0
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))
Example #8
0
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))
Example #10
0
def main():
    # configuration
    leaf_size = 32
    min_extent = 0.0001
    k = 8
    radius = 1
    N = 30000  # 对N个点做搜索

    # read data
    filename = '../000000.bin'
    db_np = read_velodyne_bin(filename)
    indices = np.arange(db_np.shape[0])

    # brute force
    begin_t = time.time()
    brute_result_indices = brute_force(db_np, indices, k, N)
    brute_time = time.time() - begin_t
    print("time consumption for brute force: ", brute_time)

    # scipy kd-tree
    begin_t = time.time()
    scipy_result_indices = scipy_kd_tree(db_np, k, leaf_size, N)
    scipy_time = time.time() - begin_t
    print("time consumption for scipy kd-tree: ", scipy_time)

    # kd tree
    begin_t = time.time()
    print("constructing a kd-tree")
    root = kd_tree.kd_tree_construction(db_np, leaf_size)
    kd_tree_construction_time = time.time() - begin_t
    print("construct a kd-tree takes ", kd_tree_construction_time)

    begin_t = time.time()
    print("using kd-tree-knn")
    kd_tree_knn_result = []
    for i in range(N):
        knn_result_set = KNNResultSet(k)
        kd_tree.kd_tree_knn_search(root, db_np, knn_result_set, db_np[i])
        kd_tree_knn_result.append(knn_result_set)
    kd_tree_knn_time = time.time() - begin_t
    print("time consumption for kd-tree-knn: ", kd_tree_knn_time)

    begin_t = time.time()
    print("using kd-tree-radius")
    kd_tree_radius_result = []
    for i in range(N):
        radius_result_set = RadiusNNResultSet(radius)
        kd_tree.kd_tree_radius_search(root, db_np, radius_result_set, db_np[i])
        kd_tree_radius_result.append(radius_result_set)
    kd_tree_radius_time = time.time() - begin_t
    print("time consumption for kd-tree-radius: ", kd_tree_radius_time)

    # octree
    begin_t = time.time()
    print("constructing a octree")
    root = octree.octree_construction(db_np, leaf_size, min_extent)
    octree_construction_time = time.time() - begin_t
    print("construct a octree takes ", octree_construction_time)

    begin_t = time.time()
    print("using octree-knn")
    octree_knn_result = []
    for i in range(N):
        knn_result_set = KNNResultSet(k)
        octree.octree_knn_search(root, db_np, knn_result_set, db_np[i])
        octree_knn_result.append(knn_result_set)
    octree_knn_time = time.time() - begin_t
    print("time consumption for octree-knn: ", octree_knn_time)

    begin_t = time.time()
    print("using octree-radius")
    octree_radius_result = []
    for i in range(N):
        radius_result_set = RadiusNNResultSet(radius)
        octree.octree_radius_search(root, db_np, radius_result_set, db_np[i])
        octree_radius_result.append(radius_result_set)
    octree_radius_time = time.time() - begin_t
    print("time consumption for octree-radius: ", octree_radius_time)

    # print the reuslt
    print("---------------- brute force reusult ---------------------: ")
    print(brute_result_indices[2])
    print("---------------- scipy kd-tree-knn reusult ----------------: ")
    print(scipy_result_indices[2])
    print("----------------- kd-tree-knn reusult ---------------------: ")
    print(kd_tree_knn_result[2])
    print("------------------ kd-tree-radius reusult -----------------: ")
    print(kd_tree_radius_result[2])
    print("------------------ octree-knn reusult ---------------------: ")
    print(octree_knn_result[2])
    print("------------------ octree-radius reusult ------------------: ")
    print(octree_radius_result[2])
Example #11
0
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)

    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
    filename = './000000.bin'
    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, knn_time_sum * 1000,
           radius_time_sum * 1000, brute_time_sum * 1000))

    begin_t = time.time()
    result_set = RadiusNNResultSet(radius=radius)
    octree.octree_radius_search_plus(root, db_np, result_set, query)
    radius_time_sum += time.time() - begin_t

    print("Octree: radius_plus %.3f" % (radius_time_sum * 1000))