aidsorb.modules
torch.nn.Module’s for point cloud processing.
Note
PointNetBackbone, PointNetClsHead and
PointNetSegHead have their initial layers lazy initialized, so
you don’t need to specify the input dimensionality.
Warning
It is recommended to use batched inputs in all cases. For example, even
if a single pcd of shape (3+C, N) is to be passed to
PointNetBackbone, reshape it to (1, 3+C, N). One way to you
can do it is the following: pcd = pcd.unsqueeze(0).
Todo
Add more modules for point cloud processing.
References
- class aidsorb.modules.PointNet(head, n_global_feats=1024, local_feats=False)[source]
Bases:
ModuleVanilla version from the [PointNet] paper where
TNet’s have been removed.PointNettakes as input a point cloud and produces one or more outputs. The type of the task is determined byhead.Currently implemented heads include:
PointNetClsHead: classification and regressionPointNetSegHead: segmentation
The input must be batched, i.e. have shape of
(B, C, N)whereBis the batch size,Cis the number of input channels andNis the number of points in each point cloud.Tip
You can define a
custom_headhead as atorch.nn.Moduleand pass it tohead.If
local_features=False, the input tocustom_headmust have the same shape as inPointNetClsHead.forward(). Otherwise, the input tocustom_headmust have the same shape as inPointNetSegHead.forward().- Parameters:
head (torch.nn.Module)
n_global_feats (int, default=1024)
local_feats (bool, default=False)
See also
modules.PointNetBackboneFor a description of
local_featsandn_global_feats.
Examples
>>> cls_head = PointNetClsHead(n_outputs=2) >>> seg_head = PointNetSegHead(n_outputs=10) >>> x = torch.randn(32, 4, 300)
>>> cls_net = PointNet(cls_head, 256) >>> cls_net(x).shape torch.Size([32, 2]) >>> cls_net.backbone(x).shape # Only features. torch.Size([32, 256])
>>> seg_net = PointNet(head=seg_head, n_global_feats=512, local_feats=True) >>> seg_net(x).shape torch.Size([32, 300, 10]) >>> seg_net.backbone(x, True)[1].shape # Features and critical indices. torch.Size([32, 512])
- class aidsorb.modules.PointNetBackbone(n_global_feats=1024, local_feats=False)[source]
Bases:
ModuleBackbone of the vanilla version from the [PointNet] paper, where
TNet’s have been removed.This module extracts features which can then be passed to a task head for predictions. This module also returns the critical indices.
The input must be batched, i.e. have shape of
(B, C, N)whereBis the batch size,Cis the number of input channels andNis the number of points in each point cloud.- Parameters:
Examples
>>> feat = PointNetBackbone(2048) >>> x = torch.randn(32, 4, 200) >>> features, indices = feat(x, return_indices=True) >>> features.shape torch.Size([32, 2048]) >>> indices.shape torch.Size([32, 2048])
>>> feat = PointNetBackbone(1024, True) >>> x = torch.randn(16, 4, 100) >>> features, indices = feat(x, return_indices=True) >>> features.shape torch.Size([16, 1088, 100]) >>> indices.shape torch.Size([16, 1024])
>>> feat = PointNetBackbone(512) >>> x = torch.randn(8, 3, 50) >>> feat(x).shape # Only features, no critical indices. torch.Size([8, 512])
- class aidsorb.modules.PointNetClsHead(n_outputs=1, dropout_rate=0)[source]
Bases:
ModuleClassification head from the [PointNet] paper.
Tip
This head can be used for classification or regression tasks.
Examples
>>> head = PointNetClsHead(n_outputs=4) >>> x = torch.randn(64, 13) >>> head(x).shape torch.Size([64, 4])
- class aidsorb.modules.PointNetSegHead(n_outputs=1)[source]
Bases:
ModuleSegmentation head from the [PointNet] paper.
Tip
This head can be used for segmentation tasks.
- Parameters:
n_outputs (int, default=1)
Examples
>>> head = PointNetSegHead(n_outputs=2) >>> x = torch.randn(32, 1088, 400) >>> head(x).shape torch.Size([32, 400, 2])
- class aidsorb.modules.TNet(embed_dim)[source]
Bases:
ModuleSpatial transformer network (STN) from the [PointNet] paper for performing the input and feature transform.
T-Nettakes as input a (possibly embedded) point cloud of shape(dim, N)and regresses a(dim, dim)matrix. Each point in the point cloud has shape(dim,).The input must be batched, i.e. have shape of
(B, dim, N), whereBis the batch size andNis the number of points in each point cloud.- Parameters:
embed_dim (int) – Embedding dimension.
Examples
>>> tnet = TNet(embed_dim=64) >>> x = torch.randn((128, 64, 42)) # Shape (B, embed_dim, N). >>> tnet(x).shape torch.Size([128, 64, 64])
- aidsorb.modules.conv1d_block(in_channels, out_channels, config_activation=None, **kwargs)[source]
Return a 1D convolutional block.
The block has the following form:
block = nn.Sequential( conv_layer, nn.BatchNorm1d(out_channels), activation_fn )
- Parameters:
- Returns:
block
- Return type:
Examples
>>> inp, out = 4, 128 >>> x = torch.randn(32, 4, 100) # Shape (B, in_channels, N). >>> config_afn = {'name': 'LeakyReLU', 'hparams': {'negative_slope': 0.5}}
>>> # Default activation function (ReLU). >>> block = conv1d_block(inp, out, kernel_size=1) >>> block(x).shape torch.Size([32, 128, 100]) >>> block[2] ReLU()
>>> # Custom activation function. >>> block = conv1d_block(inp, out, config_afn, kernel_size=1) >>> block(x).shape torch.Size([32, 128, 100]) >>> block[2] LeakyReLU(negative_slope=0.5)
>>> # Lazy initialized. >>> block = conv1d_block(None, out, kernel_size=1) >>> block(x).shape torch.Size([32, 128, 100])
- aidsorb.modules.dense_block(in_features, out_features, config_activation=None, **kwargs)[source]
Return a dense block.
The block has the following form:
block = nn.Sequential( linear_layer, nn.BatchNorm1d(out_features), activation_fn, )
- Parameters:
- Returns:
block
- Return type:
Examples
>>> inp, out = 3, 10 >>> x = torch.randn(64, inp) # Shape (B, in_features). >>> config_afn = {'name': 'SELU', 'hparams': {}}
>>> # Default activation function (ReLU). >>> block = dense_block(inp, out) >>> block(x).shape torch.Size([64, 10]) >>> block[2] ReLU()
>>> # Custom activation function. >>> block = dense_block(inp, out, config_afn) >>> block(x).shape torch.Size([64, 10]) >>> block[2] SELU()
>>> # Lazy initialized. >>> block = dense_block(None, 16) >>> block(x).shape torch.Size([64, 16])