一维和二维数据上采样通常是通过插值的方法来增加点的个数。三维点云上采样理论上也可以通过这个方法来进行。这些上采样的方法均是增加点的数量。但三维空间中点的分布并不是很均匀,不便于定义插值点的位置。因此,本节主要是针对于点云的特征进行上采样,增加的不是点的数量,而是点的特征维度,来源于PointNet++。
一维和二维数据上采样通常是通过插值的方法来增加点的个数。三维点云上采样理论上也可以通过这个方法来进行。这些上采样的方法均是增加点的数量。但三维空间中点的分布并不是很均匀,不便于定义插值点的位置。因此,本节主要是针对于点云的特征进行上采样,增加的不是点的数量,而是点的特征维度,来源于PointNet++。
PointNet++的上采样是通过插值来实现的,并且插值依赖于前后两层特征。假设前一层的点数N=64,后一层点数S=16,那么插值的任务就是把后一层的点数插值成64。主要步骤如下:
(1)以前一层的64个点为参考点,分别计算这64个点和待插值的16个点的距离,得到64x16的距离矩阵。
(2)分别在待插值的16个点中选择k=3个最接近各个参考点的点,然后将这k个点特征的加权平均值作为插值点的特征。每个参考点都会得到一个新的特征,新的特征来自于后一层点特征的加权平均。加权系数等于各个点的距离倒数除以3个点的距离倒数之和。距离越近,加权系数越大。
插值的效果直观描述为:使得前一层的点数能够获得类似后一层的特征。
示例代码中B代表Batch数量,对于单个数据B=1,设置为大于1的值时可以进行批量操作。N和S分别对应前一层和后一层的点数,D1和D2为对应的特征维度。从代码中可以看到,通常插值后的特征维度会进行拼接,即维度为D1+D2。为了使特征维度保持为D2,相应做法是加一层(D1+D2, D2)的卷积操作即可。
def point_feature_upsample(self, xyz1, xyz2, points1, points2):
"""
Input:
xyz1: input points position data, [B, 3, N]
xyz2: sampled input points position data, [B, 3, S]
points1: input points data, [B, D1, N]
points2: input points data, [B, D2, S]
Return:
new_points: upsampled points data, [B, D1+D2, N]
"""
xyz1 = xyz1.permute(0, 2, 1)
xyz2 = xyz2.permute(0, 2, 1)
points2 = points2.permute(0, 2, 1)
B, N, C = xyz1.shape
_, S, _ = xyz2.shape
if S == 1:
interpolated_points = points2.repeat(1, N, 1)
else:
dists = square_distance(xyz1, xyz2)
dists, idx = dists.sort(dim=-1)
dists, idx = dists[:, :, :3], idx[:, :, :3] # [B, N, 3]
dist_recip = 1.0 / (dists + 1e-8)
norm = torch.sum(dist_recip, dim=2, keepdim=True)
weight = dist_recip / norm
interpolated_points = torch.sum(index_points(points2, idx) * weight.view(B, N, 3, 1), dim=2)
if points1 is not None:
points1 = points1.permute(0, 2, 1)
new_points = torch.cat([points1, interpolated_points], dim=-1)
else:
new_points = interpolated_points
new_points = new_points.permute(0, 2, 1)
return new_points
def square_distance(src, dst):
"""
Calculate Euclid distance between each two points.
src^T * dst = xn * xm + yn * ym + zn * zm;
sum(src^2, dim=-1) = xn*xn + yn*yn + zn*zn;
sum(dst^2, dim=-1) = xm*xm + ym*ym + zm*zm;
dist = (xn - xm)^2 + (yn - ym)^2 + (zn - zm)^2
= sum(src**2,dim=-1)+sum(dst**2,dim=-1)-2*src^T*dst
Input:
src: source points, [B, N, C]
dst: target points, [B, M, C]
Output:
dist: per-point square distance, [B, N, M]
"""
B, N, _ = src.shape
_, M, _ = dst.shape
dist = -2 * torch.matmul(src, dst.permute(0, 2, 1))
dist += torch.sum(src ** 2, -1).view(B, N, 1)
dist += torch.sum(dst ** 2, -1).view(B, 1, M)
return dist
def index_points(points, idx):
"""
Input:
points: input points data, [B, N, C]
idx: sample index data, [B, S]
Return:
new_points:, indexed points data, [B, S, C]
"""
device = points.device
B = points.shape[0]
view_shape = list(idx.shape)
view_shape[1:] = [1] * (len(view_shape) - 1)
repeat_shape = list(idx.shape)
repeat_shape[0] = 1
batch_indices = torch.arange(B, dtype=torch.long).to(device).view(view_shape).repeat(repeat_shape)
new_points = points[batch_indices, idx, :]
return new_points
更多三维、二维感知算法和金融量化分析算法请关注“乐乐感知学堂”微信公众号,并将持续进行更新。