{"record":{"id":"e345108236d4943e","repo":"huggingface/transformers","slug":"std-must-have-num-channels-elements-if-it-is-an","errorCode":null,"errorMessage":"std must have {num_channels} elements if it is an iterable, got {len(std)}","messagePattern":"std must have (.+?) elements if it is an iterable, got (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/image_transforms.py","lineNumber":431,"sourceCode":"\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\n\n\ndef center_crop(\n    image: np.ndarray,\n    size: tuple[int, int],\n    data_format: str | ChannelDimension | None = None,\n    input_data_format: str | ChannelDimension | None = None,","sourceCodeStart":413,"sourceCodeEnd":449,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/image_transforms.py#L413-L449","documentation":"Same per-channel broadcast rule as mean: if std is a Collection its length must equal the number of image channels, else ValueError. Std is cast to the image dtype and divided against, so shape mismatch would silently broadcast wrong.","triggerScenarios":"normalize(rgb_img, mean=0.5, std=[1.0]) (3 channels vs 1 std), or stats copied from a grayscale model applied to RGB.","commonSituations":"Mixing mean and std lists of different lengths, migrating between RGB and grayscale models, or hand-typed stat constants with a missing element.","solutions":["Provide one std per channel: [0.229, 0.224, 0.225] for RGB.","Use a scalar std for all channels.","Validate len(mean) == len(std) == num_channels before the call in shared preprocessing code."],"exampleFix":"# before\nimg = normalize(rgb_img, mean=0.5, std=[0.225])  # 3 channels, 1 std\n\n# after\nimg = normalize(rgb_img, mean=0.5, std=[0.229, 0.224, 0.225])","handlingStrategy":"validation","validationCode":"n = image.shape[channel_axis]\nassert not isinstance(std, (list, tuple)) or len(std) == n, f\"std needs {n} elements\"","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep mean and std as a single (mean, std) constant pair so they cannot diverge.","Unit-test your stats constants against expected channel counts."],"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"}