{"record":{"id":"04d46d069d099bcb","repo":"huggingface/transformers","slug":"invalid-channel-dimension-format-input-data-form","errorCode":null,"errorMessage":"Invalid channel dimension format: {input_data_format}","messagePattern":"Invalid channel dimension format: (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/transformers/image_utils.py","lineNumber":427,"sourceCode":"def max_across_indices(values: Iterable[Any]) -> list[Any]:\n    \"\"\"\n    Return the maximum value across all indices of an iterable of values.\n    \"\"\"\n    return [max(values_i) for values_i in zip(*values)]\n\n\ndef get_max_height_width(\n    images: list[Union[\"torch.Tensor\", np.ndarray]], input_data_format: str | ChannelDimension = ChannelDimension.FIRST\n) -> list[int]:\n    \"\"\"\n    Get the maximum height and width across all images in a batch.\n    \"\"\"\n    if input_data_format == ChannelDimension.FIRST:\n        _, max_height, max_width = max_across_indices([img.shape for img in images])\n    elif input_data_format == ChannelDimension.LAST:\n        max_height, max_width, _ = max_across_indices([img.shape for img in images])\n    else:\n        raise ValueError(f\"Invalid channel dimension format: {input_data_format}\")\n    return (max_height, max_width)\n\n\ndef is_valid_annotation_coco_detection(annotation: dict[str, list | tuple]) -> bool:\n    if (\n        isinstance(annotation, dict)\n        and \"image_id\" in annotation\n        and \"annotations\" in annotation\n        and isinstance(annotation[\"annotations\"], (list, tuple))\n        and (\n            # an image can have no annotations\n            len(annotation[\"annotations\"]) == 0 or isinstance(annotation[\"annotations\"][0], dict)\n        )\n    ):\n        return True\n    return False\n\n","sourceCodeStart":409,"sourceCodeEnd":445,"githubUrl":"https://github.com/huggingface/transformers/blob/a597f974857b3d92939971296bc0deb93d33d780/src/transformers/image_utils.py#L409-L445","documentation":"Raised by `transformers.image_utils.get_max_height_width` when `input_data_format` (default `ChannelDimension.FIRST`) is neither FIRST nor LAST. The function takes the elementwise max of image shapes across a batch and unpacks the result as (_, H, W) or (H, W, _) depending on layout; an unrecognized format makes the unpacking undefined, so it raises. Unlike sibling functions, the default is FIRST rather than None (no inference happens here).","triggerScenarios":"Calling `get_max_height_width(images, input_data_format='channels-last')` (hyphen typo), 'NCHW', 'HWC', or any non-enum string when padding a batch to the largest image.","commonSituations":"Custom batched-padding code; layouts hard-coded from other frameworks; a default that surprises users whose data is channels-last — they override the string and mistype it.","solutions":["Pass `ChannelDimension.FIRST` or `ChannelDimension.LAST` (or exact strings 'channels_first'/'channels_last').","Remember the default assumes channels-first; channels-last batches must pass it explicitly.","Validate config-supplied strings with `ChannelDimension(value)` before use."],"exampleFix":"// before\nmh, mw = get_max_height_width(imgs, input_data_format=\"channels-last\")  # ValueError\n\n// after\nfrom transformers.image_utils import ChannelDimension\nmh, mw = get_max_height_width(imgs, input_data_format=ChannelDimension.LAST)","handlingStrategy":"validation","validationCode":"from transformers.image_utils import ChannelDimension\n\ninput_data_format = ChannelDimension(input_data_format)  # validate once\nmh, mw = get_max_height_width(images, input_data_format=input_data_format)","typeGuard":"from transformers.image_utils import ChannelDimension\n\ndef is_valid_batch_layout(v) -> bool:\n    return v in (ChannelDimension.FIRST, ChannelDimension.LAST, \"channels_first\", \"channels_last\")","tryCatchPattern":null,"preventionTips":["Remember the default here is channels_first — channels-last batches must pass the format explicitly.","Normalize layout strings through ChannelDimension(value) before batch utilities.","Keep one layout convention across the whole pipeline to avoid overrides entirely."],"tags":["image-processing","channel-dimension","enum","batching"],"backgroundTag":null,"analyzedSha":"a597f974857b3d92939971296bc0deb93d33d780","analyzedAt":"2026-08-14T18:24:08.354Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}