keras-team/keras · error · ValueError
Only one of `pad_to_aspect_ratio` & `crop_to_aspect_ratio`…
Error message
Only one of `pad_to_aspect_ratio` & `crop_to_aspect_ratio` can be `True`.
What it means
Mutual-exclusion guard in `resize` (NumPy backend): `crop_to_aspect_ratio` and `pad_to_aspect_ratio` are two different, incompatible strategies for reaching a target aspect ratio (discard pixels vs. add fill pixels). Enabling both would make the output geometry ambiguous, so the guard raises this error whenever both flags are True at once.
Solutions
- Enable exactly one of the two flags: crop_to_aspect_ratio=True or pad_to_aspect_ratio=True, leaving the other False
- Choose padding if losing image content is unacceptable, or cropping if added fill pixels are unacceptable
- Fix config code that sets both flags from independent booleans without an explicit choice
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/backend/numpy/ops/image.py:189 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/78f99e9a0b080b82.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/numpy/ops/image.py:189
crop_to_aspect_ratio=False,
pad_to_aspect_ratio=False,
fill_mode="constant",
fill_value=0.0,
data_format=None,
):
data_format = standardize_data_format(data_format)
if interpolation not in RESIZE_INTERPOLATIONS:
raise ValueError(
"Invalid value for argument `interpolation`. Expected of one "
f"{RESIZE_INTERPOLATIONS}. Received: interpolation={interpolation}"
)
if fill_mode != "constant":
raise ValueError(
"Invalid value for argument `fill_mode`. Only `'constant'` "
f"is supported. Received: fill_mode={fill_mode}"
)
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 not len(size) == 2:
raise ValueError(
"Argument `size` must be a tuple of two elements "
f"(height, width). Received: size={size}"
)
size = tuple(size)
target_height, target_width = size
if len(images.shape) == 4:
if data_format == "channels_last":
size = (images.shape[0],) + size + (images.shape[-1],)
else:
size = (images.shape[0], images.shape[1]) + size
elif len(images.shape) == 3:
if data_format == "channels_last":
size = size + (images.shape[-1],)View on GitHub (pinned to 7a34a03db6)