keras-team/keras · error · ValueError

Only one of `pad_to_aspect_ratio` & `crop_to_aspect_ratio` c

Error message

Only one of `pad_to_aspect_ratio` & `crop_to_aspect_ratio` can be `True`.

What it means

resize supports only one aspect-ratio strategy at a time. Setting both pad_to_aspect_ratio=True and crop_to_aspect_ratio=True is contradictory (pad keeps whole image with fill, crop discards edges) and raises this ValueError.

Source

Thrown at keras/src/ops/image.py:376

    if len(size) != 2:
        raise ValueError(
            "Expected `size` to be a tuple of 2 integers. "
            f"Received: size={size}"
        )
    if (isinstance(size[0], int) and size[0] <= 0) or (
        isinstance(size[1], int) and size[1] <= 0
    ):
        raise ValueError(
            f"`size` must have positive height and width. Received: size={size}"
        )
    if len(images.shape) < 3 or len(images.shape) > 4:
        raise ValueError(
            "Invalid images rank: expected rank 3 (single image) "
            "or rank 4 (batch of images). Received input with shape: "
            f"images.shape={images.shape}"
        )
    if pad_to_aspect_ratio and crop_to_aspect_ratio:
        raise ValueError(
            "Only one of `pad_to_aspect_ratio` & `crop_to_aspect_ratio` "
            "can be `True`."
        )
    if any_symbolic_tensors((images,)):
        return Resize(
            size,
            interpolation=interpolation,
            antialias=antialias,
            data_format=data_format,
            crop_to_aspect_ratio=crop_to_aspect_ratio,
            pad_to_aspect_ratio=pad_to_aspect_ratio,
            fill_mode=fill_mode,
            fill_value=fill_value,
        ).symbolic_call(images)
    return _resize(
        images,
        size,
        interpolation=interpolation,

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Choose one flag: pad_to_aspect_ratio=True (keeps full content, adds fill_value) OR crop_to_aspect_ratio=True (center-crops)
  2. Sanitize user/config kwargs: assert not (kw.get('pad_to_aspect_ratio') and kw.get('crop_to_aspect_ratio'))

Example fix

# before
y = keras.ops.image.resize(x, (224,224), pad_to_aspect_ratio=True, crop_to_aspect_ratio=True)

# after
y = keras.ops.image.resize(x, (224,224), pad_to_aspect_ratio=True)
Defensive patterns

Strategy: validation

Validate before calling

assert not (pad_to_aspect_ratio and crop_to_aspect_ratio)

Prevention

When it happens

Trigger: Calling keras.ops.image.resize(x, size, pad_to_aspect_ratio=True, crop_to_aspect_ratio=True); forwarding **kwargs from a config/augmentation preset that sets both flags.

Common situations: Augmentation search spaces or config files where both options default on; merging config presets that each enable a different strategy.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/a3e3212af9b70d7f. Report an issue: GitHub.