keras-team/keras · error · ValueError

right_cropping must be >= 0. Received: right_cropping=

Error message

right_cropping must be >= 0. Received: right_cropping={right_cropping}

What it means

Raised by Cropping2D compute_output_shape logic: right_cropping (configured or inferred as width - target_width - left_cropping) went negative, meaning the crop exceeds the available input width.

Solutions

  1. Pass non-negative right_cropping
  2. Reduce croppings or increase target_width so totals stay within the input width
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/ops/image.py:1985 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

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

                raise ValueError(
                    "target_height must be >= 0. "
                    f"Received: target_height={target_height}"
                )

        if width is not None:
            left_cropping = self.left_cropping
            right_cropping = self.right_cropping
            if left_cropping is None:
                left_cropping = width - target_width - right_cropping
            if right_cropping is None:
                right_cropping = width - target_width - left_cropping
            if left_cropping < 0:
                raise ValueError(
                    "left_cropping must be >= 0. "
                    f"Received: left_cropping={left_cropping}"
                )
            if right_cropping < 0:
                raise ValueError(
                    "right_cropping must be >= 0. "
                    f"Received: right_cropping={right_cropping}"
                )
            if target_width < 0:
                raise ValueError(
                    "target_width must be >= 0. "
                    f"Received: target_width={target_width}"
                )

        images_shape[height_axis] = target_height
        images_shape[width_axis] = target_width
        return KerasTensor(shape=images_shape, dtype=images.dtype)


@keras_export("keras.ops.image.crop_images")
def crop_images(
    images,
    top_cropping=None,

View on GitHub (pinned to 7a34a03db6)