keras-team/keras · error · ValueError

NASNet does not support the `channels_first` image data…

Error message

NASNet does not support the `channels_first` image data format. Switch to `channels_last` by editing your local config file at ~/.keras/keras.json

What it means

NASNet builders in keras/src/applications/nasnet.py only support the channels_last image data format; the pretrained weight files and layout code assume NHWC. The error fires when the global Keras configuration (~/.keras/keras.json image_data_format) is channels_first, and points the user to edit that config to switch formats.

Solutions

  1. Edit ~/.keras/keras.json and set "image_data_format": "channels_last", then restart Python
  2. Temporarily set keras.config.image_data_format() / backend setting to channels_last at runtime
  3. Convert your input tensors to channels_last ordering instead of changing model expectations only if the config is also switched
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at keras/src/applications/nasnet.py:115 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/7ab4dcdf24f14bd9. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/applications/nasnet.py:115

            - `max` means that global max pooling will
                be applied.
        classes: Optional number of classes to classify images
            into, only to be specified if `include_top` is `True`, and
            if no `weights` argument is specified.
        default_size: Specifies the default image size of the model
        classifier_activation: A `str` or callable.
            The activation function to use on the "top" layer.
            Ignored unless `include_top=True`.
            Set `classifier_activation=None` to return the logits
            of the "top" layer. When loading pretrained weights,
            `classifier_activation` can only be `None` or `"softmax"`.
        name: The name of the model (string).

    Returns:
        A model instance.
    """
    if backend.image_data_format() == "channels_first":
        raise ValueError(
            "NASNet does not support the `channels_first` image data "
            "format. Switch to `channels_last` by editing your local "
            "config file at ~/.keras/keras.json"
        )
    if not (weights in {"imagenet", None} or file_utils.exists(weights)):
        raise ValueError(
            "The `weights` argument should be either "
            "`None` (random initialization), `imagenet` "
            "(pre-training on ImageNet), "
            "or the path to the weights file to be loaded."
        )

    if weights == "imagenet" and include_top and classes != 1000:
        raise ValueError(
            'If using `weights` as `"imagenet"` with `include_top` '
            "as true, `classes` should be 1000"
        )

View on GitHub (pinned to 7a34a03db6)