tensorflow/models · error · ValueError

Batch size cannot be more than 1.

Error message

Batch size cannot be more than 1.

What it means

Batch-size guard in the semantic-segmentation serve path: the pre-processing pipeline processes images one at a time via tf.map_fn over per-image input specs, so a configured _batch_size > 1 is incompatible and rejected rather than silently producing mis-batched outputs.

Solutions

  1. Export with batch size 1 for the semantic segmentation serving module.
  2. Reshape the serving input signature to a single image per request.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at official/vision/serving/semantic_segmentation.py:95 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/3baa043c79d01b21. Report an issue: GitHub.

Appendix: source

Thrown at official/vision/serving/semantic_segmentation.py:95

            dtype=tf.float32,
        )
        image_info_spec = tf.TensorSpec(shape=[4, 2], dtype=tf.float32)

        images, image_info = tf.nest.map_structure(
            tf.identity,
            tf.map_fn(
                self._build_inputs,
                elems=images,
                fn_output_signature=(images_spec, image_info_spec),
                parallel_iterations=32))

    outputs = self.inference_step(images)

    # Optionally resize prediction to the input image size.
    if self.params.task.export_config.rescale_output:
      logits = outputs['logits']
      if logits.shape[0] != 1:
        raise ValueError('Batch size cannot be more than 1.')

      image_shape = tf.cast(image_info[0, 0, :], tf.int32)  # pyrefly: ignore[unsupported-operation]
      if self.params.task.train_data.preserve_aspect_ratio:
        rescale_size = tf.cast(
            tf.math.ceil(image_info[0, 1, :] / image_info[0, 2, :]), tf.int32)  # pyrefly: ignore[unsupported-operation]
        offsets = tf.cast(image_info[0, 3, :], tf.int32)  # pyrefly: ignore[unsupported-operation]
        logits = tf.image.resize(logits, rescale_size, method='bilinear')
        outputs['logits'] = tf.image.crop_to_bounding_box(
            logits, offsets[0], offsets[1], image_shape[0], image_shape[1])
      else:
        outputs['logits'] = tf.image.resize(
            logits, [image_shape[0], image_shape[1]], method='bilinear')
    else:
      outputs['logits'] = tf.image.resize(
          outputs['logits'], self._input_image_size, method='bilinear')

    if image_info is not None:
      outputs.update({'image_info': image_info})

View on GitHub (pinned to e006f5f0d5)