keras-team/keras · error · ValueError
Must specify exactly two of top_padding, bottom_padding, tar
Error message
Must specify exactly two of top_padding, bottom_padding, target_height. Received: top_padding={top_padding}, bottom_padding={bottom_padding}, target_height={target_height} What it means
pad_images (and its layer) requires exactly two of top_padding, bottom_padding, target_height to be given (the third is derived). This validator raises when zero, one, or all three are None — the vertical output size is then over- or under-determined.
Source
Thrown at keras/src/ops/image.py:1548
fill_value,
)
def _validate_non_negative(value, name):
if value is not None and value < 0:
raise ValueError(f"{name} must be >= 0. Received: {name}={value}")
def _validate_pad_images_args(
top_padding,
left_padding,
bottom_padding,
right_padding,
target_height,
target_width,
):
if [top_padding, bottom_padding, target_height].count(None) != 1:
raise ValueError(
"Must specify exactly two of "
"top_padding, bottom_padding, target_height. "
f"Received: top_padding={top_padding}, "
f"bottom_padding={bottom_padding}, "
f"target_height={target_height}"
)
if [left_padding, right_padding, target_width].count(None) != 1:
raise ValueError(
"Must specify exactly two of "
"left_padding, right_padding, target_width. "
f"Received: left_padding={left_padding}, "
f"right_padding={right_padding}, "
f"target_width={target_width}"
)
_validate_non_negative(top_padding, "top_padding")
_validate_non_negative(bottom_padding, "bottom_padding")
_validate_non_negative(target_height, "target_height")View on GitHub (pinned to 7a34a03db6)
Solutions
- Specify exactly two, e.g. pad_images(img, target_height=256, bottom_padding=8)
- For symmetric pad of known amount: top_padding=b, bottom_padding=b and leave target None
- For pad-to-size: target_height plus one side
Example fix
# before out = pad_images(img) # after out = pad_images(img, top_padding=8, bottom_padding=8)
Defensive patterns
Strategy: validation
Validate before calling
assert [top_padding, bottom_padding, target_height].count(None) == 1
Type guard
def height_triple_ok(t, b, th) -> bool:
return [t, b, th].count(None) == 1 Prevention
- Decide pad-to-size vs pad-by-amount up front
- Test both axes' triples in unit tests of the config
When it happens
Trigger: keras.ops.image.pad_images(images) with none of the three set (all None defaults), or with all three set, or only one set.
Common situations: Calling pad_images with no arguments expecting a no-op; copying a partial config where one of the three keys is missing or extra; version migration where target_height was previously optional.
Related errors
- {name} must be >= 0. Received: {name}={value}
- Must specify exactly two of left_padding, right_padding, tar
- The `weights` argument should be either `None` (random initi
- `padding` should have two elements. Received: padding={paddi
- Invalid padding: {padding}
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/2a2a4e0f141152f2.
Report an issue: GitHub.