{"record":{"id":"2dbbf35cfe77deb4","repo":"huggingface/transformers","slug":"mean-must-have-num-channels-elements-if-it-is-an","errorCode":null,"errorMessage":"mean must have {num_channels} elements if it is an iterable, got {len(mean)}","messagePattern":"mean must have (.+?) elements if it is an iterable, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/image_transforms.py","lineNumber":424,"sourceCode":"            The channel dimension format of the input image. If unset, will use the inferred format from the input.\n    \"\"\"\n    if not isinstance(image, np.ndarray):\n        raise TypeError(\"image must be a numpy array\")\n\n    if input_data_format is None:\n        input_data_format = infer_channel_dimension_format(image)\n\n    channel_axis = get_channel_dimension_axis(image, input_data_format=input_data_format)\n    num_channels = image.shape[channel_axis]\n\n    # We cast to float32 to avoid errors that can occur when subtracting uint8 values.\n    # We preserve the original dtype if it is a float type to prevent upcasting float16.\n    if not np.issubdtype(image.dtype, np.floating):\n        image = image.astype(np.float32)\n\n    if isinstance(mean, Collection):\n        if len(mean) != num_channels:\n            raise ValueError(f\"mean must have {num_channels} elements if it is an iterable, got {len(mean)}\")\n    else:\n        mean = [mean] * num_channels\n    mean = np.array(mean, dtype=image.dtype)\n\n    if isinstance(std, Collection):\n        if len(std) != num_channels:\n            raise ValueError(f\"std must have {num_channels} elements if it is an iterable, got {len(std)}\")\n    else:\n        std = [std] * num_channels\n    std = np.array(std, dtype=image.dtype)\n\n    if input_data_format == ChannelDimension.LAST:\n        image = (image - mean) / std\n    else:\n        image = ((image.T - mean) / std).T\n\n    image = to_channel_dimension_format(image, data_format, input_data_format) if data_format is not None else image\n    return image","sourceCodeStart":406,"sourceCodeEnd":442,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/image_transforms.py#L406-L442","documentation":"When mean is a Collection, normalize broadcasts it per channel, so its length must equal the image's channel count (inferred from the channel axis); otherwise ValueError. Scalars are fine and get replicated, only mismatched iterables fail.","triggerScenarios":"normalize(rgb_image, mean=[0.5], std=0.5) with a 3-channel image; grayscale image with 3-element mean; mean lists of length 2 for 3 channels.","commonSituations":"Hardcoding RGB stats but running grayscale/RGBA images, using dataset-specific channel counts (e.g. satellite imagery with >3 bands), or typo'd stat lists.","solutions":["Match lengths: 3-element mean/std for RGB, 1-element for grayscale.","Pass scalars to apply one value to all channels: mean=0.5.","Compute channel count first and build stats accordingly (len(img.shape[channel_axis]))."],"exampleFix":"# before\nimg = normalize(gray_img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])  # 1-channel image\n\n# after\nimg = normalize(gray_img, mean=0.5, std=0.5)","handlingStrategy":"validation","validationCode":"from transformers.image_utils import get_channel_dimension_axis\nn = image.shape[get_channel_dimension_axis(image, input_data_format=input_data_format)]\nif isinstance(mean, (list, tuple)):\n    assert len(mean) == n, f\"mean needs {n} elements, got {len(mean)}\"\nif isinstance(std, (list, tuple)):\n    assert len(std) == n, f\"std needs {n} elements, got {len(std)}\"","typeGuard":"def stats_match_channels(mean, std, n) -> bool:\n    ok = lambda v: not isinstance(v, (list, tuple)) or len(v) == n\n    return ok(mean) and ok(std)","tryCatchPattern":null,"preventionTips":["Derive stats length from the model config's num_channels.","Pass scalars when per-channel stats are unknown."],"tags":["image-processing","normalization","validation","valueerror"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}