{"record":{"id":"c5bc0c25a433a68b","repo":"keras-team/keras","slug":"for-padding-same-output-size-depth-d-mu","errorCode":null,"errorMessage":"For `padding='same'`, `output_size` depth ({D}) must be in the range ((gD-1)*pD, gD*pD], i.e. ({static_gD * pD - pD}, {static_gD * pD}]. Got: gD={static_gD}, pD={pD}.","messagePattern":"For `padding='same'`, `output_size` depth \\((.+?)\\) must be in the range \\(\\(gD-1\\)\\*pD, gD\\*pD\\], i\\.e\\. \\((.+?), (.+?)\\]\\. Got: gD=(.+?), pD=(.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/image.py","lineNumber":1398,"sourceCode":"            raise ValueError(\n                f\"`patches` last dim ({static_flat}) is not divisible by \"\n                f\"prod(size) ({pD * pH * pW}). Are `size` and the patches \"\n                f\"tensor consistent?\"\n            )\n        C = static_flat // (pD * pH * pW)\n\n    x = backend.numpy.reshape(patches, (B, gD, gH, gW, pD, pH, pW, C))\n    x = backend.numpy.transpose(x, axes=(0, 1, 4, 2, 5, 3, 6, 7))\n    x = backend.numpy.reshape(x, (B, gD * pD, gH * pH, gW * pW, C))\n\n    if padding == \"same\":\n        static_gD = patches.shape[1]\n        static_gH = patches.shape[2]\n        static_gW = patches.shape[3]\n        if isinstance(static_gD, int) and not (\n            static_gD * pD - pD < D <= static_gD * pD\n        ):\n            raise ValueError(\n                f\"For `padding='same'`, `output_size` depth ({D}) must \"\n                f\"be in the range ((gD-1)*pD, gD*pD], i.e. \"\n                f\"({static_gD * pD - pD}, {static_gD * pD}]. \"\n                f\"Got: gD={static_gD}, pD={pD}.\"\n            )\n        if isinstance(static_gH, int) and not (\n            static_gH * pH - pH < H <= static_gH * pH\n        ):\n            raise ValueError(\n                f\"For `padding='same'`, `output_size` height ({H}) must \"\n                f\"be in the range ((gH-1)*pH, gH*pH], i.e. \"\n                f\"({static_gH * pH - pH}, {static_gH * pH}]. \"\n                f\"Got: gH={static_gH}, pH={pH}.\"\n            )\n        if isinstance(static_gW, int) and not (\n            static_gW * pW - pW < W <= static_gW * pW\n        ):\n            raise ValueError(","sourceCodeStart":1380,"sourceCodeEnd":1416,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/image.py#L1380-L1416","documentation":"3D counterpart of the 'same' range check: with padding='same', the reconstructed volume is cropped to output_size, which is consistent only when depth D lies in ((gD-1)*pD, gD*pD]. It raises when D is outside that window.","triggerScenarios":"reconstruct_patches(patches, size, output_size, padding='same') on 3D patches with output_size[0] (depth) <= (gD-1)*pD or > gD*pD.","commonSituations":"Reconstructing volumetric patches (CT slices, video clips) with a depth computed from the wrong grid; mixing depth/height order in output_size; changing patch depth between extract and reconstruct.","solutions":["Use the original volume depth from extract time","Or pick D in ((gD-1)*pD, gD*pD], e.g. the full grid gD*pD","Recompute gD = ceil(D_original / pD) for same-padding extraction and validate"],"exampleFix":"# before\nreconstruct_patches(p, size=(4,8,8), output_size=(10,28,28), padding='same')\n# gD=3 -> D must be in (8,12]\n\n# after\nreconstruct_patches(p, size=(4,8,8), output_size=(11,28,28), padding='same')","handlingStrategy":"validation","validationCode":"gD, pD = patches.shape[1], size[0]\nassert not isinstance(gD, int) or (gD*pD - pD < output_size[0] <= gD*pD)","typeGuard":"def same_d_ok(patches, size, output_size) -> bool:\n    gD = patches.shape[1]\n    return not isinstance(gD, int) or (gD*size[0]-size[0] < output_size[0] <= gD*size[0])","tryCatchPattern":null,"preventionTips":["Validate all three axes against their grids in one helper","Pass original volume dims as output_size"],"tags":["keras","image","patches","shape-mismatch","input-validation"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}