keras-team/keras · error · ValueError

The `fill_value` argument should be a number (or a list of t

Error message

The `fill_value` argument should be a number (or a list of three numbers) 

What it means

RandomErasing._get_fill_value validates the fill_value argument at transformation-sampling time (called from get_random_transformation). When fill_value is a sequence it must contain exactly three numbers (one per RGB channel); any other length raises this ValueError. This only fires when a list/tuple was supplied instead of a scalar.

Source

Thrown at keras/src/layers/preprocessing/image_preprocessing/random_erasing.py:196

    def _get_fill_value(self, images, images_shape, seed):
        fill_value = self.fill_value
        if fill_value is None:
            fill_value = (
                self.backend.random.normal(
                    images_shape,
                    dtype=self.compute_dtype,
                    seed=seed,
                )
                * self.value_range[1]
            )
        else:
            error_msg = (
                "The `fill_value` argument should be a number "
                "(or a list of three numbers) "
            )
            if isinstance(fill_value, (tuple, list)):
                if len(fill_value) != 3:
                    raise ValueError(error_msg)
                fill_value = self.backend.numpy.full_like(
                    images, fill_value, dtype=self.compute_dtype
                )
            elif isinstance(fill_value, (int, float)):
                fill_value = (
                    self.backend.numpy.ones(
                        images_shape, dtype=self.compute_dtype
                    )
                    * fill_value
                )
            else:
                raise ValueError(error_msg)
        fill_value = self.backend.numpy.clip(
            fill_value, self.value_range[0], self.value_range[1]
        )
        return fill_value

    def get_random_transformation(self, data, training=True, seed=None):

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Use a list of exactly three numbers, e.g. fill_value=[0.5, 0.5, 0.5]
  2. Or pass a single scalar, e.g. fill_value=0.5, which is broadcast to all channels
  3. Remember values are clipped to value_range afterwards, so supply them in the layer's value scale (e.g. 0-1 or 0-255 consistently)

Example fix

# before
layers.RandomErasing(fill_value=[125])
# after
layers.RandomErasing(fill_value=0.5)
Defensive patterns

Strategy: validation

Validate before calling

fv = [0.5, 0.5, 0.5]
assert isinstance(fv, (int, float)) or (isinstance(fv, (tuple, list)) and len(fv) == 3), "fill_value must be a number or 3 numbers"

Type guard

def is_valid_fill_value(v) -> bool:
    return isinstance(v, (int, float)) or (isinstance(v, (tuple, list)) and len(v) == 3)

Try / catch

try:
    layer = RandomErasing(fill_value=fv)
except ValueError:
    layer = RandomErasing(fill_value=float(fv[0]) if len(fv) == 1 else 0.5)

Prevention

When it happens

Trigger: fill_value=[0.5] or fill_value=[255, 255] (wrong length), or a per-channel list with a typo'd fourth entry.

Common situations: Assuming single-channel or grayscale semantics (one value) while the layer expects RGB triples; copying 4-element RGBA defaults from image tooling.

Related errors


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