tensorflow/models · error · ValueError

len(stddev) must match the number of channels

Error message

len(stddev) must match the number of channels

What it means

Length guard in standardize_image: len(stddev) does not equal the image's channel count, so per-channel division by standard deviation cannot be applied. The stddev constants mismatch the image channels.

Solutions

  1. Use a stddev list with one value per channel (e.g. the 3-element ImageNet stddevs)
  2. Match num_channels to len(stddev)
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at official/legacy/image_classification/preprocessing.py:109 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/13a6bb57bf29e6fe. Report an issue: GitHub.

Appendix: source

Thrown at official/legacy/image_classification/preprocessing.py:109

  Args:
    image_bytes: a tensor of size [height, width, C].
    stddev: a C-vector of values to divide from each channel.
    num_channels: number of color channels in the image that will be distorted.
    dtype: the dtype to convert the images to. Set to `None` to skip conversion.

  Returns:
    the centered image.

  Raises:
    ValueError: If the rank of `image` is unknown, if `image` has a rank other
      than three or if the number of channels in `image` doesn't match the
      number of values in `stddev`.
  """
  if image_bytes.get_shape().ndims != 3:
    raise ValueError('Input must be of size [height, width, C>0]')

  if len(stddev) != num_channels:
    raise ValueError('len(stddev) must match the number of channels')

  # We have a 1-D tensor of stddev; convert to 3-D.
  # Note(b/130245863): we explicitly call `broadcast` instead of simply
  # expanding dimensions for better performance.
  stddev = tf.broadcast_to(stddev, tf.shape(image_bytes))
  if dtype is not None:
    stddev = tf.cast(stddev, dtype=dtype)

  return image_bytes / stddev


def normalize_images(features: tf.Tensor,
                     mean_rgb: Tuple[float, ...] = MEAN_RGB,
                     stddev_rgb: Tuple[float, ...] = STDDEV_RGB,
                     num_channels: int = 3,
                     dtype: tf.dtypes.DType = tf.float32,
                     data_format: Text = 'channels_last') -> tf.Tensor:
  """Normalizes the input image channels with the given mean and stddev.

View on GitHub (pinned to e006f5f0d5)