Source code for fastdev.nn.pointnet2

"""
PointNet++ implementation in PyTorch.

Adapted from: https://github.com/yanx27/Pointnet_Pointnet2_pytorch
"""

from typing import Optional, Tuple

import torch
import torch.nn as nn
import torch.nn.functional as F

from fastdev.geom.ball_query import ball_query
from fastdev.geom.sampling import sample_farthest_points
from fastdev.geom.utils import masked_gather


def sample_and_group(npoint, radius, nsample, xyz, points, returnfps=False):
    """
    Input:
        npoint:
        radius:
        nsample:
        xyz: input points position data, [B, N, 3]
        points: input points data, [B, N, D]
    Return:
        new_xyz: sampled points position data, [B, npoint, nsample, 3]
        new_points: sampled points data, [B, npoint, nsample, 3+D]
    """
    B, N, C = xyz.shape
    S = npoint

    fps_idx = sample_farthest_points(xyz, npoint, random_start=True)
    new_xyz = torch.gather(xyz, 1, fps_idx.unsqueeze(-1).expand(-1, -1, 3))

    query_results = ball_query(p1=new_xyz, p2=xyz, radius=radius, K=nsample)
    # query_results = ball_query(p1=new_xyz, p2=xyz, radius=0.06, K=nsample)
    # torch.sum(query_results.idx == -1) / (B * S * nsample)
    grouped_xyz = query_results.knn

    grouped_xyz_norm = grouped_xyz - new_xyz.view(B, S, 1, C)

    if points is not None:
        grouped_points = masked_gather(points, query_results.idx)
        new_points = torch.cat([grouped_xyz_norm, grouped_points], dim=-1)  # [B, npoint, nsample, C+D]
    else:
        new_points = grouped_xyz_norm
    if returnfps:
        return new_xyz, new_points, grouped_xyz, fps_idx
    else:
        return new_xyz, new_points


def sample_and_group_all(xyz, points):
    """
    Input:
        xyz: input points position data, [B, N, 3]
        points: input points data, [B, N, D]
    Return:
        new_xyz: sampled points position data, [B, 1, 3]
        new_points: sampled points data, [B, 1, N, 3+D]
    """
    device = xyz.device
    B, N, C = xyz.shape
    new_xyz = torch.zeros(B, 1, C).to(device)
    grouped_xyz = xyz.view(B, 1, N, C)
    if points is not None:
        new_points = torch.cat([grouped_xyz, points.view(B, 1, N, -1)], dim=-1)
    else:
        new_points = grouped_xyz
    return new_xyz, new_points


class PointNetSetAbstraction(nn.Module):
    def __init__(self, npoint, radius, nsample, in_channel, mlp, group_all):
        super().__init__()
        self.npoint = npoint
        self.radius = radius
        self.nsample = nsample
        self.mlp_convs = nn.ModuleList()
        self.mlp_bns = nn.ModuleList()
        last_channel = in_channel
        for out_channel in mlp:
            self.mlp_convs.append(nn.Conv2d(last_channel, out_channel, 1))
            self.mlp_bns.append(nn.BatchNorm2d(out_channel))
            last_channel = out_channel
        self.group_all = group_all

    def forward(self, xyz, points):
        """
        Input:
            xyz: input points position data, [B, C, N]
            points: input points data, [B, D, N]
        Return:
            new_xyz: sampled points position data, [B, C, S]
            new_points_concat: sample points feature data, [B, D', S]
        """
        xyz = xyz.permute(0, 2, 1)
        if points is not None:
            points = points.permute(0, 2, 1)

        if self.group_all:
            new_xyz, new_points = sample_and_group_all(xyz, points)
        else:
            new_xyz, new_points = sample_and_group(self.npoint, self.radius, self.nsample, xyz, points)  # type: ignore
        # new_xyz: sampled points position data, [B, npoint, C]
        # new_points: sampled points data, [B, npoint, nsample, C+D]
        new_points = new_points.permute(0, 3, 2, 1)  # [B, C+D, nsample,npoint]
        for i, conv in enumerate(self.mlp_convs):
            bn = self.mlp_bns[i]
            new_points = F.relu(bn(conv(new_points)), inplace=True)

        new_points = torch.max(new_points, 2)[0]
        new_xyz = new_xyz.permute(0, 2, 1)
        return new_xyz, new_points


