{"record":{"id":"4a7da83c80356f92","repo":"keras-team/keras","slug":"top-padding-must-be-0-received-top-padding-t","errorCode":null,"errorMessage":"top_padding must be >= 0. Received: top_padding={top_padding}","messagePattern":"top_padding must be >= 0\\. Received: top_padding=(.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/image.py","lineNumber":1678,"sourceCode":"            height_axis, width_axis = -2, -1\n            height, width = images_shape[height_axis], images_shape[width_axis]\n\n        target_height = self.target_height\n        if target_height is None and height is not None:\n            target_height = self.top_padding + height + self.bottom_padding\n        target_width = self.target_width\n        if target_width is None and width is not None:\n            target_width = self.left_padding + width + self.right_padding\n\n        if height is not None:\n            top_padding = self.top_padding\n            bottom_padding = self.bottom_padding\n            if top_padding is None:\n                top_padding = target_height - height - bottom_padding\n            if bottom_padding is None:\n                bottom_padding = target_height - height - top_padding\n            if top_padding < 0:\n                raise ValueError(\n                    \"top_padding must be >= 0. \"\n                    f\"Received: top_padding={top_padding}\"\n                )\n            if bottom_padding < 0:\n                raise ValueError(\n                    \"bottom_padding must be >= 0. \"\n                    f\"Received: bottom_padding={bottom_padding}\"\n                )\n            if target_height < 0:\n                raise ValueError(\n                    \"target_height must be >= 0. \"\n                    f\"Received: target_height={target_height}\"\n                )\n\n        if width is not None:\n            left_padding = self.left_padding\n            right_padding = self.right_padding\n            if left_padding is None:","sourceCodeStart":1660,"sourceCodeEnd":1696,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/image.py#L1660-L1696","documentation":"In pad_images, when target_height is given, the unspecified side is derived as target_height - height - given_side; if the image is already taller than the target (or the given side eats the whole budget), the derived top_padding goes negative, which is impossible, so compute_output_spec raises this specific message.","triggerScenarios":"pad_images(img, target_height=224, bottom_padding=0) on an image of height 256 — top_padding derives to 256-224-0 = -32 < 0.","commonSituations":"Fixed-size preprocessing on variable-size datasets where some images exceed target; mixing up pad and crop (wanting to shrink); wrong axis (height/width swapped) so target is compared against the wrong dimension.","solutions":["If the image may exceed target, branch: crop when height > target_height, pad when smaller (resize-with-pad pattern)","Raise target_height to at least image height + desired padding","Double-check height vs width ordering of the target"],"exampleFix":"# before\nout = pad_images(img_h256, target_height=224, bottom_padding=0)\n\n# after\nif img.shape[0] > 224:\n    out = crop_images(img, target_height=224, bottom_cropping=0)\nelse:\n    out = pad_images(img, target_height=224, bottom_padding=0)","handlingStrategy":"validation","validationCode":"h = images.shape[-3] if images.ndim == 4 else images.shape[0]\nif target_height is not None and h is not None:\n    assert target_height >= h, 'image taller than target -> use crop_images'","typeGuard":"def pad_target_feasible(images_height, target_height, given_side) -> bool:\n    return target_height - images_height - (given_side or 0) >= 0","tryCatchPattern":"try: out = pad_images(...) except ValueError: fall back to crop_images-to-target (explicit, not silent)","preventionTips":["Variable-size pipelines: branch pad vs crop on image size vs target","Swapping height/width targets is a common root cause"],"tags":["keras","image","padding","negative-dimension","input-validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}