{"record":{"id":"e63651742a602cc3","repo":"keras-team/keras","slug":"invalid-images-rank-expected-rank-3-single-image-e63651","errorCode":null,"errorMessage":"Invalid images rank: expected rank 3 (single image) or rank 4 (batch of images). Received input with shape: images.shape={images.shape}","messagePattern":"Invalid images rank: expected rank 3 \\(single image\\) or rank 4 \\(batch of images\\)\\. Received input with shape: images\\.shape=(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/image.py","lineNumber":278,"sourceCode":"        self.data_format = backend.standardize_data_format(data_format)\n\n    def call(self, images):\n        return _resize(\n            images,\n            self.size,\n            interpolation=self.interpolation,\n            antialias=self.antialias,\n            data_format=self.data_format,\n            crop_to_aspect_ratio=self.crop_to_aspect_ratio,\n            pad_to_aspect_ratio=self.pad_to_aspect_ratio,\n            fill_mode=self.fill_mode,\n            fill_value=self.fill_value,\n        )\n\n    def compute_output_spec(self, images):\n        images_shape = list(images.shape)\n        if len(images_shape) not in (3, 4):\n            raise ValueError(\n                \"Invalid images rank: expected rank 3 (single image) \"\n                \"or rank 4 (batch of images). Received input with shape: \"\n                f\"images.shape={images.shape}\"\n            )\n        if self.data_format == \"channels_last\":\n            height_axis, width_axis = -3, -2\n        else:\n            height_axis, width_axis = -2, -1\n        images_shape[height_axis] = self.size[0]\n        images_shape[width_axis] = self.size[1]\n        return KerasTensor(shape=images_shape, dtype=images.dtype)\n\n\n@keras_export(\"keras.ops.image.resize\")\ndef resize(\n    images,\n    size,\n    interpolation=\"bilinear\",","sourceCodeStart":260,"sourceCodeEnd":296,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/image.py#L260-L296","documentation":"The crop/aspect-ratio handling image op validates in compute_output_spec that images are rank 3 (single) or rank 4 (batch); other ranks are rejected before any crop/pad logic.","triggerScenarios":"Passing rank-2 masks or rank-5 video clips (frames, N, H, W, C) to a crop/affine op expecting image batches.","commonSituations":"Video pipelines where a leading time axis makes rank 5; segmentation masks stored without a channel axis.","solutions":["Reshape to rank 3/4: merge time into batch (tf.reshape/tensor.reshape(-1, H, W, C)) or add the missing channel axis","Apply the op per-frame in a loop/map for video data"],"exampleFix":"# before\ny = op(video)  # video.shape=(T,N,H,W,C) rank 5\n\n# after\nT,N,H,W,C = video.shape\ny = op(video.reshape((T*N,H,W,C))).reshape((T,N,H,W,C))","handlingStrategy":"validation","validationCode":"assert len(images.shape) in (3, 4)","typeGuard":"def is_valid_image_rank(x) -> bool:\n    return len(x.shape) in (3, 4)","tryCatchPattern":null,"preventionTips":["Collapse time/extra axes into batch before image ops","Give masks a channel axis"],"tags":["keras","image","rank-validation","crop"],"backgroundTag":"image-rank-validation","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}