anchors
Axis-aligned anchor generation and target assignment for the anchor-based BEV detectors.
A packed-format port of the anchor generator and target assigner of open-mmlab/OpenPCDet, shared by the AnchorHead (which decodes against the anchors) and the AnchorHeadLoss (which assigns targets to them).
Classes:
-
AnchorTargetsβPer-anchor training targets from
assign_anchor_targets.
Functions:
-
generate_anchorsβGenerate axis-aligned anchors for a single class over a BEV feature map.
-
assign_anchor_targetsβAssign classification and box-regression targets to a single class group of axis-aligned anchors.
generate_anchors [source] ΒΆ
generate_anchors(
point_cloud_range: Sequence[float],
feature_map_size: Tuple[int, int],
anchor_sizes: Sequence[Sequence[float]],
anchor_rotations: Sequence[float],
anchor_bottom_heights: Sequence[float],
*,
dtype: dtype = float32,
) -> Tensor
Generate axis-aligned anchors for a single class over a BEV feature map.
A port of OpenPCDet's AnchorGenerator with align_center=False: anchor centers are placed on a grid spanning point_cloud_range (endpoints inclusive), then anchor_bottom_heights are lifted by half the box height to box centers.
Parameters:
-
point_cloud_range(Sequence[float]) βRange \((x_\min, y_\min, z_\min, x_\max, y_\max, z_\max)\).
-
feature_map_size(Tuple[int, int]) βBEV feature map size \((n_x, n_y)\).
-
anchor_sizes(Sequence[Sequence[float]]) βBox sizes \((d_x, d_y, d_z)\), one row per size template.
-
anchor_rotations(Sequence[float]) βYaw angles (radians).
-
anchor_bottom_heights(Sequence[float]) βAnchor bottom \(z\) per height template.
-
dtype(dtype, default:float32) βAnchor dtype.
Returns:
-
TensorβAnchors \((1, n_y, n_x, n_\text{size}, n_\text{rot}, 7)\) as \((x, y, z, dx, dy, dz, \theta)\).
assign_anchor_targets [source] ΒΆ
assign_anchor_targets(
anchors: Tensor,
gt_boxes: Tensor,
gt_labels: Tensor,
*,
matched_threshold: float,
unmatched_threshold: float,
match_height: bool = False,
gt_batch: Optional[Tensor] = None,
batch_size: Optional[int] = None,
) -> AnchorTargets
Assign classification and box-regression targets to a single class group of axis-aligned anchors.
Each anchor is matched to the ground-truth box of highest IoU: IoU \(\ge\) matched_threshold makes it a positive carrying that box's label, IoU \(<\) unmatched_threshold makes it background, and anything in between is ignored. Each ground-truth box additionally force-matches its single highest-IoU anchor, so a box with no anchor above threshold still receives one positive. Positive anchors' regression targets are the residual encoding of their matched box against the anchor (the inverse of decode_box_residuals); the columns a box carries beyond its geometry (a velocity) are copied into the targets as they are.
Callers with several class groups (one anchor set per class) invoke this once per group with that class's anchors, ground truth, and thresholds, then concatenate the results. With gt_batch (the scene index of every box) and batch_size, one call matches the anchors in every scene of a batch: the IoU runs once over the whole batch and the outputs gain a leading scene dimension.
Parameters:
-
anchors(Tensor) βAnchors \((x, y, z, d_x, d_y, d_z, \theta)\) for one class group, shape \((A, 7)\).
-
gt_boxes(Tensor) βGround-truth boxes \((c_x, c_y, c_z, d_x, d_y, d_z, \theta, \ldots)\), shape \((G, 7 + C)\).
-
gt_labels(Tensor) βGround-truth class labels (\(1\)-based foreground indices), shape \((G,)\).
-
matched_threshold(float) βIoU at or above which an anchor becomes a positive.
-
unmatched_threshold(float) βIoU below which an anchor becomes background.
-
match_height(bool, default:False) βMatch by the rotated 3D IoU when
True, otherwise by the bird's-eye IoU of the boxes snapped to their nearest axis-aligned orientation (boxes_iou_nearest_bev). -
gt_batch(Optional[Tensor], default:None) βScene index of every ground-truth box, shape \((G,)\), to match a whole batch at once.
-
batch_size(Optional[int], default:None) βNumber of scenes \(B\) when
gt_batchis given.
Returns:
-
AnchorTargetsβA
TypedDictwithcls_labels\((A,)\) (\(-1\) ignore, \(0\) background, \(\ge 1\) foreground class) and -
AnchorTargetsβbox_reg_targets\((A, 7 + C)\) (residual encodings, zero for non-positive anchors); \((B, A)\) and -
AnchorTargetsβ\((B, A, 7 + C)\) with
gt_batch.
Shape
- anchors: \((A, 7)\)
- gt_boxes: \((G, 7 + C)\)
- gt_labels: \((G,)\)
- cls_labels: \((A,)\)
- box_reg_targets: \((A, 7 + C)\)
Example
>>> anchors = torch.tensor([[0.0, 0.0, 0.0, 4.0, 2.0, 1.5, 0.0], [20.0, 0.0, 0.0, 4.0, 2.0, 1.5, 0.0]])
>>> gt_boxes = torch.tensor([[0.0, 0.0, 0.0, 4.0, 2.0, 1.5, 0.0]])
>>> gt_labels = torch.tensor([1])
>>> out = assign_anchor_targets(anchors, gt_boxes, gt_labels, matched_threshold=0.6, unmatched_threshold=0.45)
>>> out["cls_labels"].tolist()
[1, 0]