keras-team/keras · error · ValueError

Input shapes and must match for PSNR calculation.

Error message

Input shapes {x1.shape} and {x2.shape} must match for PSNR calculation. 

What it means

The NumPy `psnr` (peak signal-to-noise ratio) implementation requires the two images to have identical shapes because it compares them element-wise before computing the MSE log-ratio. This guard fires when `x1.shape != x2.shape`, i.e. the predicted and reference tensors do not cover the same pixel grid (different resolutions, channel counts, or batch sizes).

Solutions

  1. Resize one image to match the other (e.g. keras.ops.image.resize) before computing PSNR
  2. Fix a preprocessing or generation step that produced predictions at a different resolution or channel count than the ground truth
  3. Crop or pad the images to a common shape if resizing would distort content
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/numpy/nn.py:1346 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at keras/src/backend/numpy/nn.py:1346

        )
    elif strategy == "beam_search":
        return _ctc_beam_search_decode(
            inputs,
            sequence_lengths,
            beam_width=beam_width,
            top_paths=top_paths,
            mask_index=mask_index,
        )
    else:
        raise ValueError(
            f"Invalid strategy {strategy}. Supported values are "
            "'greedy' and 'beam_search'."
        )


def psnr(x1, x2, max_val):
    if x1.shape != x2.shape:
        raise ValueError(
            f"Input shapes {x1.shape} and {x2.shape} must "
            "match for PSNR calculation. "
        )

    max_val = convert_to_tensor(max_val, dtype=x2.dtype)
    mse = np.mean(np.square(x1 - x2))
    psnr = 20 * np.log10(max_val) - 10 * np.log10(mse)
    return psnr


def _get_large_negative(dtype):
    dtype = backend.standardize_dtype(dtype)
    val = 65500.0 if dtype == "float16" else 3.38953e38
    return np.asarray(val * -0.7, dtype=dtype)


def _apply_masks(logits, mask, is_causal):
    if mask is None and not is_causal:

View on GitHub (pinned to 7a34a03db6)