py123d_detection.torch.models.common.util

Box encoding and the small numerical helpers both heads use.

A detection head does not regress a box in metres and radians. Sizes are learned in log space, so a 20 cm error on a pedestrian and on a bus cost the same relatively, and yaw is learned as a (sin, cos) pair, so the discontinuity at ±pi does not sit in the middle of the loss. normalize_bbox() and denormalize_bbox() move between that encoding and world units.

Functions

denormalize_bbox(normalized_bboxes[, pc_range])

The head's encoding back into world units, the inverse of normalize_bbox().

inverse_sigmoid(x[, eps])

The inverse of sigmoid, clamped so that 0 and 1 do not become infinite.

normalize_bbox(bboxes[, pc_range])

World-unit boxes into the head's encoding.

pos2posemb3d(pos[, num_pos_feats, temperature])

Sinusoidal embedding of 3D positions: (B, N, 3) in, (B, N, 3 * num_pos_feats) out.

py123d_detection.torch.models.common.util.normalize_bbox(bboxes, pc_range=None)[source]

World-unit boxes into the head’s encoding.

[cx, cy, cz, w, l, h, rot, vx, vy] in, and [cx, cy, log w, log l, cz, log h, sin rot, cos rot, vx, vy] out. Note the column order: cz sits between log l and log h rather than with cx and cy. That is official PETR’s layout, and the code weights are indexed by it.

Boxes without velocity (7 columns) come back without the last two.

Centres stay in world coordinates. pc_range is accepted so that call sites can pass it uniformly, but nothing here reads it: the head’s forward pass already denormalises its own sigmoid outputs into world units, which is what official PETR does.

py123d_detection.torch.models.common.util.denormalize_bbox(normalized_bboxes, pc_range=None)[source]

The head’s encoding back into world units, the inverse of normalize_bbox().

[cx, cy, log w, log l, cz, log h, sin rot, cos rot, vx, vy] in, [cx, cy, cz, w, l, h, rot, vx, vy] out.

py123d_detection.torch.models.common.util.inverse_sigmoid(x, eps=1e-5)[source]

The inverse of sigmoid, clamped so that 0 and 1 do not become infinite.

Parameters:

eps – How far from the open interval the input may sit before it is pulled back in.

py123d_detection.torch.models.common.util.pos2posemb3d(pos, num_pos_feats=128, temperature=10000)[source]

Sinusoidal embedding of 3D positions: (B, N, 3) in, (B, N, 3 * num_pos_feats) out.

The three axes are concatenated in the order y, x, z, which is what official PETR does. The order is arbitrary but not free to change: a checkpoint’s query embedding learned one of them.

Parameters:
  • num_pos_feats – Width of the embedding per axis, before concatenation.

  • temperature – Wavelength spread of the sinusoids, as in the original transformer.