keras-team/keras · error · ValueError

For `padding='same'`, `output_size` depth ({D}) must be in t

Error message

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}.

What it means

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.

Source

Thrown at keras/src/ops/image.py:1398

            raise ValueError(
                f"`patches` last dim ({static_flat}) is not divisible by "
                f"prod(size) ({pD * pH * pW}). Are `size` and the patches "
                f"tensor consistent?"
            )
        C = static_flat // (pD * pH * pW)

    x = backend.numpy.reshape(patches, (B, gD, gH, gW, pD, pH, pW, C))
    x = backend.numpy.transpose(x, axes=(0, 1, 4, 2, 5, 3, 6, 7))
    x = backend.numpy.reshape(x, (B, gD * pD, gH * pH, gW * pW, C))

    if padding == "same":
        static_gD = patches.shape[1]
        static_gH = patches.shape[2]
        static_gW = patches.shape[3]
        if isinstance(static_gD, int) and not (
            static_gD * pD - pD < D <= static_gD * pD
        ):
            raise ValueError(
                f"For `padding='same'`, `output_size` depth ({D}) must "
                f"be in the range ((gD-1)*pD, gD*pD], i.e. "
                f"({static_gD * pD - pD}, {static_gD * pD}]. "
                f"Got: gD={static_gD}, pD={pD}."
            )
        if isinstance(static_gH, int) and not (
            static_gH * pH - pH < H <= static_gH * pH
        ):
            raise ValueError(
                f"For `padding='same'`, `output_size` height ({H}) must "
                f"be in the range ((gH-1)*pH, gH*pH], i.e. "
                f"({static_gH * pH - pH}, {static_gH * pH}]. "
                f"Got: gH={static_gH}, pH={pH}."
            )
        if isinstance(static_gW, int) and not (
            static_gW * pW - pW < W <= static_gW * pW
        ):
            raise ValueError(

View on GitHub (pinned to 7a34a03db6)

Solutions

  1. Use the original volume depth from extract time
  2. Or pick D in ((gD-1)*pD, gD*pD], e.g. the full grid gD*pD
  3. Recompute gD = ceil(D_original / pD) for same-padding extraction and validate

Example fix

# before
reconstruct_patches(p, size=(4,8,8), output_size=(10,28,28), padding='same')
# gD=3 -> D must be in (8,12]

# after
reconstruct_patches(p, size=(4,8,8), output_size=(11,28,28), padding='same')
Defensive patterns

Strategy: validation

Validate before calling

gD, pD = patches.shape[1], size[0]
assert not isinstance(gD, int) or (gD*pD - pD < output_size[0] <= gD*pD)

Type guard

def same_d_ok(patches, size, output_size) -> bool:
    gD = patches.shape[1]
    return not isinstance(gD, int) or (gD*size[0]-size[0] < output_size[0] <= gD*size[0])

Prevention

When it happens

Trigger: reconstruct_patches(patches, size, output_size, padding='same') on 3D patches with output_size[0] (depth) <= (gD-1)*pD or > gD*pD.

Common situations: 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.

Related errors


AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25). Data as JSON: /api/errors/c5bc0c25a433a68b. Report an issue: GitHub.