tensorflow/models · error · ValueError

The last dimension of predicted scores should be divisible b

Error message

The last dimension of predicted scores should be divisible by {num_anchors_per_locations}.

What it means

Error "The last dimension of predicted scores should be divisible by {num_anchors_per_locations}." thrown in tensorflow/models.

Source

Thrown at official/vision/modeling/layers/detection_generator.py:850

  levels = list(raw_scores.keys())
  min_level = int(min(levels))
  max_level = int(max(levels))
  batch_size = tf.shape(raw_scores[str(min_level)])[0]

  num_anchors_per_locations_times_4 = (
      raw_boxes[str(min_level)].get_shape().as_list()[-1]
  )
  if num_anchors_per_locations_times_4 % 4 != 0:
    raise ValueError(
        'The last dimension of predicted boxes should be divisible by 4.'
    )

  num_anchors_per_locations = num_anchors_per_locations_times_4 // 4
  num_classes_times_anchors_per_location = (
      raw_scores[str(min_level)].get_shape().as_list()[-1]
  )
  if num_classes_times_anchors_per_location % num_anchors_per_locations != 0:
    raise ValueError(
        'The last dimension of predicted scores should be divisible by'
        f' {num_anchors_per_locations}.'
    )
  num_classes = (
      num_classes_times_anchors_per_location // num_anchors_per_locations
  )
  config.update({'num_classes': num_classes})

  for i in range(min_level, max_level + 1):
    scores.append(tf.reshape(raw_scores[str(i)], [batch_size, -1, num_classes]))
    boxes.append(tf.reshape(raw_boxes[str(i)], [batch_size, -1, 4]))
    anchors.append(tf.reshape(anchor_boxes[str(i)], [-1, 4]))
  scores = tf.sigmoid(tf.concat(scores, 1))
  boxes = tf.concat(boxes, 1)
  anchors = tf.concat(anchors, 0)

  ycenter_a = (anchors[..., 0] + anchors[..., 2]) / 2
  xcenter_a = (anchors[..., 1] + anchors[..., 3]) / 2

View on GitHub (pinned to e006f5f0d5)

Solutions

  1. Make the last dimension of the predicted scores divisible by num_anchors_per_locations.
  2. Check the score prediction head and anchor configuration so scores align with anchors per location.

When it happens

Trigger: Thrown at official/vision/modeling/layers/detection_generator.py:850 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/d2f3ab04b4ddb877. Report an issue: GitHub.