{"record":{"id":"e621e1e775ce4da8","repo":"keras-team/keras","slug":"patches-last-dim-static-flat-is-not-divisibl","errorCode":null,"errorMessage":"`patches` last dim ({static_flat}) is not divisible by prod(size) ({pH * pW}).","messagePattern":"`patches` last dim \\((.+?)\\) is not divisible by prod\\(size\\) \\((.+?)\\)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"keras/src/ops/image.py","lineNumber":1270,"sourceCode":"        raise ValueError(\n            \"`patches` has unexpected rank for 2D reconstruction. \"\n            \"Expected 3 (unbatched) or 4 (batched). \"\n            f\"Received shape: {patches.shape}\"\n        )\n\n    _unbatched = False\n    if len(patches.shape) == 3:\n        _unbatched = True\n        patches = backend.numpy.expand_dims(patches, axis=0)\n\n    shp = ops.shape(patches)\n    B, gH, gW = shp[0], shp[1], shp[2]\n    static_flat = patches.shape[-1]\n    if static_flat is None:\n        C = shp[3] // (pH * pW)\n    else:\n        if static_flat % (pH * pW) != 0:\n            raise ValueError(\n                f\"`patches` last dim ({static_flat}) is not divisible by \"\n                f\"prod(size) ({pH * pW}).\"\n            )\n        C = static_flat // (pH * pW)\n\n    x = backend.numpy.reshape(patches, (B, gH, gW, pH, pW, C))\n    x = backend.numpy.transpose(x, axes=(0, 1, 3, 2, 4, 5))\n    x = backend.numpy.reshape(x, (B, gH * pH, gW * pW, C))\n\n    if padding == \"same\":\n        static_gH = patches.shape[1]\n        static_gW = patches.shape[2]\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. \"","sourceCodeStart":1252,"sourceCodeEnd":1288,"githubUrl":"https://github.com/keras-team/keras/blob/7a34a03db60bf60042242d6a556fc3be119046a5/keras/src/ops/image.py#L1252-L1288","documentation":"For 2D reconstruction, the last dimension of `patches` (the flattened patch pixels) must be divisible by patch_h*patch_w from `size`, since channels are recovered as flat/(pH*pW). A non-divisible last dim means the patches were not produced with this size, so a ValueError is raised.","triggerScenarios":"Thrown at keras/src/ops/image.py:1270 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Use the same size for reconstruct_patches as for extract_patches","Make the last dim equal pH*pW*channels"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"7a34a03db60bf60042242d6a556fc3be119046a5","analyzedAt":"2026-08-25T21:25:25.994Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}