Skip to content

PointNetConv

PointNetConv(
    local_nn: Optional[Callable] = None,
    global_nn: Optional[Callable] = None,
    add_self_loops: bool = True,
    **kwargs,
)

Bases: MessagePassing

The PointNet set layer from the "PointNet: Deep Learning on Point Sets for 3D Classification and Segmentation" and "PointNet++: Deep Hierarchical Feature Learning on Point Sets in a Metric Space" papers.

\[ \mathbf{x}^{\prime}_i = \gamma_{\mathbf{\Theta}} \left( \max_{j \in \mathcal{N}(i) \cup \{ i \}} h_{\mathbf{\Theta}} ( \mathbf{x}_j, \mathbf{p}_j - \mathbf{p}_i) \right), \]

where \(\gamma_{\mathbf{\Theta}}\) (global_nn) and \(h_{\mathbf{\Theta}}\) (local_nn) are neural networks and \(\mathbf{p}_j \in \mathbb{R}^3\) are the positions of the points.

Parameters:

Name Type Description Default
local_nn Optional[Callable]

Network \(h_{\mathbf{\Theta}}\) applied to each edge, on the concatenation [x_j, pos_j - pos_i] (or pos_j - pos_i only without node features). Default: None

None
global_nn Optional[Callable]

Network \(\gamma_{\mathbf{\Theta}}\) applied to the aggregated features. Default: None

None
add_self_loops bool

If True, removes the self-loops of the graph and adds one for each node (for the first min(num_src_nodes, num_dst_nodes) nodes of a bipartite graph, as in PyG). Default: True

True
**kwargs

Arguments of MessagePassing, e.g. aggr ("max" by default).

{}

Example:

import mlx.core as mx
import mlx.nn as nn
from mlx_graphs.nn import PointNetConv
from mlx_graphs.utils import knn_graph

pos = mx.random.normal((100, 3))
x = mx.random.normal((100, 16))
edge_index = knn_graph(pos, k=8)
local_nn = nn.Sequential(nn.Linear(16 + 3, 32), nn.ReLU(), nn.Linear(32, 32))
conv = PointNetConv(local_nn)
>>> conv(edge_index, x, pos).shape
(100, 32)

Methods:

Name Description
__call__

Computes the forward pass of PointNetConv.

Source code in mlx_graphs/nn/conv/point_conv.py
def __init__(
    self,
    local_nn: Optional[Callable] = None,
    global_nn: Optional[Callable] = None,
    add_self_loops: bool = True,
    **kwargs,
):
    kwargs.setdefault("aggr", "max")
    super().__init__(**kwargs)
    self.local_nn = local_nn
    self.global_nn = global_nn
    self.add_self_loops = add_self_loops

__call__

__call__(edge_index: array, node_features: OptPair, pos: Pair) -> array

Computes the forward pass of PointNetConv.

Parameters:

Name Type Description Default
edge_index array

Edges of shape [2, num_edges], from the neighbors (sources) to the center points (destinations).

required
node_features OptPair

Features of the points, of shape [N, F], a (src_features, dst_features) tuple for bipartite graphs (only the source features are used), or None.

required
pos Pair

Positions of the points, of shape [N, 3], or a (src_pos, dst_pos) tuple for bipartite graphs.

required

Returns:

Type Description
array

The features of the destination points.

Source code in mlx_graphs/nn/conv/point_conv.py
def __call__(
    self,
    edge_index: mx.array,
    node_features: OptPair,
    pos: Pair,
) -> mx.array:
    """Computes the forward pass of PointNetConv.

    Args:
        edge_index: Edges of shape ``[2, num_edges]``, from the neighbors
            (sources) to the center points (destinations).
        node_features: Features of the points, of shape ``[N, F]``, a
            ``(src_features, dst_features)`` tuple for bipartite graphs
            (only the source features are used), or ``None``.
        pos: Positions of the points, of shape ``[N, 3]``, or a
            ``(src_pos, dst_pos)`` tuple for bipartite graphs.

    Returns:
        The features of the destination points.
    """
    x_src = node_features[0] if isinstance(node_features, tuple) else node_features
    pos = _pair(pos)
    if self.add_self_loops:
        edge_index = _set_self_loops(
            edge_index, min(pos[0].shape[0], pos[1].shape[0])
        )
    x_j = None if x_src is None else x_src[edge_index[0]]
    out = self.propagate(
        edge_index=edge_index,
        node_features=pos,
        message_kwargs={"x_j": x_j},
    )
    if self.global_nn is not None:
        out = self.global_nn(out)
    return out

from mlx_graphs.nn import PointNetConv · mlx_graphs/nn/conv/point_conv.py