{"record":{"id":"ae5152b949189373","repo":"keras-team/keras","slug":"patches-last-dim-static-flat-is-not-divisibl-ae5152","errorCode":null,"errorMessage":"`patches` last dim ({static_flat}) is not divisible by prod(size) ({pD * pH * pW}). Are `size` and the patches tensor consistent?","messagePattern":"`patches` last dim \\((.+?)\\) is not divisible by prod\\(size\\) \\((.+?)\\)\\. Are `size` and the patches tensor consistent\\?","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/image.py","lineNumber":1380,"sourceCode":"        raise ValueError(\n            \"`patches` has unexpected rank for 3D reconstruction. \"\n            \"Expected 4 (unbatched) or 5 (batched). \"\n            f\"Received shape: {patches.shape}\"\n        )\n\n    _unbatched = False\n    if len(patches.shape) == 4:\n        _unbatched = True\n        patches = backend.numpy.expand_dims(patches, axis=0)\n\n    shp = ops.shape(patches)\n    B, gD, gH, gW = shp[0], shp[1], shp[2], shp[3]\n    static_flat = patches.shape[-1]\n    if static_flat is None:\n        C = shp[4] // (pD * pH * pW)\n    else:\n        if static_flat % (pD * pH * pW) != 0:\n            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(","sourceCodeStart":1362,"sourceCodeEnd":1398,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/image.py#L1362-L1398","documentation":"In 3D reconstruction the channels count C is inferred by dividing the patches' last (flat) dimension by prod(size)=pD*pH*pW. If flat is not divisible by the patch volume, size and the patches tensor disagree, so C would not be an integer and it raises.","triggerScenarios":"reconstruct_patches(patches, size=(pD,pH,pW), ...) where patches.shape[-1] % (pD*pH*pW) != 0, e.g. size=(4,8,8) (256) but flat dim 192, or passing a 2D-consistent size for 3D patches.","commonSituations":"Using a different patch size at reconstruction than at extraction; flattening extra features into the last dim; patches produced by a different layer/version with a different patching scheme.","solutions":["Pass exactly the same size used in extract_patches","Verify patches.shape[-1] == C * pD * pH * pW; solve for size if C is known","If extra dims were concatenated into the flat dim, split them out first"],"exampleFix":"# before\nreconstruct_patches(patches, size=(2,8,8), output_size=out)\n# flat=768, 2*8*8=128 -> 768%128==0 ok; but with size=(4,8,8): 768%256!=0\n\n# after\nsize = (4, 8, 8)  # match extraction; flat=1024 for C=4\nreconstruct_patches(patches, size=size, output_size=out)","handlingStrategy":"validation","validationCode":"flat = patches.shape[-1]\nassert flat is None or flat % (size[0]*size[1]*size[2]) == 0, 'size/patches mismatch'","typeGuard":"def size_matches_patches(patches, size) -> bool:\n    flat = patches.shape[-1]\n    return flat is None or flat % (size[0]*size[1]*size[2]) == 0","tryCatchPattern":null,"preventionTips":["Persist the extraction patch size with the patches artifact","Re-run extraction if the patching scheme changed"],"tags":["keras","image","patches","divisibility","shape-mismatch"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","schemaVersion":2},"datasetVersion":"2026-08-26T02:17:13.382Z"}