keras-team/keras · error · ValueError
Expected mode to be one of `caffe`, `tf` or `torch`…
Error message
Expected mode to be one of `caffe`, `tf` or `torch`. Received: mode={mode} What it means
Validation in keras/src/applications/imagenet_utils.preprocess_input: `mode` selects the preprocessing convention (caffe, tf, or torch). Any other string, including None or a misspelling, is rejected because the function cannot know which channel ordering/scaling to apply.
Solutions
- Guard target_width >= 0 before the call
- Fix the upstream size computation
- Handle degenerate small inputs explicitly
Example fix
# before
out = ops.image.crop_images(img, target_width=(w - 2 * c, h - 2 * c)) # w < 2c
# after
tw = w - 2 * c
if tw < 0:
raise ValueError('width too small')
out = ops.image.crop_images(img, target_width=(tw, h - 2 * c)) Defensive patterns
Strategy: validation
Validate before calling
if target_width is None or int(target_width) < 0:
raise ValueError('target_width must be >= 0') Prevention
- Sanitize computed target dims before calling
- Add pipeline-level size guards for small samples
When it happens
Trigger: Passing target_width < 0, typically from a computed subtraction that underflows for small inputs.
Common situations: Dynamic per-sample widths; size-minus-margin arithmetic going negative; copy-pasted shape tuples with a negative entry.
Related errors
- The `weights` argument should be either `None` (random…
- Argument `segment_ids` and `data` should have same leading…
- Argument `segment_ids` should be an 1-D vector, got shape
- Batch dimensions of inputs to `cdist` must be…
- Expected a square matrix. Received non-square input with…
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/21954819882e711d.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/applications/imagenet_utils.py:90
ValueError: In case of unknown `data_format` argument."""
PREPROCESS_INPUT_RET_DOC_TF = """
The inputs pixel values are scaled between -1 and 1, sample-wise."""
PREPROCESS_INPUT_RET_DOC_TORCH = """
The input pixels values are scaled between 0 and 1 and each channel is
normalized with respect to the ImageNet dataset."""
PREPROCESS_INPUT_RET_DOC_CAFFE = """
The images are converted from RGB to BGR, then each color channel is
zero-centered with respect to the ImageNet dataset, without scaling."""
@keras_export("keras.applications.imagenet_utils.preprocess_input")
def preprocess_input(x, data_format=None, mode="caffe"):
"""Preprocesses a tensor or Numpy array encoding a batch of images."""
if mode not in {"caffe", "tf", "torch"}:
raise ValueError(
"Expected mode to be one of `caffe`, `tf` or `torch`. "
f"Received: mode={mode}"
)
if data_format is None:
data_format = backend.image_data_format()
elif data_format not in {"channels_first", "channels_last"}:
raise ValueError(
"Expected data_format to be one of `channels_first` or "
f"`channels_last`. Received: data_format={data_format}"
)
if isinstance(x, np.ndarray):
return _preprocess_numpy_input(x, data_format=data_format, mode=mode)
else:
return _preprocess_tensor_input(x, data_format=data_format, mode=mode)
View on GitHub (pinned to 7a34a03db6)