FPS(Farthest Point Sampling) 编译笔记

目录

作者自己实现了一版:

c代码:

FPS(Farthest Point Sampling)--最远点采样算法

报错代码:

报错解决方法:


作者自己实现了一版:

extension-cpp/cuda/setup.py at dbf58c505fe5035eccdb71195b6e49c6694f7bbc · hetong007/extension-cpp · GitHub

c代码:

https://github.com/Barbany/fps_cuda/tree/main

FPS(Farthest Point Sampling)--最远点采样算法

新版代码:

pointnet2_ops_lib/pointnet2_ops_lib/pointnet2_ops/pointnet2_utils.py at main · ybhhdt/pointnet2_ops_lib · GitHub


class BallQuery(Function):
    @staticmethod
    def forward(ctx, radius, nsample, xyz, new_xyz):
        # type: (Any, float, int, torch.Tensor, torch.Tensor) -> torch.Tensor
        r"""

        Parameters
        ----------
        radius : float
            radius of the balls
        nsample : int
            maximum number of features in the balls
        xyz : torch.Tensor
            (B, N, 3) xyz coordinates of the features
        new_xyz : torch.Tensor
            (B, npoint, 3) centers of the ball query

        Returns
        -------
        torch.Tensor
            (B, npoint, nsample) tensor with the indicies of the features that form the query balls
        """
        idx, pts_cnt= _ext.ball_query(new_xyz, xyz, radius, nsample)

        ctx.mark_non_differentiable(idx)
        ctx.mark_non_differentiable(pts_cnt)

        return idx, pts_cnt

报错代码:

 
 
import torch
from torch.autograd import Function
 
try:
    import builtins
except:
    import __builtin__ as builtins
 
try:
    import fps_cuda._ext as _ext
except ImportError:
    if not getattr(builtins, "__POINTNET2_SETUP__", False):
        raise ImportError(
            "Could not import _ext module.\n"
            "Please see the setup instructions in the README: "
            "https://github.com/erikwijmans/Pointnet2_PyTorch/blob/master/README.rst"
        )
 
class FurthestPointSampling(Function):
    @staticmethod
    def forward(ctx, xyz, npoint):
        # type: (Any, torch.Tensor, int) -> torch.Tensor
        r"""
        Uses iterative furthest point sampling to select a set of npoint features that have the largest
        minimum distance
        Parameters
        ----------
        xyz : torch.Tensor
            (B, N, 3) tensor where N > npoint
        npoint : int32
            number of features in the sampled set
        Returns
        -------
        torch.Tensor
            (B, npoint) tensor containing the set
        """
        fps_inds = _ext.furthest_point_sampling(xyz, npoint)
        ctx.mark_non_differentiable(fps_inds)
        return fps_inds
 
    @staticmethod
    def backward(xyz, a=None):
        return None, None
 
 
furthest_point_sample = FurthestPointSampling.apply    
 
 
xyz = torch.randn(5,1024,3)
npoint=512
fps_inds = furthest_point_sample(xyz, npoint)
# ctx.mark_non_differentiable(fps_inds)
print(fps_inds)

RuntimeError: Unknown layout in MultiScaleDeformableAttnFunction · Issue #284 · IDEA-Research/GroundingDINO · GitHub

报错解决方法:

pip install torch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0 --index-url https://download.pytorch.org/whl/cu118

 

当然可以!下面是一个使用Farthest Point Sampling (FPS)算法找出文件中距离最远的3张图像,并返回图像名的Python代码示例: ```python import os import numpy as np from PIL import Image def compute_distance(image1, image2): # 计算两张图像的距离,可以使用任意方法 # 这里以欧氏距离为例 return np.linalg.norm(image1 - image2) def farthest_point_sampling(images_dir, num_samples=3): # 获取目录下所有图像文件名 image_files = [file for file in os.listdir(images_dir) if file.endswith('.jpg') or file.endswith('.png')] # 随机选择第一个样本 sample_indices = [np.random.randint(len(image_files))] for _ in range(num_samples-1): max_distance = float('-inf') farthest_index = None # 遍历所有图像文件 for i, image_file in enumerate(image_files): if i not in sample_indices: # 加载图像并转换为灰度图 image_path = os.path.join(images_dir, image_file) image = np.array(Image.open(image_path).convert('L')) # 计算当前图像与已选择样本的距离之和 total_distance = 0 for index in sample_indices: sample_path = os.path.join(images_dir, image_files[index]) sample_image = np.array(Image.open(sample_path).convert('L')) total_distance += compute_distance(image, sample_image) # 更新最远距离和索引 if total_distance > max_distance: max_distance = total_distance farthest_index = i # 将最远的图像索引添加到样本列表中 sample_indices.append(farthest_index) # 返回最远的图像文件名 return [image_files[index] for index in sample_indices] # 示例使用 images_directory = 'path/to/images' farthest_images = farthest_point_sampling(images_directory, num_samples=3) print(farthest_images) ``` 你需要将 `images_directory` 替换为包含图像文件的实际路径。此代码将在指定目录中找到距离最远的3张图像,并返回它们的文件名。请确保在运行代码之前安装必要的依赖项(numpy、PIL)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值