{"record":{"id":"21954819882e711d","repo":"keras-team/keras","slug":"expected-mode-to-be-one-of-caffe-tf-or-torch","errorCode":null,"errorMessage":"Expected mode to be one of `caffe`, `tf` or `torch`. Received: mode={mode}","messagePattern":"Expected mode to be one of `caffe`, `tf` or `torch`\\. Received: mode=(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/applications/imagenet_utils.py","lineNumber":90,"sourceCode":"    ValueError: In case of unknown `data_format` argument.\"\"\"\n\nPREPROCESS_INPUT_RET_DOC_TF = \"\"\"\n      The inputs pixel values are scaled between -1 and 1, sample-wise.\"\"\"\n\nPREPROCESS_INPUT_RET_DOC_TORCH = \"\"\"\n      The input pixels values are scaled between 0 and 1 and each channel is\n      normalized with respect to the ImageNet dataset.\"\"\"\n\nPREPROCESS_INPUT_RET_DOC_CAFFE = \"\"\"\n      The images are converted from RGB to BGR, then each color channel is\n      zero-centered with respect to the ImageNet dataset, without scaling.\"\"\"\n\n\n@keras_export(\"keras.applications.imagenet_utils.preprocess_input\")\ndef preprocess_input(x, data_format=None, mode=\"caffe\"):\n    \"\"\"Preprocesses a tensor or Numpy array encoding a batch of images.\"\"\"\n    if mode not in {\"caffe\", \"tf\", \"torch\"}:\n        raise ValueError(\n            \"Expected mode to be one of `caffe`, `tf` or `torch`. \"\n            f\"Received: mode={mode}\"\n        )\n\n    if data_format is None:\n        data_format = backend.image_data_format()\n    elif data_format not in {\"channels_first\", \"channels_last\"}:\n        raise ValueError(\n            \"Expected data_format to be one of `channels_first` or \"\n            f\"`channels_last`. Received: data_format={data_format}\"\n        )\n\n    if isinstance(x, np.ndarray):\n        return _preprocess_numpy_input(x, data_format=data_format, mode=mode)\n    else:\n        return _preprocess_tensor_input(x, data_format=data_format, mode=mode)\n\n","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/applications/imagenet_utils.py#L72-L108","documentation":"The cropping operation requires a non-negative target_width; a negative value is rejected while computing the output shape.","triggerScenarios":"Passing target_width < 0, typically from a computed subtraction that underflows for small inputs.","commonSituations":"Dynamic per-sample widths; size-minus-margin arithmetic going negative; copy-pasted shape tuples with a negative entry.","solutions":["Guard target_width >= 0 before the call","Fix the upstream size computation","Handle degenerate small inputs explicitly"],"exampleFix":"# before\nout = ops.image.crop_images(img, target_width=(w - 2 * c, h - 2 * c))  # w < 2c\n# after\ntw = w - 2 * c\nif tw < 0:\n    raise ValueError('width too small')\nout = ops.image.crop_images(img, target_width=(tw, h - 2 * c))","handlingStrategy":"validation","validationCode":"if target_width is None or int(target_width) < 0:\n    raise ValueError('target_width must be >= 0')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Sanitize computed target dims before calling","Add pipeline-level size guards for small samples"],"tags":["keras","cropping","shape-validation"],"backgroundTag":"invalid-argument-validation","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}