{"record":{"id":"e86d81f3d2b98ed9","repo":"tensorflow/models","slug":"len-means-must-match-the-number-of-channels","errorCode":null,"errorMessage":"len(means) must match the number of channels","messagePattern":"len\\(means\\) must match the number of channels","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/legacy/image_classification/preprocessing.py","lineNumber":65,"sourceCode":"  Args:\n    image_bytes: a tensor of size [height, width, C].\n    means: a C-vector of values to subtract from each channel.\n    num_channels: number of color channels in the image that will be distorted.\n    dtype: the dtype to convert the images to. Set to `None` to skip conversion.\n\n  Returns:\n    the centered image.\n\n  Raises:\n    ValueError: If the rank of `image` is unknown, if `image` has a rank other\n      than three or if the number of channels in `image` doesn't match the\n      number of values in `means`.\n  \"\"\"\n  if image_bytes.get_shape().ndims != 3:\n    raise ValueError('Input must be of size [height, width, C>0]')\n\n  if len(means) != num_channels:\n    raise ValueError('len(means) must match the number of channels')\n\n  # We have a 1-D tensor of means; convert to 3-D.\n  # Note(b/130245863): we explicitly call `broadcast` instead of simply\n  # expanding dimensions for better performance.\n  means = tf.broadcast_to(means, tf.shape(image_bytes))\n  if dtype is not None:\n    means = tf.cast(means, dtype=dtype)\n\n  return image_bytes - means\n\n\ndef standardize_image(\n    image_bytes: tf.Tensor,\n    stddev: Tuple[float, ...],\n    num_channels: int = 3,\n    dtype: tf.dtypes.DType = tf.float32,\n) ->  tf.Tensor:\n  \"\"\"Divides the given stddev from each image channel.","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/legacy/image_classification/preprocessing.py#L47-L83","documentation":"Length guard in mean_image_subtraction: len(means) differs from the image's channel count C, so channel-wise mean subtraction is ill-defined. The means list (e.g. not the standard 3-element ImageNet means) mismatches the image.","triggerScenarios":"Thrown at official/legacy/image_classification/preprocessing.py:65 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Provide a means list with exactly one value per channel (3 for RGB)","Pass num_channels matching the means length"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e006f5f0d534913e49c1f1dae87364039fa607e2","analyzedAt":"2026-08-24T14:09:15.576Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}