tensorflow/models · error · ValueError

batch_size has to be an integer when is_static isTrue.

Error message

batch_size has to be an integer when is_static isTrue.

What it means

Error "batch_size has to be an integer when is_static isTrue." thrown in tensorflow/models.

Source

Thrown at official/vision/utils/object_detection/balanced_positive_negative_sampler.py:150

    Returns:
      sampled_idx_indicator: boolean tensor of shape [N], True for entries which
        are sampled. It ensures the length of output of the subsample is always
        batch_size, even when number of examples set to True in indicator is
        less than batch_size.

    Raises:
      ValueError: if labels and indicator are not 1D boolean tensors.
    """
    # Check if indicator and labels have a static size.
    if not indicator.shape.is_fully_defined():
      raise ValueError('indicator must be static in shape when is_static is'
                       'True')
    if not labels.shape.is_fully_defined():
      raise ValueError('labels must be static in shape when is_static is'
                       'True')
    if not isinstance(batch_size, int):
      raise ValueError('batch_size has to be an integer when is_static is'
                       'True.')

    input_length = tf.shape(input=indicator)[0]

    # Set the number of examples set True in indicator to be at least
    # batch_size.
    num_true_sampled = tf.reduce_sum(
        input_tensor=tf.cast(indicator, tf.float32))
    additional_false_sample = tf.less_equal(
        tf.cumsum(tf.cast(tf.logical_not(indicator), tf.float32)),
        batch_size - num_true_sampled)
    indicator = tf.logical_or(indicator, additional_false_sample)

    # Shuffle indicator and label. Need to store the permutation to restore the
    # order post sampling.
    permutation = tf.random.shuffle(tf.range(input_length))
    indicator = ops.matmul_gather_on_zeroth_axis(
        tf.cast(indicator, tf.float32), permutation)

View on GitHub (pinned to e006f5f0d5)

Solutions

  1. Pass batch_size as a Python integer when is_static=True.
  2. Set is_static=False if batch_size must be a dynamic tensor.

When it happens

Trigger: Thrown at official/vision/utils/object_detection/balanced_positive_negative_sampler.py:150 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/d2fe7cdfb55136cc. Report an issue: GitHub.