{"record":{"id":"66731455a68cb3df","repo":"huggingface/transformers","slug":"unsupported-data-format-channel-dim","errorCode":null,"errorMessage":"Unsupported data format: {channel_dim}","messagePattern":"Unsupported data format: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/image_utils.py","lineNumber":375,"sourceCode":"            The image to get the dimensions of.\n        channel_dim (`ChannelDimension`, *optional*):\n            Which dimension the channel dimension is in. If `None`, will infer the channel dimension from the image.\n\n    Returns:\n        A tuple of the image's height and width.\n    \"\"\"\n    if isinstance(image, PIL.Image.Image):\n        return image.size\n\n    if channel_dim is None:\n        channel_dim = infer_channel_dimension_format(image)\n\n    if channel_dim == ChannelDimension.FIRST:\n        return image.shape[-2], image.shape[-1]\n    elif channel_dim == ChannelDimension.LAST:\n        return image.shape[-3], image.shape[-2]\n    else:\n        raise ValueError(f\"Unsupported data format: {channel_dim}\")\n\n\ndef get_image_size_for_max_height_width(\n    image_size: tuple[int, int],\n    max_height: int,\n    max_width: int,\n) -> tuple[int, int]:\n    \"\"\"\n    Computes the output image size given the input image and the maximum allowed height and width. Keep aspect ratio.\n    Important, even if image_height < max_height and image_width < max_width, the image will be resized\n    to at least one of the edges be equal to max_height or max_width.\n\n    For example:\n        - input_size: (100, 200), max_height: 50, max_width: 50 -> output_size: (25, 50)\n        - input_size: (100, 200), max_height: 200, max_width: 500 -> output_size: (200, 400)\n\n    Args:\n        image_size (`tuple[int, int]`):","sourceCodeStart":357,"sourceCodeEnd":393,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/image_utils.py#L357-L393","documentation":"Raised by `transformers.image_utils.get_image_size` when `channel_dim` (after optional inference from the array shape) is neither `ChannelDimension.FIRST` nor `ChannelDimension.LAST`. The function returns (height, width) by indexing shape[-2:]/shape[-3:-1] depending on the layout; an unknown layout makes the indices ambiguous. PIL inputs return early via `image.size` and never hit this.","triggerScenarios":"Calling `get_image_size(np_img, channel_dim='CHW')`, 'channels-first', 'first', or any string that is not exactly 'channels_first'/'channels_last'. Passing None lets inference run, which can instead raise 'Unable to infer channel dimension format' or 'Unsupported number of image dimensions'.","commonSituations":"Custom pipelines passing layout shorthand (CHW/HWC/NCHW) borrowed from other frameworks; config files with free-text layout fields; typos like 'channels-first'.","solutions":["Pass `ChannelDimension.FIRST`/`ChannelDimension.LAST` or the exact strings 'channels_first'/'channels_last'.","Validate external strings once at the boundary: `ChannelDimension(value)`.","Pass None to infer from shape — but ensure the shape makes inference possible (1/3 channels at an end axis)."],"exampleFix":"// before\nh, w = get_image_size(arr, channel_dim=\"HWC\")  # ValueError\n\n// after\nfrom transformers.image_utils import ChannelDimension\nh, w = get_image_size(arr, channel_dim=ChannelDimension.LAST)\nh, w = get_image_size(arr)  # infer","handlingStrategy":"validation","validationCode":"from transformers.image_utils import ChannelDimension\n\nif channel_dim is not None:\n    channel_dim = ChannelDimension(channel_dim)  # clear error on bad value\nh, w = get_image_size(image, channel_dim=channel_dim)","typeGuard":"from transformers.image_utils import ChannelDimension\n\ndef is_valid_channel_dim(v) -> bool:\n    return v is None or v in (ChannelDimension.FIRST, ChannelDimension.LAST, \"channels_first\", \"channels_last\")","tryCatchPattern":null,"preventionTips":["Pass channel_dim=None to infer, or the ChannelDimension enum explicitly.","Do not use CHW/HWC/NCHW shorthand from other frameworks.","For 2D arrays, add a channel axis first — get_image_size assumes 3D+."],"tags":["image-processing","channel-dimension","enum","argument-validation"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}