tensorflow/models · error · ValueError

encoded_boxes.shape[-1] is

Error message

encoded_boxes.shape[-1] is {:d}, but must be 4.

What it means

Shape validation in decode_boxes: encoded_boxes' last dimension is not 4, so box encodings (ty, tx, th, tw) cannot be decoded against anchors. Upstream box predictions have an unexpected shape.

Solutions

  1. Check the model's box prediction head outputs 4 channels per anchor
  2. Reshape encoded_boxes to [..., 4] before decoding
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at official/legacy/detection/utils/box_utils.py:324 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24). Data as JSON: /api/errors/ce3a7de902c373d9. Report an issue: GitHub.

Appendix: source

Thrown at official/legacy/detection/utils/box_utils.py:324

    return encoded_boxes


def decode_boxes(encoded_boxes, anchors, weights=None):
  """Decode boxes.

  Args:
    encoded_boxes: a tensor whose last dimension is 4 representing the
      coordinates of encoded boxes in ymin, xmin, ymax, xmax order.
    anchors: a tensor whose shape is the same as, or `broadcastable` to `boxes`,
      representing the coordinates of anchors in ymin, xmin, ymax, xmax order.
    weights: None or a list of four float numbers used to scale coordinates.

  Returns:
    encoded_boxes: a tensor whose shape is the same as `boxes` representing the
      decoded box targets.
  """
  if encoded_boxes.shape[-1] != 4:
    raise ValueError('encoded_boxes.shape[-1] is {:d}, but must be 4.'.format(
        encoded_boxes.shape[-1]))

  with tf.name_scope('decode_boxes'):
    encoded_boxes = tf.cast(encoded_boxes, dtype=anchors.dtype)
    dy = encoded_boxes[..., 0:1]
    dx = encoded_boxes[..., 1:2]
    dh = encoded_boxes[..., 2:3]
    dw = encoded_boxes[..., 3:4]
    if weights:
      dy /= weights[0]
      dx /= weights[1]
      dh /= weights[2]
      dw /= weights[3]
    dh = tf.math.minimum(dh, BBOX_XFORM_CLIP)
    dw = tf.math.minimum(dw, BBOX_XFORM_CLIP)

    anchor_ymin = anchors[..., 0:1]
    anchor_xmin = anchors[..., 1:2]

View on GitHub (pinned to e006f5f0d5)