keras-team/keras · error · ValueError
Expected `size` to be a tuple of 2 integers. Received: size=
Error message
Expected `size` to be a tuple of 2 integers. Received: size={size} What it means
keras.ops.image.resize requires size to be exactly a 2-element sequence (height, width). Passing an int, a 3-tuple, a nested list, or an empty sequence raises this ValueError before any resizing.
Source
Thrown at keras/src/ops/image.py:359
>>> x = np.random.random((2, 4, 4, 3)) # batch of 2 RGB images
>>> y = keras.ops.image.resize(x, (2, 2))
>>> y.shape
(2, 2, 2, 3)
>>> x = np.random.random((4, 4, 3)) # single RGB image
>>> y = keras.ops.image.resize(x, (2, 2))
>>> y.shape
(2, 2, 3)
>>> x = np.random.random((2, 3, 4, 4)) # batch of 2 RGB images
>>> y = keras.ops.image.resize(x, (2, 2),
... data_format="channels_first")
>>> y.shape
(2, 3, 2, 2)
"""
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` "View on GitHub (pinned to 7a34a03db6)
Solutions
- Pass exactly two values: resize(images, (224, 224))
- If your target came as a full shape, slice it: resize(images, shape[:-1]) for channels_last or shape[1:3] as appropriate
Example fix
# before y = keras.ops.image.resize(x, (224, 224, 3)) # after y = keras.ops.image.resize(x, (224, 224))
Defensive patterns
Strategy: type-guard
Validate before calling
size = tuple(size)
assert len(size) == 2, f'size must have 2 entries, got {size!r}' Type guard
def is_valid_resize_size(size) -> bool:
try:
s = tuple(size)
except TypeError:
return False
return len(s) == 2 Prevention
- Never include channels in resize size
- Store target resolution as a (h, w) tuple in configs
When it happens
Trigger: Calling resize(images, 224); resize(images, (224, 224, 3)) (including channels); passing the output shape of another op that has 3+ entries.
Common situations: Confusing target size with full output shape including channels; passing a config value parsed as int; chaining ops where .shape slices are the wrong length.
Related errors
- `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}
- Must specify exactly two of top_padding, bottom_padding, tar
- Must specify exactly two of left_padding, right_padding, tar
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/2a44c5f2a2c97cd2.
Report an issue: GitHub.