huggingface/transformers · error · ValueError

The size dictionary must have keys 'height' and 'width'. Got

Error message

The size dictionary must have keys 'height' and 'width'. Got {size.keys()}

What it means

Error "The size dictionary must have keys 'height' and 'width'. Got {size.keys()}" thrown in huggingface/transformers.

Source

Thrown at src/transformers/image_processing_backends.py:347

            rescale_factor=rescale_factor,
            device=images.device,
        )
        if do_normalize:
            images = self.normalize(images.to(dtype=torch.float32), image_mean, image_std)
        elif do_rescale:
            images = self.rescale(images, rescale_factor)

        return images

    def center_crop(
        self,
        image: "torch.Tensor",
        size: SizeDict,
        **kwargs,
    ) -> "torch.Tensor":
        """Center crop an image using Torchvision."""
        if size.height is None or size.width is None:
            raise ValueError(f"The size dictionary must have keys 'height' and 'width'. Got {size.keys()}")
        image_height, image_width = image.shape[-2:]
        crop_height, crop_width = size.height, size.width

        if crop_width > image_width or crop_height > image_height:
            padding_ltrb = [
                (crop_width - image_width) // 2 if crop_width > image_width else 0,
                (crop_height - image_height) // 2 if crop_height > image_height else 0,
                (crop_width - image_width + 1) // 2 if crop_width > image_width else 0,
                (crop_height - image_height + 1) // 2 if crop_height > image_height else 0,
            ]
            image = tvF.pad(image, padding_ltrb, fill=0)
            image_height, image_width = image.shape[-2:]
            if crop_width == image_width and crop_height == image_height:
                return image

        crop_top = int((image_height - crop_height) / 2.0)
        crop_left = int((image_width - crop_width) / 2.0)
        return tvF.crop(image, crop_top, crop_left, crop_height, crop_width)

View on GitHub (pinned to a597f97485)

Solutions

  1. Pass size as a dict containing both 'height' and 'width' keys.
  2. Pass an int and set default_to_square=True if a square size is intended.

When it happens

Trigger: Raised in image preprocessing when a size dict is missing required 'height' or 'width' keys.

Common situations: Passing size={'shortest_edge': ...} or partial dicts where the code path strictly requires height and width.


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/001e501bb0f769a3. Report an issue: GitHub.