[docs] class PointNet2Encoder(nn.Module): def __init__( self, normal_channel: bool = False, feature_dim: int = 1024, n_points: Tuple[int, int] = (512, 128), n_samples: Tuple[int, int] = (32, 64), radius: Tuple[float, float] = (0.2, 0.4), ): super().__init__() in_channel = 6 if normal_channel else 3
[docs] self.normal_channel = normal_channel
[docs] self.sa1 = PointNetSetAbstraction( npoint=n_points[0], radius=radius[0], nsample=n_samples[0], in_channel=in_channel, mlp=[64, 64, 128], group_all=False, )
[docs] self.sa2 = PointNetSetAbstraction( npoint=n_points[1], radius=radius[1], nsample=n_samples[1], in_channel=128 + 3, mlp=[128, 128, 256], group_all=False, )
[docs] self.sa3 = PointNetSetAbstraction( npoint=None, radius=None, nsample=None, in_channel=256 + 3, mlp=[256, 512, 1024], group_all=True )
if feature_dim != 1024: self.feat_linear: Optional[nn.Linear] = nn.Linear(1024, feature_dim) else: self.feat_linear = None
[docs] def forward(self, xyz): B, D, N = xyz.shape if self.normal_channel: norm = xyz[:, 3:, :] xyz = xyz[:, :3, :] else: norm = None l1_xyz, l1_points = self.sa1(xyz, norm) l2_xyz, l2_points = self.sa2(l1_xyz, l1_points) l3_xyz, l3_points = self.sa3(l2_xyz, l2_points) x = l3_points.view(B, 1024) if self.feat_linear is not None: x = self.feat_linear(x) return x, l3_points
[docs] class PointNet2Cls(nn.Module): def __init__(self, num_class: int, normal_channel=True): super().__init__() in_channel = 6 if normal_channel else 3
[docs] self.normal_channel = normal_channel
[docs] self.sa1 = PointNetSetAbstraction( npoint=512, radius=0.2, nsample=32, in_channel=in_channel, mlp=[64, 64, 128], group_all=False )
[docs] self.sa2 = PointNetSetAbstraction( npoint=128, radius=0.4, nsample=64, in_channel=128 + 3, mlp=[128, 128, 256], group_all=False )
[docs] self.sa3 = PointNetSetAbstraction( npoint=None, radius=None, nsample=None, in_channel=256 + 3, mlp=[256, 512, 1024], group_all=True )
[docs] self.fc1 = nn.Linear(1024, 512)
[docs] self.bn1 = nn.BatchNorm1d(512)
[docs] self.drop1 = nn.Dropout(0.4)
[docs] self.fc2 = nn.Linear(512, 256)
[docs] self.bn2 = nn.BatchNorm1d(256)
[docs] self.drop2 = nn.Dropout(0.4)
[docs] self.fc3 = nn.Linear(256, num_class)
[docs] self.relu = nn.ReLU(inplace=True)
[docs] def forward(self, xyz): B, _, _ = xyz.shape if self.normal_channel: norm = xyz[:, 3:, :] xyz = xyz[:, :3, :] else: norm = None l1_xyz, l1_points = self.sa1(xyz, norm) l2_xyz, l2_points = self.sa2(l1_xyz, l1_points) l3_xyz, l3_points = self.sa3(l2_xyz, l2_points) x = l3_points.view(B, 1024) x = self.drop1(self.relu(self.bn1(self.fc1(x)))) x = self.drop2(self.relu(self.bn2(self.fc2(x)))) x = self.fc3(x) x = F.log_softmax(x, -1) return x, l3_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 class PointNetFeaturePropagation(nn.Module): def __init__(self, in_channel, mlp): super().__init__() self.mlp_convs = nn.ModuleList() self.mlp_bns = nn.ModuleList() last_channel = in_channel for out_channel in mlp: self.mlp_convs.append(nn.Conv1d(last_channel, out_channel, 1)) self.mlp_bns.append(nn.BatchNorm1d(out_channel)) last_channel = out_channel def forward(self, xyz1, xyz2, points1, points2): """ Input: xyz1: input points position data, [B, C, N] xyz2: sampled input points position data, [B, C, S] points1: input points data, [B, D, N] points2: input points data, [B, D, S] Return: new_points: upsampled points data, [B, D', 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) for i, conv in enumerate(self.mlp_convs): bn = self.mlp_bns[i] new_points = F.relu(bn(conv(new_points))) return new_points # https://github.com/yanx27/Pointnet_Pointnet2_pytorch/blob/master/models/pointnet2_sem_seg.py class PointNet2SemSeg(nn.Module): def __init__(self, num_classes): super().__init__() self.sa1 = PointNetSetAbstraction(1024, 0.1, 32, 9 + 3, [32, 32, 64], False) self.sa2 = PointNetSetAbstraction(256, 0.2, 32, 64 + 3, [64, 64, 128], False) self.sa3 = PointNetSetAbstraction(64, 0.4, 32, 128 + 3, [128, 128, 256], False) self.sa4 = PointNetSetAbstraction(16, 0.8, 32, 256 + 3, [256, 256, 512], False) self.fp4 = PointNetFeaturePropagation(768, [256, 256]) self.fp3 = PointNetFeaturePropagation(384, [256, 256]) self.fp2 = PointNetFeaturePropagation(320, [256, 128]) self.fp1 = PointNetFeaturePropagation(128, [128, 128, 128]) self.conv1 = nn.Conv1d(128, 128, 1) self.bn1 = nn.BatchNorm1d(128) self.drop1 = nn.Dropout(0.5) self.conv2 = nn.Conv1d(128, num_classes, 1) def forward(self, xyz): l0_points = xyz l0_xyz = xyz[:, :3, :] l1_xyz, l1_points = self.sa1(l0_xyz, l0_points) l2_xyz, l2_points = self.sa2(l1_xyz, l1_points) l3_xyz, l3_points = self.sa3(l2_xyz, l2_points) l4_xyz, l4_points = self.sa4(l3_xyz, l3_points) l3_points = self.fp4(l3_xyz, l4_xyz, l3_points, l4_points) l2_points = self.fp3(l2_xyz, l3_xyz, l2_points, l3_points) l1_points = self.fp2(l1_xyz, l2_xyz, l1_points, l2_points) l0_points = self.fp1(l0_xyz, l1_xyz, None, l1_points) x = self.drop1(F.relu(self.bn1(self.conv1(l0_points)))) x = self.conv2(x) x = F.log_softmax(x, dim=1) x = x.permute(0, 2, 1) return x, l4_points __all__ = ["PointNet2Encoder", "PointNet2Cls"]