{"record":{"id":"6e9e6840dbd87e8d","repo":"keras-team/keras","slug":"expected-data-format-to-be-one-of-channels-first","errorCode":null,"errorMessage":"Expected data_format to be one of `channels_first` or `channels_last`. Received: data_format={data_format}","messagePattern":"Expected data_format to be one of `channels_first` or `channels_last`\\. Received: data_format=(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/applications/imagenet_utils.py","lineNumber":98,"sourceCode":"\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\npreprocess_input.__doc__ = PREPROCESS_INPUT_DOC.format(\n    mode=PREPROCESS_INPUT_MODE_DOC,\n    ret=\"\",\n    error=PREPROCESS_INPUT_DEFAULT_ERROR_DOC,\n)\n\n\n@keras_export(\"keras.applications.imagenet_utils.decode_predictions\")","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/applications/imagenet_utils.py#L80-L116","documentation":"Runtime validation in _crop_images: only rank-3 (single image) or rank-4 (batched) image tensors are accepted; anything else raises before cropping executes.","triggerScenarios":"Eager calls to ops.image.crop_images with rank-2 arrays, rank-5 video tensors, or lists converted to wrong-rank tensors.","commonSituations":"Feeding unlabeled masks; per-frame video processing without reshaping; converting from libraries that drop singleton axes.","solutions":["Expand to rank 3/4 with [..., None] or [None, ...]","Reshape video data to rank-4 per frame","Check ops.shape(images) rank before calling"],"exampleFix":"# before\nout = ops.image.crop_images(mask, ...)  # (H, W)\n# after\nout = ops.image.crop_images(mask[..., None], ...)  # (H, W, 1)","handlingStrategy":"type-guard","validationCode":"if len(ops.shape(images)) not in (3, 4):\n    images = backend.convert_to_tensor(images)\n    if len(images.shape) == 2:\n        images = images[..., None]","typeGuard":"def is_crop_safe_rank(t) -> bool:\n    return len(t.shape) in (3, 4)","tryCatchPattern":"try:\n    out = ops.image.crop_images(images, ...)\nexcept ValueError as e:\n    if 'rank' in str(e):\n        images = normalize_rank(images)\n    else:\n        raise","preventionTips":["Keep images rank-4 (N,H,W,C) end to end","Reshape video frames to rank-4 before cropping"],"tags":["keras","rank-validation","cropping"],"backgroundTag":"tensor-rank-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}