pointnet++代码逐行解析(四)——— provider

本文详细介绍了PointNet++模型中的数据预处理方法,包括点云数据的归一化、打乱、旋转及抖动等操作,这些操作有助于提高模型的泛化能力。

继续巩固PointNet++代码的实现这篇博客,把代码逐行注释一遍!
pointnet++的所有代码和数据集都在github上,Pytorch代码:https://github.com/yanx27/Pointnet2_pytorch
深度学习中数据预处理provider.py部分的python代码注释如下:

import numpy as np
# 归一化batch_data,使用以centroid为中心的块的坐标
def normalize_data(batch_data):
    """ Normalize the batch data, use coordinates of the block centered at origin,
        Input:
            BxNxC array
        Output:
            BxNxC array
    """
    B, N, C = batch_data.shape
    normal_data = np.zeros((B, N, C))
    for b in range(B):
        pc = batch_data[b]
        centroid = np.mean(pc, axis=0)
        pc = pc - centroid
        m = np.max(np.sqrt(np.sum(pc ** 2, axis=1)))
        pc = pc / m
        normal_data[b] = pc
    return normal_data

# 打乱数据(有相应标签)
def shuffle_data(data, labels):
    """ Shuffle data and labels.
        Input:
          data: B,N,... numpy array
          label: B,... numpy array
        Return:
          shuffled data, label and shuffle indices
    """
    # arange创建等差数列,0到最大值,也就是labels的编号
    idx = np.arange(len(labels))
    # 随机打乱idx
    np.random.shuffle(idx)
    return data[idx, ...], labels[idx], idx
# 打乱每个点云中的点顺序-用于更改FPS行为。对整个batch使用想用的打乱索引|idx
def shuffle_points(batch_data):
    """ Shuffle orders of points in each point cloud -- changes FPS behavior.
        Use the same shuffling idx for the entire batch.
        Input:
            BxNxC array
        Output:
            BxNxC array
    """
    idx = np.arange(batch_data.shape[1])
    np.random.shuffle(idx)
    return batch_data[:,idx,:]
# 随机旋转点云进行数据集增广;每个形状沿向上方向旋转
def rotate_point_cloud(batch_data):
    """ Randomly rotate the point clouds to augument the dataset
        rotation is per shape based along up direction
        Input:
          BxNx3 array, original batch of point clouds
        Return:
          BxNx3 array, rotated batch of point clouds
    """
    # 根据batch_data的矩阵结构,构造一个元素都是0的矩阵
    rotated_data = np.zeros(batch_data.shape, dtype=np.float32)
    for k in range(batch_data.shape[0]):
        # 产生0-1之间的随机数,乘以2*np.pi,得到一个角度
        rotation_angle = np.random.uniform() * 2 * np.pi
        # 求此角度的cos和sin
        cosval = np.cos(rotation_angle)
        sinval = np.sin(rotation_angle)
        # 然后组成一个3*3的旋转矩阵
        rotation_matrix = np.array([[cosval, 0, sinval],
                                    [0, 1, 0],
                                    [-sinval, 0, cosval]])
        # 一个shape_pc内是把batch_data切成多个3元素数组
        shape_pc = batch_data[k, ...]
        # 旋转点云数据:乘向上旋转矩阵
        rotated_data[k, ...] = np.dot(shape_pc.reshape((-1, 3)), rotation_matrix)
    return rotated_data
#沿着z轴旋转点云做数据增强
def rotate_point_cloud_z(batch_data):
    """ Randomly rotate the point clouds to augument the dataset
        rotation is per shape based along up direction
        Input:
          BxNx3 array, original batch of point clouds
        Return:
          BxNx3 array, rotated batch of point clouds
    """
    rotated_data = np.zeros(batch_data.shape, dtype=np.float32)
    for k in range(batch_data
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值