keras-team/keras · error · ValueError

Invalid `data_format` argument: {data_format}

Error message

Invalid `data_format` argument: {data_format}

What it means

UpSampling2D._resize_images (used by call) requires data_format to be exactly 'channels_last' or 'channels_first'. The public constructor already standardizes data_format, so hitting this guard means the value bypassed standardization — an internal/private call or subclass path with a non-standard string like 'NCHW', 'NHWC', or a typo.

Source

Thrown at keras/src/layers/reshaping/up_sampling2d.py:144

        width_factor,
        data_format,
        interpolation="nearest",
    ):
        """Resizes the images contained in a 4D tensor.

        Args:
            x: Tensor or variable to resize.
            height_factor: Positive integer.
            width_factor: Positive integer.
            data_format: One of `"channels_first"`, `"channels_last"`.
            interpolation: A string, one of `"bicubic"`, `"bilinear"`,
            `"lanczos3"`, `"lanczos5"`, or `"nearest"`.

        Returns:
            A tensor.
        """
        if data_format not in {"channels_last", "channels_first"}:
            raise ValueError(f"Invalid `data_format` argument: {data_format}")

        if data_format == "channels_first":
            x = ops.transpose(x, [0, 2, 3, 1])
        # https://github.com/keras-team/keras/issues/294
        # Use `ops.repeat` for `nearest` interpolation to enable XLA
        if interpolation == "nearest":
            x = ops.repeat(x, height_factor, axis=1)
            x = ops.repeat(x, width_factor, axis=2)
        else:
            # multiply the height and width factor on each dim
            # by hand (versus using element-wise multiplication
            # by np.array([height_factor, width_factor]) then
            # list-ifying the tensor by calling `.tolist()`)
            # since when running under torchdynamo, `new_shape`
            # will be traced as a symbolic variable (specifically
            # a `FakeTensor`) which does not have a `tolist()` method.
            shape = ops.shape(x)
            new_shape = (

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Use exactly 'channels_last' or 'channels_first' (Keras vocabulary), not 'NHWC'/'NCHW'
  2. If you subclass or call internals, run keras.backend.standardize_data_format(data_format) first — or better, call the public layer API instead of _resize_images
  3. Prefer omitting data_format and letting keras.config set it globally

Example fix

# before
out = upsample._resize_images(x, size=2, data_format='NCHW')

# after
out = upsample._resize_images(x, size=2, data_format='channels_first')
Defensive patterns

Strategy: validation

Validate before calling

def valid_data_format(df):
    return df in {'channels_last', 'channels_first'}

assert valid_data_format(df), f'bad data_format: {df}'

Type guard

def is_keras_data_format(v) -> bool:
    return v in ('channels_last', 'channels_first')

Prevention

When it happens

Trigger: Calling the layer's internal _resize_images(x, size, data_format) with e.g. data_format='NCHW' or 'channel_last'; or subclass/wrapper code that forwards a raw, non-standardized string.

Common situations: Subclassing UpSampling2D or reusing its helpers with framework-style format strings ('NHWC'/'NCHW' from TF/PyTorch vocabulary); dynamically threading data_format through custom layers where one path skips standardize_data_format.

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/8b532973587c1e62. Report an issue: GitHub.