keras-team/keras · error · ValueError

Invalid images dtype: expected float dtype. Received: images

Error message

Invalid images dtype: expected float dtype. Received: images.dtype={dtype}

What it means

rgb_to_hsv mathematically requires fractional values, so its compute_output_spec verifies images.dtype is a float dtype (float16/32/64, bfloat16). Integer image arrays (uint8, int32) are rejected with this ValueError.

Source

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

class RGBToHSV(Operation):
    def __init__(self, data_format=None, *, name=None):
        super().__init__(name=name)
        self.data_format = backend.standardize_data_format(data_format)

    def call(self, images):
        return backend.image.rgb_to_hsv(images, data_format=self.data_format)

    def compute_output_spec(self, images):
        images_shape = list(images.shape)
        dtype = images.dtype
        if len(images_shape) not in (3, 4):
            raise ValueError(
                "Invalid images rank: expected rank 3 (single image) "
                "or rank 4 (batch of images). "
                f"Received: images.shape={images_shape}"
            )
        if not backend.is_float_dtype(dtype):
            raise ValueError(
                "Invalid images dtype: expected float dtype. "
                f"Received: images.dtype={dtype}"
            )
        channels_axis = -1 if self.data_format == "channels_last" else -3
        channels = images_shape[channels_axis]
        if channels is not None and channels != 3:
            raise ValueError(
                "Input images must have 3 channels, but received images with "
                f"{channels} channels."
            )
        return KerasTensor(shape=images_shape, dtype=images.dtype)


@keras_export("keras.ops.image.rgb_to_hsv")
def rgb_to_hsv(images, data_format=None):
    """Convert RGB images to HSV.

    `images` must be of float dtype, and the output is only well defined if the

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Convert and normalize first: images = images.astype('float32') / 255.0
  2. Use keras.layers.Rescaling(1./255) as the first layer of your model instead

Example fix

# before
hsv = keras.ops.image.rgb_to_hsv(img_uint8)  # uint8 -> ValueError

# after
img = img_uint8.astype('float32') / 255.0
hsv = keras.ops.image.rgb_to_hsv(img)
Defensive patterns

Strategy: validation

Validate before calling

assert images.dtype in ('float16','float32','float64','bfloat16') or str(images.dtype).startswith('float')

Type guard

def is_float_tensor(x) -> bool:
    return str(getattr(x, 'dtype', '')).startswith('float') or 'bfloat16' in str(getattr(x, 'dtype', ''))

Prevention

When it happens

Trigger: Passing uint8-loaded images (np.array(Image.open(...)) or cv2.imread output) directly to keras.ops.image.rgb_to_hsv.

Common situations: Forgetting the standard /255.0 normalization step; mixing OpenCV uint8 pipelines with Keras float ops; loading with image_dataset_from_directory without a rescale layer having been applied yet.

Related errors


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