美文网首页
FarthestPointSampling(FPS)算法学习&实

FarthestPointSampling(FPS)算法学习&实

作者: 别有路 | 来源:发表于2024-11-20 00:33 被阅读0次

最近工作需要用到最远点采样fps算法, 找了一些blog学习学习, 写了个原型:

def distance(p1, p2):
    return np.sqrt(np.sum((p1 - p2)**2))

def FPS(sample, num, center_idx: int = 0):
    '''sample:采样点云数据,
    num:需要采样的数据点个数'''
    n = sample.shape[0]
    select_p = [center_idx]  # 储存采集点索引
    L = np.full(n, 1e20, dtype=np.float64)
    for i in range(num - 1):
        for p in range(n):
            d = distance(sample[select_p[-1]], sample[p])
            if d < L[p]:
                L[p] = d
        select_p.append(np.argmax(L).item())
    return select_p

测试一下, 跑的没问题, 但是速度感人

所有点的欧氏距离计算做个向量化优化:

def fps(points, n_samples, first_idx: int = 0):
    """
    points: (N, D) array of points
    n_samples: number of points to sample
    first_idx: index of first point to sample
    """
    N = points.shape[0]
    selected_indices = [first_idx]
    distances = np.full(N, np.inf)

    for _ in range(n_samples - 1):
        # 计算所有点到最新选择点的距离
        last_selected = points[selected_indices[-1]]
        dist = np.sum((points - last_selected) ** 2, axis=1)

        # 更新最小距离
        distances = np.minimum(distances, dist)

        # 选择距离最大的点
        new_idx = np.argmax(distances).item()
        selected_indices.append(new_idx)

        distances[new_idx] = 0

    return selected_indices

这一下子快了100多倍, 能用了
找了个开源基于rust实现核心的python接口包: https://github.com/leonardodalinky/fpsample

对比了一下结果, 是没问题的, 但是他比我这个快个4,5倍
最后借助ai优化一下:

def fps_optimized(points, n_samples, first_idx: int = 0):
    """
    Optimized FPS implementation
    points: (N, D) array of points
    n_samples: number of points to sample
    first_idx: index of first point to sample
    """
    N = points.shape[0]
    selected_indices = np.zeros(n_samples, dtype=np.int64)
    distances = np.full(N, np.inf)
    selected_indices[0] = first_idx

    # 预计算点的平方和,避免重复计算
    point_squares = np.sum(points ** 2, axis=1)

    for i in range(1, n_samples):
        last_selected = points[selected_indices[i - 1]]

        # 使用广播来计算距离,避免创建临时数组
        # ||a-b||^2 = ||a||^2 + ||b||^2 - 2<a,b>
        dist = point_squares + np.sum(last_selected ** 2) - 2 * np.dot(points, last_selected)

        # 更新最小距离
        np.minimum(distances, dist, out=distances)

        # 选择距离最大的点
        new_idx = np.argmax(distances)
        selected_indices[i] = new_idx
        distances[new_idx] = 0

    return selected_indices.tolist()

又快了6,7倍, 中小规模点云用这个看起来就没啥问题了
后面如果有超大规模的需求或许可以关注fpsample作者的另一个repo: https://github.com/leonardodalinky/pytorch_fpsample

相关文章

  • 机器学习

    Python机器学习 预测分析核心算法 实用机器学习 实变函数 腾讯QQ音乐招聘 机器学习算法工程师 25k-35...

  • YOLO Objection Detection

    简介 这是一种把目标定位和检测合起来做的算法,据作者测试可以达到45fps(复杂模型)和155fps(精简模型)。...

  • Strategy 策略模式

    设计原则学习笔记 设计模式学习笔记 作用 将算法封装起来,使对象可以在不同情况,使用不同算法。 类图 Java实...

  • golang中crypto/aes包

    aes是对称加密算法,这篇博客只介绍怎么使用golang中怎么调用标准库已封装的算法实现,如果是要学习aes算法实...

  • Matrix Derivative矩阵求导

    Welcome To My Blog 学习机器学习算法时总碰见矩阵求导,现学习一波,主要总结下注意:这里只涉及实...

  • 查看iOS屏幕帧数MGFPSStatus

    在状态栏显示FPS状态,FPS是一秒钟渲染多少帧 Frame Per Second = FPS,FPS值为55~6...

  • 机器学习路径

    1. 机器学习入门课程实操: imooc 上《python3入门机器学习,经典算法与应用》——刘宇波 附带该项目G...

  • 图片及视频原理

    720p HD ,30fps (1280×720) 1080 HD,30fps/60fps (1920×1080)...

  • 2019 Python 官方年度报告强势来袭!

    完整中文版 | 2019 Python 官方年度报告强势来袭! 转自:QIML编辑部 机器学习算法与Python实...

  • VSyne 相关的概念

    帧率 即 Frame Rate,单位 fps,是指 gpu 生成帧的速率,如 33 fps,60fps,越高越好。...

网友评论

      本文标题:FarthestPointSampling(FPS)算法学习&实

      本文链接:https://www.haomeiwen.com/subject/ugibjjtx.html