Overview
Training criteria.
Generic losses are plain (input, target) modules, each next to its tensor function, which functional re-exports. The detection losses are named after the head (or model) whose output they read and assign their targets from the packed box / label / batch_box ground truth; their forward(output, data) returns a dict with the scalar loss to optimize plus its detached named terms (<term>_loss). The generic losses return a tensor; a training loop accepts either.
Modules:
-
anchorβLosses of the anchor-based BEV detection heads (SECOND, PointPillars and the multi-group nuScenes variants).
-
centerpointβLosses of the center-based detection heads, dense (
CenterHead) and fully sparse (VoxelNeXtHead). -
chamferβChamfer distance between packed point sets.
-
cornerβCorner loss of the box-refinement heads (PointRCNN).
-
focalβFocal losses: the sigmoid focal loss of the anchor / point / query heads, the Gaussian focal loss of the heatmap heads
-
functionalβFunctional API of the losses: the tensor function of every themed module, re-exported here in the spirit of
-
kpconvβFitting and repulsive regularizer of deformable kernel point convolutions.
-
lovaszβLovΓ‘sz-Softmax loss.
-
matchingβSet-prediction matching between predictions and ground-truth objects.
-
pointrcnnβTwo-stage PointRCNN detection loss: stage-1 per-point head and stage-2 ROI refinement.
-
sumβWeighted sum of several
(logits, target)losses. -
threedetrβ3DETR set-prediction detection loss: Hungarian query-to-object matching with per-layer aux losses.
-
tnetβOrthogonality regularizer of the T-Net transforms (PointNet's feature-transform regularization).
-
transfusionβLoss of the query-based
TransFusionHead(LION): Hungarian-matched query targets and a dense center heatmap. -
votenetβVoteNet detection loss: deep Hough voting target assignment and multi-task objective.
Classes:
-
AnchorHeadLossβLoss of the single-group
AnchorHead(SECOND, PointPillars). -
MultiGroupAnchorHeadLossβLoss of the
MultiGroupAnchorHead(nuScenes SECOND, PointPillars). -
CenterHeadLossβLoss of the dense
CenterHead(CenterPoint, Voxel Mamba). -
VoxelNeXtHeadLossβLoss of the fully sparse
VoxelNeXtHead. -
ChamferDistanceβModule form of
chamfer_distance. -
CornerLossβModule form of
corner_loss. -
GaussianFocalLossβModule form of
gaussian_focal_loss. -
Poly1FocalLossβModule form of
poly1_focal_loss, taking class indices. -
SigmoidFocalLossβModule form of
sigmoid_focal_loss. -
KPConvDeformRegularizerβModule form of
kpconv_deform_regularizer. -
LovaszLossβLovΓ‘sz-Softmax loss: a smooth surrogate for the mean-IoU objective.
-
PointRCNNLossβTwo-stage PointRCNN detection loss (per-point proposal head + ROI refinement head).
-
SumLossβWeighted sum of several loss modules sharing a
(logits, target)signature. -
ThreeDETRLossβ3DETR Hungarian set-prediction detection loss.
-
TNetOrthogonalityRegularizerβModule form of
tnet_orthogonality_regularizer. -
TransFusionHeadLossβLoss of the
TransFusionHead(dense heatmap, matched classification, box, IoU rescore). -
VoteNetLossβLoss of
VoteNetDetection(vote, objectness, box, semantic).
Functions:
-
chamfer_distanceβSymmetric Chamfer distance between packed point sets.
-
corner_lossβPer-box mean smooth-\(L_1\) distance between the eight corners of two box sets, robust to the heading flip.
-
gaussian_focal_lossβPenalty-reduced focal loss over a Gaussian heatmap, normalized by the number of peaks.
-
one_hot_foregroundβOne-hot encode per-row class labels, dropping the background column.
-
poly1_focal_lossβPoly-1 focal loss between per-class logits and binary targets (no reduction).
-
sigmoid_focal_lossβSigmoid focal loss between per-class logits and binary targets (no reduction).
-
kpconv_deform_regularizerβRegularize the deformed kernel points of every deformable
KPConvinmodule. -
lovasz_softmaxβMulti-class LovΓ‘sz-Softmax loss over per-point class probabilities.
-
hungarian_matchβOne-to-one assignment of predictions to targets minimizing the total cost (Hungarian algorithm).
-
hungarian_match_batchedβOne-to-one assignment of every scene's predictions to its targets, with a single device transfer.
-
tnet_orthogonality_regularizerβPull the transforms predicted by every T-Net in
moduletowards an orthogonal matrix.
AnchorHeadLoss [source] ΒΆ
AnchorHeadLoss(
num_classes: int,
*,
point_cloud_range: Sequence[float],
voxel_size: Sequence[float],
anchor_sizes: Sequence[Sequence[float]],
anchor_bottom_heights: Sequence[float],
feature_map_stride: int,
matched_thresholds: Sequence[float],
unmatched_thresholds: Sequence[float],
anchor_rotations: Sequence[float] = (0.0, 1.57),
code_weights: Sequence[float] = (
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
),
cls_weight: float = 1.0,
loc_weight: float = 2.0,
dir_weight: float = 0.2,
num_dir_bins: int = 2,
dir_offset: float = 0.78539,
dir_limit_offset: float = 0.0,
focal_alpha: float = 0.25,
focal_gamma: float = 2.0,
smooth_l1_beta: float = 1.0 / 9.0,
match_height: bool = False,
)
Bases: Module
Loss of the single-group AnchorHead (SECOND, PointPillars).
Reference: SECOND: Sparsely Embedded Convolutional Detection (Yan et al., 2018).
Per scene each class's axis-aligned anchors are matched to that class's ground-truth boxes (assign_anchor_targets) using per-class IoU thresholds, giving per-anchor class labels (\(-1\) ignore, \(0\) background, \(\ge 1\) foreground) and residual box targets. Three terms are then summed:
- Classification: sigmoid focal loss over one-hot foreground labels, weighted so ignored anchors contribute nothing and each scene is normalized by its positive count.
- Box regression: code-weighted smooth-\(L_1\) of the residual encodings, with the heading residual replaced by \(\sin(\theta_p - \theta_g)\) (the sine-difference encoding) so a half-turn error is not penalized as a position error.
- Direction: weighted softmax cross-entropy over the discretized heading bin.
Anchors are rebuilt in the constructor from the same geometry the head uses (generate_anchors); the loss holds no reference to the model.
Parameters:
-
num_classes(int) βNumber of foreground classes.
-
point_cloud_range(Sequence[float]) βRange \((x_\min, y_\min, z_\min, x_\max, y_\max, z_\max)\).
-
voxel_size(Sequence[float]) βVoxel size \((v_x, v_y, v_z)\) (used with
point_cloud_rangeto size the anchor grid). -
anchor_sizes(Sequence[Sequence[float]]) βPer-class box size \((d_x, d_y, d_z)\), one row per class.
-
anchor_bottom_heights(Sequence[float]) βPer-class anchor bottom \(z\), one per class.
-
feature_map_stride(int) βBEV feature-map stride of the head.
-
matched_thresholds(Sequence[float]) βPer-class IoU at or above which an anchor is a positive.
-
unmatched_thresholds(Sequence[float]) βPer-class IoU below which an anchor is background.
-
anchor_rotations(Sequence[float], default:(0.0, 1.57)) βYaw angles (radians) shared by all classes.
-
code_weights(Sequence[float], default:(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0)) βPer-code regression weights, shape \((7,)\).
-
cls_weight(float, default:1.0) βWeight of the classification term in the total.
-
loc_weight(float, default:2.0) βWeight of the box-regression term in the total.
-
dir_weight(float, default:0.2) βWeight of the direction term in the total.
-
num_dir_bins(int, default:2) βNumber of direction bins.
-
dir_offset(float, default:0.78539) βDirection-target angle offset.
-
dir_limit_offset(float, default:0.0) βOffset used when wrapping the heading before binning.
-
focal_alpha(float, default:0.25) βFocal-loss positive/negative balance.
-
focal_gamma(float, default:2.0) βFocal-loss focusing exponent.
-
smooth_l1_beta(float, default:1.0 / 9.0) βSmooth-\(L_1\) transition point \(\beta\).
-
match_height(bool, default:False) βMatch anchors to boxes by the rotated 3D IoU when
True, otherwise by the bird's-eye IoU of the boxes snapped to their nearest axis-aligned orientation.
Methods:
-
forwardβCompute the anchor detection loss and its terms.
forward [source] ΒΆ
forward(
output: AnchorHeadOutput, data: Dict[str, Any]
) -> Dict[str, Tensor]
Compute the anchor detection loss and its terms.
Parameters:
-
output(AnchorHeadOutput) βThe head's raw output:
cls\((B, H, W, A_\text{loc} \cdot C)\),box\((B, H, W, A_\text{loc} \cdot 7)\) anddir_cls\((B, H, W, A_\text{loc} \cdot 2)\). -
data(Dict[str, Any]) βGround truth: packed
box\((K, 7)\) full-extent (the velocity columns of \((K, 9)\) boxes are ignored),label\((K,)\) (\(0\)-based classes) andbatch_box\((K,)\) per-box scene index.
Returns:
-
Dict[str, Tensor]βA dict with the scalar
lossand detachedcls_loss,box_loss,dir_loss.
MultiGroupAnchorHeadLoss [source] ΒΆ
MultiGroupAnchorHeadLoss(
num_classes: int,
*,
class_groups: Sequence[Sequence[int]],
point_cloud_range: Sequence[float],
voxel_size: Sequence[float],
anchor_sizes: Sequence[Sequence[float]],
anchor_bottom_heights: Sequence[float],
feature_map_stride: int,
matched_thresholds: Sequence[float],
unmatched_thresholds: Sequence[float],
anchor_rotations: Sequence[float] = (0.0, 1.57),
code_weights: Sequence[float] = (
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
0.0,
0.0,
),
cls_weight: float = 1.0,
loc_weight: float = 0.25,
pos_cls_weight: float = 1.0,
neg_cls_weight: float = 2.0,
focal_alpha: float = 0.25,
focal_gamma: float = 2.0,
match_height: bool = False,
encode_angle_by_sincos: bool = True,
)
Bases: Module
Loss of the MultiGroupAnchorHead (nuScenes SECOND, PointPillars).
Reference: Class-balanced Grouping and Sampling for Point Cloud 3D Object Detection (Zhu et al., 2019).
The separate-multihead anchor head has several RPN heads, each owning a disjoint class group over a shared feature map. Per scene each class's axis-aligned anchors are matched to that class's ground-truth boxes (assign_anchor_targets) using per-class IoU thresholds, giving per-anchor class labels (\(-1\) ignore, \(0\) background, \(\ge 1\) foreground) and residual box targets. Two terms are summed:
- Classification: per head, sigmoid focal loss over the one-hot labels restricted to that head's class columns, with positive / negative anchors weighted by
pos_cls_weight/neg_cls_weightand each scene normalized by its total positive count. - Box regression: per head, code-weighted \(L_1\) over the \(10\)-dim box code \((x, y, z, d_x, d_y, d_z, \cos\Delta\theta, \sin\Delta\theta, v_x, v_y)\). The heading is encoded as a \((\cos, \sin)\) residual, so no separate direction classifier is used.
Note
Ground-truth boxes of \((K, 9)\) columns carry the velocity \((v_x, v_y)\) the last two codes regress; with \((K, 7)\) boxes those targets are zero, so leave the last two code_weights at their default \(0\) to keep the velocity branch unsupervised.
Anchors are rebuilt in the constructor from the same geometry the head uses (generate_anchors), in the head's class-group order; the loss holds no reference to the model.
Parameters:
-
num_classes(int) βNumber of foreground classes (10 for nuScenes).
-
class_groups(Sequence[Sequence[int]]) βClass-index groups, one per RPN head (e.g.
[[0], [1, 2], ...]), matching the head'shead_class_groups; the classes in each group share one head, and the flattened groups must enumerate the classes \(0 \ldots C - 1\) in ascending order (the anchor / head layout). -
point_cloud_range(Sequence[float]) βRange \((x_\min, y_\min, z_\min, x_\max, y_\max, z_\max)\).
-
voxel_size(Sequence[float]) βVoxel size \((v_x, v_y, v_z)\) (used with
point_cloud_rangeto size the anchor grid). -
anchor_sizes(Sequence[Sequence[float]]) βPer-class box size \((d_x, d_y, d_z)\), one row per class.
-
anchor_bottom_heights(Sequence[float]) βPer-class anchor bottom \(z\), one per class.
-
feature_map_stride(int) βBEV feature-map stride of the head.
-
matched_thresholds(Sequence[float]) βPer-class IoU at or above which an anchor is a positive.
-
unmatched_thresholds(Sequence[float]) βPer-class IoU below which an anchor is background.
-
anchor_rotations(Sequence[float], default:(0.0, 1.57)) βYaw angles (radians) shared by all classes.
-
code_weights(Sequence[float], default:(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0)) βPer-code regression weights, shape \((10,)\); the last two (velocity) default to \(0\).
-
cls_weight(float, default:1.0) βWeight of the classification term in the total.
-
loc_weight(float, default:0.25) βWeight of the box-regression term in the total.
-
pos_cls_weight(float, default:1.0) βClassification weight of a positive anchor.
-
neg_cls_weight(float, default:2.0) βClassification weight of a background anchor.
-
focal_alpha(float, default:0.25) βFocal-loss positive/negative balance.
-
focal_gamma(float, default:2.0) βFocal-loss focusing exponent.
-
match_height(bool, default:False) βMatch anchors to boxes by the rotated 3D IoU when
True, otherwise by the bird's-eye IoU of the boxes snapped to their nearest axis-aligned orientation. -
encode_angle_by_sincos(bool, default:True) βEncode the heading residual as \((\cos, \sin)\) (always
Truefor this head).
Methods:
-
forwardβCompute the multihead anchor detection loss and its terms.
forward [source] ΒΆ
forward(
output: AnchorHeadMultiOutput, data: Dict[str, Any]
) -> Dict[str, Tensor]
Compute the multihead anchor detection loss and its terms.
Parameters:
-
output(AnchorHeadMultiOutput) βThe head's raw output: per-head
cls\((B, A_g, C_g)\) andbox\((B, A_g, 10)\) lists, plusmultihead_label_mapping(per-head 1-based global class indices). -
data(Dict[str, Any]) βGround truth: packed
box\((K, 7)\) full-extent, or \((K, 9)\) with the velocity in the last two columns,label\((K,)\) (\(0\)-based classes) andbatch_box\((K,)\) per-box scene index.
Returns:
-
Dict[str, Tensor]βA dict with the scalar
lossand detachedcls_loss,box_loss.
CenterHeadLoss [source] ΒΆ
CenterHeadLoss(
num_classes: int,
*,
point_cloud_range: Sequence[float],
voxel_size: Sequence[float],
feature_map_stride: int,
code_weights: Sequence[float],
cls_weight: float = 1.0,
loc_weight: float = 0.25,
iou_weight: float = 0.0,
gaussian_overlap: float = 0.1,
min_radius: int = 2,
num_max_objs: int = 500,
)
Bases: Module
Loss of the dense CenterHead (CenterPoint, Voxel Mamba).
Reference: Center-based 3D Object Detection and Tracking (Yin et al., 2021).
Ground-truth boxes are splatted onto a per-class BEV Gaussian heatmap and their regression code (sub-cell center offset, \(z\), log extents and \((\cos\theta, \sin\theta)\)) is recorded at each peak cell. The heatmap is supervised by the penalty-reduced center focal loss and the regression maps by a masked, code-weighted \(L_1\) read back at those cells. When the head emits an iou map an optional \(L_1\) term regresses it toward the 3D IoU (rescaled to \([-1, 1]\)) between the decoded prediction and its matched box.
Parameters:
-
num_classes(int) βNumber of heatmap channels.
-
point_cloud_range(Sequence[float]) βRange \((x_\min, y_\min, z_\min, x_\max, y_\max, z_\max)\).
-
voxel_size(Sequence[float]) βVoxel size \((v_x, v_y, v_z)\).
-
feature_map_stride(int) βStride from the voxel grid to the BEV feature map.
-
code_weights(Sequence[float]) βPer-code regression weight, length \(8\) (the head predicts no velocity codes).
-
cls_weight(float, default:1.0) βMultiplier on the heatmap focal loss.
-
loc_weight(float, default:0.25) βMultiplier on the summed regression loss.
-
iou_weight(float, default:0.0) βMultiplier on the optional IoU-branch loss (\(0\) disables it).
-
gaussian_overlap(float, default:0.1) βMin-overlap passed to the Gaussian-radius solver.
-
min_radius(int, default:2) βLower clamp on the integer splat radius.
-
num_max_objs(int, default:500) βPer-scene object-target capacity.
Methods:
-
forwardβCompute the dense center loss and its terms.
forward [source] ΒΆ
forward(
output: CenterHeadOutput, data: Dict[str, Any]
) -> Dict[str, Tensor]
Compute the dense center loss and its terms.
Parameters:
-
output(CenterHeadOutput) βHead maps
heatmap\((B, C, H, W)\),center\((B, 2, H, W)\),center_z\((B, 1, H, W)\),dim\((B, 3, H, W)\),rot\((B, 2, H, W)\) and optionallyiou\((B, 1, H, W)\). -
data(Dict[str, Any]) βPacked GT (
DataKeys.BOX,DataKeys.LABEL,DataKeys.BATCH_BOX).
Returns:
-
Dict[str, Tensor]βA dict with the scalar
lossand detachedheatmap_loss,box_loss(andiou_losswhen enabled).
VoxelNeXtHeadLoss [source] ΒΆ
VoxelNeXtHeadLoss(
class_groups: Sequence[Sequence[int]],
*,
point_cloud_range: Sequence[float],
voxel_size: Sequence[float],
feature_map_stride: int,
code_weights: Sequence[float],
cls_weight: float = 1.0,
loc_weight: float = 0.25,
gaussian_overlap: float = 0.1,
min_radius: int = 2,
num_max_objs: int = 500,
)
Bases: Module
Loss of the fully sparse VoxelNeXtHead.
Reference: VoxelNeXt: Fully Sparse VoxelNet for 3D Object Detection and Tracking (Chen et al., 2023).
The head predicts CenterPoint-style attributes directly on the occupied BEV voxels rather than a dense map, so targets are drawn only at those voxels: the per-class heatmap is a Gaussian in squared voxel distance and each object's regression code is anchored to its nearest occupied voxel. The heatmap is supervised by the penalty-reduced center focal loss and the gathered regression rows by a masked, code-weighted \(L_1\). Classes are split into groups, one sparse head each.
Parameters:
-
class_groups(Sequence[Sequence[int]]) βZero-based global class-index groups, one per head (e.g.
[[0], [1, 2], ...]). -
point_cloud_range(Sequence[float]) βRange \((x_\min, y_\min, z_\min, x_\max, y_\max, z_\max)\).
-
voxel_size(Sequence[float]) βVoxel size \((v_x, v_y, v_z)\).
-
feature_map_stride(int) βStride from the voxel grid to the BEV feature map.
-
code_weights(Sequence[float]) βPer-code regression weight, length \(8 + \text{extra}\) (e.g. \(10\) with velocity).
-
cls_weight(float, default:1.0) βMultiplier on the heatmap focal loss.
-
loc_weight(float, default:0.25) βMultiplier on the summed regression loss.
-
gaussian_overlap(float, default:0.1) βMin-overlap passed to the Gaussian-radius solver.
-
min_radius(int, default:2) βLower clamp on the integer splat radius.
-
num_max_objs(int, default:500) βPer-scene object-target capacity.
Methods:
-
forwardβCompute the sparse center loss summed over class groups.
forward [source] ΒΆ
forward(
output: VoxelNeXtHeadOutput, data: Dict[str, Any]
) -> Dict[str, Tensor]
Compute the sparse center loss summed over class groups.
Parameters:
-
output(VoxelNeXtHeadOutput) βA
VoxelNeXtHeadOutput: per-group listshm\((V, n_g)\),center\((V, 2)\),center_z\((V, 1)\),dim\((V, 3)\),rot\((V, 2)\),vel\((V, 2)\) and sharedvoxel_indices\((V, 3)\) with columns \((\text{batch}, y, x)\). -
data(Dict[str, Any]) βPacked GT (
DataKeys.BOX,DataKeys.LABEL,DataKeys.BATCH_BOX).
Returns:
-
Dict[str, Tensor]βA dict with the scalar
lossand detachedheatmap_loss,box_loss.
ChamferDistance [source] ΒΆ
Bases: Module
Module form of chamfer_distance.
The reconstruction criterion of the masked point modeling models, whose pretraining forward returns the (pred, target, pred_batch, target_batch) tuple this module takes.
Parameters:
-
norm(Literal['l1', 'l2'], default:'l2') βDistance variant,
"l1"(euclidean) or"l2"(squared euclidean).
Example
Methods:
-
forwardβCompute the Chamfer distance between the packed sets
pred\((N, 3)\) andtarget\((M, 3)\).
CornerLoss [source] ΒΆ
Bases: Module
Module form of corner_loss.
Parameters:
-
beta(float, default:1.0) βSmooth-\(L_1\) transition point.
-
reduction(Literal['none', 'sum', 'mean'], default:'mean') β"none"returns the per-box loss,"sum"its sum,"mean"its mean.
Example
Methods:
-
forwardβCompute the corner loss between matched boxes
pred_boxes\((N, 7)\) andgt_boxes\((N, 7)\).
GaussianFocalLoss [source] ΒΆ
Bases: Module
Module form of gaussian_focal_loss.
Takes heatmap logits and applies the sigmoid itself.
Parameters:
-
alpha(float, default:2.0) βFocusing exponent on the prediction.
-
gamma(float, default:4.0) βDown-weighting exponent on the soft negatives.
Example
Methods:
-
forwardβCompute the loss from heatmap logits and the Gaussian target of the same shape.
Poly1FocalLoss [source] ΒΆ
Poly1FocalLoss(
epsilon: float = 1.0,
alpha: Optional[float] = 0.25,
gamma: float = 2.0,
reduction: Literal["none", "sum", "mean"] = "mean",
)
Bases: Module
Module form of poly1_focal_loss, taking class indices.
The targets are either class indices \((\ldots)\), one-hot encoded over the \(C\) logits, or binary targets of the logits' shape. reduction="mean" averages over every element of the \((\ldots, C)\) loss.
Parameters:
-
epsilon(float, default:1.0) βWeight \(\epsilon\) of the Poly-1 term.
-
alpha(Optional[float], default:0.25) βPositive/negative balance \(\alpha\);
Nonedisables it. -
gamma(float, default:2.0) βFocusing exponent \(\gamma\).
-
reduction(Literal['none', 'sum', 'mean'], default:'mean') β"none"returns the per-element loss,"sum"its sum,"mean"its mean.
Example
Methods:
-
forwardβCompute the loss from logits \((\ldots, C)\) and class indices \((\ldots)\) or binary targets \((\ldots, C)\).
SigmoidFocalLoss [source] ΒΆ
SigmoidFocalLoss(
alpha: float = 0.25,
gamma: float = 2.0,
reduction: Literal["none", "sum", "mean"] = "mean",
)
Bases: Module
Module form of sigmoid_focal_loss.
Parameters:
-
alpha(float, default:0.25) βPositive/negative balance \(\alpha\).
-
gamma(float, default:2.0) βFocusing exponent \(\gamma\).
-
reduction(Literal['none', 'sum', 'mean'], default:'mean') β"none"returns the per-element loss,"sum"its sum,"mean"its mean.
Example
Methods:
-
forwardβCompute the focal loss from logits \((\ldots, C)\), binary targets \((\ldots, C)\) and row weights \((\ldots)\).
KPConvDeformRegularizer [source] ΒΆ
Bases: Module
Module form of kpconv_deform_regularizer.
Parameters:
-
fitting_power(float, default:1.0) βWeight of the whole term.
-
repulse_extent(float, default:1.0) βDistance, in units of
kp_sigma, below which kernel points repel each other.
Example
import torch
from torch_pointcloud.layers import KPConv
from torch_pointcloud.losses import KPConvDeformRegularizer
conv = KPConv(spatial_dim=3, in_channels=4, out_channels=8, kernel_size=15, kp_radius=1.0, kp_sigma=1.0, deformable=True)
pos = torch.rand(32, 3)
edge_index = torch.stack([torch.arange(32).repeat_interleave(4), torch.randint(0, 32, (128,))])
out = conv(torch.randn(32, 4), pos, edge_index)
KPConvDeformRegularizer(repulse_extent=1.2)(conv).shape # torch.Size([])
LovaszLoss [source] ΒΆ
Bases: Module
LovΓ‘sz-Softmax loss: a smooth surrogate for the mean-IoU objective.
Optimizes segmentation overlap directly, and is typically summed with cross-entropy. See The LovΓ‘sz-Softmax loss: A tractable surrogate for the optimization of the intersection-over-union measure in neural networks (Berman et al., 2018). The function form on probabilities is lovasz_softmax.
Parameters:
-
ignore_index(int, default:-1) βLabel value excluded from the loss.
-
classes(Literal['present', 'all'], default:'present') β"present"averages only classes present in the targets;"all"averages every class.
Methods:
-
forwardβCompute the loss from per-point logits \((N, C)\) and labels \((N,)\).
PointRCNNLoss [source] ΒΆ
PointRCNNLoss(
num_classes: int,
*,
mean_sizes: Union[Tensor, Sequence[Sequence[float]]],
gt_extra_width: Sequence[float] = (0.2, 0.2, 0.2),
point_cls_weight: float = 1.0,
point_box_weight: float = 1.0,
point_code_weights: Sequence[float] = (1.0,) * 8,
reg_fg_thresh: float = 0.55,
cls_fg_thresh: float = 0.6,
cls_bg_thresh: float = 0.45,
rcnn_cls_weight: float = 1.0,
rcnn_reg_weight: float = 1.0,
rcnn_corner_weight: float = 1.0,
rcnn_code_weights: Sequence[float] = (1.0,) * 7,
focal_alpha: float = 0.25,
focal_gamma: float = 2.0,
smooth_l1_beta: float = 1.0 / 9.0,
)
Bases: Module
Two-stage PointRCNN detection loss (per-point proposal head + ROI refinement head).
Reference: PointRCNN: 3D Object Proposal Generation and Detection from Point Cloud (Shi et al., 2019).
Stage 1 supervises the per-point head that generates proposals: every point inside a ground-truth box is foreground (points in the gap between a box and its enlarged copy are ignored), driving a sigmoid focal classification loss over the per-point class logits and a code-weighted smooth-\(L_1\) over the residual box encoding (center offset normalized by the class mean-size diagonal, log extents, \((\cos, \sin)\) heading). Stage 2 supervises the refinement head on the sampled ROIs the model forward produces: a binary cross-entropy on the confidence logit against an IoU-thresholded label, a code-weighted smooth-\(L_1\) on the ROI-canonical box residual, and an optional corner regularization (the mean smooth-\(L_1\) over the eight box corners, robust to the heading flip).
The stage-1 point targets are assigned inside the loss (points-in-box matching + mean-size residual encoding) from the packed ground truth; the stage-2 ROI-to-ground-truth matching (which is random) is done by the model forward, which passes the per-ROI max IoU and the canonically transformed matched box in its training-mode output. The loss holds no reference to the model.
Parameters:
-
num_classes(int) βNumber of foreground classes.
-
mean_sizes(Union[Tensor, Sequence[Sequence[float]]]) βPer-class mean box size \((d_x, d_y, d_z)\), shape \((\text{num\_classes}, 3)\).
-
gt_extra_width(Sequence[float], default:(0.2, 0.2, 0.2)) βPer-axis enlargement of a box when marking ignored points around it.
-
point_cls_weight(float, default:1.0) βWeight of the stage-1 classification term.
-
point_box_weight(float, default:1.0) βWeight of the stage-1 box-regression term.
-
point_code_weights(Sequence[float], default:(1.0,) * 8) βPer-code stage-1 regression weights, shape \((8,)\).
-
reg_fg_thresh(float, default:0.55) βROI-to-GT IoU at or above which a ROI's box regression is supervised.
-
cls_fg_thresh(float, default:0.6) βROI-to-GT IoU above which a ROI's confidence label is \(1\).
-
cls_bg_thresh(float, default:0.45) βROI-to-GT IoU below which a ROI's confidence label is \(0\); the band between
cls_bg_threshandcls_fg_threshis ignored. -
rcnn_cls_weight(float, default:1.0) βWeight of the stage-2 confidence term.
-
rcnn_reg_weight(float, default:1.0) βWeight of the stage-2 box-regression term.
-
rcnn_corner_weight(float, default:1.0) βWeight of the stage-2 corner regularization (\(0\) disables it).
-
rcnn_code_weights(Sequence[float], default:(1.0,) * 7) βPer-code stage-2 regression weights, shape \((7,)\).
-
focal_alpha(float, default:0.25) βStage-1 focal-loss positive/negative balance.
-
focal_gamma(float, default:2.0) βStage-1 focal-loss focusing exponent.
-
smooth_l1_beta(float, default:1.0 / 9.0) βSmooth-\(L_1\) transition point \(\beta\) for both regression terms.
Methods:
-
forwardβCompute the two-stage PointRCNN loss and its terms.
forward [source] ΒΆ
forward(
output: PointRCNNTrainOutput, data: Dict[str, Any]
) -> Dict[str, Tensor]
Compute the two-stage PointRCNN loss and its terms.
Parameters:
-
output(PointRCNNTrainOutput) βThe model's training-mode output: stage-1
point_cls_preds\((N, C)\),point_box_preds\((N, 8)\),point_pos\((N, 3)\),point_batch\((N,)\); stage-2rcnn_cls\((M, 1)\),rcnn_reg\((M, 7)\),rcnn_boxes\((M, 7)\),rois\((M, 7)\),gt_of_rois\((M, 7)\) (ROI-canonical matched box),gt_of_rois_src\((M, 7)\) (lidar-frame matched box) androi_ious\((M,)\). -
data(Dict[str, Any]) βPacked ground truth:
DataKeys.BOX\((K, 7)\) full-extent,DataKeys.LABEL\((K,)\) (\(0\)-based classes) andDataKeys.BATCH_BOX\((K,)\) per-box scene index.
Returns:
-
Dict[str, Tensor]βA dict with the scalar
lossand detachedpoint_cls_loss,point_box_loss,rcnn_cls_loss, -
Dict[str, Tensor]βrcnn_box_loss.
SumLoss [source] ΒΆ
Bases: Module
Weighted sum of several loss modules sharing a (logits, target) signature.
Each sub-loss is evaluated on the same inputs and the results are added (e.g. cross-entropy plus LovΓ‘sz), each scaled by its weight when weights is given.
Parameters:
-
losses(Sequence[Module]) βThe loss modules to sum.
-
weights(Optional[Sequence[float]], default:None) βOne scalar per loss;
Noneadds them as they are.
Methods:
-
forwardβCompute the weighted sum of the sub-losses.
ThreeDETRLoss [source] ΒΆ
ThreeDETRLoss(
num_classes: int,
*,
num_heading_bins: int,
matcher_cls_cost: float = 1.0,
matcher_giou_cost: float = 2.0,
matcher_center_cost: float = 0.0,
matcher_objectness_cost: float = 0.0,
giou_weight: float = 0.0,
sem_cls_weight: float = 1.0,
no_object_weight: float = 0.2,
angle_cls_weight: float = 0.1,
angle_reg_weight: float = 0.5,
center_weight: float = 5.0,
size_weight: float = 1.0,
)
Bases: Module
3DETR Hungarian set-prediction detection loss.
Reference: An End-to-End Transformer Model for 3D Object Detection (Misra et al., 2021).
Object queries are matched to ground-truth boxes one-to-one per scene by a Hungarian assignment whose cost combines the negative predicted class probability, the negative generalized 3D IoU, the \(L_1\) center distance (in the per-scene min-max normalized frame) and the negative objectness. Every decoder layer is supervised (the last layer plus the intermediate layers as auxiliary outputs), each with the same weighted objective, and the per-layer losses are summed:
- Semantic classification: per-query weighted cross-entropy over the \(C + 1\) class logits, with unmatched queries assigned the background slot and that slot down-weighted by
no_object_weight. - Center: \(L_1\) distance between matched query and box centers in the normalized frame.
- Size: \(L_1\) distance between matched query and box sizes in the normalized frame.
- Angle: cross-entropy on the heading bin plus a Huber loss on the in-bin residual, over matches.
- GIoU: \(1 - \text{gIoU}_{3D}\) between matched query and box, over matches.
- Cardinality: the \(L_1\) error between the count of non-background queries and the object count (logged only, never optimized).
Ground truth is read packed from the batch (full-extent \((K, 7)\) boxes with counter-clockwise headings, plus per-box classes) and densified per scene; the headings are negated into the model's native heading space before binning. The normalization uses the model's point_cloud_dims, so the loss holds no reference to the model.
Parameters:
-
num_classes(int) βNumber of semantic classes (the class head predicts one extra background slot).
-
num_heading_bins(int) βHeading-angle bins (\(1\) for axis-aligned ScanNet, \(12\) for oriented SUN RGB-D).
-
matcher_cls_cost(float, default:1.0) βMatcher weight on the negative class probability.
-
matcher_giou_cost(float, default:2.0) βMatcher weight on the negative generalized 3D IoU.
-
matcher_center_cost(float, default:0.0) βMatcher weight on the normalized-center \(L_1\) distance.
-
matcher_objectness_cost(float, default:0.0) βMatcher weight on the negative objectness.
-
giou_weight(float, default:0.0) βWeight of the GIoU term in the total; \(0\) (the published recipe) keeps the GIoU in the matcher only. The rotated-box GIoU (scenes with non-zero headings) is computed without gradients, so a non-zero weight trains only axis-aligned scenes. Its values differ slightly from implementations that skip the polygon intersection of box pairs whose corner-spanned axis-aligned rectangles do not overlap:
box3d_overlapintersects every pair exactly. -
sem_cls_weight(float, default:1.0) βWeight of the semantic-classification term in the total.
-
no_object_weight(float, default:0.2) βCross-entropy weight of the background class.
-
angle_cls_weight(float, default:0.1) βWeight of the heading-bin classification term in the total.
-
angle_reg_weight(float, default:0.5) βWeight of the heading-residual regression term in the total.
-
center_weight(float, default:5.0) βWeight of the center term in the total.
-
size_weight(float, default:1.0) βWeight of the size term in the total.
Methods:
-
forwardβCompute the 3DETR set-prediction loss and its terms.
forward [source] ΒΆ
forward(
output: ThreeDETRTrainOutput, data: Dict[str, Any]
) -> Dict[str, Tensor]
Compute the 3DETR set-prediction loss and its terms.
Parameters:
-
output(ThreeDETRTrainOutput) βA training-mode
ThreeDETRTrainOutput:aux_outputs(a per-decoder-layer list of head dicts withsem_cls_logits,sem_cls_prob,objectness_prob,center_normalized,center_unnormalized,size_normalized,size_unnormalized,angle_logits,angle_residual_normalized,angle_continuous) andpoint_cloud_dims. -
data(Dict[str, Any]) βPacked ground truth:
DataKeys.BOX\((K, 7)\) full-extent boxes with counter-clockwise headings,DataKeys.LABEL\((K,)\) per-box classes andDataKeys.BATCH_BOX\((K,)\) per-box scene index.
Returns:
-
Dict[str, Tensor]βA dict with the scalar
loss(summed over decoder layers) and detachedsem_cls_loss, -
Dict[str, Tensor]βcenter_loss,size_loss,angle_cls_loss,angle_reg_loss,giou_lossand the -
Dict[str, Tensor]βcardinality_errordiagnostic (each summed over decoder layers).
TNetOrthogonalityRegularizer [source] ΒΆ
Bases: Module
Module form of tnet_orthogonality_regularizer.
Example
import torch
from torch_pointcloud.layers import TNet
from torch_pointcloud.losses import TNetOrthogonalityRegularizer
ftnet = TNet(local_channels=[32, 64], global_channels=[32], k=16)
out = ftnet(torch.randn(10, 16), torch.arange(2).repeat_interleave(5))
TNetOrthogonalityRegularizer()(ftnet).shape # torch.Size([])
TransFusionHeadLoss [source] ΒΆ
TransFusionHeadLoss(
num_classes: int,
*,
point_cloud_range: Sequence[float],
voxel_size: Sequence[float],
feature_map_stride: int,
num_proposals: int = 200,
gaussian_overlap: float = 0.1,
min_radius: int = 2,
matcher_cls_cost: float = 0.15,
matcher_reg_cost: float = 0.25,
matcher_iou_cost: float = 0.25,
code_weights: Sequence[float] = (
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
1.0,
0.0,
0.0,
),
cls_weight: float = 1.0,
loc_weight: float = 0.25,
heatmap_weight: float = 1.0,
iou_weight: float = 0.5,
focal_alpha: float = 0.25,
focal_gamma: float = 2.0,
)
Bases: Module
Loss of the TransFusionHead (dense heatmap, matched classification, box, IoU rescore).
Reference: TransFusion: Robust LiDAR-Camera Fusion for 3D Object Detection with Transformers (Bai et al., 2022).
The head predicts a dense per-class BEV heatmap plus a fixed set of object queries, each carrying a class logit vector and a box code. Four terms are summed:
- Heatmap: the ground-truth box centers are splatted onto a per-class BEV Gaussian map and the dense heatmap is supervised by the penalty-reduced center focal loss.
- Classification: every scene's queries are decoded to boxes and matched to the ground truth by a per-scene Hungarian assignment (cost: focal classification + normalized center \(L_1\) + 3D IoU). The per-query class logits are then trained by sigmoid focal loss over one-hot targets (background for unmatched queries), normalized by the positive count.
- Box regression: code-weighted \(L_1\) over the \(10\)-dim box code \((x, y, z + d_z / 2, \log d_x, \log d_y, \log d_z, \sin\theta, \cos\theta, v_x, v_y)\) at the matched queries.
- IoU rescore: an \(L_1\) term regressing the per-query
ioubranch toward \(2 \cdot \text{IoU}_{3D} - 1\) between each matched query's decoded box and its ground-truth box.
The loss holds no reference to the model: the grid geometry is rebuilt from the constructor params.
Parameters:
-
num_classes(int) βNumber of foreground classes (heatmap channels and query logits).
-
point_cloud_range(Sequence[float]) βRange \((x_\min, y_\min, z_\min, x_\max, y_\max, z_\max)\).
-
voxel_size(Sequence[float]) βVoxel size \((v_x, v_y, v_z)\).
-
feature_map_stride(int) βStride from the voxel grid to the BEV feature map.
-
num_proposals(int, default:200) βNumber of object queries per scene.
-
gaussian_overlap(float, default:0.1) βMin-overlap passed to the Gaussian-radius solver.
-
min_radius(int, default:2) βLower clamp on the integer splat radius.
-
matcher_cls_cost(float, default:0.15) βWeight of the focal classification term in the matching cost.
-
matcher_reg_cost(float, default:0.25) βWeight of the normalized center-\(L_1\) term in the matching cost.
-
matcher_iou_cost(float, default:0.25) βWeight of the 3D-IoU term in the matching cost.
-
code_weights(Sequence[float], default:(1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0)) βPer-code regression weight, length \(10\); the last two (velocity) default to \(0\).
-
cls_weight(float, default:1.0) βMultiplier on the classification term.
-
loc_weight(float, default:0.25) βMultiplier on the box-regression term.
-
heatmap_weight(float, default:1.0) βMultiplier on the heatmap term.
-
iou_weight(float, default:0.5) βMultiplier on the IoU-rescore term.
-
focal_alpha(float, default:0.25) βFocal positive/negative balance (classification loss and matching cost).
-
focal_gamma(float, default:2.0) βFocal focusing exponent (classification loss and matching cost).
Methods:
-
forwardβCompute the TransFusion loss and its terms.
forward [source] ΒΆ
forward(
output: TransFusionHeadOutput, data: Dict[str, Any]
) -> Dict[str, Tensor]
Compute the TransFusion loss and its terms.
Parameters:
-
output(TransFusionHeadOutput) βThe head's raw output: per-query
center\((B, 2, Q)\),height\((B, 1, Q)\),dim\((B, 3, Q)\),rot\((B, 2, Q)\),vel\((B, 2, Q)\),iou\((B, 1, Q)\) andheatmap\((B, C, Q)\) class logits, plus the densedense_heatmap\((B, C, H, W)\). -
data(Dict[str, Any]) βPacked ground truth (
DataKeys.BOX\((K, 7)\) full-extent, or \((K, 9)\) with the velocity in the last two columns,DataKeys.LABEL\((K,)\) \(0\)-based,DataKeys.BATCH_BOX\((K,)\) per-box scene index).
Returns:
-
Dict[str, Tensor]βA dict with the scalar
lossand detachedheatmap_loss,cls_loss,box_loss,iou_loss.
VoteNetLoss [source] ΒΆ
VoteNetLoss(
num_classes: int,
*,
num_heading_bins: int,
num_size_clusters: int,
mean_sizes: Union[Tensor, List[List[float]]],
near_threshold: float = 0.3,
far_threshold: float = 0.6,
objectness_weights: Tuple[float, float] = (0.2, 0.8),
gt_vote_factor: int = 3,
loss_weight: float = 10.0,
)
Bases: Module
Loss of VoteNetDetection (vote, objectness, box, semantic).
Reference: Deep Hough Voting for 3D Object Detection in Point Clouds (Qi et al., 2019).
Every target is assigned inside the loss from the packed ground-truth boxes:
- Vote: each seed point inside a box votes for its center. A seed collects the centers of the first
gt_vote_factorboxes containing it (repeating the first when inside fewer), and the smooth-\(L_1\) distance of its closest predicted vote to its closest target vote is averaged over the seeds inside a box. - Objectness: proposals are matched to objects by nearest center. A proposal is positive when its nearest center is within
near_threshold, negative beyondfar_threshold, and ignored in the band between; a weighted two-way cross-entropy supervises the objectness logits. - Box and semantic: positives drive the center (symmetric Chamfer between proposal and object centers), heading (bin cross-entropy + normalized residual smooth-\(L_1\)), size (class cross-entropy + normalized residual smooth-\(L_1\)) and semantic-class terms, each averaged over the positives.
The library heading is counter-clockwise about \(+z\) while the heading head predicts the negated angle, so the heading bins are computed from \(-\theta\). Scenes are padded to a common object count with zero centers that take part in the proposal-to-object matching.
Parameters:
-
num_classes(int) βNumber of semantic classes.
-
num_heading_bins(int) βNumber of heading-angle bins (\(1\) for axis-aligned ScanNet, \(12\) for SUN RGB-D).
-
num_size_clusters(int) βNumber of size templates (the size class is the semantic class).
-
mean_sizes(Union[Tensor, List[List[float]]]) βPer-template mean box size, shape \((\text{num\_size\_clusters}, 3)\).
-
near_threshold(float, default:0.3) βDistance (meters) below which a proposal is a positive object match.
-
far_threshold(float, default:0.6) βDistance (meters) above which a proposal is a negative match.
-
objectness_weights(Tuple[float, float], default:(0.2, 0.8)) βCross-entropy class weights \([\text{negative}, \text{positive}]\).
-
gt_vote_factor(int, default:3) βNumber of target votes (containing-box centers) collected per seed.
-
loss_weight(float, default:10.0) βGlobal multiplier applied to the summed loss.
Methods:
-
forwardβCompute the VoteNet loss and its terms.
forward [source] ΒΆ
forward(
output: VoteNetOutput, data: Dict[str, Any]
) -> Dict[str, Tensor]
Compute the VoteNet loss and its terms.
Parameters:
-
output(VoteNetOutput) βThe model's raw output: dense head tensors (
objectness_scores,center,heading_scores,heading_residuals_normalized,size_scores,size_residuals_normalized,sem_cls_scores,pos_vote_aggr) as \((B, K, \cdot)\), plus the packed seedspos_seed\((S, 3)\),batch_seed\((S,)\) and their votespos_vote\((S \cdot \text{vote\_factor}, 3)\). -
data(Dict[str, Any]) βPacked ground truth:
DataKeys.BOX\((K, 7)\) full-extent boxes with counter-clockwise headings,DataKeys.LABEL\((K,)\) per-box classes andDataKeys.BATCH_BOX\((K,)\) per-box scene index.
Returns:
-
Dict[str, Tensor]βA dict with the scalar
lossand detachedvote_loss,objectness_loss,box_loss, -
Dict[str, Tensor]βcenter_loss,heading_cls_loss,heading_res_loss,size_cls_loss,size_res_loss, -
Dict[str, Tensor]βsem_cls_lossand theobj_accdiagnostic.
chamfer_distance [source] ΒΆ
chamfer_distance(
pred: Tensor,
target: Tensor,
*,
pred_batch: Optional[Tensor] = None,
target_batch: Optional[Tensor] = None,
norm: Literal["l1", "l2"] = "l2",
) -> Tensor
Symmetric Chamfer distance between packed point sets.
Set-to-set reconstruction objective introduced for point cloud generation in A Point Set Generation Network for 3D Object Reconstruction from a Single Image (Fan et al., 2017) and standard for masked point modeling pretraining. Every point is matched to its nearest neighbor in the other set of the same index (its group, its scene), and the squared euclidean distances are averaged over all points:
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.
The nearest neighbors come from the sided_distance kernel of kaolin when it is installed and the points are on CUDA, otherwise from the kNN kernel of torch_geometric (pyg-lib); either way without autograd, and only the matched pairs are differentiated, so the memory grows with \(N + M\) rather than \(N \cdot M\) and whole clouds fit. Without either kernel the search falls back to the dense distance matrix.
Parameters:
-
pred(Tensor) βPredicted points of every set, packed, shape \((N, 3)\).
-
target(Tensor) βTarget points of every set, packed, shape \((M, 3)\).
-
pred_batch(Optional[Tensor], default:None) βSet index of every predicted point (sorted), shape \((N,)\);
Nonefor a single set. -
target_batch(Optional[Tensor], default:None) βSet index of every target point (sorted), shape \((M,)\);
Nonefor a single set. -
norm(Literal['l1', 'l2'], default:'l2') βDistance variant,
"l1"(euclidean) or"l2"(squared euclidean).
Returns:
-
TensorβScalar Chamfer distance averaged over all points.
Shape
- pred: \((N, 3)\)
- target: \((M, 3)\)
- pred_batch: \((N,)\)
- target_batch: \((M,)\)
- 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)
batch = torch.arange(64).repeat_interleave(32)
loss = chamfer_distance(pred, target, pred_batch=batch, target_batch=batch)
loss.backward()
print(loss.shape)
corner_loss [source] ΒΆ
Per-box mean smooth-\(L_1\) distance between the eight corners of two box sets, robust to the heading flip.
The corner regularization of PointRCNN: 3D Object Proposal Generation and Detection from Point Cloud (Shi et al., 2019): each predicted corner is compared with the matching corner of the ground-truth box and of that box rotated by \(\pi\), and the closer of the two distances is penalized, so a heading off by half a turn is not punished as a position error.
Parameters:
-
pred_boxes(Tensor) βPredicted boxes \((c_x, c_y, c_z, d_x, d_y, d_z, \theta)\), shape \((N, 7)\).
-
gt_boxes(Tensor) βMatched ground-truth boxes, shape \((N, 7)\).
-
beta(float, default:1.0) βSmooth-\(L_1\) transition point.
Returns:
-
TensorβThe per-box loss, shape \((N,)\).
Shape
- pred_boxes: \((N, 7)\)
- gt_boxes: \((N, 7)\)
- output: \((N,)\)
gaussian_focal_loss [source] ΒΆ
gaussian_focal_loss(
pred: Tensor,
target: Tensor,
*,
alpha: float = 2.0,
gamma: float = 4.0,
eps: float = 0.0001,
) -> Tensor
Penalty-reduced focal loss over a Gaussian heatmap, normalized by the number of peaks.
The heatmap objective of Objects as Points (Zhou et al., 2019), used by the center-based detection heads (CenterPoint, VoxelNeXt, TransFusion). Cells whose target is exactly \(1\) are positives with the \((1 - p)^\alpha \log p\) term; every other cell is a soft negative whose \(p^\alpha \log(1 - p)\) term is down-weighted by \((1 - y)^\gamma\), so cells near a peak barely count. The sum is divided by the number of positives (or left as is when there is none).
Parameters:
-
pred(Tensor) βPredicted probabilities (post-sigmoid) of any shape; clamped to \([\varepsilon, 1 - \varepsilon]\).
-
target(Tensor) βGaussian heatmap target of the same shape, values in \([0, 1]\).
-
alpha(float, default:2.0) βFocusing exponent on the prediction.
-
gamma(float, default:4.0) βDown-weighting exponent on the soft negatives.
-
eps(float, default:0.0001) βClamp keeping the logarithms finite.
Returns:
-
TensorβThe scalar loss.
Shape
- pred: \((\ldots)\)
- target: \((\ldots)\)
- output: scalar
one_hot_foreground [source] ΒΆ
One-hot encode per-row class labels, dropping the background column.
Ignored (\(-1\)) and background (\(0\)) rows map to an all-zero row; a foreground row with label \(\ell \ge 1\) maps to a one-hot row on class \(\ell - 1\). The target format of sigmoid_focal_loss for the anchor and point heads.
Parameters:
-
labels(Tensor) βClass labels (\(-1\) ignore, \(0\) background, \(\ge 1\) foreground), shape \((\ldots)\).
-
num_classes(int) βNumber of foreground classes \(C\).
Returns:
-
TensorβOne-hot foreground targets, shape \((\ldots, C)\).
Shape
- labels: \((\ldots)\)
- output: \((\ldots, C)\)
poly1_focal_loss [source] ΒΆ
poly1_focal_loss(
logits: Tensor,
targets: Tensor,
*,
epsilon: float = 1.0,
alpha: Optional[float] = 0.25,
gamma: float = 2.0,
) -> Tensor
Poly-1 focal loss between per-class logits and binary targets (no reduction).
The focal loss plus its first Poly-1 correction term of PolyLoss: A Polynomial Expansion Perspective of Classification Loss Functions (Leng et al., 2022), with \(p\) the sigmoid of the logit, \(p_t = p\) on the positive entries and \(1 - p\) elsewhere,
Parameters:
-
logits(Tensor) βPer-class logits, shape \((\ldots, C)\).
-
targets(Tensor) βBinary (one-hot) targets of the same shape.
-
epsilon(float, default:1.0) βWeight \(\epsilon\) of the Poly-1 term.
-
alpha(Optional[float], default:0.25) βPositive/negative balance \(\alpha\);
Nonedisables it. -
gamma(float, default:2.0) βFocusing exponent \(\gamma\).
Returns:
-
TensorβThe per-element loss, shape \((\ldots, C)\).
Shape
- logits: \((\ldots, C)\)
- targets: \((\ldots, C)\)
- output: \((\ldots, C)\)
sigmoid_focal_loss [source] ΒΆ
sigmoid_focal_loss(
logits: Tensor,
targets: Tensor,
weights: Optional[Tensor] = None,
*,
alpha: float = 0.25,
gamma: float = 2.0,
) -> Tensor
Sigmoid focal loss between per-class logits and binary targets (no reduction).
The classification term of Focal Loss for Dense Object Detection (Lin et al., 2017), shared by the anchor, point and query heads: binary cross-entropy on the sigmoid of each logit, scaled by \(\alpha_t (1 - p_t)^\gamma\) so confident predictions contribute little. Same math as torchvision.ops.sigmoid_focal_loss, plus an optional per-row weight.
Parameters:
-
logits(Tensor) βPer-class logits, shape \((\ldots, C)\).
-
targets(Tensor) βBinary (one-hot) targets of the same shape.
-
weights(Optional[Tensor], default:None) βOptional weight of every row, shape \((\ldots)\), broadcast over the class dimension.
-
alpha(float, default:0.25) βPositive/negative balance \(\alpha\).
-
gamma(float, default:2.0) βFocusing exponent \(\gamma\).
Returns:
-
TensorβThe weighted per-element loss, shape \((\ldots, C)\).
Shape
- logits: \((\ldots, C)\)
- targets: \((\ldots, C)\)
- weights: \((\ldots)\)
- output: \((\ldots, C)\)
kpconv_deform_regularizer [source] ΒΆ
kpconv_deform_regularizer(
module: Module,
fitting_power: float = 1.0,
repulse_extent: float = 1.0,
) -> Tensor
Regularize the deformed kernel points of every deformable KPConv in module.
The point-to-point regularizer of KPConv: Flexible and Deformable Convolution for Point Clouds (Thomas et al., 2019): a fitting term pulls every deformed kernel point towards its closest input point, a repulsive term keeps the kernel points of one location further apart than repulse_extent. Both read the statistics the layers keep from their last forward pass (running_min_d2, running_deformed_kernel), so call it after the forward and before the backward of the same batch. Distances are expressed in units of each layer's kp_sigma.
Parameters:
-
module(Module) βModel holding the
KPConvlayers. -
fitting_power(float, default:1.0) βWeight of the whole term.
-
repulse_extent(float, default:1.0) βDistance, in units of
kp_sigma, below which kernel points repel each other.
Returns:
-
TensorβA scalar,
fitting_power * (2 * fitting + repulsive)summed over the deformable layers, zero when -
Tensorβmodulehas none.
Example
>>> import torch
>>> from torch_pointcloud.layers import KPConv
>>> from torch_pointcloud.losses import kpconv_deform_regularizer
>>> conv = KPConv(spatial_dim=3, in_channels=4, out_channels=8, kernel_size=15, kp_radius=1.0, kp_sigma=1.0, deformable=True)
>>> pos = torch.rand(32, 3)
>>> edge_index = torch.stack([torch.arange(32).repeat_interleave(4), torch.randint(0, 32, (128,))])
>>> out = conv(torch.randn(32, 4), pos, edge_index)
>>> kpconv_deform_regularizer(conv).shape
torch.Size([])
lovasz_softmax [source] ΒΆ
lovasz_softmax(
probas: Tensor,
labels: Tensor,
*,
classes: Literal["present", "all"] = "present",
ignore_index: int = -1,
) -> Tensor
Multi-class LovΓ‘sz-Softmax loss over per-point class probabilities.
The smooth surrogate of the mean-IoU objective of The LovΓ‘sz-Softmax loss: A tractable surrogate for the optimization of the intersection-over-union measure in neural networks (Berman et al., 2018); the function form of LovaszLoss, which takes logits.
Parameters:
-
probas(Tensor) βPer-point class probabilities (post-softmax), shape \((N, C)\).
-
labels(Tensor) βPer-point ground-truth labels, shape \((N,)\).
-
classes(Literal['present', 'all'], default:'present') β"present"averages only classes present inlabels;"all"averages every class. -
ignore_index(int, default:-1) βLabel value excluded from the loss.
Returns:
-
TensorβThe scalar loss (zero when no point or no class contributes).
Shape
- probas: \((N, C)\)
- labels: \((N,)\)
- output: scalar
hungarian_match [source] ΒΆ
One-to-one assignment of predictions to targets minimizing the total cost (Hungarian algorithm).
The matcher of the set-prediction detectors (3DETR, TransFusion): every target is matched to exactly one prediction and the sum of the matched costs is minimal. The cost matrix is built by the caller (e.g. a weighted sum of a classification, a center-distance and an IoU term) and must be finite.
Parameters:
-
cost(Tensor) βPairwise cost of every prediction / target pair, shape \((Q, M)\).
Returns:
-
TensorβA tuple
(pred_indices, target_indices)of long tensors oncost.device, each of length -
Tensorβ\(\min(Q, M)\), such that
pred_indices[i]is matched totarget_indices[i]; both are empty when -
Tuple[Tensor, Tensor]βeither side is.
Shape
- cost: \((Q, M)\)
- pred_indices: \((\min(Q, M),)\)
- target_indices: \((\min(Q, M),)\)
hungarian_match_batched [source] ΒΆ
One-to-one assignment of every scene's predictions to its targets, with a single device transfer.
The batched form of hungarian_match for a padded cost tensor: scene \(i\) uses the first num_targets[i] target columns of cost[i]. The whole tensor is moved to the CPU once and every scene is solved there, so a loop over scenes (and decoder layers, when they are stacked along the batch dimension) costs one synchronization instead of one per scene.
Parameters:
-
cost(Tensor) βPairwise costs, shape \((N, Q, M_\max)\); the columns beyond a scene's target count are ignored and may hold anything finite or not.
-
num_targets(Sequence[int]) βNumber of valid target columns of each scene, length \(N\).
Returns:
-
TensorβThe matched target index of every prediction, shape \((N, Q)\) long on
cost.device, \(-1\) where the -
Tensorβprediction is unmatched.
Shape
- cost: \((N, Q, M_\max)\)
- output: \((N, Q)\)
tnet_orthogonality_regularizer [source] ΒΆ
Pull the transforms predicted by every T-Net in module towards an orthogonal matrix.
The feature-transform regularization of PointNet: Deep Learning on Point Sets for 3D Classification and Segmentation (Qi et al., 2017), \(\lVert I - A A^\top \rVert_F\) averaged over the samples of the batch and summed over the T-Nets found in module. Pass the T-Net (or the sub-module) to regularize, e.g. a PointNet's feature transform model.ftnet to leave its input transform free, as the PointNet paper does.
The matrices are read from the running_transform the layers keep from their last forward pass, so call it after the forward and before the backward of the same batch.
Parameters:
-
module(Module) βA
TNet/DynamicTNet, or a module containing some.
Returns:
-
TensorβThe scalar regularizer (zero when
moduleholds no T-Net).
Raises:
-
RuntimeErrorβIf a T-Net has no stored transform (no forward pass yet, or
track_running_stats=False).