keras-team/keras · error · ValueError
Must specify exactly two of top_cropping, bottom_cropping, t
Error message
Must specify exactly two of top_cropping, bottom_cropping, target_height. Received: top_cropping={top_cropping}, bottom_cropping={bottom_cropping}, target_height={target_height} What it means
crop_images (and its layer) requires exactly two of top_cropping, bottom_cropping, target_height; the third is derived. Zero, one, or three specified values make the vertical crop ambiguous and the validator raises.
Source
Thrown at keras/src/ops/image.py:1581
_validate_non_negative(top_padding, "top_padding")
_validate_non_negative(bottom_padding, "bottom_padding")
_validate_non_negative(target_height, "target_height")
_validate_non_negative(left_padding, "left_padding")
_validate_non_negative(right_padding, "right_padding")
_validate_non_negative(target_width, "target_width")
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")View on GitHub (pinned to 7a34a03db6)
Solutions
- Specify exactly two, e.g. crop_images(img, target_height=224, top_cropping=0)
- Center-crop style: top_cropping=(H-target)//2 style values on both sides
- Remove the redundant third argument
Example fix
# before out = crop_images(img, target_height=224) # after out = crop_images(img, target_height=224, top_cropping=0)
Defensive patterns
Strategy: validation
Validate before calling
assert [top_cropping, bottom_cropping, target_height].count(None) == 1
Type guard
def crop_height_triple_ok(t, b, th) -> bool:
return [t, b, th].count(None) == 1 Prevention
- Mirror the pad_images calling convention
- Reject configs with 0 or 3 values at load time
When it happens
Trigger: keras.ops.image.crop_images(images) with all cropping args None, a single value, or all three given.
Common situations: Default-constructing the op expecting a no-op; mixing pad-style kwargs (padding) into crop; config templates where one bound is always set plus target, accidentally also setting the third.
Related errors
- Must specify exactly two of left_cropping, right_cropping, t
- Expected `size` to be a tuple of 2 integers. Received: size=
- `size` must have positive height and width. Received: size={
- Only one of `pad_to_aspect_ratio` & `crop_to_aspect_ratio` c
- {name} must be >= 0. Received: {name}={value}
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/a4d224dbc9f5c155.
Report an issue: GitHub.