keras-team/keras · error · ValueError
Must specify exactly two of left_cropping, right_cropping…
Error message
Must specify exactly two of left_cropping, right_cropping, target_width. Received: left_cropping={left_cropping}, right_cropping={right_cropping}, target_width={target_width} What it means
Raised by the affine/shear/crop width helper when the three arguments left_cropping, right_cropping, and target_width do not leave exactly two of them specified. The op solves for the third quantity, so exactly two must be given (non-None) and the third left unspecified.
Solutions
- Provide exactly two, e.g. target_width=224, left_cropping=0
- Symmetric: left_cropping=c, right_cropping=c
- Validate width independently of height
Example fix
# before out = crop_images(img, target_height=224, top_cropping=0) # width underdetermined # after out = crop_images(img, target_height=224, top_cropping=0, target_width=224, left_cropping=0)
Defensive patterns
Strategy: validation
Validate before calling
assert [left_cropping, right_cropping, target_width].count(None) == 1
Type guard
def crop_width_triple_ok(l, r, tw) -> bool:
return [l, r, tw].count(None) == 1 Prevention
- Validate both crop axes together
- Watch for typos in kwargs silently yielding None
When it happens
Trigger: crop_images with width args specified 0, 1, or 3 times, e.g. only target_width=224.
Common situations: Same shape as the height variant: partial configs, typo'd keys, mixing pad/crop kwargs.
Related errors
- Must specify exactly two of top_cropping, bottom_cropping…
- Expected `size` to be a tuple of 2 integers. Received: size=
- Must specify exactly two of left_padding, right_padding…
- Must specify exactly two of top_padding, bottom_padding…
- must be >= 0. Received: =
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/1db66f95c43c22eb.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/ops/image.py:1589
def _validate_crop_images_args(
top_cropping,
left_cropping,
bottom_cropping,
right_cropping,
target_height,
target_width,
):
if [top_cropping, bottom_cropping, target_height].count(None) != 1:
raise ValueError(
"Must specify exactly two of "
"top_cropping, bottom_cropping, target_height. "
f"Received: top_cropping={top_cropping}, "
f"bottom_cropping={bottom_cropping}, "
f"target_height={target_height}"
)
if [left_cropping, right_cropping, target_width].count(None) != 1:
raise ValueError(
"Must specify exactly two of "
"left_cropping, right_cropping, target_width. "
f"Received: left_cropping={left_cropping}, "
f"right_cropping={right_cropping}, "
f"target_width={target_width}"
)
_validate_non_negative(top_cropping, "top_cropping")
_validate_non_negative(bottom_cropping, "bottom_cropping")
_validate_non_negative(target_height, "target_height")
_validate_non_negative(left_cropping, "left_cropping")
_validate_non_negative(right_cropping, "right_cropping")
_validate_non_negative(target_width, "target_width")
class PadImages(Operation):
def __init__(
self,View on GitHub (pinned to 7a34a03db6)