open-mmlab/mmdetection · error · ValueError

Unknown pos_tensor shape(-1):{}

Error message

Unknown pos_tensor shape(-1):{}

What it means

coordinate_to_encoding only encodes 2D points (last dim == 2, sine embedding) or 4D boxes (last dim == 4, CSLS embedding). A coord_tensor whose last dimension is anything else (e.g. 3, 5, 6) raises ValueError.

Source

Thrown at mmdet/models/layers/transformer/utils.py:95

                        dim=-1).flatten(2)
    pos_y = torch.stack((pos_y[..., 0::2].sin(), pos_y[..., 1::2].cos()),
                        dim=-1).flatten(2)
    if coord_tensor.size(-1) == 2:
        pos = torch.cat((pos_y, pos_x), dim=-1)
    elif coord_tensor.size(-1) == 4:
        w_embed = coord_tensor[..., 2] * scale
        pos_w = w_embed[..., None] / dim_t
        pos_w = torch.stack((pos_w[..., 0::2].sin(), pos_w[..., 1::2].cos()),
                            dim=-1).flatten(2)

        h_embed = coord_tensor[..., 3] * scale
        pos_h = h_embed[..., None] / dim_t
        pos_h = torch.stack((pos_h[..., 0::2].sin(), pos_h[..., 1::2].cos()),
                            dim=-1).flatten(2)

        pos = torch.cat((pos_y, pos_x, pos_w, pos_h), dim=-1)
    else:
        raise ValueError('Unknown pos_tensor shape(-1):{}'.format(
            coord_tensor.size(-1)))
    return pos


def inverse_sigmoid(x: Tensor, eps: float = 1e-5) -> Tensor:
    """Inverse function of sigmoid.

    Args:
        x (Tensor): The tensor to do the inverse.
        eps (float): EPS avoid numerical overflow. Defaults 1e-5.
    Returns:
        Tensor: The x has passed the inverse function of sigmoid, has the same
        shape with input.
    """
    x = x.clamp(min=0, max=1)
    x1 = x.clamp(min=eps)
    x2 = (1 - x).clamp(min=eps)
    return torch.log(x1 / x2)

View on GitHub (pinned to cfd5d3a985)

Solutions

  1. Inspect coord_tensor.shape[-1] and reduce/fix it to 2 (point) or 4 (box)
  2. If passing boxes, ensure they are exactly 4 values (cxcywh or xyxy), not concatenated with extra features
  3. For genuinely higher-dim coords, write a custom encoding instead of this utility

Example fix

# before
enc = coordinate_to_encoding(pts)  # pts.shape[-1] == 3
# after
enc = coordinate_to_encoding(pts[..., :2])
Defensive patterns

Strategy: validation

Validate before calling

assert coord_tensor.size(-1) in (2, 4), coord_tensor.shape

Type guard

def is_encodable_coord(t): return t.dim() >= 2 and t.size(-1) in (2, 4)

Prevention

When it happens

Trigger: Calling coordinate_to_encoding(pos_tensor) with shape [...,3] or [...,5+], typically from a transformer forward that passes reference points or RoI coords of unexpected dimensionality.

Common situations: Custom detection heads that feed 3D keypoints or 6D poses into a positional encoding meant for 2D detection; mismatched box encoding (xyxy vs cxcywh vs extra dims) between components.

Related errors


AI-assisted analysis of open-mmlab/mmdetection@cfd5d3a985 (2026-08-27). Data as JSON: /api/errors/8e38f81e40c0ddee. Report an issue: GitHub.