PointMLP/pointnet2_ops_lib/pointnet2_ops/pointnet2_modules.py

220 lines
6.4 KiB
Python
Raw Normal View History

2021-10-04 07:25:18 +00:00
import torch
import torch.nn as nn
import torch.nn.functional as F
2023-08-03 14:40:14 +00:00
2021-10-04 07:25:18 +00:00
from pointnet2_ops import pointnet2_utils
2023-08-03 14:40:14 +00:00
def build_shared_mlp(mlp_spec: list[int], bn: bool = True):
2021-10-04 07:25:18 +00:00
layers = []
for i in range(1, len(mlp_spec)):
layers.append(
2023-08-03 14:40:14 +00:00
nn.Conv2d(mlp_spec[i - 1], mlp_spec[i], kernel_size=1, bias=not bn),
2021-10-04 07:25:18 +00:00
)
if bn:
layers.append(nn.BatchNorm2d(mlp_spec[i]))
layers.append(nn.ReLU(True))
return nn.Sequential(*layers)
class _PointnetSAModuleBase(nn.Module):
def __init__(self):
2023-08-03 14:40:14 +00:00
super().__init__()
2021-10-04 07:25:18 +00:00
self.npoint = None
self.groupers = None
self.mlps = None
def forward(
2023-08-03 14:40:14 +00:00
self,
xyz: torch.Tensor,
features: torch.Tensor | None,
) -> tuple[torch.Tensor, torch.Tensor]:
r"""Parameters
2021-10-04 07:25:18 +00:00
----------
xyz : torch.Tensor
(B, N, 3) tensor of the xyz coordinates of the features
features : torch.Tensor
(B, C, N) tensor of the descriptors of the the features
2023-08-03 14:40:14 +00:00
Returns:
2021-10-04 07:25:18 +00:00
-------
new_xyz : torch.Tensor
(B, npoint, 3) tensor of the new features' xyz
new_features : torch.Tensor
(B, \sum_k(mlps[k][-1]), npoint) tensor of the new_features descriptors
"""
new_features_list = []
xyz_flipped = xyz.transpose(1, 2).contiguous()
new_xyz = (
pointnet2_utils.gather_operation(
2023-08-03 14:40:14 +00:00
xyz_flipped,
pointnet2_utils.furthest_point_sample(xyz, self.npoint),
2021-10-04 07:25:18 +00:00
)
.transpose(1, 2)
.contiguous()
if self.npoint is not None
else None
)
for i in range(len(self.groupers)):
new_features = self.groupers[i](
2023-08-03 14:40:14 +00:00
xyz,
new_xyz,
features,
2021-10-04 07:25:18 +00:00
) # (B, C, npoint, nsample)
new_features = self.mlps[i](new_features) # (B, mlp[-1], npoint, nsample)
new_features = F.max_pool2d(
2023-08-03 14:40:14 +00:00
new_features,
kernel_size=[1, new_features.size(3)],
2021-10-04 07:25:18 +00:00
) # (B, mlp[-1], npoint, 1)
new_features = new_features.squeeze(-1) # (B, mlp[-1], npoint)
new_features_list.append(new_features)
return new_xyz, torch.cat(new_features_list, dim=1)
class PointnetSAModuleMSG(_PointnetSAModuleBase):
2023-08-03 14:40:14 +00:00
r"""Pointnet set abstrction layer with multiscale grouping.
2021-10-04 07:25:18 +00:00
Parameters
----------
npoint : int
Number of features
radii : list of float32
list of radii to group with
nsamples : list of int32
Number of samples in each ball query
mlps : list of list of int32
Spec of the pointnet before the global max_pool for each scale
bn : bool
Use batchnorm
"""
def __init__(self, npoint, radii, nsamples, mlps, bn=True, use_xyz=True):
# type: (PointnetSAModuleMSG, int, List[float], List[int], List[List[int]], bool, bool) -> None
2023-08-03 14:40:14 +00:00
super().__init__()
2021-10-04 07:25:18 +00:00
assert len(radii) == len(nsamples) == len(mlps)
self.npoint = npoint
self.groupers = nn.ModuleList()
self.mlps = nn.ModuleList()
for i in range(len(radii)):
radius = radii[i]
nsample = nsamples[i]
self.groupers.append(
pointnet2_utils.QueryAndGroup(radius, nsample, use_xyz=use_xyz)
if npoint is not None
2023-08-03 14:40:14 +00:00
else pointnet2_utils.GroupAll(use_xyz),
2021-10-04 07:25:18 +00:00
)
mlp_spec = mlps[i]
if use_xyz:
mlp_spec[0] += 3
self.mlps.append(build_shared_mlp(mlp_spec, bn))
class PointnetSAModule(PointnetSAModuleMSG):
2023-08-03 14:40:14 +00:00
r"""Pointnet set abstrction layer.
2021-10-04 07:25:18 +00:00
Parameters
----------
npoint : int
Number of features
radius : float
Radius of ball
nsample : int
Number of samples in the ball query
mlp : list
Spec of the pointnet before the global max_pool
bn : bool
Use batchnorm
"""
def __init__(
2023-08-03 14:40:14 +00:00
self,
mlp,
npoint=None,
radius=None,
nsample=None,
bn=True,
use_xyz=True,
2021-10-04 07:25:18 +00:00
):
# type: (PointnetSAModule, List[int], int, float, int, bool, bool) -> None
2023-08-03 14:40:14 +00:00
super().__init__(
2021-10-04 07:25:18 +00:00
mlps=[mlp],
npoint=npoint,
radii=[radius],
nsamples=[nsample],
bn=bn,
use_xyz=use_xyz,
)
class PointnetFPModule(nn.Module):
2023-08-03 14:40:14 +00:00
r"""Propigates the features of one set to another.
2021-10-04 07:25:18 +00:00
Parameters
----------
mlp : list
Pointnet module parameters
bn : bool
Use batchnorm
"""
def __init__(self, mlp, bn=True):
# type: (PointnetFPModule, List[int], bool) -> None
2023-08-03 14:40:14 +00:00
super().__init__()
2021-10-04 07:25:18 +00:00
self.mlp = build_shared_mlp(mlp, bn=bn)
def forward(self, unknown, known, unknow_feats, known_feats):
# type: (PointnetFPModule, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor) -> torch.Tensor
2023-08-03 14:40:14 +00:00
r"""Parameters
2021-10-04 07:25:18 +00:00
----------
unknown : torch.Tensor
(B, n, 3) tensor of the xyz positions of the unknown features
known : torch.Tensor
(B, m, 3) tensor of the xyz positions of the known features
unknow_feats : torch.Tensor
(B, C1, n) tensor of the features to be propigated to
known_feats : torch.Tensor
(B, C2, m) tensor of features to be propigated
2023-08-03 14:40:14 +00:00
Returns:
2021-10-04 07:25:18 +00:00
-------
new_features : torch.Tensor
(B, mlp[-1], n) tensor of the features of the unknown features
"""
if known is not None:
dist, idx = pointnet2_utils.three_nn(unknown, known)
dist_recip = 1.0 / (dist + 1e-8)
norm = torch.sum(dist_recip, dim=2, keepdim=True)
weight = dist_recip / norm
interpolated_feats = pointnet2_utils.three_interpolate(
2023-08-03 14:40:14 +00:00
known_feats,
idx,
weight,
2021-10-04 07:25:18 +00:00
)
else:
interpolated_feats = known_feats.expand(
2023-08-03 14:40:14 +00:00
*(known_feats.size()[0:2] + [unknown.size(1)]),
2021-10-04 07:25:18 +00:00
)
if unknow_feats is not None:
new_features = torch.cat(
2023-08-03 14:40:14 +00:00
[interpolated_feats, unknow_feats],
dim=1,
2021-10-04 07:25:18 +00:00
) # (B, C2 + C1, n)
else:
new_features = interpolated_feats
new_features = new_features.unsqueeze(-1)
new_features = self.mlp(new_features)
return new_features.squeeze(-1)