keras-team/keras · error · ValueError
The `value_range` argument should be a list of two numbers.
Error message
The `value_range` argument should be a list of two numbers. Received: value_range={value_range} What it means
The value_range argument of AutoContrast must contain exactly two numbers (the minimum and maximum pixel values of the input). After confirming it is a tuple/list, the layer checks len(value_range) == 2 and raises this ValueError for lists of any other length; the accepted pair is stored sorted.
Source
Thrown at keras/src/layers/preprocessing/image_preprocessing/auto_contrast.py:51
"The `value_range` argument should be a list of two numbers. "
)
def __init__(
self,
value_range=(0, 255),
**kwargs,
):
super().__init__(**kwargs)
self._set_value_range(value_range)
def _set_value_range(self, value_range):
if not isinstance(value_range, (tuple, list)):
raise ValueError(
self._VALUE_RANGE_VALIDATION_ERROR
+ f"Received: value_range={value_range}"
)
if len(value_range) != 2:
raise ValueError(
self._VALUE_RANGE_VALIDATION_ERROR
+ f"Received: value_range={value_range}"
)
self.value_range = sorted(value_range)
def transform_images(self, images, transformation=None, training=True):
original_images = images
images = self._transform_value_range(
images,
original_range=self.value_range,
target_range=(0, 255),
dtype=self.compute_dtype,
)
images = self.backend.cast(images, self.compute_dtype)
low = self.backend.numpy.min(images, axis=(1, 2), keepdims=True)
high = self.backend.numpy.max(images, axis=(1, 2), keepdims=True)
scale = 255.0 / (high - low)View on GitHub (pinned to 7a34a03db6)
Solutions
- Pass exactly two numbers, e.g. (0, 255).
- If you computed per-channel ranges, reduce them to a single global (min, max) pair before passing them in.
Example fix
# before layer = keras.layers.AutoContrast(value_range=[0, 128, 255]) # after layer = keras.layers.AutoContrast(value_range=(0, 255))
Defensive patterns
Strategy: validation
Validate before calling
assert len(value_range) == 2, f"value_range must have 2 elements, got {len(value_range)}" Type guard
def is_valid_value_range(v):
return isinstance(v, (tuple, list)) and len(v) == 2 and all(isinstance(x, (int, float)) for x in v)
Prevention
- When computing ranges dynamically, assert length 2 before layer construction.
When it happens
Trigger: Passing value_range=(0,), value_range=[0, 128, 255], or an empty list.
Common situations: Building value_range dynamically (e.g. from per-channel normalization statistics that yield three values) and passing it straight to the layer.
Related errors
- {self._VALUE_RANGE_VALIDATION_ERROR}Received: value_range={v
- Layer {self.__class__.__name__} does not take a `factor` arg
- The `factor` argument should be a number (or a list of two n
- Invalid quantization mode. Expected one of {dtype_policies.Q
- Invalid value for argument `output_mode`. Expected one of {a
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/e598375c3a72d4ce.
Report an issue: GitHub.