keras-team/keras · error · ValueError

`size` must have positive height and width. Received: size={

Error message

`size` must have positive height and width. Received: size={size}

What it means

resize rejects size tuples whose height or width entries are statically-known ints <= 0. Zero or negative dimensions cannot form a valid output image.

Source

Thrown at keras/src/ops/image.py:366

    >>> 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` "
            "can be `True`."
        )
    if any_symbolic_tensors((images,)):
        return Resize(
            size,
            interpolation=interpolation,
            antialias=antialias,

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Fix the size computation so both entries are positive ints
  2. Validate config-derived sizes at startup: assert size[0] > 0 and size[1] > 0

Example fix

# before
size = (cfg.height, cfg.width)  # cfg.width missing -> 0
y = keras.ops.image.resize(x, size)

# after
size = (cfg.height or 224, cfg.width or 224)
y = keras.ops.image.resize(x, size)
Defensive patterns

Strategy: validation

Validate before calling

h, w = size
assert isinstance(h, int) and h > 0 and isinstance(w, int) and w > 0

Type guard

def is_positive_size(size) -> bool:
    return all(isinstance(v, int) and v > 0 for v in size)

Prevention

When it happens

Trigger: Calling resize with size=(0, 224), (-1, -1), or values computed from a config/env variable that defaults to 0; dynamic tensor sizes pass through since only int entries are checked.

Common situations: Reading target resolution from a config where height/width keys are absent and default to 0; computing size as target_size - padding with an off-by error going negative.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/c043dd61ab0de8cd. Report an issue: GitHub.