Skip to content

chamfer

Chamfer distance between batched point sets.

Functions:

  • chamfer_distance –

    Symmetric Chamfer distance between two batched point sets.

chamfer_distance

chamfer_distance(
    pred: Tensor,
    target: Tensor,
    norm: Literal["l1", "l2"] = "l2",
) -> Tensor

Symmetric Chamfer distance between two batched point sets.

Set-to-set reconstruction objective introduced for point cloud generation in Fan et al., 2017 and standard for masked point modeling pretraining (the SSL pretraining models return (pred, target) group coordinates in exactly this layout). For each point the squared euclidean distance to its nearest neighbor in the other set is computed, then reduced over all points and batches:

\[\text{CD}_{\ell_2} = \frac{1}{BN} \sum \min_j \lVert p_i - q_j \rVert_2^2 + \frac{1}{BM} \sum \min_i \lVert p_i - q_j \rVert_2^2\]
\[\text{CD}_{\ell_1} = \frac{1}{2} \Big( \frac{1}{BN} \sum \min_j \lVert p_i - q_j \rVert_2 + \frac{1}{BM} \sum \min_i \lVert p_i - q_j \rVert_2 \Big)\]

The "l2" variant sums the two directed means of squared distances (no square root, no halving); the "l1" variant averages the two directed means of euclidean distances. Both follow the reference pretraining convention, so losses are comparable with published values.

Parameters:

  • pred (Tensor) –

    Predicted point sets of shape \((B, N, 3)\).

  • target (Tensor) –

    Target point sets of shape \((B, M, 3)\).

  • norm (Literal['l1', 'l2'], default: 'l2' ) –

    Distance variant, "l1" (euclidean) or "l2" (squared euclidean).

Returns:

  • Tensor –

    Scalar Chamfer distance averaged over all points and batches.

Shape
  • Input: \((B, N, 3)\) and \((B, M, 3)\).
  • Output: scalar.
Example
import torch
from torch_pointcloud.losses import chamfer_distance

pred = torch.randn(64, 32, 3, requires_grad=True)
target = torch.randn(64, 32, 3)
loss = chamfer_distance(pred, target, norm="l2")
loss.backward()
print(loss.shape)