keras-team/keras · error · ValueError

`decode_predictions` expects a batch of predictions (i.e. a…

Error message

`decode_predictions` expects a batch of predictions (i.e. a 2D array of shape (samples, 1000)). Received array with shape: {preds.shape}

What it means

decode_predictions in keras/src/applications/imagenet_utils.py requires `preds` to be a 2-D array of shape (samples, 1000) — one row per image with exactly the 1000 ImageNet class probabilities. It raises when preds is 1-D (single prediction not batched), 3-D (raw logits before a softmax/flatten), or has a class dimension other than 1000.

Solutions

  1. Pass a batch: np.expand_dims(preds, 0) for a single 1-D prediction vector
  2. Ensure the model output has 1000 classes (the ImageNet classifier head) and is already softmax-normalized
  3. If your model has a different class count, decode manually instead of using decode_predictions
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/applications/imagenet_utils.py:136 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/aa00e93c7aeb841a. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/applications/imagenet_utils.py:136

    """Decodes the prediction of an ImageNet model.

    Args:
        preds: NumPy array encoding a batch of predictions.
        top: Integer, how many top-guesses to return. Defaults to `5`.

    Returns:
        A list of lists of top class prediction tuples
        `(class_name, class_description, score)`.
        One list of tuples per sample in batch input.

    Raises:
        ValueError: In case of invalid shape of the `pred` array
            (must be 2D).
    """
    global CLASS_INDEX

    if len(preds.shape) != 2 or preds.shape[1] != 1000:
        raise ValueError(
            "`decode_predictions` expects "
            "a batch of predictions "
            "(i.e. a 2D array of shape (samples, 1000)). "
            f"Received array with shape: {preds.shape}"
        )
    if CLASS_INDEX is None:
        fpath = file_utils.get_file(
            "imagenet_class_index.json",
            CLASS_INDEX_PATH,
            cache_subdir="models",
            file_hash="c2c37ea517e94d9795004a39431a14cb",
        )
        with open(fpath) as f:
            CLASS_INDEX = json.load(f)
    results = []
    preds = ops.convert_to_numpy(preds)
    for pred in preds:
        top_indices = pred.argsort()[-top:][::-1]

View on GitHub (pinned to 7a34a03db6)