keras-team/keras · error · ValueError

`cropping` cannot be negative. Received: cropping=

Error message

`cropping` cannot be negative. Received: cropping={cropping}.

What it means

Cropping2D's __init__ validates the `cropping` specification; a negative crop extent is meaningless because you cannot crop more pixels than exist. This fires when any element of `cropping` (an int or a tuple of 2/2 pairs) is < 0. Pass non-negative crop values.

Solutions

  1. Use a non-negative int, e.g. Cropping2D(cropping=2)
  2. If you meant to pad, use ZeroPadding2D instead
  3. Check sign errors when computing cropping from other values
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/layers/reshaping/cropping2d.py:61 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/ed26c42d9ffea504. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/layers/reshaping/cropping2d.py:61

        - If `data_format` is `"channels_last"`:
          `(batch_size, height, width, channels)`
        - If `data_format` is `"channels_first"`:
          `(batch_size, channels, height, width)`

    Output shape:
        4D tensor with shape:
        - If `data_format` is `"channels_last"`:
          `(batch_size, cropped_height, cropped_width, channels)`
        - If `data_format` is `"channels_first"`:
          `(batch_size, channels, cropped_height, cropped_width)`
    """

    def __init__(self, cropping=((0, 0), (0, 0)), data_format=None, **kwargs):
        super().__init__(**kwargs)
        self.data_format = backend.standardize_data_format(data_format)
        if isinstance(cropping, int):
            if cropping < 0:
                raise ValueError(
                    "`cropping` cannot be negative. "
                    f"Received: cropping={cropping}."
                )
            self.cropping = ((cropping, cropping), (cropping, cropping))
        elif hasattr(cropping, "__len__"):
            if len(cropping) != 2:
                raise ValueError(
                    "`cropping` should have two elements. "
                    f"Received: cropping={cropping}."
                )
            height_cropping = argument_validation.standardize_tuple(
                cropping[0], 2, "1st entry of cropping", allow_zero=True
            )
            width_cropping = argument_validation.standardize_tuple(
                cropping[1], 2, "2nd entry of cropping", allow_zero=True
            )
            self.cropping = (height_cropping, width_cropping)
        else:

View on GitHub (pinned to 7a34a03db6)